Example #1
0
    def test_get_days_with_data(self):
        mock_month_data = {1: {"PyCharm": 1000}, 2: {"IntelliJ": 2000}}

        file_names = ["12-2020.json", "01-2021.json", "09-2019.json"]
        for f_name in file_names:
            with open(os.path.join(self.data_directory, f_name), "w") as file:
                file.write(json.dumps(mock_month_data))

        paths = {DATA_FILES_PATH_KEYWORD: self.data_directory}

        data_backend = DataBackend(paths)
        result = data_backend.get_days_with_data()

        expected_result = {
            2020: {
                12: [1, 2]
            },
            2021: {
                1: [1, 2]
            },
            2019: {
                9: [1, 2]
            }
        }

        self.assertEqual(expected_result, result)
Example #2
0
    def test_get_res_file_path(self):
        paths = {RES_DIRECTORY_KEYWORD: Path("/")}

        data_backend = DataBackend(paths)
        result = data_backend.get_res_file_path("fonts/font.ttf")

        self.assertEqual("/fonts/font.ttf", str(result))
Example #3
0
    def test_does_config_file_exist_false(self):
        paths = {
            CONFIG_FILE_PATH_KEYWORD:
            Path(__file__).joinpath(Path("config.json"))
        }

        data_backend = DataBackend(paths)
        self.assertFalse(data_backend.does_config_file_exist())
Example #4
0
    def test_get_data_file_path_double_digit_month(self):
        paths = {DATA_FILES_PATH_KEYWORD: self.data_directory}

        test_date = date(2020, 10, 1)
        data_backend = DataBackend(paths)

        expected_result = self.data_directory.joinpath("10-2020.json")
        result = data_backend.get_data_file_path(test_date)

        self.assertEqual(expected_result, result)
Example #5
0
    def test_write_month_data_non_empty_data(self):
        paths = {DATA_FILES_PATH_KEYWORD: self.data_directory}

        test_date = date(2020, 1, 1)
        data_backend = DataBackend(paths)
        mock_data = {1: {"PyCharm": 20000, "IntelliJ": 5000}}

        data_backend.write_month_data(mock_data, test_date)

        with open(os.path.join(self.data_directory, "01-2020.json")) as file:
            self.assertEqual(json.dumps(mock_data), file.read())
Example #6
0
    def test_get_existing_months_no_matching_months(self):
        file_names = ["12-2020.json", "01-2021.json", "09-2019.json"]
        for f_name in file_names:
            with open(os.path.join(self.data_directory, f_name), "w") as _:
                pass

        paths = {DATA_FILES_PATH_KEYWORD: self.data_directory}

        data_backend = DataBackend(paths)
        result = data_backend.get_existing_months(date(2022, 1, 1))
        self.assertEqual([], result)
Example #7
0
    def test_write_config_file_not_empty(self):
        temp_config = tempfile.mkstemp()[1]
        paths = {CONFIG_FILE_PATH_KEYWORD: temp_config}

        data_backend = DataBackend(paths)
        mock_config = {"enabled": True}

        data_backend.write_config(mock_config)
        with open(temp_config, "r") as file:
            self.assertDictEqual(mock_config, json.loads(file.read()))

        os.remove(temp_config)
Example #8
0
    def test_read_month_data_file_available(self):
        paths = {DATA_FILES_PATH_KEYWORD: self.data_directory}

        test_date = date(2020, 1, 1)
        mock_data = {1: {"PyCharm": 20000, "IntelliJ": 5000}}

        file_name = "01-2020.json"
        with open(self.data_directory.joinpath(file_name), "w") as file:
            file.write(json.dumps(mock_data))

        data_backend = DataBackend(paths)
        result = data_backend.read_month_data(test_date)
        self.assertDictEqual(mock_data, result)
Example #9
0
    def test_read_config_file_available(self):
        temp_config = tempfile.mkstemp()[1]
        paths = {CONFIG_FILE_PATH_KEYWORD: temp_config}

        mock_config = {"enabled": True}

        with open(temp_config, "w") as file:
            file.write(json.dumps(mock_config))

        data_backend = DataBackend(paths)
        result = data_backend.read_config()

        self.assertDictEqual(mock_config, result)
        os.remove(temp_config)
Example #10
0
    def test_write_month_data_empty_data(self):
        paths = {DATA_FILES_PATH_KEYWORD: self.data_directory}

        test_date = date(2020, 1, 1)
        data_backend = DataBackend(paths)
        self.assertRaises(EmptyMonthDataError, data_backend.write_month_data,
                          {}, test_date)
Example #11
0
    def test_read_month_data_file_not_available(self):
        paths = {DATA_FILES_PATH_KEYWORD: Path("/Some/Invalid/Path")}

        data_backend = DataBackend(paths)
        result = data_backend.read_month_data(date(2020, 1, 1))
        self.assertDictEqual(result, {})
Example #12
0
    def test_does_config_file_exist_true(self):
        temp_config = tempfile.mkstemp()[1]
        paths = {CONFIG_FILE_PATH_KEYWORD: temp_config}

        data_backend = DataBackend(paths)
        self.assertTrue(data_backend.does_config_file_exist())
Example #13
0
 def test_write_config_file_empty_config(self):
     data_backend = DataBackend({})
     self.assertRaises(EmptyConfigError, data_backend.write_config, {})
Example #14
0
    def test_get_existing_years_no_years(self):
        paths = {DATA_FILES_PATH_KEYWORD: self.data_directory}

        data_backend = DataBackend(paths)
        result = data_backend.get_existing_years()
        self.assertEqual([], result)
Example #15
0
 def test_parse_year_from_data_file_name_valid_name_invalid_name(self):
     name = "122020.json"
     data_backend = DataBackend({})
     self.assertRaises(InvalidMonthDataFileNameError,
                       data_backend.parse_year_from_data_file_name, name)
Example #16
0
 def test_parse_year_from_data_file_name_valid_name(self):
     name = "12-2020.json"
     data_backend = DataBackend({})
     self.assertEqual(2020,
                      data_backend.parse_year_from_data_file_name(name))
Example #17
0
 def test_write_config_no_dict(self):
     data_backend = DataBackend({})
     self.assertRaises(EmptyConfigError, data_backend.write_config, "Test")
Example #18
0
            main_file.parent.joinpath(Path("data")).resolve(),
            "config": main_file.parent.joinpath(Path("config.json")).resolve(),
            "res_directory": main_file.parent.joinpath(Path("res")).resolve()
        }
    else:
        main_file = Path(__file__)
        paths = {
            "data_directory":
            main_file.parent.parent.joinpath(Path("data/")).resolve(),
            "config":
            main_file.parent.parent.joinpath(Path("config.json")).resolve(),
            "res_directory":
            main_file.parent.parent.joinpath("res/").resolve()
        }

    data_backend = DataBackend(paths)
    data_repository = CodeTimeDataRepository(data_backend=data_backend)
    data_repository.create_default_config_if_config_is_missing()

    focus_activity_provider = MacFocusActivityProvider()

    image_creator = BasicImageCreator(data_repository)
    activity_tracker = ActivityTracker(
        data_repository=data_repository,
        focus_activity_provider=focus_activity_provider)
    activity_tracker.start()

    autostart = AutostartManager(str(main_file))
    tray_handler = TrayHandler(image_creator=image_creator,
                               activity_tracker=activity_tracker,
                               data_repository=data_repository,
Example #19
0
    def test_read_config_file_not_available(self):
        paths = {CONFIG_FILE_PATH_KEYWORD: "/this/file/does/no/exist"}

        data_backend = DataBackend(paths)
        self.assertRaises(ConfigFileNotFoundError, data_backend.read_config)