-
Notifications
You must be signed in to change notification settings - Fork 1
/
tests.py
285 lines (191 loc) · 9.29 KB
/
tests.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
from flask import Flask, request, session, url_for
from models.User import User
from models.Plant import Plant
from models.PlantType import PlantType
from controllers.UserController import LoginForm, RegistrationForm
from controllers.PlantController import PlantRegistrationForm, PlantTypeRegistrationForm
from database import db
from models.Device import *
import pytest
testUsername = 'test123'
testUsername2 = 'test1234'
testPassword = 'password'
incorrectPassword = 'password!'
testMACAddress = 'A5:A6:BE:C2:16:EA'
testTime = time.time()
testEmail = 'email@example.com'
testType = 'Fern'
testLevel = 2
testLocation = 'Hallway'
plantName = 'Fern'
requiredWater = 50 # requires_water = db.Column(db.Integer)
waterFrequency = 345600 # 4 Days: watering_frequency = db.Column(db.Integer) #Interval in seconds.
minTemp = 10.0 # min_temp = db.Column(db.Float)
maxTemp = 40.0 # max_temp = db.Column(db.Float)
minHumidity = 25.0 # min_humidity = db.Column(db.Float)
maxHumidity = 70.0 # max_humidity = db.Column(db.Float)
minMoisture = 30.0 # min_moisture = db.Column(db.Float)
maxMoisture = 80.0 # max_moisture = db.Column(db.Float)
idealMoistureLevel = 40 # ideal_moisture_level = db.Column(db.Integer)
minLightIntensity = 20.0 # min_light_intensity = db.Column(db.Float)
maxLightIntensity = 100.0 # max_light_intensity = db.Column(db.Float)
lastWatered = 0 # last_watered = db.Column(db.Integer) # Time in seconds since UNIX epoch.
@pytest.fixture()
def setup(app):
testUser = User(password=testPassword, username=testUsername, email='email@example.com')
client = app.test_client()
db.session.add(testUser)
db.session.commit()
yield ""
db.session.delete(testUser)
db.session.commit()
def test_register_form_valid(app):
form = RegistrationForm(username=testUsername, password=testPassword, email=testEmail)
assert not form.validate()
def test_register_form_invalid_email(app):
form = RegistrationForm(username=testUsername, password=testPassword, email="test@testcom")
assert not form.validate()
def test_register_form_invalid_password(app):
form = RegistrationForm(username=testUsername, password=testPassword, email=testEmail)
assert not form.validate()
def test_register(app):
client = app.test_client()
receipt=client.post("/register", data={'username': testUsername2, 'password': testPassword, 'confirm_password': testPassword, 'email': testEmail})
with client:
user = User.query.filter_by(username=testUsername2).one_or_none()
assert user
assert user.login(testPassword)
db.session.delete(User.query.filter_by(username=testUsername2).one())
db.session.commit()
def test_login(app, setup):
client = app.test_client()
client.post("/login", data={'username':testUsername, 'password':testPassword})
with client:
client.get('/')
assert session.get('username') is not None
assert User.query.filter_by(username=testUsername).one() is not None
def test_incorrect_password(app, setup):
client = app.test_client()
receipt = client.post("/login", data={'username':testUsername, 'password':incorrectPassword})
with client:
assert "Incorrect password" in str(receipt.get_data())
assert session.get('username') is None
def test_multiple_incorrect_passwords(app, setup):
client = app.test_client()
for i in range(6):
receipt = client.post("/login", data={'username':testUsername, 'password':incorrectPassword})
with client:
assert "You have exceeded the maximum number of password attempts. Please try again later." in str(receipt.get_data())
assert session.get('username') is None
@pytest.fixture()
def plant_setup(app):
testPlantType = PlantType(plantName, requiredWater, waterFrequency, minTemp, maxTemp, minHumidity, maxHumidity, minMoisture, maxMoisture, idealMoistureLevel, minLightIntensity, maxLightIntensity)
device = Device(testMACAddress, testTime)
client = app.test_client()
db.session.add(device)
db.session.add(testPlantType)
db.session.commit()
yield ""
db.session.delete(testPlantType)
db.session.delete(device)
# num_rows_deleted = db.session.query(Plant).delete()
db.session.commit()
@pytest.fixture()
def plant_full_setup(app):
testPlantType = PlantType(plantName, requiredWater, waterFrequency, minTemp, maxTemp, minHumidity, maxHumidity, minMoisture, maxMoisture, idealMoistureLevel, minLightIntensity, maxLightIntensity)
device = Device(testMACAddress, testTime)
client = app.test_client()
db.session.add(device)
db.session.add(testPlantType)
db.session.commit()
testPlant = Plant(type=testPlantType.id, level=testLevel, location=testLocation)
print(testPlantType.id)
db.session.add(testPlant)
db.session.commit()
yield ""
db.session.delete(testPlant)
db.session.delete(testPlantType)
db.session.delete(device)
# num_rows_deleted = db.session.query(Plant).delete()
db.session.commit()
def test_plant_form_valid(app):
form = PlantRegistrationForm(plant_type=testType, level=testLevel, location=testLocation)
assert not form.validate()
def test_plant_type_register_form_valid(app):
form = PlantTypeRegistrationForm(name = plantName, requires_water = requiredWater , watering_frequency = waterFrequency , min_temp = minTemp ,
max_temp = maxTemp , min_humidity = minHumidity , max_humidity = maxHumidity , min_moisture = minMoisture , max_moisture = maxMoisture ,
ideal_moisture_level = idealMoistureLevel , min_light_intensity = minLightIntensity , max_light_intensity = maxLightIntensity )
assert form.validate()
# Test.No 14
def test_plant_added(app, plant_setup):
client = app.test_client()
plants = Plant.query.all()
url = "/register/plant/" + testMACAddress
# print (url)
receipt = client.post(url, data={'plant_type':testType, 'level':testLevel, 'location':testLocation})
print (str(receipt.get_data()))
# client.get('/plants')
# print (str(receipt.get_data()))
plants_after = Plant.query.all()
print (plants_after)
# plant_types = PlantType.query.all()
# print (plant_types)
# assert Plant.query.filter_by(level=testLevel) is not None
assert len(plants) != len(plants_after)
# db.session.delete(User.query.filter_by(username=testUsername2).one())
# db.session.commit()
# Assert for no error message
# assert not "Error" in str(receipt.get_data())
# Test.No 15
def test_plant_not_created(app, plant_full_setup):
client = app.test_client()
client.post("/register/plant", data={'plant_type':testType, 'level':testLevel, 'location':testLocation})
# Check there aren't two entries with the same name
assert Plant.query.filter_by(level=testLevel).one()
# Assert for error message
# assert "Error" in str(receipt.get_data())
# Test.No 16
def test_plant_not_created_with_missing_element(app, plant_setup):
client = app.test_client()
plants = Plant.query.all()
client.post("/register/plant", data={'level':testLevel, 'location':testLocation})
plants_after = Plant.query.all()
assert len(plants) == len(plants_after)
# plant = Plant.query.filter_by(level=testLevel).one()
# assert plant is None
# Assert error,
# assert "Error" in str(receipt.get_data())
# Test.No 30
def test_add_plant_type(app):
client = app.test_client()
plants_types = PlantType.query.all()
client.post("/add_plant_type", data={'name':plantName, 'requires_water':requiredWater , 'watering_frequency':waterFrequency , 'min_temp':minTemp ,
'max_temp':maxTemp , 'min_humidity':minHumidity , 'max_humidity':maxHumidity , 'min_moisture':minMoisture , 'max_moisture':maxMoisture ,
'ideal_moisture_level':idealMoistureLevel , 'min_light_intensity':minLightIntensity , 'max_light_intensity':maxLightIntensity })
plants_types_after = PlantType.query.all()
assert not len(plants_types) == len(plants_types_after)
# Test.No 31
def test_cannot_add_duplicate_plant_type(app, plant_setup):
client = app.test_client()
plants_types = PlantType.query.all()
receipt = client.post("/add_plant_type", data={'name':plantName, 'requires_water':requiredWater , 'watering_frequency':waterFrequency , 'min_temp':minTemp ,
'max_temp':maxTemp , 'min_humidity':minHumidity , 'max_humidity':maxHumidity , 'min_moisture':minMoisture , 'max_moisture':maxMoisture ,
'ideal_moisture_level':idealMoistureLevel , 'min_light_intensity':minLightIntensity , 'max_light_intensity':maxLightIntensity })
print (str(receipt.get_data()))
plants_types_after = PlantType.query.all()
assert len(plants_types) == len(plants_types_after)
# Assert Error occurs in reciept
# Test.No 25
def test_delete_plant_type(app, plant_full_setup):
client = app.test_client()
plants = Plant.query.all()
client.post("/plant_remove/1", data={'plant_id':1})
plants_after = Plant.query.all()
assert not len(plants) == len(plants_after)
# import flask_unittest
#
# class RegisterUserTest(UserTest):
# def test_registration(self, client):
# form = RegistrationForm(request.form)
# client.post(url_for('register'), data={form: form})
# self.assertIsNotNone(User.query.first())