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_add_with_tags(self, refresh_db_before): """ Create new Server row with tags many-to-many relation.""" server_name = "TestServer" server_status = "TestStatus" server_type = "TestType" tags = ["tag one", "tag two", "tag three"] ServerStatusOp.add(server_status) ServerTypeOp.add(server_type) for tag in tags: TagOp.add(tag) new_server = ServerOp.add(server_name, server_status, server_type, tags=tags) 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 tag, exp_name in zip(get_servers[0].tags, tags): self.assertEqual(tag.name, exp_name)
def case_get_by_ip(self, refresh_db_before): """ Get server row with ip keyword.""" server_name = "TestServer" server_second_name = "TestServerTwo" server_status = "TestStatus" server_type = "TestType" ServerStatusOp.add(server_status) ServerTypeOp.add(server_type) ips = ["11.11.11.11", "22.22.22.22"] for ip in ips: IpOp.add(ip) srv_one = ServerOp.add(server_name, server_status, server_type, ips=[ips[0]]) srv_two = ServerOp.add(server_second_name, server_status, server_type, ips=[ips[1]]) get_first_ip = ServerOp.get(ip=ips[0]) self.assertTrue(len(get_first_ip) is 1) self.assertEqual(get_first_ip[0], srv_one) self.assertNotEqual(get_first_ip[0], srv_two) self.assertTrue(len(get_first_ip[0].ips) is 1) self.assertEqual(get_first_ip[0].ips[0].address, ips[0])
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_tags(self, refresh_db_before): """ Get server row with tags keyword.""" server_name = "TestServer" server_second_name = "TestServerTwo" server_status = "TestStatus" server_type = "TestType" ServerStatusOp.add(server_status) ServerTypeOp.add(server_type) tags = ["tag one", "tag two", "tag three"] for tag in tags: TagOp.add(tag) srv_one = ServerOp.add(server_name, server_status, server_type, tags=[tags[0], tags[1]]) srv_two = ServerOp.add(server_second_name, server_status, server_type, tags=tags) get_lonely = ServerOp.get(tags=[tags[2]]) self.assertTrue(len(get_lonely) is 1) self.assertEqual(get_lonely[0], srv_two) self.assertEqual(len(get_lonely[0].tags), len(tags)) get_all = ServerOp.get(tags=[tags[0]]) self.assertTrue(len(get_all) is 2)
def case_add_with_ips(self, refresh_db_before): """ Create new Server row with ips many-to-many relation.""" server_name = "TestServer" server_status = "TestStatus" server_type = "TestType" ips = ["11.11.11.11", "22.22.22.22", "33.33.33.33"] ServerStatusOp.add(server_status) ServerTypeOp.add(server_type) for ip in ips: IpOp.add(ip) new_server = ServerOp.add(server_name, server_status, server_type, ips=ips) 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 srv_ip, exp_ip in zip(get_servers[0].ips, ips): self.assertEqual(srv_ip.address, exp_ip)
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_not_capital(self): """ Assumption: - given name have no capital letter at the beginning. """ name = "test" with self.assertRaisesRegex(ServerStatusNameNotValidError, "must start with capital"): ServerStatusOp.validate_name(name)
def test_validate_name_too_long(self): """ Assumptions: - given name have length == 21 """ name = "A" * 21 with self.assertRaisesRegex(ServerStatusNameNotValidError, "have wrong length"): ServerStatusOp.validate_name(name)
def test_validate_name_not_string(self): """ Assumptions: - given name is not a string """ name = 1111 with self.assertRaisesRegex(ServerStatusNameNotValidError, "must be String"): ServerStatusOp.validate_name(name)
def test_validate_id_negative(self): """ Assumptions: - given id is not Integer (String instead) """ negative_id = "1" with self.assertRaisesRegex(ServerStatusIdNotValidError, "must be Integer"): ServerStatusOp.validate_id(negative_id)
def test_validate_id_positive(self): """ Assumptions: - given id is Integer """ positive_id = 1 try: ServerStatusOp.validate_id(positive_id) except ServerStatusIdNotValidError: self.fail("ServerStatusIdNotValidError raised.")
def test_validate_name_capital(self): """ Assumption: - given name have capital letter at the beginning. """ name = "Test" try: ServerStatusOp.validate_name(name) except ServerStatusNameNotValidError: self.fail("ServerStatusNameNotValidError raised.")
def case_resolve_status_positive(self, refresh_db_before): """ Create ServerStatus rows, then resolve their names into ID.""" statuses = {"Status_one": 1, "Status_two": 2} for status in statuses.keys(): ServerStatusOp.add(status) for status_name, status_key in statuses.items(): resolved_id = ServerOp.resolve_status(status_name) self.assertEqual(status_key, resolved_id)
def test_validate_name_incorrect_regex(self): """ Assumptions: - given name does not match respective regex """ wrong_names = ["A1", "Aa+", "A-", "A!@#$%^&"] for wrong_name in wrong_names: with self.assertRaisesRegex(ServerStatusNameNotValidError, "does not match regex"): ServerStatusOp.validate_name(wrong_name)
def test_validate_name_good_length(self): """ Assumptions: - given name have good length (border values) """ names = ["A", "A" * 20] for name in names: try: ServerStatusOp.validate_name(name) except ServerStatusNameNotValidError: self.fail("ServerStatusNameNotValidError raised.")
def test_validate_name_correct_regex(self): """ Assumptions: - given correct name match regex. """ good_names = ["Test", "Test_two"] for good_name in good_names: try: ServerStatusOp.validate_name(good_name) except ServerStatusNameNotValidError: self.fail("ServerStatusNameNotValidError raised.")
def case_get_by_name_one_result(self, refresh_db_before): """ Create test records in ServerStatus table and get them by name.""" statuses_with_id = {"Status_one": 1, "Status_two": 2} for status in statuses_with_id.keys(): ServerStatusOp.add(status) for exp_status, exp_id in statuses_with_id.items(): stat_obj = ServerStatusOp.get(name=exp_status) self.assertTrue(len(stat_obj) is 1) self.assertEqual(exp_status, stat_obj[0].name) self.assertEqual(exp_id, stat_obj[0].id)
def test_get_name(self, mock_servstatus, mock_val_id, mock_val_name): """ Assumptions: - name argument given """ test_name = "test_name" ServerStatusOp.get(name=test_name) self.assertFalse(mock_val_id.called) self.assertTrue(mock_val_name.called) self.assertTrue(mock_servstatus.query.filter_by.called) exp_calls = [mock.call(name=test_name), mock.call().all()] mock_servstatus.query.filter_by.assert_has_calls(exp_calls)
def test_delete(self, mock_servstatus, mock_db): """ Assumptions: - delete method run """ ip_obj = mock_servstatus() ServerStatusOp.delete(ip_obj) db_exp_calls = [ mock.call.session.delete(ip_obj), mock.call.session.commit() ] mock_db.assert_has_calls(db_exp_calls)
def case_add_records(self, refresh_db_before): """ Add new records to ServerStatus table.""" new_statuses = ["Status_one", "Status_two"] for status in new_statuses: ServerStatusOp.add(status) check_records = ServerStatusOp.get() self.assertEqual(len(check_records), len(new_statuses)) for record, exp_status in zip(check_records, new_statuses): self.assertTrue(isinstance(record, ServerStatus)) self.assertEqual(record.name, exp_status)
def case_delete(self, refresh_db_before): """ Delete record.""" server_name = "TestServer" ServerStatusOp.add("Status") ServerTypeOp.add("TypeOne") ServerOp.add(server_name, "Status", "TypeOne") get_srv = ServerOp.get() self.assertTrue(len(get_srv) is 1) self.assertEqual(get_srv[0].name, server_name) ServerOp.delete(get_srv[0]) get_empty = ServerOp.get() self.assertFalse(get_empty)
def case_update_record(self, refresh_db_before): """ Create test record in ServerStatus table and then update it.""" original_status = "StatusOrig" update_status = "StatusUpdate" original_stat_obj = ServerStatusOp.add(original_status) updated_stat_local = ServerStatusOp.update(original_stat_obj, update_status) self.assertEqual(updated_stat_local.name, update_status) updated_stat_get = ServerStatusOp.get(name=update_status) self.assertTrue(len(updated_stat_get) is 1) self.assertEqual(updated_stat_get[0].name, update_status) self.assertEqual(updated_stat_get[0].id, original_stat_obj.id)
def case_get_without_filters(self, refresh_db_before): """ Get server rows without any filters.""" server_name = "TestServer" server_second_name = "TestServerTwo" ServerStatusOp.add("Status") ServerTypeOp.add("TypeOne") ServerTypeOp.add("TypeTwo") srv_one = ServerOp.add(server_name, "Status", "TypeOne") srv_two = ServerOp.add(server_second_name, "Status", "TypeTwo") get_all = ServerOp.get() self.assertTrue(len(get_all) is 2) self.assertEqual(get_all[0], srv_one) self.assertEqual(get_all[1], srv_two)
def case_get_by_srv_type(self, refresh_db_before): """ Get server row with srv_type keyword.""" server_name = "TestServer" server_second_name = "TestServerTwo" ServerStatusOp.add("Status") ServerTypeOp.add("TypeOne") ServerTypeOp.add("TypeTwo") srv_one = ServerOp.add(server_name, "Status", "TypeOne") srv_two = ServerOp.add(server_second_name, "Status", "TypeTwo") get_first_type = ServerOp.get(srv_type="TypeOne") self.assertTrue(len(get_first_type) is 1) self.assertEqual(get_first_type[0].id, 1) self.assertEqual(get_first_type[0], srv_one) self.assertNotEqual(get_first_type[0], srv_two)
def case_update_name(self, refresh_db_before): """ Update server name.""" server_name = "TestServer" server_second_name = "TestServerTwo" ServerStatusOp.add("Status") ServerTypeOp.add("TypeOne") srv = ServerOp.add(server_name, "Status", "TypeOne") get_before_update = ServerOp.get(name=server_name) self.assertTrue(len(get_before_update) is 1) self.assertEqual(get_before_update[0].id, 1) ServerOp.update(srv, name=server_second_name) get_srv = ServerOp.get(name=server_second_name) self.assertTrue(len(get_srv) is 1) self.assertEqual(get_srv[0].id, 1)
def case_add_without_description(self, refresh_db_before): """ Create new Server row without description field.""" server_name = "TestServer" ServerStatusOp.add("TestStatus") ServerTypeOp.add("TestType") new_server = ServerOp.add(server_name, "TestStatus", "TestType") 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].type_id, 1) self.assertEqual(get_servers[0].status_id, 1) self.assertEqual(get_servers[0].name, server_name) self.assertEqual(get_servers[0], new_server)
def case_get_by_name(self, refresh_db_before): """ Get server row with name keyword.""" server_name = "TestServer" server_second_name = "TestServerTwo" ServerStatusOp.add("TestStatus") ServerTypeOp.add("TestType") new_server = ServerOp.add(server_name, "TestStatus", "TestType") new_second_server = ServerOp.add(server_second_name, "TestStatus", "TestType") get_second_server = ServerOp.get(name=server_second_name) self.assertTrue(len(get_second_server) is 1) self.assertEqual(get_second_server[0], new_second_server) self.assertEqual(get_second_server[0].id, 2) self.assertNotEqual(get_second_server[0], new_server)
def case_update_srv_type(self, refresh_db_before): """ Update server type.""" server_name = "TestServer" ServerStatusOp.add("Status") ServerTypeOp.add("TypeOne") ServerTypeOp.add("TypeTwo") srv = ServerOp.add(server_name, "Status", "TypeOne") 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].type.name, "TypeOne") ServerOp.update(srv, srv_type="TypeTwo") get_srv = ServerOp.get() self.assertTrue(len(get_srv) is 1) self.assertEqual(get_srv[0].id, 1) self.assertEqual(get_srv[0].type.name, "TypeTwo")
def case_update_ips(self, refresh_db_before): """ Update ip addresses. """ server_name = "TestServer" ServerStatusOp.add("Status") ServerTypeOp.add("Type") ips_one = IpOp.add("11.11.11.11") ips_two = IpOp.add("22.22.22.22") srv = ServerOp.add(server_name, "Status", "Type", ips=["11.11.11.11"]) 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].ips, [ips_one]) ServerOp.update(srv, ips=["22.22.22.22"]) get_srv = ServerOp.get() self.assertTrue(len(get_srv) is 1) self.assertEqual(get_srv[0].id, 1) self.assertEqual(get_srv[0].ips, [ips_two])