def addachieve(request): title = request.POST.get('title', '') discr = request.POST.get('dis', '') image = request.POST.get('image', '') c = Achievements(Title=title, Discription=discr, Image=image) c.save() return render(request, 'office_staff/add_achievements.html')
def test_delete_one_achievement(self): """ Test delete on one valid achievement """ ac1 = Achievements(aid=32, rid=12, name='test', experience=10, points=10, type=1, value='test') ac2 = Achievements(aid=22, rid=12, name='test', experience=10, points=10, type=1, value='test') db.session.add(ac1) db.session.add(ac2) db.session.commit() errmsg = delete_achievement(32) a1 = Achievements.query.filter_by(aid=32).first() a2 = Achievements.query.filter_by(aid=22).first() self.assertEqual(errmsg, None) self.assertIsNone(a1) self.assertIsNotNone(a2)
def test_one_expired_achievement(self): """ One achievement in the achievement table are expired. Expired achievement should not be in list. """ ac1 = Achievements(aid=32, rid=12, name='test', experience=10, points=10, type=3, value='test;4;False;2020-4-11;2020-4-11') ac2 = Achievements(aid=22, rid=12, name='test', experience=10, points=10, type=3, value='test;5;False;2020-4-1;2099-4-11') db.session.add(ac1) db.session.add(ac2) db.session.commit() result = filter_expired_achievements(12) expected = [{ "aid": 22, "name": "test", "description": "Visit 5 times between 2020-4-1 and 2099-4-11.", "experience": 10, "points": 10, "progressMax": 5 }] self.assertEqual(result, expected)
def test_get_recent_achievements_less_than_three_matches(self): """ Tests get_recently_started_achievements() when the user has less than three incomplete progress entries relevant to the given achievement list. """ ac1 = Achievements(aid=11, rid=12, name='test 3', experience=15, points=20, type=1, value='test') ac2 = Achievements(aid=12, rid=12, name='test 2', experience=15, points=15, type=1, value='test') db.session.add(ac1) db.session.add(ac2) achievementProgress1 = Customer_Achievement_Progress(uid=5, aid=11, progress=2, total=5) achievementProgress2 = Customer_Achievement_Progress(uid=5, aid=12, progress=1, total=3) db.session.add(achievementProgress1) db.session.add(achievementProgress2) db.session.commit() achievement_list = self.achievement_list_helper() self.assertEqual( achievementhelper.get_recently_started_achievements( achievement_list, 5), [{ "aid": 12, "name": "test 3", "description": "description 3", "experience": 15, "points": 15, "progressMax": 3, "progress": 1, "status": 1, "expired": 0 }, { "aid": 11, "name": "test 2", "description": "description 2", "experience": 15, "points": 20, "progressMax": 5, "progress": 2, "status": 1, "expired": 0 }])
def test_sort_on_short(self): """ Test sorting on less than or equal to 3 achievement progress """ ac1 = Achievements(aid=12, rid=12, name='test 3', experience=15, points=20, type=1, value='test') ac2 = Achievements(aid=7, rid=12, name='test 2', experience=15, points=15, type=1, value='test') ac3 = Achievements(aid=22, rid=12, name='test', experience=10, points=15, type=1, value='test') db.session.add(ac1) db.session.add(ac2) db.session.add(ac3) ap1 = Customer_Achievement_Progress(aid=12, uid=7, progress=2, total=3, update=NOW) ap2 = Customer_Achievement_Progress(aid=7, uid=7, progress=1, total=5, update=MIN) ap3 = Customer_Achievement_Progress(aid=22, uid=7, progress=5, total=5, update=MAX) db.session.add(ap1) db.session.add(ap2) db.session.add(ap3) db.session.commit() achievements = achievementhelper.get_recently_update_achievements(7) # if sort success, order should be ap3->ap1->ap2 self.assertEqual(achievements[0].aid, 22) self.assertEqual(achievements[1].aid, 12) self.assertEqual(achievements[2].aid, 7)
def test_get_exist_many(self): """ Test getting a list of aid in database with more than one achievement in the database """ ac1 = Achievements(aid=32, rid=12, name='test', experience=10, points=10, type=1, value='test') ac2 = Achievements(aid=22, rid=12, name='test', experience=10, points=10, type=1, value='test') ac3 = Achievements(aid=42, rid=52, name='test', experience=10, points=10, type=1, value='test') db.session.add(ac1) db.session.add(ac2) db.session.add(ac3) db.session.commit() aid_list = get_exist_aid() self.assertEqual([22, 32, 42], aid_list)
def test_get_achievements_progress_data_some_achievements_to_filter(self): """ Tests get_achievements_with_progress_data() when the given achievement list to filter is not empty """ ac1 = Achievements(aid=11, rid=12, name='test', experience=10, points=15, type=1, value='test') ac2 = Achievements(aid=12, rid=12, name='test 2', experience=15, points=20, type=1, value='test') ac3 = Achievements(aid=13, rid=12, name='test 3', experience=15, points=15, type=1, value='test') db.session.add(ac1) db.session.add(ac2) db.session.add(ac3) achievementProgress1 = Customer_Achievement_Progress(uid=5, aid=11, progress=2, total=5) achievementProgress2 = Customer_Achievement_Progress(uid=5, aid=12, progress=3, total=3) achievementProgress3 = Customer_Achievement_Progress(uid=5, aid=13, progress=3, total=6) db.session.add(achievementProgress1) db.session.add(achievementProgress2) db.session.add(achievementProgress3) db.session.commit() achievement_list = self.achievement_list_helper() self.assertEqual(achievementhelper.get_achievements_with_progress_data(achievement_list, 5), [{"aid": 10, "name": "test", "description": "description", "experience": 10, "points": 15, "progressMax": 6, "progress": 0, "status": 0, "expired": 0 }, {"aid": 11, "name": "test 2", "description": "description 2", "experience": 15, "points": 20, "progressMax": 5, "progress": 2, "status": 1, "expired": 0 }, {"aid": 12, "name": "test 3", "description": "description 3", "experience": 15, "points": 15, "progressMax": 3, "progress": 3, "status": 2, "expired": 0 }])
def test_get_single_achievement_progress(self): """ Tests get_achievement_progress_by_uid() when one achievement has a uid matching the given uid. """ ac1 = Achievements(aid=10, rid=12, name='test1', experience=101, points=101, type=1, value='test') ac2 = Achievements(aid=11, rid=12, name='test2', experience=100, points=100, type=1, value='test') db.session.add(ac1) db.session.add(ac2) achievementProgress1 = Customer_Achievement_Progress(uid=5, aid=10, progress=3, total=6) achievementProgress2 = Customer_Achievement_Progress(uid=6, aid=11, progress=1, total=5) db.session.add(achievementProgress1) db.session.add(achievementProgress2) db.session.commit() achievement_progress_list = achievementhelper.get_achievement_progress_by_uid( 5) self.assertEqual(achievement_progress_list, [{ 'aid': 10, 'progress': 3, 'progressMax': 6, 'uid': 5, 'update': None }]) achievement_list = achievementhelper.get_achievement_progress_by_uid(6) self.assertEqual(achievement_list, [{ 'aid': 11, 'progress': 1, 'progressMax': 5, 'uid': 6, 'update': None }])
def insert_achievement(rid, name, experience, points, type, value): """ Gets all error messages that can occur from inserting a achievement. Args: rid: A restuarants ID. Integer value. name: The name of a restaurant. String value. experience: The reward experience value. Integer value. points: The reward points value. Integer value type: The type of achievement: 0: buy item amount times. 1: Spend $$.$$ amount. 2: Visit with a group. 3: Visit a specific amount of time. Interger value. value: of the form: "INT;STRING;BOOLEAN;DATE;DATE" """ achievement = Achievements(rid=rid, name=name, experience=experience, points=points, type=type, value=value) db.session.add(achievement) db.session.commit()
def test_get_values(self): """ Tests get_achievement_data() for all achievement types. """ achievement1 = Achievements(rid=12, name="test", points=10, experience=15, type=0, value="Item;5;;;") achievement2 = Achievements(rid=13, name="test 2", points=15, experience=20, type=1, value=";6.99;;;") achievement3 = Achievements(rid=13, name="test 3", points=15, experience=20, type=2, value=";6;;;") achievement4 = Achievements(rid=13, name="test 3", points=15, experience=20, type=2, value=";6;True;;") achievement5 = Achievements(rid=13, name="test 3", points=15, experience=20, type=2, value=";6;False;2020-08-1;2020-08-31") self.assertEqual(get_achievement_data(achievement1), ["Item", "5", "", "", ""]) self.assertEqual(get_achievement_data(achievement2), ["", "6.99", "", "", ""]) self.assertEqual(get_achievement_data(achievement3), ["", "6", "", "", ""]) self.assertEqual(get_achievement_data(achievement4), ["", "6", "True", "", ""]) self.assertEqual(get_achievement_data(achievement5), ["", "6", "False", "2020-08-1", "2020-08-31"])
def test_get_exist_one(self): """ Test getting a list of aid with only one achievement in the database, which is only 1 aid(valid) """ ac1 = Achievements(aid=32, rid=12, name='test', experience=10, points=10, type=1, value='test') db.session.add(ac1) db.session.commit() aid_list = get_exist_aid() self.assertEqual([32], aid_list)
def achievements(): json = request.get_json() db.session.add( Achievements(user_id=json['user_id'], achievement=json['achievement'], reward=json['reward'])) db.session.commit() return jsonify(json)
def test_get_single_achievements(self): """ Tests get_achievements_by_rid() when one achievement has a rid matching the given rid. """ achievement1 = Achievements(rid=12, name="test", points=10, experience=15, type=0, value="Item;5;;;") achievement2 = Achievements(rid=13, name="test 2", points=15, experience=20, type=1, value=";6.99;;;") db.session.add(achievement1) db.session.add(achievement2) db.session.commit() achievement_list = get_achievements_by_rid(12) self.assertEqual(achievement_list, [{ 'aid': 1, 'name': 'test', 'points': 10, 'experience': 15, 'description': "Buy Item 5 times.", 'progressMax': 5, 'expired': 0 }]) achievement_list = get_achievements_by_rid(13) self.assertEqual(achievement_list, [{ 'aid': 2, 'name': 'test 2', 'points': 15, 'experience': 20, 'description': "Spend $6.99 in a single visit.", 'progressMax': 1, 'expired': 0 }])
def test_indefinite_expiry(self): """ Tests when the achievement's expiry date is indefinite. """ achievement = Achievements(aid=32, rid=12, name='test', experience=10, points=10, type=2, value='test;4;True;;') self.assertEqual(is_today_in_achievement_date_range(achievement), 0)
def test_after_expiry(self): """ Tests when today's date is after the expiry date. """ achievement = Achievements(aid=32, rid=12, name='test', experience=10, points=10, type=2, value='test;4;False;2020-4-11;2020-4-12') self.assertEqual(is_today_in_achievement_date_range(achievement), 1)
def test_during_range(self): """ Tests when today's date is between the start date and the expiry date. """ achievement = Achievements(aid=32, rid=12, name='test', experience=10, points=10, type=2, value='test;4;False;2020-4-11;2099-4-11') self.assertEqual(is_today_in_achievement_date_range(achievement), 0)
def test_before_start(self): """ Tests when today's date is before the start date. """ achievement = Achievements(aid=32, rid=12, name='test', experience=10, points=10, type=2, value='test;4;False;2099-4-11;2099-4-12') self.assertEqual(is_today_in_achievement_date_range(achievement), -1)
def updateUserAchievements(account_id): try: req = request.get_json() user = User.query.filter_by(account_id=account_id).options( db.load_only("id")).first() achiev = Achievements(user_id=user.id, achievement=req['achievement']) db.session.add(achiev) db.session.commit() resp = {"success": "achievement has been added successfully"} return respond(resp, 201) except: return respond({"error": "user not found"}, 404)
def test_get_progress_max(self): """ Tests get_achievement_progress_maximum() for all achievement types. """ achievement1 = Achievements(rid=12, name="test", points=10, experience=15, type=0, value="Item;5;;;") achievement2 = Achievements(rid=13, name="test 2", points=15, experience=20, type=1, value=";6.99;;;") achievement3 = Achievements(rid=13, name="test 3", points=15, experience=20, type=2, value=";6;;;") achievement4 = Achievements(rid=13, name="test 4", points=15, experience=20, type=3, value=";6;True;;") achievement5 = Achievements(rid=13, name="test 5", points=15, experience=20, type=3, value=";6;False;2020-08-1;2020-08-31") self.assertEqual(get_achievement_progress_maximum(achievement1), 5) self.assertEqual(get_achievement_progress_maximum(achievement2), 1) self.assertEqual(get_achievement_progress_maximum(achievement3), 1) self.assertEqual(get_achievement_progress_maximum(achievement4), 6) self.assertEqual(get_achievement_progress_maximum(achievement5), 6)
def test_on_expiry(self): """ Tests when today's date is equal to the expiry date. """ achievement = Achievements(aid=32, rid=12, name='test', experience=10, points=10, type=2, value='test;4;False;2020-4-11;' + datetime.now().strftime("%Y-%m-%d")) self.assertEqual(is_today_in_achievement_date_range(achievement), 0)
def test_get_nonexistent_achievements(self): """ Tests get_achievements_by_rid() when no achievements have a rid matching the given rid. """ achievement1 = Achievements(rid=12, name="test", points=10, experience=15, type=0, value="Item;5") achievement2 = Achievements(rid=13, name="test 2", points=15, experience=20, type=1, value=";6.99") db.session.add(achievement1) db.session.add(achievement2) db.session.commit() achievement_list = get_achievements_by_rid(10) self.assertEqual(achievement_list, [])
def test_many_expired_achievements(self): """ Many coupons in the achievement table are expired. The expired achievements should not be in list. """ ac1 = Achievements(aid=32, rid=12, name='test', experience=10, points=10, type=3, value='test;4;False;2020-4-11;2020-4-11') ac2 = Achievements(aid=22, rid=12, name='test', experience=10, points=10, type=2, value='test;5;False;2020-04-1;2020-04-11') db.session.add(ac1) db.session.add(ac2) db.session.commit() result = filter_expired_achievements(12) expected = [] self.assertEqual(result, expected)
def test_add_complete(self): """ Test make progress on an achievement and complete the achievement, this is valid. """ ap = Customer_Achievement_Progress(aid=1, uid=3, progress=1, total=2) user = User(uid=3, name='cus', password='******', email='test', type=-1) points = Points(pid=1, uid=3, rid=1, points=20) achievement = Achievements(aid=1, rid=1, name='test', experience=20, points=20, type=0, value='test;10') db.session.add(ap) db.session.add(user) db.session.add(points) db.session.add(achievement) db.session.commit() achievementhelper.add_one_progress_bar(ap, 1, 3) self.assertEqual(points.points, 40) self.assertEqual(ap.progress, 2)
def test_add_type_one(self): """ Test make progress on an achievement and immediately complete, this is valid as some type of achievement only has two situation: not started/completed. """ ap = Customer_Achievement_Progress(aid=1, uid=3, progress=0, total=1) user = User(uid=3, name='cus', password='******', email='test', type=-1) points = Points(pid=1, uid=3, rid=1, points=20) achievement = Achievements(aid=1, rid=1, name='test', experience=20, points=20, type=0, value=';20') db.session.add(ap) db.session.add(user) db.session.add(points) db.session.add(achievement) db.session.commit() achievementhelper.add_one_progress_bar(ap, 1, 3) self.assertEqual(points.points, 40) self.assertEqual(ap.progress, 1)
def test_get_achievement_with_progress_data_by_uid_with_no_progress(self): ''' Tests get_achievement_with_progress_data(aid, uid) for a uid that does not match any achievement progress entry. ''' achievement = Achievements(rid=12, name="test", points=10, experience=15, type=0, value="Item;5;;;") db.session.add(achievement) db.session.commit() self.assertEqual(achievementhelper.get_achievement_with_progress_data(1, 6), { "aid": 1, "name": "test", "description": "Buy Item 5 times.", "experience": 15, "points": 10, "progressMax": 5, "progress": 0 })
def test_get_achievement_with_progress_data_with_valid_aid_and_progress(self): ''' Tests get_achievement_with_progress_data(aid, uid) for an existing aid and an existing customer progress entry. ''' achievement = Achievements(rid=12, name="test", points=10, experience=15, type=0, value="Item;5;;;") achievementProgress = Customer_Achievement_Progress(uid=6, aid=1, progress=2, total=5) db.session.add(achievement) db.session.add(achievementProgress) db.session.commit() self.assertEqual(achievementhelper.get_achievement_with_progress_data(1, 6), { "aid": 1, "name": "test", "description": "Buy Item 5 times.", "experience": 15, "points": 10, "progressMax": 5, "progress": 2 })
def test_get_info(self): """ Test on get info function """ ap1 = Customer_Achievement_Progress(aid=12, uid=7, progress=2, total=3, update=NOW) ap2 = Customer_Achievement_Progress(aid=7, uid=7, progress=1, total=5, update=BIG) a12 = Achievements(aid=12, rid=3, name='10off', experience=10, points=10, type=0, value="coffee;3;False;2020-08-11;2020-08-12") a7 = Achievements(aid=7, rid=1, name='20off', experience=1, points=10, type=2, value=";5;False;2020-08-11;2020-08-12") r1 = Restaurant(rid=1, name="kfc", address="kfc road", uid=888) r3 = Restaurant(rid=3, name="Starbucks", address="Starbucks Road", uid=666) db.session.add(ap1) db.session.add(ap2) db.session.add(a12) db.session.add(a7) db.session.add(r1) db.session.add(r3) db.session.commit() recent_achievements = achievementhelper.get_recently_update_achievements( 7) achievement = achievementhelper.get_updated_info(recent_achievements) self.assertEqual( { 'aid': 7, 'uid': 7, 'progress': 1, 'progressMax': 5, 'description': 'Visit with a group of at least 5 people between 2020-08-11 and 2020-08-12.', 'name': '20off', 'points': 10, 'experience': 1, 'rname': 'kfc', 'raddress': 'kfc road' }, achievement[0]) self.assertEqual( { 'aid': 12, 'uid': 7, 'progress': 2, 'progressMax': 3, 'description': 'Buy coffee 3 times between 2020-08-11 and 2020-08-12.', 'name': '10off', 'points': 10, 'experience': 10, 'rname': 'Starbucks', 'raddress': 'Starbucks Road' }, achievement[1])
def test_get_description(self): """ Tests get_achievement_description() for all achievement types. """ achievement1 = Achievements(rid=12, name="test", points=10, experience=15, type=0, value="Item;5;True;;") achievement2 = Achievements(rid=13, name="test 2", points=15, experience=20, type=1, value=";6.99;True;;") achievement3 = Achievements(rid=13, name="test 3", points=15, experience=20, type=2, value=";2;True;;") achievement4 = Achievements(rid=13, name="test 4", points=15, experience=20, type=3, value=";5;True;;") achievement5 = Achievements(rid=12, name="test 5", points=10, experience=15, type=0, value="Item;5;False;2020-08-01;2020-08-31") achievement6 = Achievements(rid=13, name="test 6", points=15, experience=20, type=1, value=";6.99;False;2020-08-01;2020-08-31") achievement7 = Achievements(rid=13, name="test 7", points=15, experience=20, type=2, value=";2;False;2020-08-01;2020-08-31") achievement8 = Achievements(rid=13, name="test 8", points=15, experience=20, type=3, value=";5;False;2020-08-01;2020-08-31") self.assertEqual(get_achievement_description(achievement1), "Buy Item 5 times.") self.assertEqual(get_achievement_description(achievement2), "Spend $6.99 in a single visit.") self.assertEqual(get_achievement_description(achievement3), "Visit with a group of at least 2 people.") self.assertEqual(get_achievement_description(achievement4), "Visit 5 times.") self.assertEqual( get_achievement_description(achievement5), "Buy Item 5 times between 2020-08-01 and 2020-08-31.") self.assertEqual( get_achievement_description(achievement6), "Spend $6.99 in a single visit between 2020-08-01 and 2020-08-31.") self.assertEqual( get_achievement_description(achievement7), "Visit with a group of at least 2 people between 2020-08-01 and 2020-08-31." ) self.assertEqual(get_achievement_description(achievement8), "Visit 5 times between 2020-08-01 and 2020-08-31.")
def test_sort_on_long(self): """ Test sorting on more than 3 achievement progress """ ac1 = Achievements(aid=12, rid=12, name='test 3', experience=15, points=20, type=1, value='test') ac2 = Achievements(aid=7, rid=12, name='test 2', experience=15, points=15, type=1, value='test') ac3 = Achievements(aid=22, rid=12, name='test', experience=10, points=15, type=1, value='test') ac4 = Achievements(aid=18, rid=12, name='test 4', experience=10, points=10, type=1, value='test') ac5 = Achievements(aid=17, rid=12, name='test 5', experience=5, points=15, type=1, value='test') db.session.add(ac1) db.session.add(ac2) db.session.add(ac3) db.session.add(ac4) db.session.add(ac5) ap1 = Customer_Achievement_Progress(aid=12, uid=7, progress=2, total=3, update=NOW) ap2 = Customer_Achievement_Progress(aid=7, uid=7, progress=1, total=5, update=MIN) ap3 = Customer_Achievement_Progress(aid=22, uid=7, progress=5, total=5, update=MAX) ap4 = Customer_Achievement_Progress(aid=18, uid=7, progress=2, total=3, update=BIG) ap5 = Customer_Achievement_Progress(aid=17, uid=7, progress=1, total=5, update=SMALL) db.session.add(ap1) db.session.add(ap2) db.session.add(ap3) db.session.add(ap4) db.session.add(ap5) db.session.commit() achievements = achievementhelper.get_recently_update_achievements(7) # if sort success, order should be ap3->ap4->ap1 self.assertEqual(achievements[0].aid, 22) self.assertEqual(achievements[1].aid, 18) self.assertEqual(achievements[2].aid, 12)