def test_get_recent_achievements_no_relevant_achievements_to_filter(self):
        """
        Tests get_recently_started_achievements() when the user does not have progress relevant
        to the achievements in the given achievement list.
        """
        achievementProgress1 = Customer_Achievement_Progress(uid=5,
                                                             aid=5,
                                                             progress=2,
                                                             total=5)
        achievementProgress2 = Customer_Achievement_Progress(uid=6,
                                                             aid=12,
                                                             progress=1,
                                                             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_recently_started_achievements(
                achievement_list, 5), [])
Exemple #2
0
 def test_many_achievements(self):
     """
     Test a list of many coupons
     """
     achievementProgress1 = Customer_Achievement_Progress(uid=5,
                                                          aid=10,
                                                          progress=3,
                                                          total=3)
     achievementProgress2 = Customer_Achievement_Progress(uid=6,
                                                          aid=10,
                                                          progress=2,
                                                          total=3)
     achievementProgress3 = Customer_Achievement_Progress(uid=6,
                                                          aid=11,
                                                          progress=2,
                                                          total=6)
     db.session.add(achievementProgress1)
     db.session.add(achievementProgress2)
     db.session.add(achievementProgress3)
     db.session.commit()
     achievement_list = [{
         "aid": 10,
         "name": "testing",
         "description": "This is just for a test",
         "experience": 50,
         "points": 100,
         "progressMax": 3,
         "expired": False
     }, {
         "aid": 11,
         "name": "another test",
         "description": "This is just for a test",
         "experience": 200,
         "points": 50,
         "progressMax": 6,
         "expired": True
     }]
     actual = get_achievement_progress_stats(achievement_list)
     expected = [{
         "aid": 10,
         "name": "testing",
         "description": "This is just for a test",
         "experience": 50,
         "points": 100,
         "progressMax": 3,
         "expired": False,
         "in progress": 1,
         "complete": 1
     }, {
         "aid": 11,
         "name": "another test",
         "description": "This is just for a test",
         "experience": 200,
         "points": 50,
         "progressMax": 6,
         "expired": True,
         "in progress": 1,
         "complete": 0
     }]
     self.assertEqual(actual, 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
                }])
Exemple #4
0
    def test_some_achievements_have_progress(self):
        '''
        Tests appending progress entry count data to an achievement list
        where some of the achievements have progress entries
        '''
        progress1 = Customer_Achievement_Progress(aid=10,
                                                  uid=3,
                                                  progress=4,
                                                  total=6)
        progress2 = Customer_Achievement_Progress(aid=10,
                                                  uid=4,
                                                  progress=6,
                                                  total=6)
        progress3 = Customer_Achievement_Progress(aid=11,
                                                  uid=4,
                                                  progress=1,
                                                  total=5)

        db.session.add(progress1)
        db.session.add(progress2)
        db.session.add(progress3)
        db.session.commit()

        achievement_list = self.achievement_list_helper()
        self.assertEqual(
            achievementhelper.get_achievements_with_progress_entry_count(
                achievement_list), [{
                    "aid": 10,
                    "name": "test",
                    "description": "description",
                    "experience": 10,
                    "points": 15,
                    "progressMax": 6,
                    "expired": 0,
                    "progress_entries": 2
                }, {
                    "aid": 11,
                    "name": "test 2",
                    "description": "description 2",
                    "experience": 15,
                    "points": 20,
                    "progressMax": 5,
                    "expired": 0,
                    "progress_entries": 1
                }, {
                    "aid": 12,
                    "name": "test 3",
                    "description": "description 3",
                    "experience": 15,
                    "points": 15,
                    "progressMax": 3,
                    "expired": 0,
                    "progress_entries": 0
                }])
Exemple #5
0
    def test_get_achievements_progress_data_no_achievements_to_filter(self):
        """
        Tests get_achievements_with_progress_data() when the given achievement list to
        filter is empty.
        """
        achievementProgress1 = Customer_Achievement_Progress(uid=5, aid=10, progress=3, total=6)
        achievementProgress2 = Customer_Achievement_Progress(uid=5, aid=11, progress=1, total=5)
        db.session.add(achievementProgress1)
        db.session.add(achievementProgress2)
        db.session.commit()

        self.assertEqual(achievementhelper.get_achievements_with_progress_data([], 5), [])
 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)
Exemple #7
0
    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_found_with_same_aid(self):
     """
     Test get the normal achievement progress by providing valid the same aid(same achievement) and uid.
     """
     ap1 = Customer_Achievement_Progress(aid=18, uid=4, progress=3, total=5)
     ap2 = Customer_Achievement_Progress(aid=18, uid=8, progress=3, total=5)
     db.session.add(ap1)
     db.session.add(ap2)
     db.session.commit()
     a = achievementhelper.get_exact_achivement_progress(aid=18, uid=8)
     self.assertEqual(a.aid, ap2.aid)
     self.assertEqual(a.uid, ap2.uid)
     self.assertEqual(a.progress, ap2.progress)
     self.assertEqual(a.total, ap2.total)
    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 test_uid_not_found(self):
     """
     Test get the achievement progress by providing valid aid but invalid uid. Expect an error message.
     """
     achievement_progress = Customer_Achievement_Progress(aid=17, uid=4, progress=3, total=5)
     db.session.add(achievement_progress)
     db.session.commit()
     a = achievementhelper.get_exact_achivement_progress(aid=17, uid=5)
     self.assertEqual(a, 'Not Found')
    def test_get_recent_achievements_only_complete_achievements(self):
        """Tests get_recently_started_achievements() when the user only has complete progress (progress
        = progressMax) relevant to the achievements in the given achievement list."""
        achievementProgress1 = Customer_Achievement_Progress(uid=5,
                                                             aid=11,
                                                             progress=5,
                                                             total=5)
        achievementProgress2 = Customer_Achievement_Progress(uid=5,
                                                             aid=12,
                                                             progress=3,
                                                             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), [])
Exemple #12
0
 def test_progress_complete(self):
     '''
     Check status of a complete achievement progress entry
     '''
     progress = Customer_Achievement_Progress(aid=1,
                                              uid=3,
                                              progress=4,
                                              total=4)
     self.assertEqual(
         achievementhelper.get_progress_completion_status(progress), 2)
 def test_get_nonexistent_achievement_progress(self):
     """
     Tests get_achievement_progress_by_uid() when no achievements have a uid
     matching the given uid.
     """
     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(
         10)
     self.assertEqual(achievement_progress_list, [])
 def test_normal_found(self):
     """
     Test get the normal achievement progress by providing valid aid and uid.
     """
     achievement_progress = Customer_Achievement_Progress(aid=17, uid=4, progress=3, total=5)
     db.session.add(achievement_progress)
     db.session.commit()
     a = achievementhelper.get_exact_achivement_progress(aid=17, uid=4)
     self.assertEqual(a.aid, achievement_progress.aid)
     self.assertEqual(a.uid, achievement_progress.uid)
     self.assertEqual(a.progress, achievement_progress.progress)
     self.assertEqual(a.total, achievement_progress.total)
Exemple #15
0
    def test_achievement_list_empty(self):
        '''
        Tests appending progress entry count data to an empty achievement
        list.
        '''
        progress = Customer_Achievement_Progress(aid=1,
                                                 uid=3,
                                                 progress=3,
                                                 total=4)
        db.session.add(progress)
        db.session.commit()

        self.assertEqual(
            achievementhelper.get_achievements_with_progress_entry_count([]),
            [])
 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 insert_new_achievement(aid, uid, total):
    """
    Insert a new achievement_progress into the database.

    Args:
        aid: achievement id of the achievement
        uid: user id of the achievement_progress, the owner of the achievement_progress
        total: the maxProgress of the achievement_progress

    Returns:
        the newly inserted achievement_progress
    """
    update = datetime.now()
    ap = Customer_Achievement_Progress(aid=aid, uid=uid, progress=0, total=total, update=update)
    db.session.add(ap)
    db.session.commit()
    return ap
 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_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_achievement_with_progress_data_by_uid_with_no_relevant_progress(self):
        '''
        Tests get_achievement_with_progress_data(aid, uid) for a uid that
        does not match any achievement progress entry for the given aid.
        '''
        achievement = Achievements(rid=12, name="test", points=10, experience=15, type=0, value="Item;5;;;")
        achievementProgress = Customer_Achievement_Progress(uid=6, aid=11, 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": 0
            })
 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_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)