def test_validate_id_negative(self): """ Assumptions: - given id is not Integer (String instead) """ negative_id = "1" with self.assertRaisesRegex(AdminIdNotValidError, "must be Integer"): AdminOp.validate_id(negative_id)
def case_add_with_admins(self, refresh_db_before): """ Create new Server row with admins many-to-many relation.""" server_name = "TestServer" server_status = "TestStatus" server_type = "TestType" admins = ["Admin One", "Admin Two"] ServerStatusOp.add(server_status) ServerTypeOp.add(server_type) for admin in admins: AdminOp.add(admin) new_server = ServerOp.add(server_name, server_status, server_type, admins=admins) get_servers = ServerOp.get() self.assertTrue(len(get_servers) is 1) self.assertEqual(get_servers[0].description, None) self.assertEqual(get_servers[0].id, 1) self.assertEqual(get_servers[0].name, server_name) self.assertEqual(get_servers[0], new_server) for admin, exp_name in zip(get_servers[0].admins, admins): self.assertEqual(admin.name, exp_name)
def case_get_by_admins(self, refresh_db_before): """ Get server row with admins keyword.""" server_name = "TestServer" server_second_name = "TestServerTwo" server_status = "TestStatus" server_type = "TestType" ServerStatusOp.add(server_status) ServerTypeOp.add(server_type) admins = ["Admin One", "Admin Two", "Admin Three"] for admin in admins: AdminOp.add(admin) srv_one = ServerOp.add(server_name, server_status, server_type, admins=[admins[0], admins[1]]) srv_two = ServerOp.add(server_second_name, server_status, server_type, admins=admins) get_lonely = ServerOp.get(admins=[admins[2]]) self.assertTrue(len(get_lonely) is 1) self.assertEqual(get_lonely[0], srv_two) self.assertEqual(len(get_lonely[0].admins), len(admins)) get_all = ServerOp.get(admins=[admins[0]]) self.assertTrue(len(get_all) is 2)
def case_update_all(self, refresh_db_before): """ Update all fields.""" server_name = "TestServer" server_name_two = "TestServerTwo" ServerStatusOp.add("Status") ServerStatusOp.add("StatusTwo") ServerTypeOp.add("TypeOne") ServerTypeOp.add("TypeTwo") desc_one = "Desc one" desc_two = "Desc two" ips_one = IpOp.add("11.11.11.11") ips_two = IpOp.add("22.22.22.22") tag_one = TagOp.add("tag one") tag_two = TagOp.add("tag two") admin_one = AdminOp.add("Admin One") admin_two = AdminOp.add("Admin Two") srv = ServerOp.add( server_name, "Status", "TypeOne", description=desc_one, ips=["11.11.11.11"], tags=["tag one"], admins=["Admin One"], ) get_before_update = ServerOp.get() self.assertTrue(len(get_before_update) is 1) self.assertEqual(get_before_update[0].id, 1) self.assertEqual(get_before_update[0].name, server_name) self.assertEqual(get_before_update[0].description, desc_one) self.assertEqual(get_before_update[0].status.name, "Status") self.assertEqual(get_before_update[0].type.name, "TypeOne") self.assertEqual(get_before_update[0].ips, [ips_one]) self.assertEqual(get_before_update[0].tags, [tag_one]) self.assertEqual(get_before_update[0].admins, [admin_one]) ServerOp.update( srv, name=server_name_two, description=desc_two, srv_status="StatusTwo", srv_type="TypeTwo", ips=["22.22.22.22"], tags=["tag two"], admins=["Admin Two"], ) get_srv = ServerOp.get() self.assertTrue(len(get_srv) is 1) self.assertEqual(get_srv[0].id, 1) self.assertEqual(get_srv[0].name, server_name_two) self.assertEqual(get_srv[0].description, desc_two) self.assertEqual(get_srv[0].status.name, "StatusTwo") self.assertEqual(get_srv[0].type.name, "TypeTwo") self.assertEqual(get_srv[0].ips, [ips_two]) self.assertEqual(get_srv[0].tags, [tag_two]) self.assertEqual(get_srv[0].admins, [admin_two])
def test_validate_name_too_long(self): """ Assumptions: - given name have 21 length """ long_name = "T" * 21 with self.assertRaisesRegex(AdminNameNotValidError, "wrong length"): AdminOp.validate_name(long_name)
def test_validate_name_too_short(self): """ Assumptions: - given name have 0 length """ short_name = "" with self.assertRaisesRegex(AdminNameNotValidError, "wrong length"): AdminOp.validate_name(short_name)
def test_validate_name_wrong_type(self): """ Assumptions: - given name is not String (Integer instead) """ wrong_type_name = 123 with self.assertRaisesRegex(AdminNameNotValidError, "must be String"): AdminOp.validate_name(wrong_type_name)
def test_validate_id_positive(self): """ Assumptions: - given id is Integer """ positive_id = 1 try: AdminOp.validate_id(positive_id) except DbError: self.fail("Database error raised.")
def test_validate_name_positive(self): """ Assumptions: - given name met requirements """ good_name = "Good Name" try: AdminOp.validate_name(good_name) except DbError: self.fail("Database error raised.")
def test_validate_name_wrong_characters(self): """ Assumptions: - given name contains incorrect characters """ wrong_chars_names = ["(", ")", "_", "-", "*", "$"] for char in wrong_chars_names: with self.assertRaisesRegex(AdminNameNotValidError, "does not match regex"): AdminOp.validate_name(char)
def case_get_by_name_one_result(self, refresh_db_before): """ Create test records in Admin table and get them by name.""" names_with_id = {"admin1": 1, "admin2": 2, "admin3": 3} for name in names_with_id.keys(): AdminOp.add(name) for exp_name, exp_id in names_with_id.items(): admin_obj = AdminOp.get(name=exp_name) self.assertTrue(len(admin_obj) is 1) self.assertEqual(exp_name, admin_obj[0].name) self.assertEqual(exp_id, admin_obj[0].id)
def case_add_records(self, refresh_db_before): """ Add new records to Admin table.""" new_names = ["admin1", "admin2", "admin3"] for name in new_names: AdminOp.add(name) check_records = AdminOp.get() self.assertEqual(len(check_records), len(new_names)) for record, exp_name in zip(check_records, new_names): self.assertTrue(isinstance(record, Admin)) self.assertEqual(record.name, exp_name)
def test_delete(self, mock_admin, mock_db): """ Assumptions: - delete method run """ admin_obj = mock_admin() AdminOp.delete(admin_obj) db_exp_calls = [ mock.call.session.delete(admin_obj), mock.call.session.commit() ] mock_db.assert_has_calls(db_exp_calls)
def test_get_id(self, mock_admin, mock_val_id, mock_val_name): """ Assumptions: - id argument given """ test_id = 1 AdminOp.get(id=test_id) self.assertTrue(mock_val_id.called) self.assertFalse(mock_val_name.called) self.assertTrue(mock_admin.query.filter_by.called) exp_calls = [mock.call(id=test_id), mock.call().all()] mock_admin.query.filter_by.assert_has_calls(exp_calls)
def case_update_record(self, refresh_db_before): """ Create test record in Admin table and then update it.""" original_name = "admin3" update_name = "admin33" original_admin = AdminOp.add(original_name) updated_admin_local = AdminOp.update(original_admin, update_name) self.assertEqual(updated_admin_local.name, update_name) updated_admin_get = AdminOp.get(name=update_name) self.assertTrue(len(updated_admin_get) is 1) self.assertEqual(updated_admin_get[0].name, update_name) self.assertEqual(updated_admin_get[0].id, original_admin.id)
def case_resolve_admin_positive(self, refresh_db_before): """ Try to resolve existing Admin name.""" adm_name = "New Admin" adm_obj = AdminOp.add(adm_name) adm_res_obj = ServerOp.resolve_admin(adm_name) self.assertEqual(adm_obj, adm_res_obj) self.assertEqual(adm_res_obj.name, adm_name)
def case_update_admins(self, refresh_db_before): """ Update admins.""" server_name = "TestServer" ServerStatusOp.add("Status") ServerTypeOp.add("Type") admin_one = AdminOp.add("Admin One") admin_two = AdminOp.add("Admin Two") srv = ServerOp.add(server_name, "Status", "Type", admins=["Admin One"]) get_before_update = ServerOp.get() self.assertTrue(len(get_before_update) is 1) self.assertEqual(get_before_update[0].id, 1) self.assertEqual(get_before_update[0].admins, [admin_one]) ServerOp.update(srv, admins=["Admin Two"]) get_srv = ServerOp.get() self.assertTrue(len(get_srv) is 1) self.assertEqual(get_srv[0].id, 1) self.assertEqual(get_srv[0].admins, [admin_two])
def test_update(self, mock_admin, mock_val_name, mock_db): """ Assumptions: - update method run """ new_name = "Test Name" admin_obj = mock_admin() updated_admin = AdminOp.update(admin_obj, new_name) self.assertTrue(mock_val_name.called) db_exp_calls = [ mock.call.session.add(updated_admin), mock.call.session.commit(), ] mock_db.assert_has_calls(db_exp_calls)
def resolve_admin(cls, admin_name): """ Find Admin record according to given name. Return Admin model object. Args: admin_name(str): name to find Returns: admin_obj(app.db.models.admin.Admin): admin object """ admin_results = AdminOp.get(name=admin_name) if len(admin_results) is not 1: raise ServerAdminNotFoundError(f"Not found Admin: {admin_name}.") admin_obj = admin_results[0] return admin_obj
def case_delete_records(self, refresh_db_before): """ Create new record in Admin table and then delete it.""" new_name = "admin1" AdminOp.add(new_name) adm_obj = AdminOp.get(name=new_name) self.assertTrue(len(adm_obj) is 1) self.assertEqual(adm_obj[0].name, new_name) AdminOp.delete(adm_obj[0]) adm_obj = AdminOp.get(name=new_name) self.assertFalse(adm_obj)
def test_add(self, mock_admin, mock_val_name, mock_db): """ Assumptions: - add method run """ new_name = "Test Name" new_admin = AdminOp.add(new_name) exp_calls = [mock.call(new_name)] mock_admin.assert_has_calls(exp_calls) self.assertEqual(new_admin, mock_admin()) self.assertTrue(mock_val_name.called) db_exp_calls = [ mock.call.session.add(new_admin), mock.call.session.commit() ] mock_db.assert_has_calls(db_exp_calls)
def case_get_by_all(self, refresh_db_before): """ Get server row with both srv_status srv_type keywords. """ server_name = "TestServer" server_second_name = "TestServerTwo" server_third_name = "TestServerThree" server_fourth_name = "TestServerFour" ServerStatusOp.add("StatusOne") ServerStatusOp.add("StatusTwo") ServerTypeOp.add("TypeOne") ServerTypeOp.add("TypeTwo") IpOp.add("11.11.11.11") IpOp.add("22.22.22.22") TagOp.add("Tag one") TagOp.add("Tag two") AdminOp.add("Admin One") AdminOp.add("Admin Two") srv_one = ServerOp.add( server_name, "StatusOne", "TypeOne", ips=["11.11.11.11"], tags=["Tag one"], admins=["Admin One"], ) ServerOp.add( server_second_name, "StatusTwo", "TypeTwo", ips=["22.22.22.22"], tags=["Tag two"], admins=["Admin Two"], ) ServerOp.add( server_third_name, "StatusOne", "TypeTwo", ips=["22.22.22.22"], tags=["Tag two"], admins=["Admin Two"], ) srv_four = ServerOp.add( server_fourth_name, "StatusOne", "TypeOne", ips=["11.11.11.11"], tags=["Tag one"], admins=["Admin One"], ) get_by_all = ServerOp.get( srv_status="StatusOne", srv_type="TypeOne", ip="11.11.11.11", tags=["Tag one"], admins=["Admin One"], ) self.assertTrue(len(get_by_all) is 2) self.assertEqual(get_by_all[0].id, 1) self.assertEqual(get_by_all[1].id, 4) self.assertEqual(get_by_all[0], srv_one) self.assertEqual(get_by_all[1], srv_four)