-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_app.py
184 lines (143 loc) · 6.14 KB
/
test_app.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
from fastapi.testclient import TestClient
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from datetime import datetime, timedelta
import pytest
import json
from database import Base
from models import Weather
from app import app, get_db
engine = create_engine("sqlite:///test.db")
TestingSessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
Base.metadata.create_all(bind=engine)
def override_get_db():
try:
db = TestingSessionLocal()
yield db
finally:
db.close()
app.dependency_overrides[get_db] = override_get_db
client = TestClient(app)
@pytest.fixture(scope='function')
def teardown(request):
def teardown_db():
db = TestingSessionLocal()
db.query(Weather).delete()
db.commit()
request.addfinalizer(teardown_db)
def test_get_last_searched_oder_by_search_date(teardown):
db = TestingSessionLocal()
today = datetime.now()
yesterday = datetime.now() - timedelta(days=1)
db_weather_1 = Weather(zipcode='zipcode',
country_code='country_code',
city='today',
temperature=0,
last_searched=today,
last_updated=today)
db_weather_2 = Weather(zipcode='zipcode',
country_code='country_code',
city='yesterday',
temperature=0,
last_searched=yesterday,
last_updated=yesterday)
db.add(db_weather_1)
db.add(db_weather_2)
db.commit()
response = client.get("/api/weathers/")
assert response.status_code == 200
assert len(response.json()["items"]) == 2
assert response.json()["items"][0]["city"] == "today"
def test_get_last_searched_paging(teardown):
db = TestingSessionLocal()
today = datetime.now()
yesterday = datetime.now() - timedelta(days=1)
db_weather_1 = Weather(zipcode='zipcode',
country_code='country_code',
city='today',
temperature=0,
last_searched=today,
last_updated=today)
db_weather_2 = Weather(zipcode='zipcode',
country_code='country_code',
city='yesterday',
temperature=0,
last_searched=yesterday,
last_updated=yesterday)
db.add(db_weather_1)
db.add(db_weather_2)
db.commit()
response = client.get("/api/weathers/?page=0&size=1")
assert response.status_code == 200
assert len(response.json()["items"]) == 1
assert response.json()["total"] == 2
def test_check_weather_first_time(mocker, teardown):
def mock_openweather(zipcode, country_code, units):
return {"cod": 200, "name": "mock_owm", "main": { "temp": 25 }}
mocker.patch('app.owm.get_weather', mock_openweather)
response = client.get("/api/weathers/88000,BR")
assert response.json()["temperature"] == 25
def test_check_weather_owm_error(mocker, teardown):
def mock_openweather(zipcode, country_code, units):
return {"cod": 404, "message": "city not found"}
mocker.patch('app.owm.get_weather', mock_openweather)
response = client.get("/api/weathers/88000,BR")
assert response.status_code == 404
assert response.json()["detail"] == "city not found"
def test_check_weather_already_searched_city(mocker, teardown):
db = TestingSessionLocal()
today = datetime.now()
db_weather_1 = Weather(zipcode='88000',
country_code='BR',
city='today',
temperature=0,
last_searched=today,
last_updated=today)
db.add(db_weather_1)
db.commit()
def mock_openweather(zipcode, country_code, units):
return {"cod": 200, "name": "today", "main": { "temp": 25 }}
mocker.patch('app.owm.get_weather', mock_openweather)
response = client.get("/api/weathers/88000,BR")
assert response.status_code == 200
assert response.json()["temperature"] == 0
assert response.json()["last_searched"] != today.isoformat()
assert response.json()["last_updated"] == today.isoformat()
def test_check_weather_update_searched_city_one_hour_gap(mocker, teardown):
db = TestingSessionLocal()
one_hour_ago = datetime.now() - timedelta(hours=1, seconds=1)
db_weather_1 = Weather(zipcode='88000',
country_code='BR',
city='today',
temperature=0,
last_searched=one_hour_ago,
last_updated=one_hour_ago)
db.add(db_weather_1)
db.commit()
def mock_openweather(zipcode, country_code, units):
return {"cod": 200, "name": "today", "main": { "temp": 25 }}
mocker.patch('app.owm.get_weather', mock_openweather)
response = client.get("/api/weathers/88000,BR")
assert response.status_code == 200
assert response.json()["temperature"] == 25
assert response.json()["last_searched"] != one_hour_ago.isoformat()
assert response.json()["last_updated"] != one_hour_ago.isoformat()
def test_check_weather_update_searched_city_one_day_gap(mocker, teardown):
db = TestingSessionLocal()
one_day_ago = datetime.now() - timedelta(days=1)
db_weather_1 = Weather(zipcode='88000',
country_code='BR',
city='today',
temperature=0,
last_searched=one_day_ago,
last_updated=one_day_ago)
db.add(db_weather_1)
db.commit()
def mock_openweather(zipcode, country_code, units):
return {"cod": 200, "name": "today", "main": { "temp": 25 }}
mocker.patch('app.owm.get_weather', mock_openweather)
response = client.get("/api/weathers/88000,BR")
assert response.status_code == 200
assert response.json()["temperature"] == 25
assert response.json()["last_searched"] != one_day_ago.isoformat()
assert response.json()["last_updated"] != one_day_ago.isoformat()