Beispiel #1
0
def to_autocheck_wrapper(task_doc):
    # We're going to put the task information into the notes, after the word AUTOCHECK.
    task_doc_ad = ad.AttrDict(task_doc)
    note = task_doc_ad.notes
    logging.debug("Considering note = %s" % note)
    if "AUTOCHECK:" in note:
        autocheck_formula_start = note.find(":")
        autocheck_formula_str = note[autocheck_formula_start+1:]
        logging.debug("Found autocheck formula %s" % autocheck_formula_str)
        autocheck_formula = json.loads(autocheck_formula_str)
        t = enehat.Task(autocheck_formula)
        t.habitica_task = task_doc
        t.task_id = task_doc_ad.id
        return t
    else:
        logging.debug("no autocheck found")
        return None
    def testAutomaticRewardActiveTransportation(self):
        # Create a task that we can retrieve later

        self.new_task_text = randomGen()
        new_habit = {
            'type':
            "habit",
            'text':
            self.new_task_text,
            'notes':
            'AUTOCHECK: {"mapper": "active_distance",'
            '"args": {"walk_scale": 1000, "bike_scale": 3000}}'
        }
        habit_id = proxy.create_habit(self.testUUID, new_habit)

        self.dummy_task = enehat.Task()
        self.dummy_task.task_id = habit_id
        logging.debug("in testAutomaticRewardActiveTransportation,"
                      "the new habit id is = %s and task is %s" %
                      (habit_id, self.dummy_task))

        #Create test data -- code copied from TestTimeGrouping
        key = (2016, 5, 3)
        test_section_list = []
        #
        # Since PST is UTC-7, all of these will be in the same UTC day
        # 13:00, 17:00, 21:00
        # so we expect the local date and UTC bins to be the same
        test_section_list.append(
            self._createTestSection(
                arrow.Arrow(2016, 5, 3, 6, tzinfo=tz.gettz(PST)), PST))
        test_section_list.append(
            self._createTestSection(
                arrow.Arrow(2016, 5, 3, 10, tzinfo=tz.gettz(PST)), PST))
        test_section_list.append(
            self._createTestSection(
                arrow.Arrow(2016, 5, 3, 14, tzinfo=tz.gettz(PST)), PST))

        self._fillModeDistanceDuration(test_section_list)
        #logging.debug("durations = %s" % [s.data.duration for s in test_section_list])

        summary_ts = earmt.group_by_timestamp(
            self.testUUID,
            arrow.Arrow(2016, 5, 1).timestamp,
            arrow.Arrow(2016, 6, 1).timestamp, None, [earmts.get_distance])
        logging.debug(
            "in testAutomaticRewardActiveTransportation, result = %s" %
            summary_ts)

        #Get user data before scoring
        user_before = autocheck.get_task_state(self.testUUID, self.dummy_task)
        self.assertIsNone(user_before)

        # Needed to work, otherwise sections from may won't show up in the query!
        modification = {
            "last_timestamp": arrow.Arrow(2016, 5, 1).timestamp,
            "bike_count": 0,
            "walk_count": 0
        }
        autocheck.save_task_state(self.testUUID, self.dummy_task, modification)

        user_before = autocheck.get_task_state(self.testUUID, self.dummy_task)
        self.assertEqual(int(user_before['bike_count']), 0)

        habits_before = proxy.habiticaProxy(self.testUUID, 'GET',
                                            "/api/v3/tasks/user?type=habits",
                                            None).json()
        bike_pts_before = [
            habit['history'] for habit in habits_before['data']
            if habit['text'] == self.new_task_text
        ]
        #Score points
        autocheck.give_points_for_all_tasks(self.testUUID)
        #Get user data after scoring and check results
        user_after = autocheck.get_task_state(self.testUUID, self.dummy_task)
        self.assertEqual(int(user_after['bike_count']), 1500)
        habits_after = proxy.habiticaProxy(self.testUUID, 'GET',
                                           "/api/v3/tasks/user?type=habits",
                                           None).json()
        bike_pts_after = [
            habit['history'] for habit in habits_after['data']
            if habit['text'] == self.new_task_text
        ]
        self.assertTrue(len(bike_pts_after[0]) - len(bike_pts_before[0]) == 2)