class PostgresNoSQLViewTest(unittest.TestCase): def setUp(self): self.client = PostgresNoSQLClient(host=TEST_DB_HOST, user=TEST_DB_USER, password=TEST_DB_PASSWORD) # pre cleanup if self.client.database_exists(TEST_DATABASE): self.client.drop_database(TEST_DATABASE) self.database = self.client.create_database(TEST_DATABASE) self.user = self.database.create_table('users', name='text') self.info = self.database.create_table('info', fk_user='******', age='integer') # fill table with data self.user.insert(name='Simon') self.user.insert(name='Felix') self.info.insert(fk_user=1, age=30) self.info.insert(fk_user=2, age=40) # create view self.database.execute('CREATE VIEW user_view AS SELECT users.name, info.age FROM users JOIN info ON users.id = info.fk_user') # get view self.user_view = self.database.get_view('user_view') def tearDown(self): # cleanup if self.database is not None: self.database.close() if self.client.database_exists(self.database.name): self.client.drop_database(self.database.name) def test_query_view(self): records = self.user_view.query() self.assertEqual(2, len(records))
class PostgresNoSQLResultItemTest(unittest.TestCase): def setUp(self): self.client = PostgresNoSQLClient(host=TEST_DB_HOST, user=TEST_DB_USER, password=TEST_DB_PASSWORD) # pre cleanup if self.client.database_exists(TEST_DATABASE): self.client.drop_database(TEST_DATABASE) self.database = self.client.create_database(TEST_DATABASE) self.table = self.database.create_table(TEST_TABLE) self.relational_table = self.database.create_table(TEST_RELATIONAL_TABLE, **RELATIONAL_FIELDS) # fill table self.first = self.table.put(JSON_DATA) self.first_relational = self.relational_table.put(JSON_DATA, **RELATIONAL_DATA) def tearDown(self): # cleanup if self.database is not None: self.database.close() if self.client.database_exists(self.database.name): self.client.drop_database(self.database.name) def test_json_serialize(self): record = self.table.get(self.first) # todo: useless unit test atm json_string = json.dumps(record.get_record()) self.assertEqual(json.dumps(record.get_record()), json_string)
class PostgresNoSQLDatabaseTest(unittest.TestCase): def setUp(self): self.client = PostgresNoSQLClient(host=TEST_DB_HOST, user=TEST_DB_USER, password=TEST_DB_PASSWORD) # pre cleanup if self.client.database_exists(TEST_DATABASE): self.client.drop_database(TEST_DATABASE) self.database = self.client.create_database(TEST_DATABASE) self.table = self.database.create_table(TEST_TABLE) def tearDown(self): # cleanup if self.database is not None: self.database.close() if self.client.database_exists(self.database.name): self.client.drop_database(self.database.name) self.database = None self.table = None self.client = None def test_table_exists(self): self.assertEqual(True, self.database.table_exists(TEST_TABLE)) self.assertEqual(False, self.database.table_exists(TEST_TABLE+'12345')) def test_get_table_with_existing(self): self.assertNotEqual(None, self.database.get_table(TEST_TABLE)) def test_get_table_without_existing(self): self.assertEqual(None, self.database.get_table(TEST_TABLE+'12345')) def test_create_table(self): temp = self.database.create_table(TEST_TEMP_TABLE) self.assertEqual(TEST_TEMP_TABLE, temp.name) def test_create_table_with_bigserial(self): temp = self.database.create_table(TEST_TEMP_TABLE, row_identifier_type='BIGSERIAL') self.assertEqual(TEST_TEMP_TABLE, temp.name) def test_create_table_with_relational_fields(self): temp = self.database.create_table(TEST_TEMP_TABLE, **RELATIONAL_FIELDS) self.assertEqual(TEST_TEMP_TABLE, temp.name) def test_drop_table(self): self.database.drop_table(TEST_TABLE) self.assertEqual(False, self.database.table_exists(TEST_TABLE)) def test_get_or_create_table_with_existing(self): temp = self.database.get_or_create_table(TEST_TABLE) self.assertEqual(TEST_TABLE, temp.name) def test_get_or_create_table_without_existing(self): temp = self.database.get_or_create_table(TEST_TEMP_TABLE) self.assertEqual(TEST_TEMP_TABLE, temp.name)
class PostgresNoSQLClientTest(unittest.TestCase): def setUp(self): self.client = PostgresNoSQLClient(host=TEST_DB_HOST, user=TEST_DB_USER, password=TEST_DB_PASSWORD) # pre init cleanup if self.client.database_exists(TEST_DATABASE): self.client.drop_database(TEST_DATABASE) print('DB TRUE (%s)' % TEST_DATABASE) else: print('DB FALSE (%s)' % TEST_DATABASE) if self.client.database_exists(TEST_TEMP_DATABASE): self.client.drop_database(TEST_TEMP_DATABASE) self.database = self.client.create_database(TEST_DATABASE) self.temp_db = None def tearDown(self): # cleanup if self.temp_db is not None: self.temp_db.close() if self.database is not None: self.database.close() if self.client.database_exists(TEST_TEMP_DATABASE): self.client.drop_database(TEST_TEMP_DATABASE) if self.client.database_exists(self.database.name): self.client.drop_database(self.database.name) self.database = None self.temp_db = None self.client = None def test_init_without_parameter(self): client = PostgresNoSQLClient() self.assertEqual(DEFAULT_POSTGRES_HOST, client.host) self.assertEqual(DEFAULT_POSTGRES_PORT, client.port) self.assertEqual(DEFAULT_POSTGRES_DATABASE, client.database) def test_init_with_parameter(self): client = PostgresNoSQLClient(host='test', port=2000, database='my_test', user='******', password='******') self.assertEqual('test', client.host) self.assertEqual(2000, client.port) self.assertEqual('my_test', client.database) def test_create_database(self): self.temp_db = self.client.create_database(TEST_TEMP_DATABASE) self.assertEqual(TEST_TEMP_DATABASE, self.temp_db.name) self.assertNotEqual(None, self.temp_db.connection, 'no connection established') def test_database_exists(self): self.assertEqual(True, self.client.database_exists(TEST_DATABASE)) self.assertEqual(False, self.client.database_exists(TEST_DATABASE+'12345')) def test_drop_database(self): self.temp_db = self.client.create_database(TEST_TEMP_DATABASE) self.temp_db.close() self.client.drop_database(TEST_TEMP_DATABASE) self.assertEqual(False, self.client.database_exists(TEST_TEMP_DATABASE)) def test_get_database_with_existing(self): self.temp_db = self.client.get_database(TEST_DATABASE) self.assertEqual(TEST_DATABASE, self.temp_db.name) self.assertNotEqual(None, self.temp_db.connection, 'no connection established') def test_get_database_without_existing(self): self.temp_db = self.client.get_database(TEST_DATABASE+'12345') self.assertEqual(None, self.temp_db, 'there should be no database') def test_get_or_create_with_existing(self): self.temp_db = self.client.get_or_create_database(TEST_DATABASE) self.assertEqual(TEST_DATABASE, self.temp_db.name) def test_get_or_create_without_existing(self): self.temp_db = self.client.get_or_create_database(TEST_TEMP_DATABASE) self.assertEqual(TEST_TEMP_DATABASE, self.temp_db.name)
class PostgresNoSQLTableTest(unittest.TestCase): def setUp(self): self.client = PostgresNoSQLClient(host=TEST_DB_HOST, user=TEST_DB_USER, password=TEST_DB_PASSWORD) # pre cleanup if self.client.database_exists(TEST_DATABASE): self.client.drop_database(TEST_DATABASE) self.database = self.client.create_database(TEST_DATABASE) self.table = self.database.create_table(TEST_TABLE) self.relational_table = self.database.create_table(TEST_RELATIONAL_TABLE, **RELATIONAL_FIELDS) # fill table self.first = self.table.put(JSON_DATA) self.first_relational = self.relational_table.put(JSON_DATA, **RELATIONAL_DATA) def tearDown(self): # cleanup if self.database is not None: self.database.close() if self.client.database_exists(self.database.name): self.client.drop_database(self.database.name) def test_get_empty_record(self): record = self.table.get(self.first+1) self.assertEqual(None, record) def test_get_json_field(self): record = self.table.get(self.first) self.assertEqual('blue', record.json['color']) def test_get_relational_field(self): record = self.relational_table.get(self.first_relational) self.assertEqual(24, record['age']) def test_insert(self): id = self.relational_table.insert(name="pascal", age=22, height=182) record = self.relational_table.get(id) self.assertEqual(22, record['age']) def test_put_json(self): id = self.table.put(JSON_DATA) record = self.table.get(id) self.assertEqual(3, record.json['items']) def test_put_json_with_none(self): id = self.table.put(None) record = self.table.get(id) self.assertEqual(None, record.get_record()[DEFAULT_JSON_COLUMN_NAME]) def test_put_relational(self): id = self.relational_table.put(JSON_DATA, **RELATIONAL_DATA) record = self.relational_table.get(id) self.assertEqual(179.5, record['height']) def test_put_relational_without_auto_commit(self): id = self.relational_table.put(JSON_DATA, auto_commit=False, **RELATIONAL_DATA) self.relational_table.connection.rollback() record = self.relational_table.get(id) self.assertEqual(None, record) def test_put_relational_with_none(self): id = self.relational_table.put(JSON_DATA, name='florian', age=24, height=None) record = self.relational_table.get(id) self.assertEqual(None, record['height']) def test_update(self): record = self.relational_table.get(self.first_relational) self.relational_table.update(record.id, age=5) record = self.relational_table.get(self.first_relational) self.assertEqual(5, record['age']) def test_save_on_item(self): record = self.relational_table.get(self.first_relational) record['age'] = 5 record.save() record = self.relational_table.get(self.first_relational) self.assertEqual(5, record['age']) def test_save_json(self): record = self.table.get(self.first) record.json['items'] = 15 self.table.save(record) record = self.table.get(self.first) self.assertEqual(15, record.json['items']) def test_save_object_further_use(self): record = self.relational_table.get(self.first_relational) record.json['items'] = 15 self.relational_table.save(record) self.assertEqual(15, record.json['items']) self.assertEqual('florian', record['name']) def test_save_json_with_none(self): record = self.table.get(self.first) record.json['items'] = None self.table.save(record) record = self.table.get(self.first) self.assertEqual(None, record.json['items']) def test_save_relational(self): record = self.relational_table.get(self.first_relational) record['age'] = 25 self.relational_table.save(record) record = self.relational_table.get(self.first_relational) self.assertEqual(25, record['age']) def test_save_auto_commit(self): record = self.relational_table.get(self.first_relational) record['age'] = 25 self.relational_table.save(record) self.relational_table.connection.rollback() record = self.relational_table.get(self.first_relational) self.assertEqual(25, record['age']) def test_save_without_auto_commit(self): record = self.relational_table.get(self.first_relational) record['age'] = 25 self.relational_table.save(record, auto_commit=False) self.relational_table.connection.rollback() record = self.relational_table.get(self.first_relational) self.assertEqual(24, record['age']) def test_save_relational_with_none(self): record = self.relational_table.get(self.first_relational) record['height'] = None self.relational_table.save(record) record = self.relational_table.get(self.first_relational) self.assertEqual(None, record['height']) def test_query(self): records = self.relational_table.query() self.assertNotEqual(None, records) def test_delete_record(self): self.table.delete(self.first) record = self.table.get(self.first) self.assertEqual(None, record)