def test_unrecognised_type_raises_not_implemented_error(self): class Command(object): class Query(object): def serialize(self, *args, **kwargs): return '{}' query = Query() with self.assertRaises(NotImplementedError): generate_sql_representation(Command())
def test_select_star(self): command = SelectCommand(connections['default'], FormattingTestModel.objects.all().query) sql = generate_sql_representation(command) expected = """ SELECT (*) FROM {} """.format(FormattingTestModel._meta.db_table).strip() self.assertEqual(expected, sql)
def test_limit_applied(self): command = SelectCommand(connections['default'], FormattingTestModel.objects.all()[10:15].query) sql = generate_sql_representation(command) expected = """ SELECT (*) FROM {} OFFSET 10 LIMIT 5 """.format(FormattingTestModel._meta.db_table).strip() self.assertEqual(expected, sql)
def test_unicode_error(self): command = SelectCommand( connections['default'], FormattingTestModel.objects.filter(field2=u"Jacqu\xe9s").query) sql = generate_sql_representation(command) expected = u""" SELECT (*) FROM {} WHERE (field2='Jacqu\xe9s') """.format(FormattingTestModel._meta.db_table).strip() self.assertEqual(expected, sql)
def test_delete_filtered(self): command = DeleteCommand( connections['default'], FormattingTestModel.objects.filter(field1=1).query) sql = generate_sql_representation(command) expected = """ DELETE FROM {} WHERE (field1=1) """.format(FormattingTestModel._meta.db_table).strip() self.assertEqual(expected, sql)
def test_limit_applied(self): command = SelectCommand( connections['default'], FormattingTestModel.objects.all()[10:15].query ) sql = generate_sql_representation(command) expected = """ SELECT (*) FROM {} OFFSET 10 LIMIT 5 """.format(FormattingTestModel._meta.db_table).strip() self.assertEqual(expected, sql)
def test_delete_filtered(self): command = DeleteCommand( connections['default'], FormattingTestModel.objects.filter(field1=1).query ) sql = generate_sql_representation(command) expected = """ DELETE FROM {} WHERE (field1=1) """.format(FormattingTestModel._meta.db_table).strip() self.assertEqual(expected, sql)
def test_unicode_error(self): command = SelectCommand( connections['default'], FormattingTestModel.objects.filter(field2=u"Jacqu\xe9s").query ) sql = generate_sql_representation(command) expected = u""" SELECT (*) FROM {} WHERE (field2='Jacqu\xe9s') """.format(FormattingTestModel._meta.db_table).strip() self.assertEqual(expected, sql)
def test_update_filtered(self): query = FormattingTestModel.objects.filter( field1=1).query.clone(UpdateQuery) query.add_update_values({"field1": 2}) command = UpdateCommand(connections['default'], query) sql = generate_sql_representation(command) expected = """ REPLACE INTO {} (field1) VALUES (2) WHERE (field1=1) """.format(FormattingTestModel._meta.db_table).strip() self.assertEqual(expected, sql)
def test_ordering_applied(self): command = SelectCommand( connections['default'], FormattingTestModel.objects.order_by("-field1").query) sql = generate_sql_representation(command) expected = """ SELECT (*) FROM {} ORDER BY field1 DESC """.format(FormattingTestModel._meta.db_table).strip() self.assertEqual(expected, sql) command = SelectCommand( connections['default'], FormattingTestModel.objects.order_by("field1", "-field2").query) sql = generate_sql_representation(command) expected = """ SELECT (*) FROM {} ORDER BY field1, field2 DESC """.format(FormattingTestModel._meta.db_table).strip() self.assertEqual(expected, sql)
def test_update_filtered(self): query = FormattingTestModel.objects.filter(field1=1).query.clone(UpdateQuery) query.add_update_values({"field1": 2}) command = UpdateCommand( connections['default'], query ) sql = generate_sql_representation(command) expected = """ REPLACE INTO {} (field1) VALUES (2) WHERE (field1=1) """.format(FormattingTestModel._meta.db_table).strip() self.assertEqual(expected, sql)
def test_select_in(self): """ We don't build explicit IN queries, only multiple OR branches there is essentially no difference between the two """ command = SelectCommand( connections['default'], FormattingTestModel.objects.filter(field1__in=[1, 2]).query) sql = generate_sql_representation(command) expected = """ SELECT (*) FROM {} WHERE (field1=2) OR (field1=1) """.format(FormattingTestModel._meta.db_table).strip() self.assertEqual(expected, sql)
def test_ordering_applied(self): command = SelectCommand( connections['default'], FormattingTestModel.objects.order_by("-field1").query ) sql = generate_sql_representation(command) expected = """ SELECT (*) FROM {} ORDER BY field1 DESC """.format(FormattingTestModel._meta.db_table).strip() self.assertEqual(expected, sql) command = SelectCommand( connections['default'], FormattingTestModel.objects.order_by("field1", "-field2").query ) sql = generate_sql_representation(command) expected = """ SELECT (*) FROM {} ORDER BY field1, field2 DESC """.format(FormattingTestModel._meta.db_table).strip() self.assertEqual(expected, sql)
def test_select_in(self): """ We don't build explicit IN queries, only multiple OR branches there is essentially no difference between the two """ command = SelectCommand( connections['default'], FormattingTestModel.objects.filter(field1__in=[1, 2]).query ) sql = generate_sql_representation(command) expected = """ SELECT (*) FROM {} WHERE (field1=2) OR (field1=1) """.format(FormattingTestModel._meta.db_table).strip() self.assertEqual(expected, sql)
def test_single_insert(self): instance = FormattingTestModel(field1=1, field2="Two", field3="Three") command = InsertCommand(connections["default"], FormattingTestModel, [instance], [ FormattingTestModel._meta.get_field(x) for x in ("field1", "field2", "field3") ], True) sql = generate_sql_representation(command) expected = """ INSERT INTO {} (field1, field2, field3) VALUES (1, "Two", "Three") """.format(FormattingTestModel._meta.db_table).strip() self.assertEqual(expected, sql)
def test_single_insert(self): instance = FormattingTestModel(field1=1, field2="Two", field3="Three", field4=b'\xff') command = InsertCommand( connections["default"], FormattingTestModel, [instance], [ FormattingTestModel._meta.get_field(x) for x in ("field1", "field2", "field3", "field4") ], True ) sql = generate_sql_representation(command) expected = """ INSERT INTO {} (field1, field2, field3, field4) VALUES (1, 'Two', 'Three', '<binary>') """.format(FormattingTestModel._meta.db_table).strip() self.assertEqual(expected, sql)
def __unicode__(self): return generate_sql_representation(self)
def __repr__(self): return force_str(generate_sql_representation(self))
def __str__(self): return generate_sql_representation(self)
def test_unrecognised_type_raises_not_implemented_error(self): command = mock.Mock() command.query.serialize.return_value = '{}' with self.assertRaises(NotImplementedError): generate_sql_representation(command)