##// END OF EJS Templates
tests: ensure we never hit same user_id twice in voting tests.
marcink -
r3274:7a9fae48 default
parent child Browse files
Show More
@@ -1,127 +1,127 b''
1 # -*- coding: utf-8 -*-
1 # -*- coding: utf-8 -*-
2
2
3 # Copyright (C) 2010-2018 RhodeCode GmbH
3 # Copyright (C) 2010-2018 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 import pytest
21 import pytest
22 import random
22 import random
23 from mock import Mock, MagicMock
23 from mock import Mock, MagicMock
24 from rhodecode.model import db
24 from rhodecode.model import db
25 from rhodecode.model.changeset_status import ChangesetStatusModel
25 from rhodecode.model.changeset_status import ChangesetStatusModel
26
26
27
27
28 status_approved = db.ChangesetStatus.STATUS_APPROVED
28 status_approved = db.ChangesetStatus.STATUS_APPROVED
29 status_rejected = db.ChangesetStatus.STATUS_REJECTED
29 status_rejected = db.ChangesetStatus.STATUS_REJECTED
30 status_under_review = db.ChangesetStatus.STATUS_UNDER_REVIEW
30 status_under_review = db.ChangesetStatus.STATUS_UNDER_REVIEW
31
31
32
32
33 pytestmark = [
33 pytestmark = [
34 pytest.mark.backends("git", "hg"),
34 pytest.mark.backends("git", "hg"),
35 ]
35 ]
36
36
37
37
38 class ReviewerMock(object):
38 class ReviewerMock(object):
39 def __init__(self, reviewer_def):
39 def __init__(self, reviewer_def):
40 self.reviewer_def = reviewer_def
40 self.reviewer_def = reviewer_def
41
41
42 def rule_user_group_data(self):
42 def rule_user_group_data(self):
43 return {'vote_rule': self.reviewer_def['vote_rule']}
43 return {'vote_rule': self.reviewer_def['vote_rule']}
44
44
45
45
46 class MemberMock(object):
46 class MemberMock(object):
47 def __init__(self, reviewer_def):
47 def __init__(self, reviewer_def):
48 self.reviewer_def = reviewer_def
48 self.reviewer_def = reviewer_def
49 self.user_id = random.randint(1, 1024)
49 self.user_id = random.randint(1, 1024*1024)
50
50
51
51
52 class Statuses(object):
52 class Statuses(object):
53 def __init__(self, member_status):
53 def __init__(self, member_status):
54 self.member_status = member_status
54 self.member_status = member_status
55
55
56 def get_statuses(self):
56 def get_statuses(self):
57 if not self.member_status:
57 if not self.member_status:
58 return []
58 return []
59
59
60 ver = 1
60 ver = 1
61 latest = MagicMock(status=self.member_status)
61 latest = MagicMock(status=self.member_status)
62 return [
62 return [
63 [ver, latest]
63 [ver, latest]
64 ]
64 ]
65
65
66
66
67 @pytest.mark.parametrize("reviewers_def, expected_votes", [
67 @pytest.mark.parametrize("reviewers_def, expected_votes", [
68 # empty values
68 # empty values
69 ({},
69 ({},
70 []),
70 []),
71
71
72 # 3 members, 1 votes approved, 2 approvals required
72 # 3 members, 1 votes approved, 2 approvals required
73 ({'members': [status_approved, None, None], 'vote_rule':2},
73 ({'members': [status_approved, None, None], 'vote_rule':2},
74 [status_approved, status_under_review, status_under_review]),
74 [status_approved, status_under_review, status_under_review]),
75
75
76 # 3 members, 2 approvals required
76 # 3 members, 2 approvals required
77 ({'members': [status_approved, status_approved, None], 'vote_rule': 2},
77 ({'members': [status_approved, status_approved, None], 'vote_rule': 2},
78 [status_approved, status_approved, status_approved]),
78 [status_approved, status_approved, status_approved]),
79
79
80 # 3 members, 3 approvals required
80 # 3 members, 3 approvals required
81 ({'members': [status_approved, status_approved, None], 'vote_rule': 3},
81 ({'members': [status_approved, status_approved, None], 'vote_rule': 3},
82 [status_approved, status_approved, status_under_review]),
82 [status_approved, status_approved, status_under_review]),
83
83
84 # 3 members, 1 votes approved, 2 approvals required
84 # 3 members, 1 votes approved, 2 approvals required
85 ({'members': [status_approved, status_approved, status_rejected], 'vote_rule': 2},
85 ({'members': [status_approved, status_approved, status_rejected], 'vote_rule': 2},
86 [status_approved, status_approved, status_approved]),
86 [status_approved, status_approved, status_approved]),
87
87
88 # 2 members, 1 votes approved, ALL approvals required
88 # 2 members, 1 votes approved, ALL approvals required
89 ({'members': [status_approved, None,], 'vote_rule': -1},
89 ({'members': [status_approved, None,], 'vote_rule': -1},
90 [status_approved, status_under_review]),
90 [status_approved, status_under_review]),
91
91
92 # 4 members, 2 votes approved, 2 rejected, 3 approvals required
92 # 4 members, 2 votes approved, 2 rejected, 3 approvals required
93 ({'members': [status_approved, status_rejected, status_approved, status_rejected], 'vote_rule': 3},
93 ({'members': [status_approved, status_rejected, status_approved, status_rejected], 'vote_rule': 3},
94 [status_approved, status_rejected, status_approved, status_rejected]),
94 [status_approved, status_rejected, status_approved, status_rejected]),
95
95
96 # 2 members, ALL approvals required
96 # 2 members, ALL approvals required
97 ({'members': [status_approved, status_approved], 'vote_rule': -1},
97 ({'members': [status_approved, status_approved], 'vote_rule': -1},
98 [status_approved, status_approved]),
98 [status_approved, status_approved]),
99
99
100 # 3 members, 4 approvals required
100 # 3 members, 4 approvals required
101 ({'members': [status_approved, None, None], 'vote_rule': 4},
101 ({'members': [status_approved, None, None], 'vote_rule': 4},
102 [status_approved, status_under_review, status_under_review]),
102 [status_approved, status_under_review, status_under_review]),
103
103
104 # 4 members, 3 approvals required
104 # 4 members, 3 approvals required
105 ({'members': [status_approved, status_approved, status_rejected, status_approved], 'vote_rule': 3},
105 ({'members': [status_approved, status_approved, status_rejected, status_approved], 'vote_rule': 3},
106 [status_approved, status_approved, status_approved, status_approved]),
106 [status_approved, status_approved, status_approved, status_approved]),
107
107
108 # 4 members, 3 approvals required
108 # 4 members, 3 approvals required
109 ({'members': [status_rejected, status_rejected, status_approved, status_approved], 'vote_rule': 3},
109 ({'members': [status_rejected, status_rejected, status_approved, status_approved], 'vote_rule': 3},
110 [status_rejected, status_rejected, status_approved, status_approved]),
110 [status_rejected, status_rejected, status_approved, status_approved]),
111
111
112 ])
112 ])
113 def test_calculate_group_vote(reviewers_def, expected_votes):
113 def test_calculate_group_vote(reviewers_def, expected_votes):
114 reviewers_data = []
114 reviewers_data = []
115
115
116 for member_status in reviewers_def.get('members', []):
116 for member_status in reviewers_def.get('members', []):
117 mandatory_flag = True
117 mandatory_flag = True
118 reviewers_data.append((
118 reviewers_data.append((
119 ReviewerMock(reviewers_def),
119 ReviewerMock(reviewers_def),
120 MemberMock(reviewers_def),
120 MemberMock(reviewers_def),
121 'Test Reason',
121 'Test Reason',
122 mandatory_flag,
122 mandatory_flag,
123 Statuses(member_status).get_statuses()
123 Statuses(member_status).get_statuses()
124 ))
124 ))
125
125
126 votes = ChangesetStatusModel().calculate_group_vote(123, reviewers_data)
126 votes = ChangesetStatusModel().calculate_group_vote(123, reviewers_data)
127 assert votes == expected_votes
127 assert votes == expected_votes
General Comments 0
You need to be logged in to leave comments. Login now