/
test_workers_startup.py
166 lines (134 loc) · 7.31 KB
/
test_workers_startup.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# -*- coding: utf-8 -*-
# ***************************************************************************
# * Copyright (c) 2022 FreeCAD Project Association *
# * *
# * This file is part of the FreeCAD CAx development system. *
# * *
# * This library 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. *
# * *
# * This library 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 this library; if not, write to the Free Software *
# * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA *
# * 02110-1301 USA *
# * *
# ***************************************************************************
import json
import unittest
import os
import tempfile
import FreeCAD
from PySide2 import QtCore
import NetworkManager
from Addon import Addon
from addonmanager_workers_startup import (
CreateAddonListWorker,
LoadPackagesFromCacheWorker,
LoadMacrosFromCacheWorker,
)
class TestWorkersStartup(unittest.TestCase):
MODULE = "test_workers_startup" # file name without extension
def setUp(self):
""" Set up the test """
self.test_dir = os.path.join(FreeCAD.getHomePath(), "Mod", "AddonManager", "AddonManagerTest", "data")
self.saved_mod_directory = Addon.mod_directory
self.saved_cache_directory = Addon.cache_directory
Addon.mod_directory = os.path.join(tempfile.gettempdir(),"FreeCADTesting","Mod")
Addon.cache_directory = os.path.join(tempfile.gettempdir(),"FreeCADTesting","Cache")
os.makedirs(Addon.mod_directory, mode=0o777, exist_ok=True)
os.makedirs(Addon.cache_directory, mode=0o777, exist_ok=True)
url = "https://api.github.com/zen"
NetworkManager.InitializeNetworkManager()
result = NetworkManager.AM_NETWORK_MANAGER.blocking_get(url)
if result is None:
self.skipTest("No active internet connection detected")
self.addon_list = []
self.macro_counter = 0
self.workbench_counter = 0
self.prefpack_counter = 0
self.addon_from_cache_counter = 0
self.macro_from_cache_counter = 0
# Populated when the addon list is created in the first test
self.package_cache = {}
self.macro_cache = []
self.package_cache_filename = os.path.join(Addon.cache_directory,"packages.json")
self.macro_cache_filename = os.path.join(Addon.cache_directory,"macros.json")
def tearDown(self):
""" Tear down the test """
Addon.mod_directory = self.saved_mod_directory
Addon.cache_directory = self.saved_cache_directory
def test_create_addon_list_worker(self):
""" Test whether any addons are added: runs the full query, so this potentially is a SLOW
test. Note that this test must be run before any of the other tests, so that the cache gets
created. """
worker = CreateAddonListWorker()
worker.addon_repo.connect(self._addon_added)
worker.start()
while worker.isRunning():
QtCore.QCoreApplication.processEvents(QtCore.QEventLoop.AllEvents, 50)
QtCore.QCoreApplication.processEvents(QtCore.QEventLoop.AllEvents)
self.assertGreater(self.macro_counter,0, "No macros returned")
self.assertGreater(self.workbench_counter,0, "No workbenches returned")
self.assertGreater(self.prefpack_counter,0, "No preference packs returned")
# Write the cache data
if hasattr(self, "package_cache"):
with open(self.package_cache_filename,"w",encoding="utf-8") as f:
f.write(json.dumps(self.package_cache, indent=" "))
if hasattr(self, "macro_cache"):
with open(self.macro_cache_filename,"w",encoding="utf-8") as f:
f.write(json.dumps(self.macro_cache, indent=" "))
def _addon_added(self, addon:Addon):
""" Callback for adding an Addon: tracks the list, and counts the various types """
print (f"Addon Test: {addon.name}")
self.addon_list.append(addon)
if addon.contains_workbench():
self.workbench_counter += 1
if addon.contains_macro():
self.macro_counter += 1
if addon.contains_preference_pack():
self.prefpack_counter += 1
# Also record the information for cache purposes
self.package_cache[addon.name] = addon.to_cache()
if addon.macro is not None:
self.macro_cache.append(addon.macro.to_cache())
def test_load_packages_from_cache_worker(self):
""" Test loading packages from the cache """
worker = LoadPackagesFromCacheWorker(self.package_cache_filename)
worker.override_metadata_cache_path(os.path.join(Addon.cache_directory,"PackageMetadata"))
worker.addon_repo.connect(self._addon_added_from_cache)
self.addon_from_cache_counter = 0
worker.start()
while worker.isRunning():
QtCore.QCoreApplication.processEvents(QtCore.QEventLoop.AllEvents, 50)
QtCore.QCoreApplication.processEvents(QtCore.QEventLoop.AllEvents)
self.assertGreater(self.addon_from_cache_counter,0, "No addons in the cache")
def _addon_added_from_cache(self, addon:Addon):
""" Callback when addon added from cache """
print (f"Addon Cache Test: {addon.name}")
self.addon_from_cache_counter += 1
def test_load_macros_from_cache_worker(self):
""" Test loading macros from the cache """
worker = LoadMacrosFromCacheWorker(self.macro_cache_filename)
worker.add_macro_signal.connect(self._macro_added_from_cache)
self.macro_from_cache_counter = 0
worker.start()
while worker.isRunning():
QtCore.QCoreApplication.processEvents(QtCore.QEventLoop.AllEvents, 50)
QtCore.QCoreApplication.processEvents(QtCore.QEventLoop.AllEvents)
self.assertGreater(self.macro_from_cache_counter,0, "No macros in the cache")
def _macro_added_from_cache(self, addon:Addon):
""" Callback for adding macros from the cache """
print (f"Macro Cache Test: {addon.name}")
self.macro_from_cache_counter += 1
def test_update_checker(self):
""" Test the code that checks a single addon for available updates. """
# First, install a specific Addon of each kind
# Arrange for those addons to be out-of-date
# Check for updates