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_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 case_get_by_name_one_result(self, refresh_db_before): """ Create test records in Tag table and get them by name.""" names_with_id = {"tag1": 1, "tag2": 2, "tag3": 3} for name in names_with_id.keys(): TagOp.add(name) for exp_name, exp_id in names_with_id.items(): tag_obj = TagOp.get(name=exp_name) self.assertTrue(len(tag_obj) is 1) self.assertEqual(exp_name, tag_obj[0].name) self.assertEqual(exp_id, tag_obj[0].id)
def case_delete_records(self, refresh_db_before): """ Create new record in Tag table and then delete it.""" new_name = "tag1" TagOp.add(new_name) tag_obj = TagOp.get(name=new_name) self.assertTrue(len(tag_obj) is 1) self.assertEqual(tag_obj[0].name, new_name) TagOp.delete(tag_obj[0]) tag_obj = TagOp.get(name=new_name) self.assertFalse(tag_obj)
def case_add_records(self, refresh_db_before): """ Add new records to Tag table.""" new_names = ["tag1", "tag2", "tag3"] for name in new_names: TagOp.add(name) check_records = TagOp.get() self.assertEqual(len(check_records), len(new_names)) for record, exp_name in zip(check_records, new_names): self.assertTrue(isinstance(record, Tag)) self.assertEqual(record.name, exp_name)
def case_resolve_tag_positive(self, refresh_db_before): """ Try to resolve existing Tag name.""" tag_name = "new tag" tag_obj = TagOp.add(tag_name) tag_res_obj = ServerOp.resolve_tag(tag_name) self.assertEqual(tag_obj, tag_res_obj) self.assertEqual(tag_res_obj.name, tag_name)
def case_update_tags(self, refresh_db_before): """ Update tags.""" server_name = "TestServer" ServerStatusOp.add("Status") ServerTypeOp.add("Type") tag_one = TagOp.add("tag one") tag_two = TagOp.add("tag two") srv = ServerOp.add(server_name, "Status", "Type", tags=["tag 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].tags, [tag_one]) ServerOp.update(srv, tags=["tag two"]) get_srv = ServerOp.get() self.assertTrue(len(get_srv) is 1) self.assertEqual(get_srv[0].id, 1) self.assertEqual(get_srv[0].tags, [tag_two])
def case_update_record(self, refresh_db_before): """ Create test record in Tag table and then update it.""" original_name = "tag3" update_name = "tag33" original_tag = TagOp.add(original_name) updated_tag_local = TagOp.update(original_tag, update_name) self.assertEqual(updated_tag_local.name, update_name) updated_tag_get = TagOp.get(name=update_name) self.assertTrue(len(updated_tag_get) is 1) self.assertEqual(updated_tag_get[0].name, update_name) self.assertEqual(updated_tag_get[0].id, original_tag.id)
def test_add(self, mock_tag, mock_val_name, mock_db): """ Assumptions: - add method run """ new_name = "Test Name" new_tag = TagOp.add(new_name) exp_calls = [mock.call(new_name)] mock_tag.assert_has_calls(exp_calls) self.assertEqual(new_tag, mock_tag()) self.assertTrue(mock_val_name.called) db_exp_calls = [ mock.call.session.add(new_tag), 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)