def test_time_left(self, start, end, time_left, expected):
     # 1. No start, no end, no time left
     # 2. Has start, no end, no time left
     # 3. Has start, no end, has time left
     # 4. Has start, has end, has no time left
     # 5. Has start, has end, has time left
     program = TrackedProgram("test", 10, start, end, time_left)
     program.elapsed_time = 0
     assert program.time_left == expected
    def test_get_program(self):
        # Tests that a program object with the same name is retrieved from the program_objs
        t1_empty = TrackedProgram.min_init("test1", 1)
        t2_empty = TrackedProgram.min_init("test2", 2)
        t3_empty = TrackedProgram.min_init("test3", 3)
        programs = [t1_empty, t2_empty, t3_empty]

        assert Program.get_program(t1_empty, programs) == t1_empty
        assert Program.get_program(t2_empty, programs) == t2_empty
        assert Program.get_program(t3_empty, programs) == t3_empty
Esempio n. 3
0
def test_get_program_from_arr(state_change_blank_fixture, default_init):
    state_detector = state_change_blank_fixture
    program1 = default_init
    program2 = TrackedProgram.min_init("test2", 200)
    all_programs = [program1, program2]
    state_detector.curr_state = all_programs

    # Retrieves the same program, but may not be the same instance. Is based off of name
    retrieved_pg = Program.get_program(TrackedProgram.min_init("test2", 500),
                                       state_detector.curr_state)
    assert retrieved_pg == program2
 def test_time_left_w_elapsed(self, start, end, time_left, elapsed,
                              expected):
     # 1. No start, no end, no time left, has elapsed (impossible)
     # 2. Has start, no end, no time left, has elapsed
     # 3. Has start, no end, has time left, has elapsed
     # 4. Has start, has end, has no time left, has elapsed
     # 5. Has start, has end, has time left, has elapsed
     # 6. No start, no end, time left as max time, no elapsed
     # 6. No start, no end, time left not as max tim, no elapsed
     program = TrackedProgram("test", 10, start, end, time_left)
     program.elapsed_time = elapsed
     assert program.time_left == expected
Esempio n. 5
0
def test_get_latest_save_empty(reset_db):
    base_date = datetime.today().timestamp()
    # Programs that ran on a past date
    program1_a = TrackedProgram("test1", 100, 200, 300, 0)
    program2_a = TrackedProgram("test2", 100, 300, 375, 25)

    programs = [program1_a, program2_a]
    DataManager.store_many(*programs)

    program1_blank = TrackedProgram.min_init("test1", 100)
    program2_blank = TrackedProgram.min_init("test2", 100)

    latest_1 = DataManager.get_latest_save(program1_blank)
    latest_2 = DataManager.get_latest_save(program2_blank)

    assert latest_1 == None
    assert latest_2 == None
Esempio n. 6
0
def test_get_latest_save(reset_db):
    base_date = datetime.today().timestamp()
    # Programs that ran on a past date
    program1_a = TrackedProgram("test1", 100, 200, 300, 0)
    program2_a = TrackedProgram("test2", 100, 300, 375, 25)

    # Tests that the most current program from the current day is retrieved
    time_offset = 100
    program1_b = TrackedProgram("test1", 100, base_date, base_date + 50, 50)
    program1_c = TrackedProgram("test1", 100, base_date + time_offset,
                                base_date + time_offset + 75, 25)
    program2_b = TrackedProgram("test2", 100, base_date + 2 * time_offset,
                                base_date + 2 * time_offset + 100, 0)

    programs = [program1_a, program2_a, program1_b, program2_b, program1_c]
    DataManager.store_many(*programs)

    program1_blank = TrackedProgram.min_init("test1", 100)
    program2_blank = TrackedProgram.min_init("test2", 100)

    latest_1 = DataManager.get_latest_save(program1_blank)
    latest_2 = DataManager.get_latest_save(program2_blank)

    assert latest_1 == program1_c
    assert latest_2 == program2_b
 def test_init(self, ):
     pg = TrackedProgram("test", 2, 1, 2, 0)
     assert pg.name == "test"
     assert pg.max_time == 2
     assert pg.db_id is None
     assert pg.start_time == 1
     assert pg.end_time == 2
     assert pg.time_left == 0.0
     assert pg.blocked is False
 def test_from_dict(self, default_init, dict_params):
     pg = TrackedProgram.dict_init(dict_params)
     assert pg.name == default_init.name
     assert pg.max_time == default_init.max_time
     assert pg.db_id == default_init.db_id
     assert pg.start_time == default_init.start_time
     assert pg.end_time == default_init.end_time
     assert pg.time_left == default_init.time_left
     assert pg.blocked == default_init.blocked
 def test_min_init(self, ):
     pg = TrackedProgram.min_init("test", 10)
     assert pg.name == "test"
     assert pg.max_time == 10
     assert pg.db_id is None
     assert pg.start_time == 0
     assert pg.end_time == 0
     assert pg._time_left == 10.0
     assert pg.blocked is False
Esempio n. 10
0
def test_init_program_objs_with_save_no_db(time_left, expected_left,
                                           expected_block):
    # Tests if there is a saved program and it updates only the time left and blocked status
    test1 = TrackedProgram.min_init("test1", 100)
    test1_saved = TrackedProgram("test1", 100, 50, 60, time_left)

    with patch('manager_core.CurbTheScreen.ProgramStates.tracked_from_settings'
               ) as tracked:
        tracked.return_value = [test1]
        with patch('manager_core.CurbTheScreen.TrackedProgram.has_save_today'
                   ) as has_save:
            has_save.return_value = True
            with patch('manager_core.CurbTheScreen.DataManager.get_latest_save'
                       ) as latest_save:
                latest_save.return_value = test1_saved
                ps = ProgramStates()

                assert ps.program_objs[0].time_left == expected_left
                assert ps.program_objs[0].blocked == expected_block
Esempio n. 11
0
def date_fixture(create_db):
    base = datetime(2020, 11, 1)
    date1 = base + timedelta(days=1)
    date2 = base + timedelta(days=2)
    date3 = base + timedelta(days=3)
    offset = 1000

    test1 = TrackedProgram("test1", 10, base.timestamp(),
                           base.timestamp() + offset, 100)
    test2 = TrackedProgram("test2", 10, date1.timestamp(),
                           date1.timestamp() + offset, 100)
    test3 = TrackedProgram("test2", 10, date2.timestamp(),
                           date2.timestamp() + offset, 100)
    test4 = TrackedProgram("test4", 10, date3.timestamp(),
                           date3.timestamp() + offset, 100)
    DataManager.store_many(test1, test2, test3, test4)

    date_values = {
        "test_objs": (test1, test2, test3, test4),
        "date_objs": (base, date1, date2, date3)
    }

    return date_values
Esempio n. 12
0
def test_populate_program_pids(states_fixture_full):
    ps = states_fixture_full

    with patch('manager_core.CurbTheScreen.psutil.process_iter'
               ) as mocked_process_iter:
        with patch(
                'manager_core.CurbTheScreen.psutil.Process') as mocked_process:
            temp_program = TrackedProgram.min_init("test1", 100)

            # Tests when pids are added that match the name of the program
            mocked_process.info = {"name": 'test1', "pid": 1}
            mocked_process_iter.return_value = iter([mocked_process])
            ps.populate_program_pids()
            mocked_process_iter.assert_called_with(attrs=["name", "pid"])

            assert [1] == ps.get_program(temp_program).PIDS

            mocked_process.info = {"name": 'test1', "pid": 2}
            mocked_process_iter.return_value = iter([mocked_process])
            ps.populate_program_pids()

            assert [1, 2] == ps.get_program(temp_program).PIDS

            # Tests that the program is retrieved when it contains the name of the program
            mocked_process.info = {"name": 'Containstest1', "pid": 3}
            mocked_process_iter.return_value = iter([mocked_process])

            ps.populate_program_pids()
            assert [1, 2, 3] == ps.get_program(temp_program).PIDS

            # Tests when the name is not contained
            # Tests that the program is retrieved when it contains the name of the program
            mocked_process.info = {"name": 'test42', "pid": 10}
            mocked_process_iter.return_value = iter([mocked_process])

            ps.populate_program_pids()
            assert [1, 2, 3] == ps.get_program(temp_program).PIDS
Esempio n. 13
0
def test_get_day(reset_db, date_fixture):
    base, date1, date2, date3 = date_fixture['date_objs']
    test1, test2, test3, test4 = date_fixture['test_objs']
    offset = 1000

    # Test getting a single days result
    day_results = DataManager.get_day(base)
    assert [test1] == day_results

    day_results = DataManager.get_day(date1)
    assert [test2] == day_results

    # Tests with a filtering argument
    day_results = DataManager.get_day(base, name="test5")
    assert [] == day_results

    # Getting multiple objects that are in the same day
    test5 = TrackedProgram("test5", 10,
                           base.timestamp() + offset,
                           base.timestamp() + 2 * offset, 10)
    DataManager.store_new(test5)

    day_results = DataManager.get_day(base)
    assert [test1, test5] == day_results
Esempio n. 14
0
 def test_max_time_setter(self):
     pg = TrackedProgram.min_init("test1", 90)
     pg.max_time = 100
     assert pg.max_time == 100
Esempio n. 15
0
 def test_name_setter(self):
     pg = TrackedProgram.min_init("test1", 90)
     pg.name = "test2"
     assert pg.name == "test2"
Esempio n. 16
0
 def test_time_left_setter(self):
     pg = TrackedProgram.min_init("test1", 90)
     pg.time_left = 100
     assert pg.time_left == 100
Esempio n. 17
0
def example_pg_class():
    pg = Program(TrackedProgram.min_init("test", 100))
    return pg
Esempio n. 18
0
def program_class_fixture():
    test1 = TrackedProgram('test1', 100, 0, 0, 100)
    test2 = TrackedProgram('test2', 50, 0, 0, 50)
    test3 = TrackedProgram('test3', 25, 0, 0, 25)
    return test1, test2, test3
Esempio n. 19
0
def blank():
    return TrackedProgram("test", 1, 0, 0, 0)
Esempio n. 20
0
def default_init():
    return TrackedProgram.min_init("test", 100)
Esempio n. 21
0
 def test_equal(self, default_init, name, blocked, start, end, max_time,
                time_left, expected):
     pg1 = TrackedProgram(name, max_time, start, end, time_left)
     pg1.blocked = blocked
     result = pg1 == default_init
     assert result == expected