File: test_all.py

package info (click to toggle)
pyopenweathermap 0.2.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 236 kB
  • sloc: python: 547; makefile: 7
file content (117 lines) | stat: -rw-r--r-- 3,741 bytes parent folder | download
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
import os

import json
import pytest
from pyopenweathermap import RequestError, create_owm_client
from pyopenweathermap.data_converter import DataConverter

LATITUDE = '52.3731339'
LONGITUDE = '4.8903147'


@pytest.mark.network
@pytest.mark.asyncio
async def test_api_30():
    api_key = os.getenv('OWM_API_KEY')
    client = create_owm_client(api_key, 'v3.0')
    report = await client.get_weather(LATITUDE, LONGITUDE)
    assert report.current.date_time is not None
    assert report.hourly_forecast[0].condition.id is not None
    assert report.daily_forecast[0].condition.id is not None
    
    
@pytest.mark.network
@pytest.mark.asyncio
async def test_free_current_weather():
    api_key = os.getenv('OWM_API_KEY')
    client = create_owm_client(api_key, 'current')
    report = await client.get_weather(LATITUDE, LONGITUDE)
    assert report.current.date_time is not None
    assert len(report.hourly_forecast) is 0
    assert len(report.daily_forecast) is 0
    
    
@pytest.mark.network
@pytest.mark.asyncio
async def test_free_forecast_weather():
    api_key = os.getenv('OWM_API_KEY')
    client = create_owm_client(api_key, 'forecast')
    report = await client.get_weather(LATITUDE, LONGITUDE)
    assert report.current is None
    assert report.hourly_forecast[0].temperature is not None
    assert len(report.daily_forecast) is 0

@pytest.mark.network
@pytest.mark.asyncio
async def test_air_pollution():
    api_key = os.getenv('OWM_API_KEY')
    client = create_owm_client(api_key, 'air_pollution')

    report = await client.get_air_pollution(LATITUDE, LONGITUDE)
    assert report.current.date_time is not None
    assert isinstance(report.current.aqi, int)
    assert report.current.co is not None
    assert len(report.hourly_forecast) > 0
    assert isinstance(report.hourly_forecast[0].aqi, int)
    assert report.hourly_forecast[0].co is not None


@pytest.mark.asyncio
async def test_request_error():
    api_key = os.getenv('OWM_API_KEY')
    client = create_owm_client(api_key, 'v3.0')
    with pytest.raises(RequestError) as error:
        await client.get_weather('100', LONGITUDE)
    assert error is not None


@pytest.mark.network
@pytest.mark.asyncio
async def test_api_key_validation():
    client = create_owm_client('123', 'v3.0')
    assert await client.validate_key() is False


def test_current_weather_converter():
    data = None
    with open('tests/onecall_current.json') as f:
        data = json.load(f)
    weather = DataConverter.onecall_to_current_weather(data)
    assert weather.date_time is not None
    assert weather.condition.id is not None


def test_minutely_weather_deserialization():
    data = None
    with open('tests/onecall_minutely.json') as f:
        data = json.load(f)
    weather = DataConverter.onecall_to_minutely_weather_forecast(data)
    assert weather.date_time is not None
    assert weather.precipitation is not None


def test_hourly_weather_deserialization():
    data = None
    with open('tests/onecall_hourly.json') as f:
        data = json.load(f)
    weather = DataConverter.onecall_to_hourly_weather_forecast(data)
    assert weather.date_time is not None
    assert weather.condition.id is not None


def test_weather_deserialization():
    data = None
    with open('tests/free_current.json') as f:
        data = json.load(f)
    weather = DataConverter.free_to_current_weather(data)
    assert weather.date_time is not None
    assert weather.condition.id is not None


def test_forecast_deserialization():
    data = None
    with open('tests/free_forecast.json') as f:
        data = json.load(f)
    weather = DataConverter.free_to_hourly_weather_forecast(data)
    assert weather.date_time is not None
    assert weather.condition.id is not None