def test_encounter_model_can_update_attributes(self): quest = Quest(name="Make'a da pancake!", xp=5, level=1, encounter_req=3, type='active') quest.insert() encounter_1 = Encounter( monster_image= 'https://images.huffingtonpost.com/2015-02-05-trollersTroll-thumb.jpg', quest_id=quest.id, progress=1) encounter_1.insert() self.assertEqual(1, encounter_1.progress) encounter_1 = Encounter( monster_image= 'https://images.huffingtonpost.com/2015-02-05-trollersTroll-thumb.jpg', quest_id=quest.id, progress=2) encounter_1.update() self.assertEqual(2, encounter_1.progress)
def test_quest_model_missing_type(self): try: quest = Quest(name="Make'a da pancake!", xp=0, level=1, encounter_req=3, type=None) quest.insert() except IntegrityError: self.assertTrue(True) else: # we should not end up in here self.assertTrue(False) # pragma: no cover
def test_quest_model_blank_name(self): try: quest = Quest(name='', xp=0, level=1, encounter_req=3, type='active') quest.insert() except IntegrityError: self.assertTrue(True) else: # we should not end up in here self.assertTrue(False) # pragma: no cover
def test_quest_model(self): quest = Quest(name="Make'a da pancake!", xp=5, level=1, encounter_req=3, type='active') quest.insert() self.assertIsInstance(quest, Quest) self.assertIsNotNone(quest.id) self.assertEqual("Make'a da pancake!", quest.name) self.assertEqual(5, quest.xp) self.assertEqual(1, quest.level) self.assertEqual(3, quest.encounter_req) self.assertEqual('active', quest.type)
def test_encounter_model_missing_monster_image(self): try: quest = Quest(name="Make'a da pancake!", xp=5, level=1, encounter_req=3, type='active') quest.insert() encounter_1 = Encounter(monster_image=None, quest_id=quest.id, progress=1) encounter_1.insert() except IntegrityError: self.assertTrue(True) else: # we should not end up in here self.assertTrue(False) # pragma: no cover
def test_encounter_model(self): quest = Quest(name="Make'a da pancake!", xp=5, level=1, encounter_req=3, type='active') quest.insert() encounter_1 = Encounter( monster_image= 'https://images.huffingtonpost.com/2015-02-05-trollersTroll-thumb.jpg', quest_id=quest.id, progress=1) encounter_1.insert() self.assertIsInstance(encounter_1, Encounter) self.assertIsNotNone(encounter_1.id) self.assertEqual( 'https://images.huffingtonpost.com/2015-02-05-trollersTroll-thumb.jpg', encounter_1.monster_image) self.assertEqual(quest.id, encounter_1.quest_id) self.assertEqual(1, encounter_1.progress)
class GetEncountersByQuest(unittest.TestCase): def setUp(self): self.app = create_app('testing') self.app_context = self.app.app_context() self.app_context.push() db.create_all() self.client = self.app.test_client() self.user_1 = User(username='******', email="*****@*****.**", xp=1000000000) self.user_1.insert() self.quest_1 = Quest(name="Make'a da pancake!", xp=5, level=1, encounter_req=3, type='active') self.quest_2 = Quest(name="Make'a da biscuit!", xp=10, level=2, encounter_req=3, type='active') self.quest_1.insert() self.quest_2.insert() self.user_quest_1 = UserQuest(quest_id=self.quest_1.id, user_id=self.user_1.id, progress=1, completion_status=False) self.user_quest_2 = UserQuest(quest_id=self.quest_2.id, user_id=self.user_1.id, progress=1, completion_status=True) self.user_quest_1.insert() self.user_quest_2.insert() self.encounter_1 = Encounter( monster_image= 'https://images.huffingtonpost.com/2015-02-05-trollersTroll-thumb.jpg', quest_id=self.quest_1.id, progress=1) self.encounter_1.insert() self.encounter_2 = Encounter( monster_image= 'https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcSdIfSV09BWeNuPejZM4txwTFJJKikYV_WMLg&usqp=CAU', quest_id=self.quest_1.id, progress=2) self.encounter_2.insert() self.action_1 = Action(encounter_id=self.encounter_1.id, description='Send a message to a recruiter') self.action_2 = Action(encounter_id=self.encounter_1.id, description='Apply to a Job') self.action_3 = Action( encounter_id=self.encounter_2.id, description='Schedule a coffee meetup with a target Company') self.action_1.insert() self.action_2.insert() self.action_3.insert() def tearDown(self): db.session.remove() db_drop_everything(db) self.app_context.pop() def test_happy_path_get_encounters_for_quest_by_progress(self): response = self.client.get( f'/api/v1/quests/{self.quest_1.id}/encounters?progress=2', content_type='application/json') self.assertEqual(200, response.status_code) data = json.loads(response.data.decode('utf-8')) assert_payload_field_type(self, data, 'data', dict) encounter_data = data['data'] assert_payload_field_type_value(self, encounter_data, 'id', int, self.encounter_2.id) assert_payload_field_type_value(self, encounter_data, 'type', str, 'encounters') attributes = encounter_data['attributes'] assert_payload_field_type(self, encounter_data, 'attributes', dict) assert_payload_field_type_value(self, attributes, 'progress', int, self.encounter_2.progress) assert_payload_field_type_value(self, attributes, 'monster_image', str, self.encounter_2.monster_image) assert_payload_field_type(self, attributes, 'actions', list) actions = attributes['actions'] assert_payload_field_type_value(self, actions[0], 'id', int, self.action_3.id) assert_payload_field_type_value(self, actions[0], 'description', str, self.action_3.description) def test_sad_path_get_encounters_for_quest_when_no_progress_given(self): response = self.client.get( f'/api/v1/quests/{self.quest_1.id}/encounters?progress=', content_type='application/json') self.assertEqual(500, response.status_code) def test_sad_path_get_encounters_for_quest_when_wrong_progress_given(self): response = self.client.get( f'/api/v1/quests/{self.quest_1.id}/encounters?progress=896', content_type='application/json') self.assertEqual(404, response.status_code)
class GetQuestsTest(unittest.TestCase): def setUp(self): self.app = create_app('testing') self.app_context = self.app.app_context() self.app_context.push() db.create_all() self.client = self.app.test_client() self.user_1 = User(username='******', email="*****@*****.**", xp=1000000000) self.user_1.insert() self.quest_1 = Quest(name="Make'a da pancake!", xp=5, level=1, encounter_req=3, type='active') self.quest_2 = Quest(name="Make'a da biscuit!", xp=10, level=2, encounter_req=3, type='active') self.quest_3 = Quest(name="Make'a da nuggets!", xp=10, level=2, encounter_req=3, type='active') self.quest_4 = Quest(name="Make'a da cupcake!", xp=10, level=2, encounter_req=3, type='passive') self.quest_5 = Quest(name="Make'a da pizza!", xp=10, level=2, encounter_req=3, type='passive') self.quest_6 = Quest(name="Make'a da bacon!", xp=10, level=2, encounter_req=3, type='passive') self.quest_7 = Quest(name="Make'a da pie!", xp=10, level=2, encounter_req=3, type='supportive') self.quest_1.insert() self.quest_2.insert() self.quest_3.insert() self.quest_4.insert() self.quest_5.insert() self.quest_6.insert() self.quest_7.insert() self.user_quest_1 = UserQuest(quest_id=self.quest_1.id, user_id=self.user_1.id, progress=1, completion_status=False) self.user_quest_2 = UserQuest(quest_id=self.quest_2.id, user_id=self.user_1.id, progress=1, completion_status=True) self.user_quest_1.insert() self.user_quest_2.insert() def tearDown(self): db.session.remove() db_drop_everything(db) self.app_context.pop() def test_happy_path_get_quests_for_user_true(self): response = self.client.get(f'/api/v1/users/{self.user_1.id}/quests?completion_status=true', content_type='application/json') self.assertEqual(200, response.status_code) data = json.loads(response.data.decode('utf-8')) assert_payload_field_type(self, data, 'data', dict) all_quest_data = data['data'] assert_payload_field_type_value(self, all_quest_data, 'id', str, 'Null') assert_payload_field_type_value(self, all_quest_data, 'type', str, 'quests') attributes = all_quest_data['attributes'] assert_payload_field_type(self, attributes, 'quests', list) quest = attributes['quests'][0] assert_payload_field_type(self, quest, 'quest_id_2', dict) quest_data = quest['quest_id_2'] assert_payload_field_type_value(self, quest_data, 'encounter_req', int, self.quest_2.encounter_req) assert_payload_field_type_value(self, quest_data, 'id', int, self.quest_2.id) assert_payload_field_type_value(self, quest_data, 'name', str, self.quest_2.name) assert_payload_field_type_value(self, quest_data, 'progress', int, self.user_quest_2.progress) assert_payload_field_type_value(self, quest_data, 'type', str, self.quest_2.type) assert_payload_field_type_value(self, quest_data, 'xp', int, self.quest_2.xp) assert_payload_field_type_value(self, quest_data, 'level', int, self.quest_2.level) def test_happy_path_get_quests_for_user_false(self): response = self.client.get(f'/api/v1/users/{self.user_1.id}/quests?completion_status=false', content_type='application/json') self.assertEqual(200, response.status_code) data = json.loads(response.data.decode('utf-8')) assert_payload_field_type(self, data, 'data', dict) all_quest_data = data['data'] assert_payload_field_type_value(self, all_quest_data, 'id', str, 'Null') assert_payload_field_type_value(self, all_quest_data, 'type', str, 'quests') attributes = all_quest_data['attributes'] assert_payload_field_type(self, attributes, 'quests', list) quest = attributes['quests'][0] assert_payload_field_type(self, quest, 'quest_id_1', dict) quest_data = quest['quest_id_1'] assert_payload_field_type_value(self, quest_data, 'encounter_req', int, self.quest_1.encounter_req) assert_payload_field_type_value(self, quest_data, 'id', int, self.quest_1.id) assert_payload_field_type_value(self, quest_data, 'name', str, self.quest_1.name) assert_payload_field_type_value(self, quest_data, 'progress', int, self.user_quest_1.progress) assert_payload_field_type_value(self, quest_data, 'type', str, self.quest_1.type) assert_payload_field_type_value(self, quest_data, 'xp', int, self.quest_1.xp) assert_payload_field_type_value(self, quest_data, 'level', int, self.quest_1.level) def test_sad_path_get_quests_for_user_no_params(self): response = self.client.get(f"/api/v1/users/10/quests?completion_status=false", content_type='application/json') self.assertEqual(404, response.status_code) # TODO: BadRequestKeyError WHYY # response = self.client.get(f"/api/v1/users/{self.user_1.id}/quests?completion_status=2}") # breakpoint() # self.assertEqual(400, response.status_code) def test_for_user_without_user_quests(self): new_user = User(username='******', email="*****@*****.**", xp=0) new_user.insert() user_quests = new_user.user_quests.all().__len__() self.assertEqual(0, user_quests) response = self.client.get(f'/api/v1/users/{new_user.id}/quests?completion_status=false', content_type='application/json') user_quests = new_user.user_quests.all().__len__() self.assertEqual(3, user_quests)
class PatchQuestsTest(unittest.TestCase): def setUp(self): self.app = create_app('testing') self.app_context = self.app.app_context() self.app_context.push() db.create_all() self.client = self.app.test_client() self.user_1 = User(username='******', email="*****@*****.**", xp=0) self.user_1.insert() self.user_2 = User(username='******', email="*****@*****.**", xp=0) self.user_2.insert() self.quest_1 = Quest(name="Make'a da pancake!", xp=5, level=1, encounter_req=3, type='active') self.quest_2 = Quest(name="Make'a da biscuit!", xp=10, level=2, encounter_req=1, type='active') self.quest_3 = Quest(name="Make'a da nuggets!", xp=15, level=3, encounter_req=5, type='active') self.quest_4 = Quest(name="Make'a da wings!", xp=15, level=3, encounter_req=5, type='passive') self.quest_1.insert() self.quest_2.insert() self.quest_3.insert() self.quest_4.insert() self.user_quest_1 = UserQuest(quest_id=self.quest_1.id, user_id=self.user_1.id, progress=1, completion_status=False) self.user_quest_2 = UserQuest(quest_id=self.quest_2.id, user_id=self.user_1.id, progress=1, completion_status=False) self.user_quest_3 = UserQuest(quest_id=self.quest_4.id, user_id=self.user_1.id, progress=5, completion_status=False) self.user_quest_4 = UserQuest(quest_id=self.quest_2.id, user_id=self.user_2.id, progress=1, completion_status=False) self.user_quest_1.insert() self.user_quest_2.insert() self.user_quest_3.insert() self.user_quest_4.insert() self.payload = {'quest_id': int(self.quest_1.id), 'progress': int(2)} self.payload2 = {'quest_id': int(self.quest_2.id), 'progress': int(2)} self.payload3 = {'quest_id': int(self.quest_4.id), 'progress': int(6)} self.payload4 = {'quest_id': int(self.quest_2.id), 'progress': int(1)} def tearDown(self): db.session.remove() db_drop_everything(db) self.app_context.pop() def test_happy_path_user_quest_progress_can_be_patched(self): payload = deepcopy(self.payload) response = self.client.patch(f'/api/v1/users/{self.user_1.id}/quests', json=payload, content_type='application/json') self.assertEqual(201, response.status_code) data = json.loads(response.data.decode('utf-8')) assert_payload_field_type(self, data, 'data', dict) all_user_quest_data = data['data'] assert_payload_field_type_value(self, all_user_quest_data, 'id', int, self.user_quest_1.id) assert_payload_field_type_value(self, all_user_quest_data, 'type', str, 'user_quests') assert_payload_field_type(self, all_user_quest_data, 'attributes', dict) attributes = all_user_quest_data['attributes'] assert_payload_field_type_value(self, attributes, 'response', str, 'successful') assert_payload_field_type_value(self, attributes, 'progress', int, int(payload['progress'])) assert_payload_field_type_value(self, attributes, 'completion_status', bool, False) def test_happy_path_progress_change_can_update_completion_status(self): payload2 = deepcopy(self.payload2) response = self.client.patch(f'/api/v1/users/{self.user_1.id}/quests', json=payload2, content_type='application/json') self.assertEqual(201, response.status_code) data = json.loads(response.data.decode('utf-8')) assert_payload_field_type(self, data, 'data', dict) all_user_quest_data = data['data'] assert_payload_field_type_value(self, all_user_quest_data, 'id', int, self.user_quest_2.id) assert_payload_field_type_value(self, all_user_quest_data, 'type', str, 'user_quests') assert_payload_field_type(self, all_user_quest_data, 'attributes', dict) attributes = all_user_quest_data['attributes'] assert_payload_field_type_value(self, attributes, 'response', str, 'successful') assert_payload_field_type_value(self, attributes, 'progress', int, int(payload2['progress'])) assert_payload_field_type_value(self, attributes, 'completion_status', bool, True) def test_sad_path_get_quests_for_user_no_params(self): response = self.client.patch(f"/api/v1/users/{self.user_1.id}/quests", content_type='application/json') self.assertEqual(400, response.status_code) # Come back and add in error messaging later def test_user_gains_xp_when_they_complete_quests(self): self.assertEqual(0, self.user_1.xp) payload2 = deepcopy(self.payload2) response = self.client.patch(f'/api/v1/users/{self.user_1.id}/quests', json=payload2, content_type='application/json') self.assertEqual(10, self.user_1.xp) def test_user_gets_new_quest_when_they_complete_quests(self): user_quests_total = self.user_1.user_quests.all().__len__() self.assertEqual(3, user_quests_total) payload2 = deepcopy(self.payload2) response = self.client.patch(f'/api/v1/users/{self.user_1.id}/quests', json=payload2, content_type='application/json') user_quests_total = self.user_1.user_quests.all().__len__() self.assertEqual(4, user_quests_total) new_user_quest = self.user_1.user_quests.all()[-1] self.assertEqual(3, new_user_quest.quest_id) quest = Quest.query.filter_by(id=new_user_quest.quest_id).one() self.assertEqual(self.quest_3.level, quest.level) self.assertEqual(self.quest_3.name, quest.name) self.assertEqual(self.quest_3.type, quest.type) def test_when_user_completes_all_levels_of_quest_path(self): payload3 = deepcopy(self.payload3) response = self.client.patch(f'/api/v1/users/{self.user_1.id}/quests', json=payload3, content_type='application/json') user_quests_total = self.user_1.user_quests.all().__len__() self.assertEqual(3, user_quests_total) user_xp = self.user_1.xp self.assertEqual(15, user_xp) user_quest_status = self.user_quest_3.completion_status self.assertEqual(True, user_quest_status) def test_sad_path_progress_doesnt_change(self): payload4 = deepcopy(self.payload4) response = self.client.patch(f'/api/v1/users/{self.user_2.id}/quests', json=payload4, content_type='application/json') self.assertEqual(404, response.status_code) def test_sad_path_user_does_not_exist(self): payload4 = deepcopy(self.payload4) response = self.client.patch(f'/api/v1/users/12/quests', json=payload4, content_type='application/json') self.assertEqual(404, response.status_code)