|
| 1 | +import testing.postgresql |
| 2 | +from unittest import TestCase |
| 3 | + |
| 4 | +from easy_postgres_engine.tests.test_table_schema import TEST_TABLE_SCHEMA |
| 5 | +from easy_postgres_engine.postgres_engine import PostgresEngine |
| 6 | + |
| 7 | + |
| 8 | +def create_table(postgresqlConnection): |
| 9 | + config = postgresqlConnection.dsn() |
| 10 | + dbEngine = PostgresEngine( |
| 11 | + databaseName=config['database'], |
| 12 | + user=config['user'], |
| 13 | + password=config.get('password'), |
| 14 | + port=config['port'], |
| 15 | + host=config['host'] |
| 16 | + ) |
| 17 | + dbEngine.create_table(schema=TEST_TABLE_SCHEMA) |
| 18 | + |
| 19 | + |
| 20 | +# Generate Postgresql class which shares the generated database |
| 21 | +Postgresql = testing.postgresql.PostgresqlFactory(cache_initialized_db=True, on_initialized=create_table) |
| 22 | + |
| 23 | + |
| 24 | +def tearDownModule(): |
| 25 | + # clear cached database at end of tests |
| 26 | + Postgresql.clear_cache() |
| 27 | + |
| 28 | + |
| 29 | +class TestPostgresEngine(TestCase): |
| 30 | + |
| 31 | + def __init__(self, methodName='runTest'): |
| 32 | + super(TestPostgresEngine, self).__init__(methodName=methodName) |
| 33 | + self.firstCustomerId = 10 |
| 34 | + self.firstCustomerName = 'Mary' |
| 35 | + self.secondCustomerId = 50 |
| 36 | + self.secondCustomerName = 'John' |
| 37 | + |
| 38 | + def setUp(self): |
| 39 | + super().setUp() |
| 40 | + self.postgresql = Postgresql() |
| 41 | + config = self.postgresql.dsn() |
| 42 | + self.dbEngine = PostgresEngine( |
| 43 | + databaseName=config['database'], |
| 44 | + user=config['user'], |
| 45 | + password=config.get('password'), |
| 46 | + port=config['port'], |
| 47 | + host=config['host'] |
| 48 | + ) |
| 49 | + |
| 50 | + def tearDown(self): |
| 51 | + super().tearDown() |
| 52 | + self.postgresql.stop() |
| 53 | + |
| 54 | + def test_engine(self): |
| 55 | + # TODO(Mike): probably better to test insertion + selection queries separately, but DB persistence between test functions is awkward |
| 56 | + TEST_INSERTION_QUERY = """ |
| 57 | + INSERT INTO |
| 58 | + tbl_example(customer_id, customer_name) |
| 59 | + VALUES |
| 60 | + (%(customerId)s, %(customerName)s) |
| 61 | + """ |
| 62 | + insertedId1 = self.dbEngine.run_update_query( |
| 63 | + query=TEST_INSERTION_QUERY, |
| 64 | + parameters={'customerId': self.firstCustomerId, 'customerName': self.firstCustomerName} |
| 65 | + ) |
| 66 | + self.assertEqual(insertedId1, 1) |
| 67 | + insertedId2 = self.dbEngine.run_update_query( |
| 68 | + query=TEST_INSERTION_QUERY, |
| 69 | + parameters={'customerId': self.secondCustomerId, 'customerName': self.secondCustomerName} |
| 70 | + ) |
| 71 | + self.assertEqual(insertedId2, 2) |
| 72 | + |
| 73 | + queryResults = self.dbEngine.run_select_query(query='SELECT * FROM tbl_example') |
| 74 | + self.assertSequenceEqual(list(queryResults['customer_id'].values), [self.firstCustomerId, self.secondCustomerId]) |
| 75 | + self.assertSequenceEqual(list(queryResults['customer_name'].values), [self.firstCustomerName, self.secondCustomerName]) |
| 76 | + |
| 77 | + specificQueryResults = self.dbEngine.run_select_query( |
| 78 | + query=""" |
| 79 | + SELECT |
| 80 | + * |
| 81 | + FROM |
| 82 | + tbl_example |
| 83 | + WHERE |
| 84 | + customer_id = %(customerId)s |
| 85 | + """, |
| 86 | + parameters={'customerId': self.firstCustomerId} |
| 87 | + ) |
| 88 | + self.assertEqual(len(specificQueryResults), 1) |
| 89 | + self.assertEqual(specificQueryResults['customer_name'].iloc[0], self.firstCustomerName) |
0 commit comments