2018-01-27 23:36:49 +01:00
|
|
|
import os
|
|
|
|
|
|
2015-04-06 20:00:30 +02:00
|
|
|
import pytest
|
2023-12-28 21:02:41 +01:00
|
|
|
from datetime import datetime, timezone
|
2015-04-06 20:00:30 +02:00
|
|
|
|
|
|
|
|
from pyhamtools.lookuplib import LookupLib
|
|
|
|
|
from pyhamtools.exceptions import APIKeyMissingError
|
|
|
|
|
from pyhamtools.consts import LookupConventions as const
|
|
|
|
|
|
2018-01-27 23:36:49 +01:00
|
|
|
try:
|
|
|
|
|
QRZ_USERNAME = str(os.environ['QRZ_USERNAME'])
|
|
|
|
|
QRZ_PWD = str(os.environ['QRZ_PWD'])
|
|
|
|
|
except Exception:
|
|
|
|
|
pytestmark = pytest.mark.skip("Environment variables with QRZ.com credentials not set")
|
2016-01-11 23:38:28 +01:00
|
|
|
|
2018-01-27 23:36:49 +01:00
|
|
|
#Fixtures
|
|
|
|
|
#===========================================================
|
2015-04-06 20:00:30 +02:00
|
|
|
|
2019-05-30 14:24:22 +02:00
|
|
|
response_1A1AB = {
|
2023-12-28 21:02:41 +01:00
|
|
|
u'biodate': datetime(2018, 9, 7, 21, 17, 7, tzinfo=timezone.utc),
|
2019-05-30 14:24:22 +02:00
|
|
|
u'bio': u'0',
|
|
|
|
|
u'license_class': u'C',
|
2023-12-28 21:02:41 +01:00
|
|
|
u'moddate': datetime(2008, 11, 2, 15, 0, 38, tzinfo=timezone.utc),
|
2019-05-30 14:24:22 +02:00
|
|
|
u'locator': u'JN61fw',
|
|
|
|
|
u'callsign': u'1A1AB',
|
|
|
|
|
u'addr2': u'00187 Rome',
|
|
|
|
|
u'user': u'1A1AB',
|
|
|
|
|
u'adif': 246,
|
|
|
|
|
u'addr1': u'Via Condotti, 68',
|
|
|
|
|
u'mqsl': True,
|
|
|
|
|
u'ccode': 128,
|
|
|
|
|
u'land': u'SMO Malta',
|
|
|
|
|
u'codes': u'HVB',
|
|
|
|
|
u'name': u'Morgan',
|
|
|
|
|
u'geoloc': u'user',
|
|
|
|
|
u'country': u'Italy',
|
|
|
|
|
u'lotw': True,
|
|
|
|
|
u'longitude': 12.456779,
|
|
|
|
|
u'eqsl': True,
|
|
|
|
|
u'fname': u'Jonas',
|
|
|
|
|
u'latitude': 41.94417
|
|
|
|
|
}
|
2015-04-06 20:00:30 +02:00
|
|
|
|
|
|
|
|
response_333 = {
|
2016-01-11 23:38:28 +01:00
|
|
|
const.COUNTRY: u'Iraq',
|
|
|
|
|
u'cc': u'IQ',
|
|
|
|
|
const.LONGITUDE: 44.362793,
|
|
|
|
|
const.CQZ: 21,
|
|
|
|
|
const.ITUZ: 39,
|
|
|
|
|
const.LATITUDE: 33.358062,
|
|
|
|
|
u'timezone': 3.0,
|
|
|
|
|
const.ADIF: 333,
|
|
|
|
|
const.CONTINENT: u'AS',
|
2015-04-06 20:00:30 +02:00
|
|
|
u'ccc': u'IRQ'
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#TESTS
|
|
|
|
|
#===========================================================
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class TestQrzConstructur:
|
2016-01-11 23:38:28 +01:00
|
|
|
|
2015-04-06 20:00:30 +02:00
|
|
|
def test_get_session_key(self):
|
|
|
|
|
lib = LookupLib(lookuptype="qrz", username=QRZ_USERNAME, pwd=QRZ_PWD)
|
|
|
|
|
assert len(lib._apikey) == 32
|
2016-01-11 23:38:28 +01:00
|
|
|
|
2015-04-06 20:00:30 +02:00
|
|
|
def test_get_session_key_with_invalid_username(self):
|
|
|
|
|
with pytest.raises(ValueError):
|
|
|
|
|
lib = LookupLib(lookuptype="qrz", username="hello", pwd=QRZ_PWD)
|
2016-01-11 23:38:28 +01:00
|
|
|
|
2015-04-06 20:00:30 +02:00
|
|
|
def test_get_session_key_with_invalid_password(self):
|
|
|
|
|
with pytest.raises(ValueError):
|
|
|
|
|
lib = LookupLib(lookuptype="qrz", username=QRZ_USERNAME, pwd="hello")
|
|
|
|
|
|
|
|
|
|
def test_get_session_key_with_empty_username_and_password(self):
|
|
|
|
|
with pytest.raises(ValueError):
|
|
|
|
|
lib = LookupLib(lookuptype="qrz", username="", pwd="")
|
|
|
|
|
|
|
|
|
|
|
2016-01-11 23:38:28 +01:00
|
|
|
class TestQrz_Callsign_Lookup:
|
|
|
|
|
|
2015-04-06 20:00:30 +02:00
|
|
|
def test_lookup_callsign(self, fix_qrz):
|
2016-01-11 23:38:28 +01:00
|
|
|
|
2019-05-30 14:24:22 +02:00
|
|
|
data = fix_qrz._lookup_qrz_callsign("1A1AB", fix_qrz._apikey)
|
2015-04-06 20:00:30 +02:00
|
|
|
data.pop('u_views', None)
|
2019-05-30 14:24:22 +02:00
|
|
|
assert data == response_1A1AB
|
|
|
|
|
assert len(data) == len(response_1A1AB)
|
2016-01-11 23:38:28 +01:00
|
|
|
|
2015-04-06 20:00:30 +02:00
|
|
|
def test_lookup_callsign_with_unicode_escaping(self, fix_qrz):
|
2019-05-30 14:24:22 +02:00
|
|
|
data = fix_qrz._lookup_qrz_callsign("1A1AB", fix_qrz._apikey)
|
2015-04-06 20:00:30 +02:00
|
|
|
data.pop('u_views', None)
|
2019-05-30 14:24:22 +02:00
|
|
|
assert data == response_1A1AB
|
2016-01-11 23:38:28 +01:00
|
|
|
|
2015-04-06 20:00:30 +02:00
|
|
|
def test_lookup_callsign_does_not_exist(self, fix_qrz):
|
|
|
|
|
with pytest.raises(KeyError):
|
|
|
|
|
fix_qrz._lookup_qrz_callsign("XX8XX", fix_qrz._apikey)
|
2016-01-11 23:38:28 +01:00
|
|
|
|
2015-04-06 20:00:30 +02:00
|
|
|
def test_lookup_callsign_with_empty_input(self, fix_qrz):
|
|
|
|
|
with pytest.raises(ValueError):
|
|
|
|
|
fix_qrz._lookup_qrz_callsign("", fix_qrz._apikey)
|
2016-01-11 23:38:28 +01:00
|
|
|
|
2015-04-06 20:00:30 +02:00
|
|
|
def test_lookup_callsign_with_invalid_input(self, fix_qrz):
|
|
|
|
|
with pytest.raises(AttributeError):
|
|
|
|
|
fix_qrz._lookup_qrz_callsign(3, fix_qrz._apikey)
|
|
|
|
|
|
|
|
|
|
|
2016-01-11 23:38:28 +01:00
|
|
|
class TestQrz_DXCC_Lookup:
|
|
|
|
|
|
2015-04-06 20:00:30 +02:00
|
|
|
def test_lookup_dxcc_with_int(self, fix_qrz):
|
|
|
|
|
data = fix_qrz._lookup_qrz_dxcc(333, fix_qrz._apikey)
|
2016-01-11 23:38:28 +01:00
|
|
|
assert data == response_333 #check content
|
2015-04-06 20:00:30 +02:00
|
|
|
assert len(data) == len(response_333) #ensure all fields are included
|
2016-01-11 23:38:28 +01:00
|
|
|
|
2015-04-06 20:00:30 +02:00
|
|
|
def test_lookup_dxcc_with_string(self, fix_qrz):
|
|
|
|
|
data = fix_qrz._lookup_qrz_dxcc("333", fix_qrz._apikey)
|
2016-01-11 23:38:28 +01:00
|
|
|
assert data == response_333 #check content
|
2015-04-06 20:00:30 +02:00
|
|
|
assert len(data) == len(response_333) #ensure all fields are included
|
|
|
|
|
|
|
|
|
|
def test_lookup_dxcc_does_not_exist(self, fix_qrz):
|
|
|
|
|
with pytest.raises(KeyError):
|
|
|
|
|
fix_qrz._lookup_qrz_dxcc('854', fix_qrz._apikey)
|
2016-01-11 23:38:28 +01:00
|
|
|
|
2015-04-06 20:00:30 +02:00
|
|
|
def test_lookup_dxcc_wrong_input(self, fix_qrz):
|
|
|
|
|
with pytest.raises(ValueError):
|
|
|
|
|
fix_qrz._lookup_qrz_dxcc('', fix_qrz._apikey)
|
|
|
|
|
|
|
|
|
|
def test_lookup_dxcc(self, fix_qrz):
|
|
|
|
|
data = fix_qrz.lookup_entity(333)
|
2016-01-11 23:38:28 +01:00
|
|
|
assert data == response_333 #check content
|