/sqlalchemy60/test/sql/labels.py

https://github.com/arianepaola/tg2jython
Python | 195 lines | 155 code | 36 blank | 4 comment | 5 complexity | 6c0a1655d90e5360acfa47a431a6fc7c MD5 | raw file
  1. import testenv; testenv.configure_for_tests()
  2. from sqlalchemy import *
  3. from sqlalchemy import exc as exceptions
  4. from testlib import *
  5. from sqlalchemy.engine import default
  6. IDENT_LENGTH = 29
  7. class LabelTypeTest(TestBase):
  8. def test_type(self):
  9. m = MetaData()
  10. t = Table('sometable', m,
  11. Column('col1', Integer),
  12. Column('col2', Float))
  13. assert isinstance(t.c.col1.label('hi').type, Integer)
  14. assert isinstance(select([t.c.col2]).as_scalar().label('lala').type, Float)
  15. class LongLabelsTest(TestBase, AssertsCompiledSQL):
  16. def setUpAll(self):
  17. global metadata, table1, table2, maxlen
  18. metadata = MetaData(testing.db)
  19. table1 = Table("some_large_named_table", metadata,
  20. Column("this_is_the_primarykey_column", Integer, Sequence("this_is_some_large_seq"), primary_key=True),
  21. Column("this_is_the_data_column", String(30))
  22. )
  23. table2 = Table("table_with_exactly_29_characs", metadata,
  24. Column("this_is_the_primarykey_column", Integer, Sequence("some_seq"), primary_key=True),
  25. Column("this_is_the_data_column", String(30))
  26. )
  27. metadata.create_all()
  28. maxlen = testing.db.dialect.max_identifier_length
  29. testing.db.dialect.max_identifier_length = IDENT_LENGTH
  30. def tearDown(self):
  31. table1.delete().execute()
  32. def tearDownAll(self):
  33. metadata.drop_all()
  34. testing.db.dialect.max_identifier_length = maxlen
  35. def test_too_long_name_disallowed(self):
  36. m = MetaData(testing.db)
  37. t1 = Table("this_name_is_too_long_for_what_were_doing_in_this_test", m, Column('foo', Integer))
  38. self.assertRaises(exceptions.IdentifierError, m.create_all)
  39. self.assertRaises(exceptions.IdentifierError, m.drop_all)
  40. self.assertRaises(exceptions.IdentifierError, t1.create)
  41. self.assertRaises(exceptions.IdentifierError, t1.drop)
  42. def test_result(self):
  43. table1.insert().execute(**{"this_is_the_primarykey_column":1, "this_is_the_data_column":"data1"})
  44. table1.insert().execute(**{"this_is_the_primarykey_column":2, "this_is_the_data_column":"data2"})
  45. table1.insert().execute(**{"this_is_the_primarykey_column":3, "this_is_the_data_column":"data3"})
  46. table1.insert().execute(**{"this_is_the_primarykey_column":4, "this_is_the_data_column":"data4"})
  47. s = table1.select(use_labels=True, order_by=[table1.c.this_is_the_primarykey_column])
  48. r = s.execute()
  49. result = []
  50. for row in r:
  51. result.append((row[table1.c.this_is_the_primarykey_column], row[table1.c.this_is_the_data_column]))
  52. assert result == [
  53. (1, "data1"),
  54. (2, "data2"),
  55. (3, "data3"),
  56. (4, "data4"),
  57. ], repr(result)
  58. # some dialects such as oracle (and possibly ms-sql in a future version)
  59. # generate a subquery for limits/offsets.
  60. # ensure that the generated result map corresponds to the selected table, not
  61. # the select query
  62. r = s.limit(2).execute()
  63. result = []
  64. for row in r:
  65. result.append((row[table1.c.this_is_the_primarykey_column], row[table1.c.this_is_the_data_column]))
  66. assert result == [
  67. (1, "data1"),
  68. (2, "data2"),
  69. ], repr(result)
  70. r = s.limit(2).offset(1).execute()
  71. result = []
  72. for row in r:
  73. result.append((row[table1.c.this_is_the_primarykey_column], row[table1.c.this_is_the_data_column]))
  74. assert result == [
  75. (2, "data2"),
  76. (3, "data3"),
  77. ], repr(result)
  78. def test_table_alias_names(self):
  79. self.assert_compile(
  80. table2.alias().select(),
  81. "SELECT table_with_exactly_29_c_1.this_is_the_primarykey_column, table_with_exactly_29_c_1.this_is_the_data_column FROM table_with_exactly_29_characs AS table_with_exactly_29_c_1"
  82. )
  83. ta = table2.alias()
  84. dialect = default.DefaultDialect()
  85. dialect.max_identifier_length = IDENT_LENGTH
  86. self.assert_compile(
  87. select([table1, ta]).select_from(table1.join(ta, table1.c.this_is_the_data_column==ta.c.this_is_the_data_column)).\
  88. where(ta.c.this_is_the_data_column=='data3'),
  89. "SELECT some_large_named_table.this_is_the_primarykey_column, some_large_named_table.this_is_the_data_column, "
  90. "table_with_exactly_29_c_1.this_is_the_primarykey_column, table_with_exactly_29_c_1.this_is_the_data_column FROM "
  91. "some_large_named_table JOIN table_with_exactly_29_characs AS table_with_exactly_29_c_1 ON "
  92. "some_large_named_table.this_is_the_data_column = table_with_exactly_29_c_1.this_is_the_data_column "
  93. "WHERE table_with_exactly_29_c_1.this_is_the_data_column = :this_is_the_data_column_1",
  94. dialect=dialect
  95. )
  96. table2.insert().execute(
  97. {"this_is_the_primarykey_column":1, "this_is_the_data_column":"data1"},
  98. {"this_is_the_primarykey_column":2, "this_is_the_data_column":"data2"},
  99. {"this_is_the_primarykey_column":3, "this_is_the_data_column":"data3"},
  100. {"this_is_the_primarykey_column":4, "this_is_the_data_column":"data4"},
  101. )
  102. r = table2.alias().select().execute()
  103. assert r.fetchall() == [(x, "data%d" % x) for x in range(1, 5)]
  104. def test_colbinds(self):
  105. table1.insert().execute(**{"this_is_the_primarykey_column":1, "this_is_the_data_column":"data1"})
  106. table1.insert().execute(**{"this_is_the_primarykey_column":2, "this_is_the_data_column":"data2"})
  107. table1.insert().execute(**{"this_is_the_primarykey_column":3, "this_is_the_data_column":"data3"})
  108. table1.insert().execute(**{"this_is_the_primarykey_column":4, "this_is_the_data_column":"data4"})
  109. r = table1.select(table1.c.this_is_the_primarykey_column == 4).execute()
  110. assert r.fetchall() == [(4, "data4")]
  111. r = table1.select(or_(
  112. table1.c.this_is_the_primarykey_column == 4,
  113. table1.c.this_is_the_primarykey_column == 2
  114. )).execute()
  115. assert r.fetchall() == [(2, "data2"), (4, "data4")]
  116. def test_insert_no_pk(self):
  117. table1.insert().execute(**{"this_is_the_data_column":"data1"})
  118. table1.insert().execute(**{"this_is_the_data_column":"data2"})
  119. table1.insert().execute(**{"this_is_the_data_column":"data3"})
  120. table1.insert().execute(**{"this_is_the_data_column":"data4"})
  121. @testing.requires.subqueries
  122. def test_subquery(self):
  123. q = table1.select(table1.c.this_is_the_primarykey_column == 4).alias('foo')
  124. x = select([q])
  125. print x.execute().fetchall()
  126. @testing.requires.subqueries
  127. def test_anon_alias(self):
  128. compile_dialect = default.DefaultDialect()
  129. compile_dialect.max_identifier_length = IDENT_LENGTH
  130. q = table1.select(table1.c.this_is_the_primarykey_column == 4).alias()
  131. x = select([q], use_labels=True)
  132. self.assert_compile(x, "SELECT anon_1.this_is_the_primarykey_column AS anon_1_this_is_the_prim_1, anon_1.this_is_the_data_column AS anon_1_this_is_the_data_2 "
  133. "FROM (SELECT some_large_named_table.this_is_the_primarykey_column AS this_is_the_primarykey_column, some_large_named_table.this_is_the_data_column AS this_is_the_data_column "
  134. "FROM some_large_named_table "
  135. "WHERE some_large_named_table.this_is_the_primarykey_column = :this_is_the_primarykey__1) AS anon_1", dialect=compile_dialect)
  136. print x.execute().fetchall()
  137. def test_adjustable(self):
  138. q = table1.select(table1.c.this_is_the_primarykey_column == 4).alias('foo')
  139. x = select([q])
  140. compile_dialect = default.DefaultDialect(label_length=10)
  141. self.assert_compile(x, "SELECT foo.this_is_the_primarykey_column, foo.this_is_the_data_column FROM "
  142. "(SELECT some_large_named_table.this_is_the_primarykey_column AS this_1, some_large_named_table.this_is_the_data_column "
  143. "AS this_2 FROM some_large_named_table WHERE some_large_named_table.this_is_the_primarykey_column = :this_1) AS foo", dialect=compile_dialect)
  144. compile_dialect = default.DefaultDialect(label_length=4)
  145. self.assert_compile(x, "SELECT foo.this_is_the_primarykey_column, foo.this_is_the_data_column FROM "
  146. "(SELECT some_large_named_table.this_is_the_primarykey_column AS _1, some_large_named_table.this_is_the_data_column AS _2 "
  147. "FROM some_large_named_table WHERE some_large_named_table.this_is_the_primarykey_column = :_1) AS foo", dialect=compile_dialect)
  148. q = table1.select(table1.c.this_is_the_primarykey_column == 4).alias()
  149. x = select([q], use_labels=True)
  150. compile_dialect = default.DefaultDialect(label_length=10)
  151. self.assert_compile(x, "SELECT anon_1.this_is_the_primarykey_column AS anon_1, anon_1.this_is_the_data_column AS anon_2 FROM "
  152. "(SELECT some_large_named_table.this_is_the_primarykey_column AS this_3, some_large_named_table.this_is_the_data_column AS this_4 "
  153. "FROM some_large_named_table WHERE some_large_named_table.this_is_the_primarykey_column = :this_1) AS anon_1", dialect=compile_dialect)
  154. compile_dialect = default.DefaultDialect(label_length=4)
  155. self.assert_compile(x, "SELECT _1.this_is_the_primarykey_column AS _1, _1.this_is_the_data_column AS _2 FROM "
  156. "(SELECT some_large_named_table.this_is_the_primarykey_column AS _3, some_large_named_table.this_is_the_data_column AS _4 "
  157. "FROM some_large_named_table WHERE some_large_named_table.this_is_the_primarykey_column = :_1) AS _1", dialect=compile_dialect)
  158. if __name__ == '__main__':
  159. testenv.main()