def test_method_stops_tracking_last_activity_properly_when_there_are_more_than_one_activities_in_db(
         self, test_activity,
         test_user_with_multiple_activities_on_multiple_days):
     ActivityManager.stop_tracking_last_activity()
     assert ActivityGateway.fetch_last_activity_started(
     )._id == test_activity._id
     assert ActivityGateway.fetch_last_activity_started().ended
 def stop_tracking_last_activity(cls) -> None:
     try:
         a = ActivityGateway.fetch_last_activity_started()
         a.end()
         ActivityGateway.update_activity_in_db(a)
     except IndexError:
         pass
 def test_total_activity_length_is_correct_activity_was_performed_once_today_and_hasnt_ended(
         self, test_db, test_activity):
     test_activity.started_at = test_activity.started_at.shift(
         seconds=-1000)
     ActivityGateway.update_activity_in_db(test_activity)
     report = TimeSpentInCategoryReport.generate_for_this_category_of_activity(
         test_activity.category, 0)
     assert report[test_activity.category] == 1000
 def test_activity_was_performed_once_today_and_hasnt_ended(
         self, test_activity):
     test_activity.started_at = test_activity.started_at.shift(
         seconds=-1000)
     ActivityGateway.update_activity_in_db(test_activity)
     report = TimeSpentInCategoryReport.generate_for_all_categories_of_activity(
         0)
     assert report[test_activity.category] == 1000
 def test_after_activity_ends_its_status_changes_and_end_time_is_recorded(
         self, test_activity):
     test_activity.end()
     ActivityGateway.update_activity_in_db(test_activity)
     assert test_activity.ended_at is not None
     assert test_activity.status != test_activity.STARTED
     assert ActivityGateway.fetch_activity(
         test_activity).status == test_activity.ENDED
    def test_one_acitivity_returned_when_theres_only_one_with_such_category(
            self, test_activity):
        test_activity.started_at = test_activity.started_at.shift(days=-3)
        ActivityGateway.update_activity_in_db(test_activity)

        assert len(
            ActivityGateway.activities_in_last_n_days_in_this_category(
                3, test_activity.category)) == 1
    def test_no_activities_returned_when_there_are_no_activitie_on_or_after_specificed_day(
            self, test_activity):
        test_activity.started_at = test_activity.started_at.shift(days=-4)
        test_activity.end()
        ActivityGateway.update_activity_in_db(test_activity)

        assert len(
            ActivityGateway.activities_in_last_n_days_in_this_category(
                3, test_activity.category)) == 0
 def test_no_activities_returned_when_activitie_with_such_category_dont_exist_at_all(
         self, test_activity):
     test_activity.started_at = test_activity.started_at.shift(days=-2)
     test_activity.end()
     ActivityGateway.update_activity_in_db(test_activity)
     ActivityManager.start_new_activity('sleeping')
     assert len(
         ActivityGateway.activities_in_last_n_days_in_this_category(
             2, 'working')) == 0
    def test_activity_updated_is_persisted(self, test_activity):
        a = ActivityGateway.fetch_activity(test_activity)
        assert a.ended is False

        test_activity.end()
        ActivityGateway.update_activity_in_db(test_activity)

        a = ActivityGateway.fetch_activity(test_activity)
        assert a.ended is True
    def test_user_has_more_than_one_activity_today_but_only_one_is_what_we_want_and_more_activities_on_other_days(
            self, test_user_with_multiple_activities_on_multiple_days, test_db,
            test_activity):
        test_working_activity = ActivityManager.start_new_activity('working')
        ActivityGateway.add_new_activity_to_db(test_working_activity)

        todays_activities = ActivityGateway.activities_today_in_this_category(
            'working')
        assert len(todays_activities) == 1
        assert test_working_activity._id == todays_activities[0]._id
    def test_user_has_more_than_one_activity_today_and_more_on_other_days(
            self, test_user_with_multiple_activities_on_multiple_days, test_db,
            test_activity):
        test_working_activity = ActivityManager.start_new_activity('working')
        ActivityGateway.add_new_activity_to_db(test_working_activity)

        todays_activities = ActivityGateway.activities_today()
        assert len(todays_activities) == 2
        _ids = [a._id for a in todays_activities]
        assert test_activity._id in _ids
        assert test_working_activity._id in _ids
    def test_multiple_activities_that_started_today_and_have_ended(
            self, test_activity):
        test_activity.started_at = test_activity.started_at.shift(
            seconds=-1000)
        ActivityGateway.update_activity_in_db(test_activity)
        make_activity(0, 200, 'sleeping')

        report = TimeSpentInCategoryReport.generate_for_all_categories_of_activity(
            0)
        assert report[test_activity.category] == 1000
        assert report['sleeping'] == 200
Example #13
0
def make_activity(started_days_ago, length_in_seconds, category):
    if started_days_ago == 0:
        seconds_to_shift = length_in_seconds
    else:
        seconds_to_shift = 0

    act1 = ActivityManager.start_tracking_new_activity(category)
    act1.started_at = act1.started_at.shift(days=-started_days_ago,
                                            seconds=-seconds_to_shift)
    act1.end()
    act1.ended_at = act1.started_at.shift(seconds=length_in_seconds)
    ActivityGateway.update_activity_in_db(act1)
    def test_one_day_report_shown_when_days_argument_is_not_given_and_activity_is_specified(
            self, test_activity):
        test_activity.end()
        test_activity.ended_at = test_activity.started_at.shift(seconds=120)
        ActivityGateway.update_activity_in_db(test_activity)

        runner = CliRunner()
        result = runner.invoke(cli, ['--activity', 'test_activity'])

        assert result.exit_code == 0
        assert result.output.count('\n') == 1
        _assert_in_output(result.output,
                          ['test_activity', 'today', '2 minutes'])
    def test_activity_was_performed_multiple_times_today_and_all_except_one_already_ended(
            self, test_db,
            test_user_with_multiple_activities_of_same_category_done_today,
            test_activity):
        unfinished_activity = ActivityManager.start_new_activity(
            test_activity.category)
        unfinished_activity.started_at = unfinished_activity.started_at.shift(
            seconds=-500)
        ActivityGateway.add_new_activity_to_db(unfinished_activity)

        report = TimeSpentInCategoryReport.generate_for_this_category_of_activity(
            test_activity.category, 0)
        assert report[test_activity.category] == 8500
    def test_activity_performed_multiple_times_on_different_days(
            self, test_activity):
        test_activity.started_at = test_activity.started_at.shift(
            seconds=-2000)
        test_activity.end()
        ActivityGateway.update_activity_in_db(test_activity)

        make_activity(1, 3000, test_activity.category)
        make_activity(2, 1000, test_activity.category)
        make_activity(3, 500, test_activity.category)

        report = TimeSpentInCategoryReport.generate_for_this_category_of_activity(
            test_activity.category, 2)
        assert report[test_activity.category] == 6000
Example #17
0
    def test_last_activity_ends_no_new_activity_is_created(
            self, test_activity):
        assert len(ActivityGateway.activities()) == 1
        assert test_activity.ended is False

        runner = CliRunner()
        result = runner.invoke(cli, ['stop'])

        assert result.exit_code == 0
        assert result.output.count('\n') == 1
        _assert_in_output(result.output, ['Stopped', test_activity.category])

        assert ActivityGateway.fetch_last_activity_started().ended is True
        assert len(ActivityGateway.activities()) == 1
Example #18
0
    def generate_for_this_category_of_activity(cls, category: str,
                                               days: int) -> Dict[str, int]:
        total_activity_length = 0

        if days == 0:
            for activity in ActivityGateway.activities_today_in_this_category(
                    category):
                total_activity_length += activity.length
        else:
            for activity in ActivityGateway.activities_in_last_n_days_in_this_category(
                    days, category):
                total_activity_length += activity.length

        return {category: total_activity_length}
Example #19
0
    def test_track_new_activity_when_another_one_is_already_being_tracked(
            self, test_activity):
        assert len(ActivityGateway.activities()) == 1
        assert test_activity.ended is False

        runner = CliRunner()
        result = runner.invoke(cli, ['working'])

        assert result.exit_code == 0
        assert result.output.count('\n') == 2
        _assert_in_output(
            result.output,
            ['Started', 'tracking', 'working', 'So far', 'spent', 'today'])

        assert ActivityGateway.fetch_activity(test_activity).ended is True
        assert len(ActivityGateway.activities()) == 2
Example #20
0
def cli(activity):
    """
    Track how much time you spend on different activities throughout the day!
    ACTIVITY is the type of activity you want to start tracking. Examples: working, reading, studying.
    To see a report, use the "timereport" command.
    """

    if activity in RESERVED_ARGUMENTS:
        if activity.lower() == STOP_TRACKING_TIME_ARGUMENT:
            try:
                last_activity = ActivityGateway.fetch_last_activity_started()
                ActivityManager.stop_tracking_last_activity()
                click.echo('Stopped tracking {}'.format(last_activity.category))
            except IndexError:
                click.echo('I was not tracking any activity!')

    else:
        a = ActivityManager.start_tracking_new_activity(activity)
        click.echo('Started tracking "%s".' % a.category)
        click.echo(
            "So far, you've spent {} on {} today!".format(
                format_seconds_returnbed_by_report(
                    TimeSpentInCategoryReport.generate_for_this_category_of_activity(activity, 0)[activity]
                ),
                activity
            )
        )
    def test_correct_user_activities_are_found_when_user_has_multiple_activities(
            self, test_sleeping_activity, test_working_activity, test_db):

        test_db.data = [test_sleeping_activity, test_working_activity]
        activities = ActivityGateway.activities()

        assert len(activities) == 2
 def test_user_has_activities_on_different_days_but_only_1_today(
         self, test_user_with_multiple_activities_on_multiple_days,
         test_activity, test_db):
     todays_activities = ActivityGateway.activities_today_in_this_category(
         test_activity.category)
     assert len(todays_activities) == 1
     assert test_activity._id == todays_activities[0]._id
    def test_correct_activities_returned_when_there_are_activities_on_and_after_specified_day(
            self):
        make_activity(2, 10000, 'working')
        make_activity(3, 20000, 'sleeping')
        make_activity(4, 20000, 'eating')

        assert len(ActivityGateway.activities_in_the_last_n_days(4)) == 3
    def test_number_of_days_is_zero(self, test_activity):
        make_activity(1, 10000, 'working')
        make_activity(3, 20000, 'working')

        assert len(
            ActivityGateway.activities_in_last_n_days_in_this_category(
                0, test_activity.category)) == 1
Example #25
0
    def generate_for_all_categories_of_activity(cls,
                                                days: int) -> Dict[str, int]:
        activities_length = defaultdict(int)

        for activity in ActivityGateway.activities_in_the_last_n_days(days):
            activities_length[activity.category] += activity.length

        return activities_length
Example #26
0
    def test_new_ativity_to_track_is_same_category_of_the_last_activity_being_tracked(
            self, test_activity):
        assert len(ActivityGateway.activities()) == 1
        assert test_activity.ended is False

        runner = CliRunner()
        result = runner.invoke(cli, [test_activity.category])

        assert result.exit_code == 0
        assert result.output.count('\n') == 2
        _assert_in_output(result.output, [
            'Started', 'tracking', test_activity.category, 'So far', 'spent',
            'today'
        ])

        assert ActivityGateway.fetch_activity(test_activity).ended is True
        assert len(ActivityGateway.activities()) == 2
    def test_only_one_activity_returned_when_the_rest_the_activities_with_such_category_are_on_previous_days(
            self):
        make_activity(2, 10000, 'working')
        make_activity(3, 20000, 'working')
        make_activity(4, 20000, 'working')

        assert len(
            ActivityGateway.activities_in_last_n_days_in_this_category(
                2, 'working')) == 1
Example #28
0
    def test_theres_no_activity_being_tracked_when_stop_command_is_issued(
            self):
        assert len(ActivityGateway.activities()) == 0

        runner = CliRunner()
        result = runner.invoke(cli, ['stop'])

        assert result.exit_code == 0
        assert result.output.count('\n') == 1
        _assert_in_output(result.output, ['not tracking'])
Example #29
0
    def test_track_new_activity_for_the_first_time(self):
        assert len(ActivityGateway.activities()) == 0

        runner = CliRunner()
        result = runner.invoke(cli, ['working'])

        assert result.exit_code == 0
        assert result.output.count('\n') == 2
        _assert_in_output(
            result.output,
            ['Started', 'tracking', 'working', 'So far', 'spent', 'today'])
    def test_new_activity_turns_up_in_search_results(self, test_db,
                                                     test_activity):
        assert len(ActivityGateway.activities()) == 1
        test_activity.end()
        ActivityGateway.update_activity_in_db(test_activity)

        a = ActivityManager.start_new_activity('test2_activity')
        ActivityGateway.add_new_activity_to_db(a)
        assert len(ActivityGateway.activities()) == 2