1
1
import pytest
2
- from server import app
2
+ import json
3
3
import html
4
+ from server import app
5
+
6
+
7
+ @pytest .fixture
8
+ def temp_competitions_file (tmpdir , request ):
9
+ original_competitions_file = tmpdir .join ("competitions.json" )
10
+ num_places = request .param if hasattr (request , "param" ) else "10"
11
+ initial_competitions = {
12
+ "competitions" : [
13
+ {
14
+ "name" : "Spring Festival" ,
15
+ "date" : "2020-03-27 10:00:00" ,
16
+ "numberOfPlaces" : "13" ,
17
+ },
18
+ {
19
+ "name" : "Fall Classic" ,
20
+ "date" : "2020-10-22 13:30:00" ,
21
+ "numberOfPlaces" : num_places ,
22
+ },
23
+ ]
24
+ }
25
+ with open (original_competitions_file , "w" ) as competitions_file :
26
+ json .dump (initial_competitions , competitions_file , indent = 4 )
27
+ app .config ["COMPETITIONS_FILE" ] = str (original_competitions_file )
28
+ return initial_competitions
29
+
4
30
31
+ @pytest .fixture
32
+ def client (temp_competitions_file ):
33
+ with app .test_client () as client :
34
+ yield client
5
35
6
- @pytest .fixture ()
7
- def client ():
8
- return app .test_client ()
9
36
10
- def test_purchasePlaces_input_less_12 (client ):
11
- response = client .post ("/purchasePlaces" , data = {"places" :11 ,"competition" :"Fall Classic" ,"club" :"Iron Temple" })
37
+ @pytest .fixture (autouse = True )
38
+ def reset_competitions (temp_competitions_file ):
39
+ from server import competitions
40
+
41
+ competitions .clear ()
42
+ competitions .extend (temp_competitions_file ["competitions" ])
43
+
44
+
45
+ @pytest .mark .parametrize ("temp_competitions_file" , ["10" ], indirect = True )
46
+ def test_purchasePlaces_input_less_12_sucess (client , tmpdir ):
47
+ response = client .post (
48
+ "/purchasePlaces" ,
49
+ data = {
50
+ "places" : 9 ,
51
+ "competition" : "Fall Classic" ,
52
+ "club" : "Iron Temple" ,
53
+ },
54
+ )
12
55
assert response .status_code == 200
13
- assert b'Great-booking complete!' in response .data
56
+ assert b"Great-booking complete!" in response .data
57
+
58
+
59
+ @pytest .mark .parametrize ("temp_competitions_file" , ["10" ], indirect = True )
60
+ def test_purchasePlaces_input_less_12_but_more_than_places_available (
61
+ client , tmpdir
62
+ ):
63
+ response = client .post (
64
+ "/purchasePlaces" ,
65
+ data = {
66
+ "places" : 11 ,
67
+ "competition" : "Fall Classic" ,
68
+ "club" : "Iron Temple" ,
69
+ },
70
+ )
71
+ assert response .status_code == 302
72
+ response_redirect = client .get ("/book/Fall%20Classic/Iron%20Temple" )
73
+ decoding_response_data = response_redirect .data .decode ("UTF-8" )
74
+ converted_str = html .unescape (decoding_response_data )
75
+ assert (
76
+ "Places required must be lower than places available: 10"
77
+ in converted_str
78
+ )
14
79
15
80
16
- def test_purchasePlaces_input_more_12 (client ):
17
- response = client .post ("/purchasePlaces" , data = {"places" :13 ,"competition" :"Fall Classic" ,"club" :"Iron Temple" })
81
+ @pytest .mark .parametrize ("temp_competitions_file" , ["14" ], indirect = True )
82
+ def test_purchasePlaces_input_more_12 (client , tmpdir ):
83
+ response = client .post (
84
+ "/purchasePlaces" ,
85
+ data = {
86
+ "places" : 13 ,
87
+ "competition" : "Fall Classic" ,
88
+ "club" : "Iron Temple" ,
89
+ },
90
+ )
18
91
assert response .status_code == 302
19
92
response_redirect = client .get ("/book/Fall%20Classic/Iron%20Temple" )
20
- decoding_response_data = response_redirect .data .decode ('UTF-8' )
21
- converted_str = html .unescape (decoding_response_data )
22
- assert "Places required must be a positive integer that does not exceed 12" in converted_str
93
+ decoding_response_data = response_redirect .data .decode ("UTF-8" )
94
+ converted_str = html .unescape (decoding_response_data )
95
+ assert (
96
+ "Places required must be a positive integer that does not exceed 12"
97
+ in converted_str
98
+ )
23
99
24
- def test_purchasePlaces_input_negatif (client ):
25
- response = client .post ("/purchasePlaces" , data = {"places" :- 1 ,"competition" :"Fall Classic" ,"club" :"Iron Temple" })
100
+
101
+ @pytest .mark .parametrize ("temp_competitions_file" , ["10" ], indirect = True )
102
+ def test_purchasePlaces_input_negatif (client , tmpdir ):
103
+ response = client .post (
104
+ "/purchasePlaces" ,
105
+ data = {
106
+ "places" : - 1 ,
107
+ "competition" : "Fall Classic" ,
108
+ "club" : "Iron Temple" ,
109
+ },
110
+ )
26
111
assert response .status_code == 302
27
112
response_redirect = client .get ("/book/Fall%20Classic/Iron%20Temple" )
28
- decoding_response_data = response_redirect .data .decode (' UTF-8' )
29
- converted_str = html .unescape (decoding_response_data )
113
+ decoding_response_data = response_redirect .data .decode (" UTF-8" )
114
+ converted_str = html .unescape (decoding_response_data )
30
115
assert "Places required must be a positive integer" in converted_str
31
116
32
- def test_purchasePlaces_input_none (client ):
33
- response = client .post ("/purchasePlaces" , data = {"places" :"" ,"competition" :"Fall Classic" ,"club" :"Iron Temple" })
117
+
118
+ @pytest .mark .parametrize ("temp_competitions_file" , ["10" ], indirect = True )
119
+ def test_purchasePlaces_input_none (client , tmpdir ):
120
+ response = client .post (
121
+ "/purchasePlaces" ,
122
+ data = {
123
+ "places" : "" ,
124
+ "competition" : "Fall Classic" ,
125
+ "club" : "Iron Temple" ,
126
+ },
127
+ )
34
128
assert response .status_code == 302
35
129
response_redirect = client .get ("/book/Fall%20Classic/Iron%20Temple" )
36
- decoding_response_data = response_redirect .data .decode ('UTF-8' )
37
- converted_str = html .unescape (decoding_response_data )
38
- assert "Places required must be a positive integer" in converted_str
130
+ decoding_response_data = response_redirect .data .decode ("UTF-8" )
131
+ converted_str = html .unescape (decoding_response_data )
132
+ assert "Places required must be a positive integer" in converted_str
133
+
134
+
135
+ @pytest .mark .parametrize ("temp_competitions_file" , ["10" ], indirect = True )
136
+ def test_purchasePlaces_update_number_places (client , tmpdir ):
137
+ response = client .post (
138
+ "/purchasePlaces" ,
139
+ data = {
140
+ "places" : 1 ,
141
+ "competition" : "Fall Classic" ,
142
+ "club" : "Iron Temple" ,
143
+ },
144
+ )
145
+ assert response .status_code == 200
146
+ assert b"Great-booking complete!" in response .data
147
+ # Verify number of places have been updated
148
+ with open (app .config ["COMPETITIONS_FILE" ]) as f :
149
+ updated_competitions = json .load (f )["competitions" ]
150
+ updated_competition = next (
151
+ c for c in updated_competitions if c ["name" ] == "Fall Classic"
152
+ )
153
+ print ("in test updated competition" , updated_competition )
154
+ assert updated_competition ["numberOfPlaces" ] == "9"
0 commit comments