def test_connections(self): connection_created.connect(self.count_conn) self.connections = 0 # Checking if DatabaseWrapper object creates a connection by default conn = DatabaseWrapper(settings.DATABASES['default']) dbo = DatabaseOperations(conn) dbo.adapt_timefield_value(datetime.time(3, 3, 3)) self.assertEqual(self.connections, 0)
def test_connections(self): connection_created.connect(self.count_conn) self.connections = 0 # Checking if DatabaseWrapper object creates a connection by default conn = DatabaseWrapper(settings.DATABASES['default']) dbo = DatabaseOperations(conn) dbo.value_to_db_time(datetime.time(3, 3, 3)) self.assertEqual(self.connections, 0)
class DjangoDatabaseOperations(tests.MySQLConnectorTests): """Test the Django base.DatabaseOperations class""" def setUp(self): dbconfig = tests.get_mysql_config() self.conn = mysql.connector.connect(**dbconfig) self.cnx = DatabaseWrapper(settings.DATABASES['default']) self.dbo = DatabaseOperations(self.cnx) def test_value_to_db_time(self): if tests.DJANGO_VERSION < (1, 9): value_to_db_time = self.dbo.value_to_db_time else: value_to_db_time = self.dbo.adapt_timefield_value self.assertEqual(None, value_to_db_time(None)) value = datetime.time(0, 0, 0) exp = self.conn.converter._time_to_mysql(value) self.assertEqual(exp, value_to_db_time(value)) value = datetime.time(2, 5, 7) exp = self.conn.converter._time_to_mysql(value) self.assertEqual(exp, value_to_db_time(value)) def test_value_to_db_datetime(self): if tests.DJANGO_VERSION < (1, 9): value_to_db_datetime = self.dbo.value_to_db_datetime else: value_to_db_datetime = self.dbo.adapt_datetimefield_value self.assertEqual(None, value_to_db_datetime(None)) value = datetime.datetime(1, 1, 1) exp = self.conn.converter._datetime_to_mysql(value) self.assertEqual(exp, value_to_db_datetime(value)) value = datetime.datetime(2, 5, 7, 10, 10) exp = self.conn.converter._datetime_to_mysql(value) self.assertEqual(exp, value_to_db_datetime(value)) def test_bulk_insert_sql(self): num_values = 5 fields = ["col1", "col2", "col3"] placeholder_rows = [["%s"] * len(fields) for _ in range(num_values)] exp = "VALUES {0}".format(", ".join( ["({0})".format(", ".join(["%s"] * len(fields)))] * num_values)) if tests.DJANGO_VERSION < (1, 9): self.assertEqual( exp, self.dbo.bulk_insert_sql(fields, num_values)) else: self.assertEqual( exp, self.dbo.bulk_insert_sql(fields, placeholder_rows))
class DjangoDatabaseOperations(tests.MySQLConnectorTests): """Test the Django base.DatabaseOperations class""" def setUp(self): dbconfig = tests.get_mysql_config() self.conn = mysql.connector.connect(**dbconfig) self.cnx = DatabaseWrapper(settings.DATABASES['default']) self.dbo = DatabaseOperations(self.cnx) def test_value_to_db_time(self): if tests.DJANGO_VERSION < (1, 9): value_to_db_time = self.dbo.value_to_db_time else: value_to_db_time = self.dbo.adapt_timefield_value self.assertEqual(None, value_to_db_time(None)) value = datetime.time(0, 0, 0) exp = self.conn.converter._time_to_mysql(value) self.assertEqual(exp, value_to_db_time(value)) value = datetime.time(2, 5, 7) exp = self.conn.converter._time_to_mysql(value) self.assertEqual(exp, value_to_db_time(value)) def test_value_to_db_datetime(self): if tests.DJANGO_VERSION < (1, 9): value_to_db_datetime = self.dbo.value_to_db_datetime else: value_to_db_datetime = self.dbo.adapt_datetimefield_value self.assertEqual(None, value_to_db_datetime(None)) value = datetime.datetime(1, 1, 1) exp = self.conn.converter._datetime_to_mysql(value) self.assertEqual(exp, value_to_db_datetime(value)) value = datetime.datetime(2, 5, 7, 10, 10) exp = self.conn.converter._datetime_to_mysql(value) self.assertEqual(exp, value_to_db_datetime(value)) def test_bulk_insert_sql(self): num_values = 5 fields = ["col1", "col2", "col3"] placeholder_rows = [["%s"] * len(fields) for _ in range(num_values)] exp = "VALUES {0}".format(", ".join( ["({0})".format(", ".join(["%s"] * len(fields)))] * num_values)) if tests.DJANGO_VERSION < (1, 9): self.assertEqual(exp, self.dbo.bulk_insert_sql(fields, num_values)) else: self.assertEqual( exp, self.dbo.bulk_insert_sql(fields, placeholder_rows))
class DjangoDatabaseOperations(tests.MySQLConnectorTests): """Test the Django base.DatabaseOperations class""" def setUp(self): dbconfig = tests.get_mysql_config() self.conn = mysql.connector.connect(**dbconfig) self.cnx = DatabaseWrapper(settings.DATABASES['default']) self.dbo = DatabaseOperations(self.cnx) def test_value_to_db_time(self): self.assertEqual(None, self.dbo.value_to_db_time(None)) value = datetime.time(0, 0, 0) exp = self.conn.converter._time_to_mysql(value) self.assertEqual(exp, self.dbo.value_to_db_time(value)) value = datetime.time(2, 5, 7) exp = self.conn.converter._time_to_mysql(value) self.assertEqual(exp, self.dbo.value_to_db_time(value)) def test_value_to_db_datetime(self): self.assertEqual(None, self.dbo.value_to_db_datetime(None)) value = datetime.datetime(1, 1, 1) exp = self.conn.converter._datetime_to_mysql(value) self.assertEqual(exp, self.dbo.value_to_db_datetime(value)) value = datetime.datetime(2, 5, 7, 10, 10) exp = self.conn.converter._datetime_to_mysql(value) self.assertEqual(exp, self.dbo.value_to_db_datetime(value))
def setUp(self): dbconfig = tests.get_mysql_config() self.conn = mysql.connector.connect(**dbconfig) self.cnx = DatabaseWrapper(settings.DATABASES['default']) self.dbo = DatabaseOperations(self.cnx)