-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_app.py
47 lines (40 loc) · 1.98 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
import pytest
from app import app
@pytest.fixture
def client():
with app.test_client() as client:
yield client
# Home route test
def test_home(client):
response = client.get('/')
assert response.status_code == 200
assert response.json == {'version': '1.0'}
# Fahrenheit to Celsius
@pytest.mark.parametrize("temp, expected", [(212, 100.0), (32, 0.0), (-40, -40.0)])
def test_convert_temp_f_to_c(client, temp, expected):
response = client.get(f'/convert-temp?temp={temp}&scale=fahrenheit&target_scale=celsius')
assert response.status_code == 200
assert response.json['converted_temp'] == pytest.approx(expected, rel=1e-2)
# Celsius to Fahrenheit
@pytest.mark.parametrize("temp, expected", [(100, 212.0), (0, 32.0), (-40, -40.0)])
def test_convert_temp_c_to_f(client, temp, expected):
response = client.get(f'/convert-temp?temp={temp}&scale=celsius&target_scale=fahrenheit')
assert response.status_code == 200
assert response.json['converted_temp'] == pytest.approx(expected, rel=1e-2)
# Kelvin to Celsius
@pytest.mark.parametrize("temp, expected", [(273.15, 0.0), (373.15, 100.0), (233.15, -40.0)])
def test_convert_temp_k_to_c(client, temp, expected):
response = client.get(f'/convert-temp?temp={temp}&scale=kelvin&target_scale=celsius')
assert response.status_code == 200
assert response.json['converted_temp'] == pytest.approx(expected, rel=1e-2)
# Kelvin to Fahrenheit
@pytest.mark.parametrize("temp, expected", [(273.15, 32.0), (373.15, 212.0), (233.15, -40.0)])
def test_convert_temp_k_to_f(client, temp, expected):
response = client.get(f'/convert-temp?temp={temp}&scale=kelvin&target_scale=fahrenheit')
assert response.status_code == 200
assert response.json['converted_temp'] == pytest.approx(expected, rel=1e-2)
# Invalid scale handling
def test_convert_temp_invalid_scale(client):
response = client.get('/convert-temp?temp=100&scale=unknown&target_scale=celsius')
assert response.status_code == 400
assert 'error' in response.json