-
-
Notifications
You must be signed in to change notification settings - Fork 3.6k
/
Copy pathtest_filetreediff.py
134 lines (123 loc) · 4.53 KB
/
test_filetreediff.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
import json
from contextlib import contextmanager
from unittest import mock
from django.test import TestCase
from django_dynamic_fixture import get
from readthedocs.builds.constants import BUILD_STATE_FINISHED, LATEST
from readthedocs.builds.models import Build, Version
from readthedocs.filetreediff import get_diff
from readthedocs.projects.models import Project
from readthedocs.rtd_tests.storage import BuildMediaFileSystemStorageTest
# We are overriding the storage class instead of using RTD_BUILD_MEDIA_STORAGE,
# since the setting is evaluated just once (first test to use the storage
# backend will set it for the whole test suite).
@mock.patch(
"readthedocs.filetreediff.build_media_storage",
new=BuildMediaFileSystemStorageTest(),
)
class TestsFileTreeDiff(TestCase):
def setUp(self):
self.project = get(Project)
self.version_a = self.project.versions.get(slug=LATEST)
self.build_a = get(
Build,
project=self.project,
version=self.version_a,
state=BUILD_STATE_FINISHED,
success=True,
)
self.version_b = get(
Version,
project=self.project,
slug="v2",
active=True,
built=True,
)
self.build_b = get(
Build,
project=self.project,
version=self.version_b,
state=BUILD_STATE_FINISHED,
success=True,
)
def _mock_open(self, content):
@contextmanager
def f(*args, **kwargs):
read_mock = mock.MagicMock()
read_mock.read.return_value = content
yield read_mock
return f
def _mock_manifest(self, build_id: int, files: dict[str, str]):
return self._mock_open(
json.dumps(
{
"build": {"id": build_id},
"files": {
file_path: {"main_content_hash": main_content_hash}
for file_path, main_content_hash in files.items()
},
}
)
)
@mock.patch.object(BuildMediaFileSystemStorageTest, "open")
def test_diff_no_changes(self, storage_open):
files_a = {
"index.html": "hash1",
"tutorials/index.html": "hash2",
}
storage_open.side_effect = [
self._mock_manifest(self.build_a.id, files_a)(),
self._mock_manifest(self.build_b.id, files_a)(),
]
diff = get_diff(self.version_a, self.version_b)
assert diff.added == []
assert diff.deleted == []
assert diff.modified == []
assert not diff.outdated
@mock.patch.object(BuildMediaFileSystemStorageTest, "open")
def test_diff_changes(self, storage_open):
files_a = {
"index.html": "hash1",
"tutorials/index.html": "hash2",
"new-file.html": "hash-new",
}
files_b = {
"index.html": "hash1",
"tutorials/index.html": "hash-changed",
"deleted.html": "hash-deleted",
}
storage_open.side_effect = [
self._mock_manifest(self.build_a.id, files_a)(),
self._mock_manifest(self.build_b.id, files_b)(),
]
diff = get_diff(self.version_a, self.version_b)
assert diff.added == ["new-file.html"]
assert diff.deleted == ["deleted.html"]
assert diff.modified == ["tutorials/index.html"]
assert not diff.outdated
@mock.patch.object(BuildMediaFileSystemStorageTest, "open")
def test_missing_manifest(self, storage_open):
storage_open.side_effect = FileNotFoundError
diff = get_diff(self.version_a, self.version_b)
assert diff is None
@mock.patch.object(BuildMediaFileSystemStorageTest, "open")
def test_outdated_diff(self, storage_open):
files_a = {
"index.html": "hash1",
"tutorials/index.html": "hash2",
"new-file.html": "hash-new",
}
files_b = {
"index.html": "hash1",
"tutorials/index.html": "hash-changed",
"deleted.html": "hash-deleted",
}
storage_open.side_effect = [
self._mock_manifest(self.build_a.id + 5, files_a)(),
self._mock_manifest(self.build_b.id + 5, files_b)(),
]
diff = get_diff(self.version_a, self.version_b)
assert diff.added == ["new-file.html"]
assert diff.deleted == ["deleted.html"]
assert diff.modified == ["tutorials/index.html"]
assert diff.outdated