test_workshop_flow()   F
last analyzed

Complexity

Conditions 11

Size

Total Lines 133

Duplication

Lines 0
Ratio 0 %

Importance

Changes 10
Bugs 0 Features 0
Metric Value
c 10
b 0
f 0
dl 0
loc 133
rs 3.1764
cc 11

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

Complexity

Complex classes like test_workshop_flow() often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

1
from datetime import datetime, timedelta
2
3
import re
4
from wye.base.constants import WorkshopStatus
5
from tests import factories as f
6
7
8
def test_workshop_wrong_action(base_url, browser, outbox):
9
    tutor_type = f.create_usertype(slug='tutor', display_name='tutor')
10
    poc_type = f.create_usertype(slug='poc', display_name='poc')
11
12
    user = f.create_user()
13
    user.set_password('123123')
14
    user.save()
15
    url = base_url + '/workshop/'
16
    browser.visit(url)
17
    browser.fill('login', user.email)
18
    browser.fill('password', '123123')
19
    browser.find_by_css('[type=submit]')[0].click()
20
    assert len(outbox) == 1
21
    mail = outbox[0]
22
    confirm_link = re.findall(r'http.*/accounts/.*/', mail.body)
23
    assert confirm_link
24
    browser.visit(confirm_link[0])
25
    assert browser.title, "Confirm E-mail Address"
26
    browser.find_by_css('[type=submit]')[0].click()
27
28
    user.profile.usertype.clear()
29
    user.profile.usertype.add(poc_type)
30
    user.save()
31
    org = f.create_organisation()
32
    org.user.add(user)
33
    user.profile.interested_locations.add(org.location)
34
    user.profile.location = org.location
35
    user.profile.save()
36
    org.save()
37
38
    workshop = f.create_workshop(requester=org)
39
40
    workshop.expected_date = datetime.now() + timedelta(days=20)
41
    workshop.status = WorkshopStatus.REQUESTED
42
    workshop.location = org.location
43
    workshop.save()
44
    url = base_url + '/workshop/update/{}/'.format(workshop.id)
45
    browser.visit(url)
46
    browser.fill('login', user.email)
47
    browser.fill('password', '123123')
48
    browser.find_by_css('[type=submit]')[0].click()
49
50
    section1 = f.create_workshop_section(name='section1')
51
    location = org.location
52
    state = f.create_state(name='state2')
53
54
    user.profile.usertype.clear()
55
    user.profile.usertype.add(tutor_type)
56
57
    user.profile.location = location
58
    user.profile.interested_states.add(state)
59
    user.profile.mobile = '1234567890'
60
    user.profile.interested_sections.add(section1)
61
    user.profile.interested_level = 1
62
    user.profile.github = 'https://github.com'
63
    user.profile.occupation = 'occupation'
64
    user.profile.work_location = 'work_location'
65
    user.profile.work_experience = 1
66
    user.profile.save()
67
    user.save()
68
69
    url = base_url + '/workshop/' + 'feedback/000/'
70
    browser.visit(url)
71
72
    url = base_url + '/workshop/feedback/{}/'.format(workshop.id)
73
    browser.visit(url)
74
75
76
def test_workshop_flow(base_url, browser, outbox):
77
    tutor_type = f.create_usertype(slug='tutor', display_name='tutor')
78
    poc_type = f.create_usertype(slug='poc', display_name='poc')
79
    wr1 = f.create_workshop_rating()
80
    wr2 = f.create_workshop_rating()
81
    wr3 = f.create_workshop_rating()
82
    wr4 = f.create_workshop_rating()
83
    state1 = f.create_state(name='state1')
84
    state2 = f.create_state(name='state2')
85
86
    user = f.create_user()
87
    user.set_password('123123')
88
    user.save()
89
    url = base_url + '/workshop/'
90
    browser.visit(url)
91
    browser.fill('login', user.email)
92
    browser.fill('password', '123123')
93
    browser.find_by_css('[type=submit]')[0].click()
94
    assert len(outbox) == 1
95
    mail = outbox[0]
96
    confirm_link = re.findall(r'http.*/accounts/.*/', mail.body)
97
    assert confirm_link
98
    browser.visit(confirm_link[0])
99
    assert browser.title, "Confirm E-mail Address"
100
    browser.find_by_css('[type=submit]')[0].click()
101
102
    user.profile.usertype.clear()
103
    user.profile.usertype.add(poc_type)
104
    user.save()
105
    location = f.create_locaiton(state=state1)
106
    org = f.create_organisation(location=location)
107
    org.user.add(user)
108
    user.profile.interested_locations.add(org.location)
109
    user.profile.location = org.location
110
    user.profile.occupation = 'occupation'
111
    user.profile.work_location = 'work_location'
112
    user.profile.work_experience = 1
113
    user.profile.save()
114
    org.save()
115
116
    workshop = f.create_workshop(
117
        requester=org,
118
        status=WorkshopStatus.REQUESTED)
119
120
    workshop.expected_date = datetime.now() + timedelta(days=20)
121
    workshop.presenter.add(user)
122
    workshop.save()
123
    url = base_url + '/workshop/update/{}/'.format(workshop.id)
124
    browser.visit(url)
125
    browser.fill('login', user.email)
126
    browser.fill('password', '123123')
127
    browser.find_by_css('[type=submit]')[0].click()
128
129
    section1 = f.create_workshop_section(name='section1')
130
    location = org.location
131
132
    user.profile.usertype.clear()
133
    user.profile.usertype.add(tutor_type)
134
135
    user.profile.location = location
136
    user.profile.interested_states.add(state2)
137
    user.profile.interested_states.add(state1)
138
    user.profile.mobile = '1234567890'
139
    user.profile.interested_sections.add(section1)
140
    user.profile.interested_level = 1
141
    user.profile.github = 'https://github.com'
142
    user.profile.occupation = 'occupation'
143
    user.profile.work_location = 'work_location'
144
    user.profile.work_experience = 1
145
    user.profile.save()
146
    user.save()
147
148
    url = base_url + '/workshop/{}/'.format(workshop.id)
149
    browser.visit(url)
150
151
    accept_workshop_link = browser.find_by_text('Accept')[0]
152
    assert accept_workshop_link
153
    accept_workshop_link.click()
154
155
    user.profile.usertype.clear()
156
    user.profile.usertype.add(poc_type)
157
    user.profile.save()
158
    user.save()
159
160
#   checking to move requested workshop in hold state
161
    url = base_url + '/workshop/{}/'.format(workshop.id)
162
    browser.visit(url)
163
    hold_workshop_link = browser.find_by_text('Hold')[0]
164
    assert hold_workshop_link
165
    hold_workshop_link.click()
166
167
#   checking to move on hold workshop into requested state
168
    browser.visit(url)
169
    publish_workshop_link = browser.find_by_text('Publish/Request')[0]
170
    assert publish_workshop_link
171
    publish_workshop_link.click()
172
173
    browser.visit(url)
174
    # browser.screenshot()
175
    hold_workshop_link = browser.find_by_text('Hold')[0]
176
    assert hold_workshop_link
177
    hold_workshop_link.click()
178
179
    browser.visit(url)
180
    publish_workshop_link = browser.find_by_text('Publish/Request')[0]
181
    assert publish_workshop_link
182
    publish_workshop_link.click()
183
184
    browser.visit(url)
185
    accept_workshop_link = browser.find_by_text('Accept')[0]
186
    assert accept_workshop_link
187
    accept_workshop_link.click()
188
189
    # print(datetime.now() + timedelta(days=-10))
190
    workshop.expected_date = datetime.now() + timedelta(days=-60)
191
    # workshop.status = WorkshopStatus.FEEDBACK_PENDING
192
    workshop.save()
193
194
    browser.visit(url)
195
    print(wr1)
196
    print('{}-{}'.format(wr1.id, wr1.feedback_type))
197
    print(wr2)
198
    print(wr3)
199
    print(wr4)
200
    url = base_url + '/workshop/feedback/{}'.format(workshop.id)
201
    browser.visit(url)
202
    for wr in [wr1, wr2, wr3, wr4]:
203
        id = '{}-{}'.format(wr1.feedback_type, wr1.id)
204
        browser.check(id)
205
        # browser.check('3-1')
206
    browser.fill('comment', "Testing comments")
207
208
    browser.find_by_css('[type=submit]')[0].click()
209