class BaseDeleteGrammarTest: def setUp(self): self.builder = QueryBuilder(GrammarFactory.make("sqlite"), table="users") def test_can_compile_delete(self): to_sql = self.builder.delete("id", 1, query=True).to_sql() sql = getattr( self, inspect.currentframe().f_code.co_name.replace("test_", ""))() self.assertEqual(to_sql, sql) def test_can_compile_delete_in(self): to_sql = self.builder.delete("id", [1, 2, 3], query=True).to_sql() sql = getattr( self, inspect.currentframe().f_code.co_name.replace("test_", ""))() self.assertEqual(to_sql, sql) def test_can_compile_delete_with_where(self): to_sql = (self.builder.where("age", 20).where("profile", 1).delete(query=True).to_sql()) sql = getattr( self, inspect.currentframe().f_code.co_name.replace("test_", ""))() self.assertEqual(to_sql, sql)
class BaseTestCaseUpdateGrammar: def setUp(self): self.builder = QueryBuilder(GrammarFactory.make(self.grammar), table="users") def test_can_compile_update(self): to_sql = (self.builder.where("name", "bob").update({ "name": "Joe" }, dry=True).to_sql()) sql = getattr( self, inspect.currentframe().f_code.co_name.replace("test_", ""))() self.assertEqual(to_sql, sql) def test_can_compile_multiple_update(self): to_sql = self.builder.update({ "name": "Joe", "email": "*****@*****.**" }, dry=True).to_sql() sql = getattr( self, inspect.currentframe().f_code.co_name.replace("test_", ""))() self.assertEqual(to_sql, sql) def test_can_compile_update_with_multiple_where(self): to_sql = (self.builder.where("name", "bob").where("age", 20).update( { "name": "Joe" }, dry=True).to_sql()) sql = getattr( self, inspect.currentframe().f_code.co_name.replace("test_", ""))() self.assertEqual(to_sql, sql) def test_can_compile_increment(self): to_sql = self.builder.increment("age").to_sql() sql = getattr( self, inspect.currentframe().f_code.co_name.replace("test_", ""))() self.assertEqual(to_sql, sql) def test_can_compile_decrement(self): to_sql = self.builder.decrement("age", 20).to_sql() sql = getattr( self, inspect.currentframe().f_code.co_name.replace("test_", ""))() self.assertEqual(to_sql, sql)
class BaseQMarkTest: def setUp(self): self.builder = QueryBuilder(GrammarFactory.make("mysql"), table="users") def test_can_compile_select(self): mark = self.builder.select("username").where("name", "Joe") sql, bindings = getattr( self, inspect.currentframe().f_code.co_name.replace("test_", ""))() self.assertEqual(mark.to_qmark(), sql) self.assertEqual(mark._bindings, bindings) def test_can_compile_delete(self): mark = self.builder.where("name", "Joe").delete(query=True) sql, bindings = getattr( self, inspect.currentframe().f_code.co_name.replace("test_", ""))() self.assertEqual(mark.to_qmark(), sql) self.assertEqual(mark._bindings, bindings) def test_can_compile_update(self): mark = self.builder.update({ "name": "Bob" }, dry=True).where("name", "Joe") sql, bindings = getattr( self, inspect.currentframe().f_code.co_name.replace("test_", ""))() self.assertEqual(mark.to_qmark(), sql) self.assertEqual(mark._bindings, bindings) def test_can_compile_where_in(self): mark = self.builder.where_in("id", [1, 2, 3]) sql, bindings = getattr( self, inspect.currentframe().f_code.co_name.replace("test_", ""))() self.assertEqual(mark.to_qmark(), sql) self.assertEqual(mark._bindings, bindings) def test_can_compile_where_not_null(self): mark = self.builder.where_not_null("id") sql, bindings = getattr( self, inspect.currentframe().f_code.co_name.replace("test_", ""))() self.assertEqual(mark.to_qmark(), sql) self.assertEqual(mark._bindings, bindings)
class BaseInsertGrammarTest: def setUp(self): self.builder = QueryBuilder(GrammarFactory.make(self.grammar), table="users") def test_can_compile_insert(self): to_sql = self.builder.create({"name": "Joe"}, query=True).to_sql() sql = getattr( self, inspect.currentframe().f_code.co_name.replace("test_", "") )() self.assertEqual(to_sql, sql) def test_can_compile_insert_with_keywords(self): to_sql = self.builder.create(name="Joe", query=True).to_sql() sql = getattr( self, inspect.currentframe().f_code.co_name.replace("test_", "") )() self.assertEqual(to_sql, sql)
class TestMySQLDeleteGrammar(unittest.TestCase): def setUp(self): self.builder = QueryBuilder(GrammarFactory.make("mssql"), table="users") def test_can_compile_delete(self): to_sql = self.builder.delete("id", 1, query=True).to_sql() sql = "DELETE FROM [users] WHERE [users].[id] = '1'" self.assertEqual(to_sql, sql) def test_can_compile_delete_with_where(self): to_sql = (self.builder.where("age", 20).where( "profile", 1).set_action("delete").delete(query=True).to_sql()) sql = ( "DELETE FROM [users] WHERE [users].[age] = '20' AND [users].[profile] = '1'" ) self.assertEqual(to_sql, sql)
def test_builder_alone(self): self.assertTrue( QueryBuilder( connection_details={ "default": "sqlite", "sqlite": { "driver": "sqlite", "database": "orm.sqlite3", "prefix": "", }, }).table("users"))
class TestMySQLInsertGrammar(unittest.TestCase): def setUp(self): self.builder = QueryBuilder(GrammarFactory.make("mssql"), table="users") def test_can_compile_insert(self): to_sql = self.builder.create({"name": "Joe"}, query=True).to_sql() sql = "INSERT INTO [users] ([users].[name]) VALUES ('Joe')" self.assertEqual(to_sql, sql)
class TestMSSQLQmark(unittest.TestCase): def setUp(self): self.builder = QueryBuilder(GrammarFactory.make("mssql"), table="users") def test_can_compile_select(self): mark = self.builder.select("username").where("name", "Joe") sql = "SELECT [users].[username] FROM [users] WHERE [users].[name] = '?'" self.assertEqual(mark.to_qmark(), sql) self.assertEqual(mark._bindings, ("Joe", )) def test_can_compile_update(self): mark = self.builder.update({ "name": "Bob" }, dry=True).where("name", "Joe") sql = "UPDATE [users] SET [users].[name] = '?' WHERE [users].[name] = '?'" self.assertEqual(mark.to_qmark(), sql) self.assertEqual(mark._bindings, ( "Bob", "Joe", ))
class TestMSSQLUpdateGrammar(unittest.TestCase): def setUp(self): self.builder = QueryBuilder(GrammarFactory.make("mssql"), table="users") def test_can_compile_update(self): to_sql = (self.builder.where("name", "bob").update({ "name": "Joe" }, dry=True).to_sql()) sql = "UPDATE [users] SET [users].[name] = 'Joe' WHERE [users].[name] = 'bob'" self.assertEqual(to_sql, sql) def test_can_compile_update_with_multiple_where(self): to_sql = (self.builder.where("name", "bob").where("age", 20).update( { "name": "Joe" }, dry=True).to_sql()) sql = "UPDATE [users] SET [users].[name] = 'Joe' WHERE [users].[name] = 'bob' AND [users].[age] = '20'" self.assertEqual(to_sql, sql)
def test_builder_alone(self): self.assertTrue( QueryBuilder( dry=True, connection_details={ "default": "postgres", "postgres": { "driver": "postgres", "host": "localhost", "user": "******", "password": "******", "database": "orm", "port": "5432", "prefix": "", "grammar": "postgres", }, }, ).table("users"))
def test_builder_alone(self): self.assertTrue( QueryBuilder( connection_details={ "default": "mysql", "mysql": { "driver": "mysql", "host": "localhost", "username": "******", "password": "", "database": "orm", "port": "3306", "prefix": "", "grammar": "mysql", "options": {"charset": "utf8mb4",}, }, } ).table("users") )
def setUp(self): self.builder = QueryBuilder(GrammarFactory.make("mssql"), table="users")
def get_builder(self, table="users"): connection = MockConnectionFactory().make("default") return QueryBuilder(self.grammar, connection, table=table, owner=User)