def testCall(self): result = API('https://ipapi.co/json/', result_filter=Filter(lat='latitude', long='longitude')) > \ API('https://api.sunrise-sunset.org/json?lat={lat}&lng={long}&date=today', validate=lambda response: response['status'] == 'OK', result_filter=Filter(sunrise='results sunrise')) > \ Container() container = Container() container.value = referenceAPI() # noinspection PyUnresolvedReferences self.assertEqual(container.value, result.value, 'The call operator does not work properly!')
def testClassGetitem(self): result = API[ API(LOCATION_URL, result_filter=Filter(lat='latitude', long='longitude')), API(TIME_URL, validate=lambda response: response['status'] == 'OK', result_filter=Filter(sunrise='results sunrise'))] container = Container() container.value = referenceAPI() self.assertEqual(container.value, result.value, 'The class_getitem operator does not work properly!')
def testNoInternet(self): api = API('https://ipapi.co/json/', result_filter=Filter(lat='latitude', long='longitude')) with no_connection(): self.assertRaises( RequestException, api.request, )
def testInit(self): validate = lambda data: 'latitude' in data and 'longitude' in data api = API(url=LOCATION_URL, method=Method.POST, result_filter=Filter(lat='latitude', long='longitude'), validate=validate, post_data=dict(date='today')) self.assertEqual(LOCATION_URL, api.url, 'Url not passed properly!') self.assertEqual(Method.POST, api.method, 'Method not passed properly!') self.assertEqual(Filter(lat='latitude', long='longitude'), api.result_filter, 'Filter not passed properly!') self.assertEqual(validate, api.validate, 'Validate not passed properly!') self.assertEqual(dict(date='today'), api.post_data, 'Post-Data not passed properly!')
def testString(self): d = dict(sunrise='results sunrise') f = Filter(**d) self.assertEqual( f'Filter[{d}]', str(f), 'String of filter was not correct!' ) self.assertEqual( str(f), repr(f), 'repr() of Filter was not correct!' )
def testBadFilter(self): data = { 'results': {'sunrise': '4:06:40 AM', 'sunset': '6:33:30 PM', 'solar_noon': '11:20:05 AM', 'day_length': '14:26:50', 'civil_twilight_begin': '3:30:47 AM', 'civil_twilight_end': '7:09:23 PM', 'nautical_twilight_begin': '2:45:20 AM', 'nautical_twilight_end': '7:54:50 PM', 'astronomical_twilight_begin': '1:52:18 AM', 'astronomical_twilight_end': '8:47:52 PM'}, 'status': 'OK' } f = Filter(sunrise='result sunrise') self.assertRaises( APIException, f, data )
def testFilterNonFlat(self): data = { 'results': {'sunrise': '4:06:40 AM', 'sunset': '6:33:30 PM', 'solar_noon': '11:20:05 AM', 'day_length': '14:26:50', 'civil_twilight_begin': '3:30:47 AM', 'civil_twilight_end': '7:09:23 PM', 'nautical_twilight_begin': '2:45:20 AM', 'nautical_twilight_end': '7:54:50 PM', 'astronomical_twilight_begin': '1:52:18 AM', 'astronomical_twilight_end': '8:47:52 PM'}, 'status': 'OK' } expected = {'sunrise': '4:06:40 AM'} f = Filter(sunrise='results sunrise') self.assertDictEqual( expected, f(data), 'Filter did not perform correctly!' )
def testFilterFlat(self): data = { 'ip': '127.0.0.1', 'city': 'Town', 'region': 'Region', 'region_code': 'hahaha', 'country': 'DE', 'country_code': 'DE', 'country_code_iso3': 'DEU', 'country_capital': 'Berlin', 'country_tld': '.de', 'country_name': 'Germany', 'continent_code': 'EU', 'in_eu': True, 'postal': '00000', 'latitude': 48.3214, 'longitude': 9.7864, 'timezone': 'Europe/Berlin', 'utc_offset': '+0200', 'country_calling_code': '+49', 'currency': 'EUR', 'currency_name': 'Euro', 'languages': 'de', 'country_area': 357021.0, 'country_population': 81802257.0, 'asn': 'whatever', 'org': 'some isp' } expected = dict(lat=48.3214, long=9.7864) f = Filter(lat='latitude', long='longitude') self.assertDictEqual( expected, f(data), 'Flat Filter did not perform correctly!' )