MCPcopy Create free account
hub / github.com/sqlalchemy/sqlalchemy / test_order_by

Method test_order_by

test/sql/test_query.py:374–477  ·  view source on GitHub ↗

Exercises ORDER BY clause generation. Tests simple, compound, aliased and DESC clauses.

(self, connection)

Source from the content-addressed store, hash-verified

372 )
373
374 def test_order_by(self, connection):
375 """Exercises ORDER BY clause generation.
376
377 Tests simple, compound, aliased and DESC clauses.
378 """
379
380 users = self.tables.users
381
382 connection.execute(users.insert(), dict(user_id=1, user_name="c"))
383 connection.execute(users.insert(), dict(user_id=2, user_name="b"))
384 connection.execute(users.insert(), dict(user_id=3, user_name="a"))
385
386 def a_eq(executable, wanted):
387 got = list(connection.execute(executable))
388 eq_(got, wanted)
389
390 for labels in False, True:
391 label_style = (
392 LABEL_STYLE_NONE
393 if labels is False
394 else LABEL_STYLE_TABLENAME_PLUS_COL
395 )
396
397 def go(stmt):
398 if labels:
399 stmt = stmt.set_label_style(label_style)
400 return stmt
401
402 a_eq(
403 users.select()
404 .order_by(users.c.user_id)
405 .set_label_style(label_style),
406 [(1, "c"), (2, "b"), (3, "a")],
407 )
408
409 a_eq(
410 users.select()
411 .order_by(users.c.user_name, users.c.user_id)
412 .set_label_style(label_style),
413 [(3, "a"), (2, "b"), (1, "c")],
414 )
415
416 a_eq(
417 go(
418 select(users.c.user_id.label("foo")).order_by(
419 users.c.user_id
420 )
421 ),
422 [(1,), (2,), (3,)],
423 )
424
425 a_eq(
426 go(
427 select(
428 users.c.user_id.label("foo"), users.c.user_name
429 ).order_by(users.c.user_name, users.c.user_id),
430 ),
431 [(3, "a"), (2, "b"), (1, "c")],

Callers

nothing calls this directly

Calls 11

selectFunction · 0.90
descFunction · 0.90
goFunction · 0.50
executeMethod · 0.45
insertMethod · 0.45
set_label_styleMethod · 0.45
order_byMethod · 0.45
selectMethod · 0.45
labelMethod · 0.45
distinctMethod · 0.45
descMethod · 0.45

Tested by

no test coverage detected