##// END OF EJS Templates
tests: Adapt tests to use the new get_vcs_instance function instead of removed get_repo.
Martin Bornhold -
r487:f0165f76 default
parent child Browse files
Show More
@@ -1,171 +1,171 b''
1 1 # -*- coding: utf-8 -*-
2 2
3 3 # Copyright (C) 2010-2016 RhodeCode GmbH
4 4 #
5 5 # This program is free software: you can redistribute it and/or modify
6 6 # it under the terms of the GNU Affero General Public License, version 3
7 7 # (only), as published by the Free Software Foundation.
8 8 #
9 9 # This program is distributed in the hope that it will be useful,
10 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 12 # GNU General Public License for more details.
13 13 #
14 14 # You should have received a copy of the GNU Affero General Public License
15 15 # along with this program. If not, see <http://www.gnu.org/licenses/>.
16 16 #
17 17 # This program is dual-licensed. If you wish to learn more about the
18 18 # RhodeCode Enterprise Edition, including its added features, Support services,
19 19 # and proprietary license terms, please see https://rhodecode.com/licenses/
20 20
21 21 """
22 22 Load tests of certain vcs operations which can be executed by the test runner.
23 23
24 24 To gather timing information, run like this::
25 25
26 26 py.test rhodecode/tests/vcs/test_load.py --duration=0
27 27
28 28 To use this file with an old codebase which does not provide a compatible
29 29 fixture setup, make sure that py.test is installed inside of the environment
30 30 and copy this file in a place outside of the current repository::
31 31
32 32 TEST_HG_REPO=~/tmp/repos/vcs-hg TEST_GIT_REPO=~/tmp/repos/vcs-git \
33 33 py.test test_load.py --duration=0
34 34
35 35 """
36 36 import os
37 37
38 38 import pytest
39 39
40 40 from rhodecode.lib.vcs import create_vcsserver_proxy
41 from rhodecode.lib.vcs.backends import get_repo, get_backend
41 from rhodecode.lib.vcs.backends import get_backend, get_vcs_instance
42 42 from rhodecode.tests import TEST_HG_REPO, TEST_GIT_REPO
43 43
44 44
45 45 # Allows to inject different repository paths. Used to run this
46 46 # file with an pre-pytest codebase of rhodecode.
47 47 TEST_HG_REPO = os.environ.get('TEST_HG_REPO', TEST_HG_REPO)
48 48 TEST_GIT_REPO = os.environ.get('TEST_GIT_REPO', TEST_GIT_REPO)
49 49
50 50
51 51 @pytest.fixture(params=('hg', 'git'))
52 52 def repo(request, pylonsapp):
53 53 repos = {
54 54 'hg': TEST_HG_REPO,
55 55 'git': TEST_GIT_REPO,
56 56 }
57 repo = get_repo(repos[request.param])
57 repo = get_vcs_instance(repos[request.param])
58 58 return repo
59 59
60 60
61 61 @pytest.fixture
62 62 def server(pylonsapp):
63 63 """
64 64 Returns a proxy of the server object.
65 65 """
66 66 server_and_port = pylonsapp.config['vcs.server']
67 67 protocol = pylonsapp.config['vcs.server.protocol']
68 68 server = create_vcsserver_proxy(server_and_port, protocol)
69 69 return server
70 70
71 71
72 72 def test_server_echo(server):
73 73 resp = server.echo('a')
74 74 assert resp == 'a'
75 75
76 76
77 77 def test_server_echo_no_data(server, repeat):
78 78 for x in xrange(repeat):
79 79 server.echo(None)
80 80
81 81
82 82 @pytest.mark.parametrize("payload", [
83 83 {'a': 'dict', 'with': 'values'},
84 84 [1, 2, 3, 4, 5] * 5,
85 85 ['a', 1, 1.2, None, {}] * 5,
86 86 ], ids=['dict', 'list-int', 'list-mix'])
87 87 def test_server_echo_small_payload(server, repeat, payload):
88 88 for x in xrange(repeat):
89 89 server.echo(payload)
90 90
91 91
92 92 @pytest.mark.parametrize("payload", [
93 93 [{'a': 'dict', 'with': 'values'}] * 100,
94 94 [1, 2, 3, 4, 5] * 100,
95 95 ['a', 1, 1.2, None, {}] * 100,
96 96 ], ids=['dict', 'list-int', 'list-mix'])
97 97 def test_server_echo_middle_payload(server, repeat, payload):
98 98 for x in xrange(repeat):
99 99 server.echo(payload)
100 100
101 101
102 102 @pytest.mark.parametrize("payload", [
103 103 [{'a': 'dict', 'with': 'values'}] * 1000,
104 104 [1, 2, 3, 4, 5] * 1000,
105 105 ['a', 1, 1.2, None, {}] * 1000,
106 106 ], ids=['dict', 'list-int', 'list-mix'])
107 107 def test_server_echo_large_payload(server, repeat, payload):
108 108 for x in xrange(repeat):
109 109 server.echo(payload)
110 110
111 111
112 112 def test_create_repo_object(repo, repeat):
113 113 backend = get_backend(repo.alias)
114 114 for x in xrange(repeat):
115 115 repo = backend(repo.path)
116 116
117 117
118 118 def test_get_first_commit_of_repository(repo, repeat):
119 119 for x in xrange(repeat):
120 120 repo.get_commit(commit_idx=1)
121 121
122 122
123 123 def test_get_first_commits_slicing(repo, repeat):
124 124 count_commits = repeat / 10
125 125 commit = repo[0:count_commits]
126 126 commit = list(commit)
127 127
128 128
129 129 def test_get_first_commits(repo, repeat):
130 130 end_idx = repeat / 10
131 131 start = repo.commit_ids[0]
132 132 end = repo.commit_ids[end_idx]
133 133 commit = repo.get_commits(start_id=start, end_id=end)
134 134 commit = list(commit)
135 135
136 136
137 137 def test_fetch_file(repo, repeat):
138 138 path = 'vcs/cli.py'
139 139 tip = repo.get_commit()
140 140 for x in xrange(repeat):
141 141 tip.get_file_content(path=path)
142 142
143 143
144 144 def test_annotate_file(repo, repeat):
145 145 path = 'vcs/cli.py'
146 146 tip = repo.get_commit()
147 147 for x in xrange(repeat / 10):
148 148 annotation_generator = tip.get_file_annotate(path=path)
149 149 list(annotation_generator)
150 150
151 151
152 152 def test_read_full_file_tree_using_walk(repo):
153 153 tip = repo.get_commit()
154 154
155 155 for topnode, dirs, files in tip.walk():
156 156 for f in files:
157 157 len(f.content)
158 158
159 159
160 160 def test_commit_diff(repo, repeat):
161 161 tip = repo.get_commit()
162 162 for x in xrange(repeat / 10):
163 163 tip.diff()
164 164
165 165
166 166 def test_walk_changelog(repo, repeat):
167 167 page_size = 20
168 168 for page in xrange(repeat / 50):
169 169 start = page * page_size
170 170 end = start + page_size - 1
171 171 list(repo[start:end])
@@ -1,114 +1,144 b''
1 1 # -*- coding: utf-8 -*-
2 2
3 3 # Copyright (C) 2010-2016 RhodeCode GmbH
4 4 #
5 5 # This program is free software: you can redistribute it and/or modify
6 6 # it under the terms of the GNU Affero General Public License, version 3
7 7 # (only), as published by the Free Software Foundation.
8 8 #
9 9 # This program is distributed in the hope that it will be useful,
10 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 12 # GNU General Public License for more details.
13 13 #
14 14 # You should have received a copy of the GNU Affero General Public License
15 15 # along with this program. If not, see <http://www.gnu.org/licenses/>.
16 16 #
17 17 # This program is dual-licensed. If you wish to learn more about the
18 18 # RhodeCode Enterprise Edition, including its added features, Support services,
19 19 # and proprietary license terms, please see https://rhodecode.com/licenses/
20 20
21 21 """
22 22 Tests for main module's methods.
23 23 """
24 24
25 import mock
25 26 import os
26 27 import shutil
28 import tempfile
27 29
28 30 import pytest
29 31
30 from rhodecode.lib.vcs import VCSError, get_repo, get_backend
32 from rhodecode.lib.vcs import VCSError, get_backend, get_vcs_instance
31 33 from rhodecode.lib.vcs.backends.hg import MercurialRepository
32 from rhodecode.tests import TEST_HG_REPO, TEST_GIT_REPO, TESTS_TMP_PATH
34 from rhodecode.tests import TEST_HG_REPO, TEST_GIT_REPO
33 35
34 36
35 37 pytestmark = pytest.mark.usefixtures("pylonsapp")
36 38
37 39
38 40 def test_get_backend():
39 41 hg = get_backend('hg')
40 42 assert hg == MercurialRepository
41 43
42 44
43 45 def test_alias_detect_hg():
44 46 alias = 'hg'
45 47 path = TEST_HG_REPO
46 48 backend = get_backend(alias)
47 49 repo = backend(path)
48 50 assert 'hg' == repo.alias
49 51
50 52
51 53 def test_alias_detect_git():
52 54 alias = 'git'
53 55 path = TEST_GIT_REPO
54 56 backend = get_backend(alias)
55 57 repo = backend(path)
56 58 assert 'git' == repo.alias
57 59
58 60
59 61 def test_wrong_alias():
60 62 alias = 'wrong_alias'
61 63 with pytest.raises(VCSError):
62 64 get_backend(alias)
63 65
64 66
65 def test_get_repo():
66 alias = 'hg'
67 path = TEST_HG_REPO
68 backend = get_backend(alias)
69 repo = backend(path)
67 def test_get_vcs_instance_by_path(vcs_repo):
68 repo = get_vcs_instance(vcs_repo.path)
70 69
71 assert repo.__class__, get_repo(path == alias).__class__
72 assert repo.path, get_repo(path == alias).path
70 assert repo.__class__ == vcs_repo.__class__
71 assert repo.path == vcs_repo.path
72 assert repo.alias == vcs_repo.alias
73 assert repo.name == vcs_repo.name
73 74
74 75
75 def test_get_repo_autoalias_hg():
76 alias = 'hg'
77 path = TEST_HG_REPO
78 backend = get_backend(alias)
79 repo = backend(path)
76 @mock.patch('rhodecode.lib.vcs.backends.get_scm')
77 @mock.patch('rhodecode.lib.vcs.backends.get_backend')
78 def test_get_vcs_instance_by_path_args_passed(
79 get_backend_mock, get_scm_mock):
80 """
81 Test that the arguments passed to ``get_vcs_instance_by_path`` are
82 forewarded to the vcs backend class.
83 """
84 backend = mock.MagicMock()
85 get_backend_mock.return_value = backend
86 args = ['these-are-test-args', 0, True, None]
87 get_vcs_instance(TEST_HG_REPO, *args)
80 88
81 assert repo.__class__ == get_repo(path).__class__
82 assert repo.path == get_repo(path).path
89 backend.assert_called_with(*args, repo_path=TEST_HG_REPO)
83 90
84 91
85 def test_get_repo_autoalias_git():
86 alias = 'git'
87 path = TEST_GIT_REPO
88 backend = get_backend(alias)
89 repo = backend(path)
92 @mock.patch('rhodecode.lib.vcs.backends.get_scm')
93 @mock.patch('rhodecode.lib.vcs.backends.get_backend')
94 def test_get_vcs_instance_by_path_kwargs_passed(
95 get_backend_mock, get_scm_mock):
96 """
97 Test that the keyword arguments passed to ``get_vcs_instance_by_path`` are
98 forewarded to the vcs backend class.
99 """
100 backend = mock.MagicMock()
101 get_backend_mock.return_value = backend
102 kwargs = {
103 'foo': 'these-are-test-args',
104 'bar': 0,
105 'baz': True,
106 'foobar': None
107 }
108 get_vcs_instance(TEST_HG_REPO, **kwargs)
90 109
91 assert repo.__class__ == get_repo(path).__class__
92 assert repo.path == get_repo(path).path
110 backend.assert_called_with(repo_path=TEST_HG_REPO, **kwargs)
93 111
94 112
95 def test_get_repo_err():
96 blank_repo_path = os.path.join(TESTS_TMP_PATH, 'blank-error-repo')
97 if os.path.isdir(blank_repo_path):
98 shutil.rmtree(blank_repo_path)
113 def test_get_vcs_instance_by_path_err(request):
114 """
115 Test that ``get_vcs_instance_by_path`` returns None if a path is passed
116 to an empty directory.
117 """
118 empty_dir = tempfile.mkdtemp(prefix='pytest-empty-dir-')
99 119
100 os.mkdir(blank_repo_path)
101 pytest.raises(VCSError, get_repo, blank_repo_path)
102 pytest.raises(VCSError, get_repo, blank_repo_path + 'non_existing')
120 def fin():
121 shutil.rmtree(empty_dir)
122 request.addfinalizer(fin)
123
124 repo = get_vcs_instance(empty_dir)
125
126 assert repo is None
103 127
104 128
105 def test_get_repo_multialias():
106 multialias_repo_path = os.path.join(TESTS_TMP_PATH, 'hg-git-repo')
107 if os.path.isdir(multialias_repo_path):
108 shutil.rmtree(multialias_repo_path)
129 def test_get_vcs_instance_by_path_multiple_repos(request):
130 """
131 Test that ``get_vcs_instance_by_path`` returns None if a path is passed
132 to a directory with multiple repositories.
133 """
134 empty_dir = tempfile.mkdtemp(prefix='pytest-empty-dir-')
135 os.mkdir(os.path.join(empty_dir, '.git'))
136 os.mkdir(os.path.join(empty_dir, '.hg'))
109 137
110 os.mkdir(multialias_repo_path)
138 def fin():
139 shutil.rmtree(empty_dir)
140 request.addfinalizer(fin)
111 141
112 os.mkdir(os.path.join(multialias_repo_path, '.git'))
113 os.mkdir(os.path.join(multialias_repo_path, '.hg'))
114 pytest.raises(VCSError, get_repo, multialias_repo_path)
142 repo = get_vcs_instance(empty_dir)
143
144 assert repo is None
General Comments 0
You need to be logged in to leave comments. Login now