def test_delete_birthdays_should_do_nothing_when_no_relatives_and_no_birth_date_in_patch( self): db = test_utils.get_fake_db() db['birthdays'].insert_one({'import_id': 0}) patch_citizen_handler._delete_birthdays_data(0, {}, None, db, None) count = db['birthdays'].count_documents({'import_id': 0}) self.assertEqual(1, count)
def test_delete_birthdays_should_do_nothing_when_no_birthdays(self): db = test_utils.get_fake_db() lock = MongoLock(client=db.client, db='db') patch_citizen_handler._delete_birthdays_data( 0, {'birth_date': datetime(2019, 1, 1)}, lock, db, None) count = db['birthdays'].count_documents({'import_id': 0}) self.assertEqual(0, count)
def test_add_import_id_should_add_count_of_imports(self): db = test_utils.get_fake_db() db['imports'].insert_one({}) import_data = {} post_import_handler._add_import_id(import_data, db) self.assertIn('import_id', import_data) self.assertEqual(1, import_data['import_id'])
def test_delete_percentile_age_should_delete_when_town_in_patch(self): db = test_utils.get_fake_db() lock = MongoLock(client=db.client, db='db') db['percentile_age'].insert_one({'import_id': 0}) patch_citizen_handler._delete_percentile_age_data( 0, {'town': 'A'}, lock, db, None) count = db['percentile_age'].count_documents({'import_id': 0}) self.assertEqual(0, count)
def test_delete_birthdays_should_delete_when_relatives_in_patch(self): db = test_utils.get_fake_db() lock = MongoLock(client=db.client, db='db') db['birthdays'].insert_one({'import_id': 0}) patch_citizen_handler._delete_birthdays_data(0, {'relatives': []}, lock, db, None) count = db['birthdays'].count_documents({'import_id': 0}) self.assertEqual(0, count)
def test_write_to_db_should_insert_import_data_in_db(self): db = test_utils.get_fake_db() import_data = {'import_id': 0} data, status = post_import_handler._write_to_db(import_data, db) self.assertEqual(201, status) self.assertEqual(import_data['import_id'], data['data']['import_id']) inserted_import_data = db['imports'].find_one( {'import_id': import_data['import_id']}) self.assertEqual(import_data, inserted_import_data)
def test_check_citizens_exists_should_not_raise_when_citizen_exists(self): db = test_utils.get_fake_db() db['imports'].insert_one({ 'import_id': 0, 'citizens': [{ 'citizen_id': 0 }] }) update_relatives._check_all_citizens_exist({0}, 0, db, None) self.assertTrue(True)
def test_write_to_db_should_raise_error_when_write_not_acknowledged(self): class FakeInsertOneResult: def __init__(self): self.acknowledged = False db = test_utils.get_fake_db() db['imports'].insert_one = MagicMock( return_value=FakeInsertOneResult()) with self.assertRaises(PyMongoError): post_import_handler._write_to_db({}, db)
def test_check_citizens_exists_should_raise_when_at_least_one_citizen_dont_exists( self): db = test_utils.get_fake_db() db['imports'].insert_one({ 'import_id': 0, 'citizens': [{ 'citizen_id': 0 }] }) with self.assertRaises(PyMongoError): update_relatives._check_all_citizens_exist({0, 1}, 0, db, None)
def test_get_citizens_should_return_data_if_found(self): db = test_utils.get_fake_db() db['imports'].insert_one({ 'import_id': 0, 'citizens': [{ 'birth_date': 0, 'relatives': [] }] }) citizens = shared.get_citizens(0, db) self.assertEqual([{'birth_date': 0, 'relatives': []}], citizens)
def test_get_relatives_should_make_set_of_relatives(self): db = test_utils.get_fake_db() db['imports'].insert_one({ 'import_id': 0, 'citizens': [{ 'citizen_id': 0, 'relatives': [1, 1, 2, 2, 3, 3] }] }) relatives = update_relatives._get_relatives(0, 0, db, None) self.assertEqual({1, 2, 3}, relatives)
def test_get_citizens_should_select_with_projection(self): db = test_utils.get_fake_db() db['imports'].insert_one({ 'import_id': 0, 'citizens': [{ 'citizen_id': 0, 'birth_date': 0, 'relatives': [] }] }) citizens = shared.get_citizens(0, db, {'citizens.citizen_id': 0}) self.assertEqual([{'birth_date': 0, 'relatives': []}], citizens)
def test_write_citizen_update_should_update_one_field(self): db = test_utils.get_fake_db() db['imports'].insert_one({ 'import_id': 0, 'citizens': [{ 'citizen_id': 0, 'name': 'test' }] }) db_response = patch_citizen_handler._write_citizen_update( 0, 0, {'name': 'aaa'}, db, None) self.assertEqual('aaa', db_response['citizens'][0]['name']) self.assertEqual( 'aaa', db['imports'].find_one({'import_id': 0})['citizens'][0]['name'])
def test_write_relatives_update_should_do_nothing_if_requests_empty(self): db = test_utils.get_fake_db() update_relatives._write_relatives_update([], db, None) db_response = db['imports'].find({}) self.assertEqual(0, db_response.count())
def test_check_citizens_exists_should_raise_when_citizen_dont_exists(self): db = test_utils.get_fake_db() with self.assertRaises(PyMongoError): update_relatives._check_all_citizens_exist({0}, 0, db, None)
def test_get_relatives_should_raise_exception_when_import_not_found(self): db = test_utils.get_fake_db() with self.assertRaises(PyMongoError): update_relatives._get_relatives(0, 0, db, None)
def test_get_citizens_should_raise_if_not_found(self): db = test_utils.get_fake_db() with self.assertRaises(PyMongoError): shared.get_citizens(0, db)
def test_get_relatives_should_raise_exception_when_citizen_not_found(self): db = test_utils.get_fake_db() db['imports'].insert_one({'import_id': 0, 'citizens': []}) with self.assertRaises(PyMongoError): update_relatives._get_relatives(0, 0, db, None)
def test_write_citizen_update_should_raise_when_import_not_found(self): db = test_utils.get_fake_db() with self.assertRaises(PyMongoError): patch_citizen_handler._write_citizen_update(0, 0, {}, db, None)
def test_write_citizen_update_should_raise_when_citizen_not_found(self): db = test_utils.get_fake_db() db['imports'].insert_one({'import_id': 0, 'citizens': []}) with self.assertRaises(PyMongoError): patch_citizen_handler._write_citizen_update(0, 0, {}, db, None)
def test_check_citizens_exists_should_do_nothing_when_citizens_empty(self): db = test_utils.get_fake_db() update_relatives._check_all_citizens_exist(set(), 0, db, None) self.assertTrue(True)
def setUp(self) -> None: self.db = test_utils.get_fake_db()
def test_add_import_id_should_add_zero_when_db_empty(self): db = test_utils.get_fake_db() import_data = {} post_import_handler._add_import_id(import_data, db) self.assertIn('import_id', import_data) self.assertEqual(0, import_data['import_id'])