##// END OF EJS Templates
tests: fixed scm tests
super-admin -
r5155:528da87c default
parent child Browse files
Show More
@@ -1,188 +1,189 b''
1 1
2 2 # Copyright (C) 2010-2023 RhodeCode GmbH
3 3 #
4 4 # This program is free software: you can redistribute it and/or modify
5 5 # it under the terms of the GNU Affero General Public License, version 3
6 6 # (only), as published by the Free Software Foundation.
7 7 #
8 8 # This program is distributed in the hope that it will be useful,
9 9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 11 # GNU General Public License for more details.
12 12 #
13 13 # You should have received a copy of the GNU Affero General Public License
14 14 # along with this program. If not, see <http://www.gnu.org/licenses/>.
15 15 #
16 16 # This program is dual-licensed. If you wish to learn more about the
17 17 # RhodeCode Enterprise Edition, including its added features, Support services,
18 18 # and proprietary license terms, please see https://rhodecode.com/licenses/
19 19
20 20 import pytest
21 21 from mock import Mock, patch, DEFAULT
22 22
23 23 import rhodecode
24 24 from rhodecode.model import db, scm
25 25
26 26
27 27 def test_scm_instance_config(backend):
28 28 repo = backend.create_repo()
29 29 with patch.multiple('rhodecode.model.db.Repository',
30 30 _get_instance=DEFAULT,
31 31 _get_instance_cached=DEFAULT) as mocks:
32 32
33 33 repo.scm_instance()
34 34 mocks['_get_instance'].assert_called_with(
35 35 config=None, cache=False)
36 36
37 37 repo.scm_instance(vcs_full_cache=False)
38 38 mocks['_get_instance'].assert_called_with(
39 39 config=None, cache=False)
40 40
41 41 repo.scm_instance(vcs_full_cache=True)
42 42 mocks['_get_instance_cached'].assert_called()
43 43
44 44
45 45 def test_get_instance_config(backend):
46 46 repo = backend.create_repo()
47 47 vcs_class = Mock()
48 48 with patch.multiple('rhodecode.lib.vcs.backends',
49 49 get_scm=DEFAULT,
50 50 get_backend=DEFAULT) as mocks:
51 51 mocks['get_scm'].return_value = backend.alias
52 52 mocks['get_backend'].return_value = vcs_class
53 53 with patch('rhodecode.model.db.Repository._config') as config_mock:
54 54 repo._get_instance()
55 55 vcs_class.assert_called_with(
56 56 repo_path=repo.repo_full_path, config=config_mock,
57 57 create=False, with_wire={'cache': True, 'repo_state_uid': None})
58 58
59 59 new_config = {'override': 'old_config'}
60 60 repo._get_instance(config=new_config)
61 61 vcs_class.assert_called_with(
62 62 repo_path=repo.repo_full_path, config=new_config, create=False,
63 63 with_wire={'cache': True, 'repo_state_uid': None})
64 64
65 65
66 66 def test_mark_for_invalidation_config(backend):
67 67 repo = backend.create_repo()
68 68 with patch('rhodecode.model.db.Repository.update_commit_cache') as _mock:
69 69 scm.ScmModel().mark_for_invalidation(repo.repo_name)
70 70 _, kwargs = _mock.call_args
71 71 assert kwargs['config'].__dict__ == repo._config.__dict__
72 72
73 73
74 74 def test_mark_for_invalidation_with_delete_updates_last_commit(backend):
75 75 commits = [{'message': 'A'}, {'message': 'B'}]
76 76 repo = backend.create_repo(commits=commits)
77 77 scm.ScmModel().mark_for_invalidation(repo.repo_name, delete=True)
78 78 assert repo.changeset_cache['revision'] == 1
79 79
80 80
81 81 def test_mark_for_invalidation_with_delete_updates_last_commit_empty(backend):
82 82 repo = backend.create_repo()
83 83 scm.ScmModel().mark_for_invalidation(repo.repo_name, delete=True)
84 84 assert repo.changeset_cache['revision'] == -1
85 85
86 86
87 87 def test_strip_with_multiple_heads(backend_hg):
88 88 commits = [
89 89 {'message': 'A'},
90 90 {'message': 'a'},
91 91 {'message': 'b'},
92 92 {'message': 'B', 'parents': ['A']},
93 93 {'message': 'a1'},
94 94 ]
95 95 repo = backend_hg.create_repo(commits=commits)
96 96 commit_ids = backend_hg.commit_ids
97 97
98 98 model = scm.ScmModel()
99 99 model.strip(repo, commit_ids['b'], branch=None)
100 100
101 101 vcs_repo = repo.scm_instance()
102 102 rest_commit_ids = [c.raw_id for c in vcs_repo.get_commits()]
103 103 assert len(rest_commit_ids) == 4
104 104 assert commit_ids['b'] not in rest_commit_ids
105 105
106 106
107 107 def test_strip_with_single_heads(backend_hg):
108 108 commits = [
109 109 {'message': 'A'},
110 110 {'message': 'a'},
111 111 {'message': 'b'},
112 112 ]
113 113 repo = backend_hg.create_repo(commits=commits)
114 114 commit_ids = backend_hg.commit_ids
115 115
116 116 model = scm.ScmModel()
117 117 model.strip(repo, commit_ids['b'], branch=None)
118 118
119 119 vcs_repo = repo.scm_instance()
120 120 rest_commit_ids = [c.raw_id for c in vcs_repo.get_commits()]
121 121 assert len(rest_commit_ids) == 2
122 122 assert commit_ids['b'] not in rest_commit_ids
123 123
124 124
125 125 def test_get_nodes_returns_unicode_flat(backend):
126 126 repo = backend.repo
127 127 commit_id = repo.get_commit(commit_idx=0).raw_id
128 128 directories, files = scm.ScmModel().get_nodes(repo.repo_name, commit_id, flat=True)
129 assert_contains_only_unicode(directories)
130 assert_contains_only_unicode(files)
129 assert_contains_only_str_chars(directories)
130 assert_contains_only_str_chars(files)
131 131
132 132
133 133 def test_get_nodes_returns_unicode_non_flat(backend):
134 134 repo = backend.repo
135 135 commit_id = repo.get_commit(commit_idx=0).raw_id
136 136
137 137 directories, files = scm.ScmModel().get_nodes(repo.repo_name, commit_id, flat=False)
138 138 # johbo: Checking only the names for now, since that is the critical
139 139 # part.
140 assert_contains_only_unicode([d['name'] for d in directories])
141 assert_contains_only_unicode([f['name'] for f in files])
140 assert_contains_only_str_chars([d['name'] for d in directories])
141 assert_contains_only_str_chars([f['name'] for f in files])
142 142
143 143
144 144 def test_get_nodes_max_file_bytes(backend_random):
145 145 repo = backend_random.repo
146 146 max_file_bytes = 10
147 147 directories, files = scm.ScmModel().get_nodes(
148 148 repo.repo_name, repo.get_commit(commit_idx=0).raw_id, content=True,
149 149 extended_info=True, flat=False)
150 150 assert any(file['content'] and len(file['content']) > max_file_bytes
151 151 for file in files)
152 152
153 153 directories, files = scm.ScmModel().get_nodes(
154 154 repo.repo_name, repo.get_commit(commit_idx=0).raw_id, content=True,
155 155 extended_info=True, flat=False, max_file_bytes=max_file_bytes)
156 156 assert all(
157 157 file['content'] is None if file['size'] > max_file_bytes else True
158 158 for file in files)
159 159
160 160
161 def assert_contains_only_unicode(structure):
161 def assert_contains_only_str_chars(structure):
162 162 assert structure
163 163 for value in structure:
164 164 assert isinstance(value, str)
165 165
166 166
167 167 @pytest.mark.backends("hg", "git")
168 def test_get_non_unicode_reference(backend):
168 def test_get_non_str_reference(backend):
169 169 model = scm.ScmModel()
170 non_unicode_list = ["Adını".encode("cp1254")]
170 special_name = "Adını"
171 non_str_list = [special_name]
171 172
172 173 def scm_instance():
173 174 return Mock(
174 branches=non_unicode_list, bookmarks=non_unicode_list,
175 tags=non_unicode_list, alias=backend.alias)
175 branches=non_str_list, bookmarks=non_str_list,
176 tags=non_str_list, alias=backend.alias)
176 177
177 178 repo = Mock(__class__=db.Repository, scm_instance=scm_instance)
178 179 choices, __ = model.get_repo_landing_revs(translator=lambda s: s, repo=repo)
179 180 if backend.alias == 'hg':
180 181 valid_choices = [
181 'rev:tip', 'branch:Ad\xc4\xb1n\xc4\xb1',
182 'book:Ad\xc4\xb1n\xc4\xb1', 'tag:Ad\xc4\xb1n\xc4\xb1']
182 'rev:tip', f'branch:{special_name}',
183 f'book:{special_name}', f'tag:{special_name}']
183 184 else:
184 185 valid_choices = [
185 'rev:tip', 'branch:Ad\xc4\xb1n\xc4\xb1',
186 'tag:Ad\xc4\xb1n\xc4\xb1']
186 'rev:tip', f'branch:{special_name}',
187 f'tag:{special_name}']
187 188
188 189 assert choices == valid_choices
General Comments 0
You need to be logged in to leave comments. Login now