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_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 test_validate_name_not_string(self): """ Assumptions: - given name is not string (int instead) """ name = 111 with self.assertRaisesRegex(ServerNameNotValidError, "must be String"): ServerOp.validate_name(name)
def test_validate_name_too_long(self): """ Assumptions: - given name have 31 length """ name = "a" * 31 with self.assertRaisesRegex(ServerNameNotValidError, "wrong length"): ServerOp.validate_name(name)
def test_validate_id_negative(self): """ Assumptions: - given id is not Integer (String instead) """ negative_id = "1" with self.assertRaisesRegex(ServerIdNotValidError, "must be Integer"): ServerOp.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_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_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_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_detele(self, mock_db): """ Delete record.""" mock_srv_obj = mock.MagicMock() ServerOp.delete(mock_srv_obj) self.assertTrue(mock_db.session.delete.called) self.assertTrue(mock_db.session.commit.called)
def test_validate_description_too_long(self): """ Assumptions: - given description have 61 length """ desc = "a" * 61 with self.assertRaisesRegex(ServerDescriptionNotValidError, "wrong length"): ServerOp.validate_description(desc)
def test_validate_description_not_string(self): """ Assumptions: - given description is not string (int instead) """ desc = 111 with self.assertRaisesRegex(ServerDescriptionNotValidError, "must be String"): ServerOp.validate_description(desc)
def test_validate_id_positive(self): """ Assumptions: - given id is Integer """ positive_id = 1 try: ServerOp.validate_id(positive_id) except ServerIdNotValidError: self.fail("ServerIdNotValidError raised.")
def case_resolve_tag_not_found(self, refresh_db_before): """ Try to resolve non-existing tag name.""" exception_raised = False try: ServerOp.resolve_tag("not exist tag") except ServerTagNotFoundError: exception_raised = True self.assertTrue(exception_raised)
def case_resolve_ip_not_found(self, refresh_db_before): """ Try to resolve Ip row which does not exists.""" exception_raised = False try: ServerOp.resolve_ip("11.11.11.11") except ServerIpNotFoundError: exception_raised = True self.assertTrue(exception_raised)
def case_resolve_type_not_found(self, refresh_db_before): """ Try to resolve ServerType row which name does not exist.""" exception_raised = False try: ServerOp.resolve_type("Test type") except ServerTypeNotFoundError: exception_raised = True self.assertTrue(exception_raised)
def case_resolve_admin_not_found(self, refresh_db_before): """ Try to resolve non-existing admin name.""" exception_raised = False try: ServerOp.resolve_admin("Adm") except ServerAdminNotFoundError: exception_raised = True self.assertTrue(exception_raised)
def case_resolve_status_not_found(self, refresh_db_before): """ Try to resolve ServerStatus row which name does not exist.""" exception_raised = False try: ServerOp.resolve_status("TestStatus") except ServerStatusNotFoundError: exception_raised = True self.assertTrue(exception_raised)
def test_validate_name_positive(self): """ Assumptions: - given name is string with length >= 1 and <= 30 and matched regex [A-Za-z0-9_]+ """ name = "aA09_" try: ServerOp.validate_name(name) except ServerNameNotValidError: self.fail("ServerNameNotValidError raised.")
def test_validate_description_positive(self): """ Assumptions: - given description is string with length >= 1 and <= 60 and matched regex [A-Za-z0-9_]+ """ desc = "aA09_" try: ServerOp.validate_description(desc) except ServerDescriptionNotValidError: self.fail("ServerDescriptionNotValidError raised.")
def test_resolve_type_not_found(self, mock_type_op): """ Assumptions: - ServerTypeOp.get() returns no records """ mock_type_op.get.return_value = list() with self.assertRaisesRegex(ServerTypeNotFoundError, "Not found"): ServerOp.resolve_type("TypeName") mock_status_get_calls = [mock.call(name="TypeName")] mock_type_op.get.assert_has_calls(mock_status_get_calls)
def test_resolve_ip_not_found(self, mock_ip_op): """ Assumptions: - IpOp.get() returns no records """ mock_ip_op.get.return_value = list() with self.assertRaisesRegex(ServerIpNotFoundError, "Not found"): ServerOp.resolve_ip("11.11.11.11") mock_ip_get_calls = [mock.call(address="11.11.11.11")] mock_ip_op.get.assert_has_calls(mock_ip_get_calls)
def test_resolve_tag_not_found(self, mock_tag_op): """ Assumptions: - TagOp.get() returns no records """ mock_tag_op.get.return_value = list() with self.assertRaisesRegex(ServerTagNotFoundError, "Not found"): ServerOp.resolve_tag("tag") mock_tag_get_calls = [mock.call(name="tag")] mock_tag_op.get.assert_has_calls(mock_tag_get_calls)
def test_resolve_admin_not_found(self, mock_admin_op): """ Assumptions: - AdminOp.get() returns no records """ mock_admin_op.get.return_value = list() with self.assertRaisesRegex(ServerAdminNotFoundError, "Not found"): ServerOp.resolve_admin("admin") mock_adm_get_calls = [mock.call(name="admin")] mock_admin_op.get.assert_has_calls(mock_adm_get_calls)
def test_validate_name_wrong_characters(self): """ Assumptions: - given name does not match regex [A-Za-z0-9_]+ """ name_regex = r"[A-Za-z0-9_]+" wrong_chars = [ char for char in list(string.printable) if not re.match(name_regex, char) ] for wrong_char in wrong_chars: with self.assertRaisesRegex(ServerNameNotValidError, "does not match regex"): ServerOp.validate_name(wrong_char)
def test_update_admins( self, mock_val_name, mock_res_status, mock_res_type, mock_val_desc, mock_res_ip, mock_res_tag, mock_res_adm, mock_db, ): """ Update admins in the existing record.""" mock_srv_obj = mock.MagicMock() mock_srv_obj.admins = ["adm"] mock_updated = ServerOp.update(mock_srv_obj, admins=["adm"]) self.assertEqual(mock_updated.admins, [mock_res_adm()]) self.assertFalse(mock_val_name.called) self.assertFalse(mock_res_status.called) self.assertFalse(mock_res_type.called) self.assertFalse(mock_val_desc.called) self.assertFalse(mock_res_ip.called) self.assertFalse(mock_res_tag.called) self.assertTrue(mock_res_adm.called) self.assertTrue(mock_db.session.add.called) self.assertTrue(mock_db.session.commit.called)
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 test_update_srv_type( self, mock_val_name, mock_res_status, mock_res_type, mock_val_desc, mock_res_ip, mock_res_tag, mock_res_adm, mock_db, ): """ Update srv_type in the existing record.""" mock_res_type.return_value = 222 mock_srv_obj = mock.MagicMock() mock_srv_obj.type_id = 111 mock_updated = ServerOp.update(mock_srv_obj, srv_type="SrvType") self.assertEqual(mock_updated.type_id, 222) self.assertFalse(mock_val_name.called) self.assertFalse(mock_res_status.called) self.assertTrue(mock_res_type.called) self.assertFalse(mock_val_desc.called) self.assertFalse(mock_res_ip.called) self.assertFalse(mock_res_tag.called) self.assertFalse(mock_res_adm.called) self.assertTrue(mock_db.session.add.called) self.assertTrue(mock_db.session.commit.called)
def test_update_description( self, mock_val_name, mock_res_status, mock_res_type, mock_val_desc, mock_res_ip, mock_res_tag, mock_res_adm, mock_db, ): """ Update description in the existing record.""" mock_srv_obj = mock.MagicMock() mock_srv_obj.description = "DescOne" mock_updated = ServerOp.update(mock_srv_obj, description="DescTwo") self.assertEqual(mock_updated.description, "DescTwo") self.assertFalse(mock_val_name.called) self.assertFalse(mock_res_status.called) self.assertFalse(mock_res_type.called) self.assertTrue(mock_val_desc.called) self.assertFalse(mock_res_ip.called) self.assertFalse(mock_res_tag.called) self.assertFalse(mock_res_adm.called) self.assertTrue(mock_db.session.add.called) self.assertTrue(mock_db.session.commit.called)