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_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_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_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_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(ServerTypeNameNotValidError, "must start with capital"): ServerTypeOp.validate_name(name)
def test_validate_name_too_long(self): """ Assumptions: - given name have length == 21 """ name = "A" * 21 with self.assertRaisesRegex(ServerTypeNameNotValidError, "have wrong length"): ServerTypeOp.validate_name(name)
def test_validate_name_not_string(self): """ Assumptions: - given name is not a string """ name = 1111 with self.assertRaisesRegex(ServerTypeNameNotValidError, "must be String"): ServerTypeOp.validate_name(name)
def test_validate_id_negative(self): """ Assumptions: - given id is not Integer (String instead) """ negative_id = "1" with self.assertRaisesRegex(ServerTypeIdNotValidError, "must be Integer"): ServerTypeOp.validate_id(negative_id)
def test_validate_name_capital(self): """ Assumption: - given name have capital letter at the beginning. """ name = "Test" try: ServerTypeOp.validate_name(name) except ServerTypeNameNotValidError: self.fail("ServerTypeNameNotValidError raised.")
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(ServerTypeNameNotValidError, "does not match regex"): ServerTypeOp.validate_name(wrong_name)
def test_validate_id_positive(self): """ Assumptions: - given id is Integer """ positive_id = 1 try: ServerTypeOp.validate_id(positive_id) except ServerTypeIdNotValidError: self.fail("ServerTypeIdNotValidError raised.")
def case_resolve_type_positive(self, refresh_db_before): """ Create ServerType rows, then resolve their names into ID.""" types = {"Type one": 1, "Type two": 2} for srv_type in types.keys(): ServerTypeOp.add(srv_type) for srv_type_name, srv_type_id in types.items(): resolved_id = ServerOp.resolve_type(srv_type_name) self.assertEqual(srv_type_id, resolved_id)
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: ServerTypeOp.validate_name(good_name) except ServerTypeNameNotValidError: self.fail("ServerTypeNameNotValidError raised.")
def test_validate_name_good_length(self): """ Assumptions: - given name have good length (border values) """ names = ["A", "A" * 20] for name in names: try: ServerTypeOp.validate_name(name) except ServerTypeNameNotValidError: self.fail("ServerTypeNameNotValidError raised.")
def case_get_by_name_one_result(self, refresh_db_before): """ Create test records in ServerType table and get them by name.""" types_with_id = {"Type one": 1, "Type two": 2} for serv_type in types_with_id.keys(): ServerTypeOp.add(serv_type) for exp_type, exp_id in types_with_id.items(): type_obj = ServerTypeOp.get(name=exp_type) self.assertTrue(len(type_obj) is 1) self.assertEqual(exp_type, type_obj[0].name) self.assertEqual(exp_id, type_obj[0].id)
def test_delete(self, mock_servtype, mock_db): """ Assumptions: - delete method run """ ip_obj = mock_servtype() ServerTypeOp.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 ServerType table.""" new_types = ["Type one", "Type two"] for serv_type in new_types: ServerTypeOp.add(serv_type) check_records = ServerTypeOp.get() self.assertEqual(len(check_records), len(new_types)) for record, exp_status in zip(check_records, new_types): self.assertTrue(isinstance(record, ServerType)) self.assertEqual(record.name, exp_status)
def test_get_name(self, mock_servtype, mock_val_id, mock_val_name): """ Assumptions: - name argument given """ test_name = "test_name" ServerTypeOp.get(name=test_name) self.assertFalse(mock_val_id.called) self.assertTrue(mock_val_name.called) self.assertTrue(mock_servtype.query.filter_by.called) exp_calls = [mock.call(name=test_name), mock.call().all()] mock_servtype.query.filter_by.assert_has_calls(exp_calls)
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_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_update_record(self, refresh_db_before): """ Create test record in ServerType table and then update it.""" original_type = "TypeOrig" update_type = "TypeUpdate" original_type_obj = ServerTypeOp.add(original_type) updated_type_local = ServerTypeOp.update(original_type_obj, update_type) self.assertEqual(updated_type_local.name, update_type) updated_type_get = ServerTypeOp.get(name=update_type) self.assertTrue(len(updated_type_get) is 1) self.assertEqual(updated_type_get[0].name, update_type) self.assertEqual(updated_type_get[0].id, original_type_obj.id)
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])