1- from airly . _private import _DictToObj
1+ import json
22from unittest import TestCase
33
4+ import aiohttp
5+ from aioresponses import aioresponses
6+ from aiounittest import AsyncTestCase
7+
8+ from airly import Airly
9+ from airly ._private import _DictToObj
10+
11+ LATITUDE = 12
12+ LONGITUDE = 21
13+
414class _DictToObjTestCase (TestCase ):
515 def test_init_with_iterable (self ):
616 data = { 'key1' : 'value1' , 'key2' : 2 }
@@ -16,3 +26,50 @@ def test_init_with_kwargs(self):
1626 self .assertEqual ('value1' , sut .key1 )
1727 self .assertEqual (2 , sut ['key2' ])
1828 self .assertEqual (2 , sut .key2 )
29+
30+
31+ class RemainingRequestsTestCase (AsyncTestCase ):
32+ async def test_valid_remaining_requests_headers (self ):
33+ with open ("data/measurements_typical.json" ) as file :
34+ data = json .load (file )
35+ headers = {"X-RateLimit-Limit-day" : "1000" , "X-RateLimit-Remaining-day" : "993" }
36+
37+ session = aiohttp .ClientSession ()
38+
39+ with aioresponses () as session_mock :
40+ session_mock .get (
41+ 'https://airapi.airly.eu/v2/measurements/point?lat=12.000000&lng=21.000000' ,
42+ payload = data ,
43+ headers = headers ,
44+ )
45+ airly = Airly ("abcdef" , session )
46+ measurements = airly .create_measurements_session_point (LATITUDE , LONGITUDE )
47+ await measurements .update ()
48+
49+ await session .close ()
50+
51+ self .assertEqual (1000 , airly .requests_per_day )
52+ self .assertEqual (993 , airly .requests_remaining )
53+
54+ async def test_invalid_remaining_requests_headers (self ):
55+ with open ("data/measurements_typical.json" ) as file :
56+ data = json .load (file )
57+ headers = {}
58+
59+ session = aiohttp .ClientSession ()
60+
61+ with aioresponses () as session_mock :
62+ session_mock .get (
63+ 'https://airapi.airly.eu/v2/measurements/point?lat=12.000000&lng=21.000000' ,
64+ payload = data ,
65+ headers = headers ,
66+ )
67+ airly = Airly ("abcdef" , session )
68+ measurements = airly .create_measurements_session_point (LATITUDE , LONGITUDE )
69+ await measurements .update ()
70+
71+ await session .close ()
72+
73+ self .assertIsNone (airly .requests_per_day )
74+ self .assertIsNone (airly .requests_remaining )
75+
0 commit comments