Addon Manager: Refactor local cache update check

This commit is contained in:
Chris Hennes
2023-09-04 15:11:51 -05:00
parent 9a7523fb4b
commit 92016c4c9c
6 changed files with 314 additions and 78 deletions

View File

@@ -35,7 +35,7 @@ class GitFailed(RuntimeError):
class MockConsole:
"""Mock for the FreeCAD.Console -- does NOT print anything out, just logs it."""
"""Spy for the FreeCAD.Console -- does NOT print anything out, just logs it."""
def __init__(self):
self.log = []
@@ -401,3 +401,63 @@ class MockThread:
):
return True
return False
class MockPref:
def __init__(self):
self.prefs = {}
self.pref_set_counter = {}
self.pref_get_counter = {}
def set_prefs(self, pref_dict: dict) -> None:
self.prefs = pref_dict
def GetInt(self, key: str, default: int) -> int:
return self.Get(key, default)
def GetString(self, key: str, default: str) -> str:
return self.Get(key, default)
def GetBool(self, key: str, default: bool) -> bool:
return self.Get(key, default)
def Get(self, key: str, default):
if key not in self.pref_set_counter:
self.pref_get_counter[key] = 1
else:
self.pref_get_counter[key] += 1
if key in self.prefs:
return self.prefs[key]
raise ValueError(f"Expected key not in mock preferences: {key}")
def SetInt(self, key: str, value: int) -> None:
return self.Set(key, value)
def SetString(self, key: str, value: str) -> None:
return self.Set(key, value)
def SetBool(self, key: str, value: bool) -> None:
return self.Set(key, value)
def Set(self, key: str, value):
if key not in self.pref_set_counter:
self.pref_set_counter[key] = 1
else:
self.pref_set_counter[key] += 1
self.prefs[key] = value
class MockExists:
def __init__(self, files: List[str] = None):
"""Returns True for all files in files, and False for all others"""
self.files = files
self.files_checked = []
def exists(self, check_file: str):
self.files_checked.append(check_file)
if not self.files:
return False
for file in self.files:
if check_file.endswith(file):
return True
return False

View File

@@ -0,0 +1,126 @@
# SPDX-License-Identifier: LGPL-2.1-or-later
# ***************************************************************************
# * *
# * Copyright (c) 2022-2023 FreeCAD Project Association *
# * *
# * This file is part of FreeCAD. *
# * *
# * FreeCAD is free software: you can redistribute it and/or modify it *
# * under the terms of the GNU Lesser General Public License as *
# * published by the Free Software Foundation, either version 2.1 of the *
# * License, or (at your option) any later version. *
# * *
# * FreeCAD is distributed in the hope that it will be useful, but *
# * WITHOUT ANY WARRANTY; without even the implied warranty of *
# * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
# * Lesser General Public License for more details. *
# * *
# * You should have received a copy of the GNU Lesser General Public *
# * License along with FreeCAD. If not, see *
# * <https://www.gnu.org/licenses/>. *
# * *
# ***************************************************************************
import datetime
import sys
import unittest
from datetime import date
from unittest import TestCase
from unittest.mock import MagicMock, patch
sys.path.append("../..")
import addonmanager_cache as cache
from AddonManagerTest.app.mocks import MockPref, MockExists
class TestCache(TestCase):
@patch("addonmanager_freecad_interface.getUserCachePath")
@patch("addonmanager_freecad_interface.ParamGet")
@patch("os.remove", MagicMock())
@patch("os.makedirs", MagicMock())
def test_local_cache_needs_update(self, param_mock: MagicMock, cache_mock: MagicMock):
cache_mock.return_value = ""
param_mock.return_value = MockPref()
default_prefs = {
"UpdateFrequencyComboEntry": 0,
"LastCacheUpdate": "2000-01-01",
"CustomRepoHash": "",
"CustomRepositories": "",
}
today = date.today().isoformat()
yesterday = (date.today() - datetime.timedelta(1)).isoformat()
# Organize these as subtests because of all the patching that has to be done: once we are in this function,
# the patch is complete, and we can just modify the return values of the fakes one by one
tests = (
{
"case": "No existing cache",
"files_that_exist": [],
"prefs_to_set": {},
"expect": True,
},
{
"case": "Last cache update was interrupted",
"files_that_exist": ["CACHE_UPDATE_INTERRUPTED"],
"prefs_to_set": {},
"expect": True,
},
{
"case": "Cache exists and updating is blocked",
"files_that_exist": ["AddonManager"],
"prefs_to_set": {},
"expect": False,
},
{
"case": "Daily updates set and last update was long ago",
"files_that_exist": ["AddonManager"],
"prefs_to_set": {"UpdateFrequencyComboEntry": 1},
"expect": True,
},
{
"case": "Daily updates set and last update was today",
"files_that_exist": ["AddonManager"],
"prefs_to_set": {"UpdateFrequencyComboEntry": 1, "LastCacheUpdate": today},
"expect": False,
},
{
"case": "Daily updates set and last update was yesterday",
"files_that_exist": ["AddonManager"],
"prefs_to_set": {"UpdateFrequencyComboEntry": 1, "LastCacheUpdate": yesterday},
"expect": True,
},
{
"case": "Weekly updates set and last update was long ago",
"files_that_exist": ["AddonManager"],
"prefs_to_set": {"UpdateFrequencyComboEntry": 1},
"expect": True,
},
{
"case": "Weekly updates set and last update was yesterday",
"files_that_exist": ["AddonManager"],
"prefs_to_set": {"UpdateFrequencyComboEntry": 2, "LastCacheUpdate": yesterday},
"expect": False,
},
{
"case": "Custom repo list changed",
"files_that_exist": ["AddonManager"],
"prefs_to_set": {"CustomRepositories": "NewRepo"},
"expect": True,
},
)
for test_case in tests:
with self.subTest(test_case["case"]):
case_prefs = default_prefs
for pref, setting in test_case["prefs_to_set"].items():
case_prefs[pref] = setting
param_mock.return_value.set_prefs(case_prefs)
exists_mock = MockExists(test_case["files_that_exist"])
with patch("os.path.exists", exists_mock.exists):
if test_case["expect"]:
self.assertTrue(cache.local_cache_needs_update())
else:
self.assertFalse(cache.local_cache_needs_update())
if __name__ == "__main__":
unittest.main()