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