-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtests.py
174 lines (148 loc) · 5.94 KB
/
tests.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
167
168
169
170
171
172
173
174
# /// script
# requires-python = "~=3.12.0"
# dependencies = ["python-dotenv~=1.0.0"]
# ///
"""
Usage:
uv run ./tests.py
"""
import logging
import sys
import unittest
from pathlib import Path
## set up logging ---------------------------------------------------
logging.basicConfig(
level=logging.DEBUG,
format='[%(asctime)s] %(levelname)s [%(module)s-%(funcName)s()::%(lineno)d] %(message)s',
datefmt='%d/%b/%Y %H:%M:%S',
)
log = logging.getLogger(__name__)
# ## add project to path ----------------------------------------------
this_file_path = Path(__file__).resolve()
stuff_dir = this_file_path.parent.parent
sys.path.append(str(stuff_dir))
from self_updater_code import ( # noqa: E402 (disables linter warning that this import is not at the top)
lib_django_updater,
lib_git_handler,
)
from self_updater_code.lib_compilation_evaluator import CompiledComparator # noqa: E402 (prevents linter problem-indicator)
class TestGitCommands(unittest.TestCase):
def setUp(self):
pass
def tearDown(self):
pass
def test_git_pull__A(self):
"""
Checks that `Already up to date.` is detected properly.
Assumes current-project is, actually, already up-to-date.
"""
cur_dir = Path('./').resolve()
log.debug(f'cur_dir: {cur_dir}')
git_result: tuple[bool, dict] = lib_git_handler.run_git_pull(cur_dir)
(ok, output) = git_result
self.assertTrue(ok is True)
self.assertIn('Already up to date.', output['stdout'])
def test_git_status_clean(self):
"""
Checks that `On branch main` is detected properly.
Assumes current-project is on branch `main`.
Note: just looking for the word 'clean' because one version of git says "working tree clean"
and another says "working directory clean". TODO: consider just checking the ok boolean.
"""
cur_dir = Path('./').resolve()
log.debug(f'cur_dir: {cur_dir}')
git_result: tuple[bool, dict] = lib_git_handler.run_git_status(cur_dir)
(ok, output) = git_result
self.assertTrue(ok is True)
self.assertIn('clean', output['stdout'])
def test_git_status_not_clean(self):
"""
Checks that various non-"clean" states are detected properly.
Assumes current-project is on branch `main`.
"""
target_dir = Path('../git_tests/check_changes_not_staged/').resolve()
log.debug(f'cur_dir: {target_dir}')
git_result: tuple[bool, dict] = lib_git_handler.run_git_status(target_dir)
(ok, output) = git_result
self.assertTrue(ok is True)
self.assertNotIn('clean', output['stdout'])
class TestMiscellaneous(unittest.TestCase):
def setUp(self):
pass
def tearDown(self):
pass
def test_check_for_django_update__no_django(self):
"""
Check that django is not detected properly.
"""
incoming_text = 'foo\nbar\nbaz\n'
expected = False
result = lib_django_updater.check_for_django_update(incoming_text)
self.assertEqual(expected, result)
def test_check_for_django_update__django_present(self):
"""
Check that django is detected properly.
"""
incoming_text = """
--- staging_2025-01-14T02-00-05.txt
+++ staging_2025-01-15T02-00-04.txt
---
+++
@@ -12,7 +12,7 @@
# httpx
cffi==1.17.1 ; implementation_name != 'pypy' and os_name == 'nt'
# via trio
-django==4.2.17
+django==4.2.18
# via -r requirements/base.in
h11==0.14.0
# via httpcore
"""
incoming_text: str = incoming_text.replace(' ', '') # removes indentation-spaces
expected = True
result = lib_django_updater.check_for_django_update(incoming_text)
self.assertEqual(expected, result)
class TestComparison(unittest.TestCase):
def setUp(self):
self.compiled_comparator = CompiledComparator()
pass
def tearDown(self):
pass
def test__compare_with_previous_backup__no_differences_A(self):
"""
Files A and B differ only in date in comment-line, so should be considered equal.
"""
file_a_new_path = Path('./test_docs/no_differences_A/file_a.txt').resolve()
file_b_old_path = Path('./test_docs/no_differences_A/file_b.txt').resolve()
project_path = None
expected = False
change_check_result = self.compiled_comparator.compare_with_previous_backup(
file_a_new_path, file_b_old_path, project_path
)
self.assertEqual(expected, change_check_result)
def test__compare_with_previous_backup__no_differences_B(self):
"""
Files A and B differ in date in comment-line, A has "ACTIVE" in comment-line -- so should be considered equal.
"""
file_a_new_path = Path('./test_docs/no_differences_B/file_a.txt').resolve()
file_b_old_path = Path('./test_docs/no_differences_B/file_b.txt').resolve()
project_path = None
expected = False
change_check_result = self.compiled_comparator.compare_with_previous_backup(
file_a_new_path, file_b_old_path, project_path
)
self.assertEqual(expected, change_check_result)
def test__compare_with_previous_backup__differences(self):
"""
Files A and B differ in actual package version, so should be considered different.
"""
file_a_new_path = Path('./test_docs/differences/file_a.txt').resolve()
file_b_old_path = Path('./test_docs/differences/file_b.txt').resolve()
project_path = None
expected = True
change_check_result = self.compiled_comparator.compare_with_previous_backup(
file_a_new_path, file_b_old_path, project_path
)
self.assertEqual(expected, change_check_result)
if __name__ == '__main__':
unittest.main()