2018-01-27 23:36:49 +01:00
|
|
|
import os
|
|
|
|
|
|
2015-04-06 20:00:30 +02:00
|
|
|
import pytest
|
|
|
|
|
from datetime import datetime
|
|
|
|
|
|
|
|
|
|
from pyhamtools.lookuplib import LookupLib
|
|
|
|
|
from pyhamtools.exceptions import APIKeyMissingError
|
|
|
|
|
from pyhamtools.consts import LookupConventions as const
|
|
|
|
|
|
|
|
|
|
import pytz
|
|
|
|
|
UTC = pytz.UTC
|
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
response_XX2XX = {
|
2016-01-11 23:38:28 +01:00
|
|
|
u'adif': 446,
|
2018-01-27 15:11:46 +01:00
|
|
|
u'bio': u'349',
|
|
|
|
|
u'biodate': datetime(2017, 9, 5, 22, 28, 42, tzinfo=UTC),
|
2017-08-20 16:26:37 +02:00
|
|
|
u'born': 1932,
|
2016-01-11 23:38:28 +01:00
|
|
|
u'callsign': u'XX2XX',
|
2017-08-20 16:26:37 +02:00
|
|
|
u'ccode': 1230,
|
2016-01-11 23:38:28 +01:00
|
|
|
u'codes': u'ZZ',
|
2017-08-20 16:26:37 +02:00
|
|
|
u'country': u'Temotu',
|
|
|
|
|
u'cqz': 4,
|
|
|
|
|
u'email': 'dummy2@qrz.com',
|
|
|
|
|
u'eqsl': False,
|
|
|
|
|
u'fname': u'Gooberd',
|
2018-01-27 15:11:46 +01:00
|
|
|
u'geoloc': u'grid',
|
2017-08-20 16:26:37 +02:00
|
|
|
u'image': u'https://s3.amazonaws.com/files.qrz.com/x/xx2xx/oval_bumper_sticker4.png',
|
2016-01-11 23:38:28 +01:00
|
|
|
u'imageinfo': u'285:500:44218',
|
2017-08-20 16:26:37 +02:00
|
|
|
u'iota': u'NA-022',
|
|
|
|
|
u'ituz': 5,
|
2016-01-11 23:38:28 +01:00
|
|
|
u'land': u'Morocco',
|
2018-01-27 15:11:46 +01:00
|
|
|
u'latitude': 52.1875,
|
2017-08-20 16:26:37 +02:00
|
|
|
u'license_class': u'A',
|
|
|
|
|
u'locator': u'JO02be',
|
2018-01-27 15:11:46 +01:00
|
|
|
u'longitude': 0.125,
|
2017-08-20 16:26:37 +02:00
|
|
|
u'lotw': False,
|
|
|
|
|
u'moddate': datetime(2017, 6, 16, 19, 22, 21, tzinfo=UTC),
|
2016-01-11 23:38:28 +01:00
|
|
|
u'mqsl': False,
|
2017-08-20 16:26:37 +02:00
|
|
|
u'name': u'Blufferd',
|
2016-01-11 23:38:28 +01:00
|
|
|
u'qslmgr': u'nobody here. gone fishing, permanently',
|
2017-08-20 16:26:37 +02:00
|
|
|
u'state': u'mt',
|
2018-05-20 17:24:51 +02:00
|
|
|
u'user': u'XX1XX',
|
2016-01-11 23:38:28 +01:00
|
|
|
u'zipcode': u'112233'
|
|
|
|
|
}
|
2015-04-06 20:00:30 +02:00
|
|
|
|
|
|
|
|
response_XX3XX = {
|
2016-01-11 23:38:28 +01:00
|
|
|
u'addr1': u'1234 Main St.3',
|
|
|
|
|
u'addr2': u'Shady Circle Roads',
|
|
|
|
|
u'adif': 79,
|
|
|
|
|
u'aliases': [u'XX3XX/W7'],
|
2018-01-27 15:11:46 +01:00
|
|
|
u'bio': u'16',
|
|
|
|
|
u'biodate': datetime(2018, 1, 24, 20, 55, 27, tzinfo=UTC),
|
2016-01-11 23:38:28 +01:00
|
|
|
u'born': 2010,
|
|
|
|
|
u'callsign': u'XX3XX',
|
|
|
|
|
u'ccode': 130,
|
|
|
|
|
u'country': u'Jamaica',
|
|
|
|
|
u'email': u'fred@qrz.com',
|
|
|
|
|
u'eqsl': False,
|
|
|
|
|
u'fname': u'TEST CALLSIGN',
|
|
|
|
|
u'geoloc': u'user',
|
2017-08-20 16:26:37 +02:00
|
|
|
u'image': u'https://s3.amazonaws.com/files.qrz.com/x/xx3xx/oval_bumper_sticker_600.png',
|
2016-01-11 23:38:28 +01:00
|
|
|
u'imageinfo': u'600:600:87971',
|
|
|
|
|
u'land': u'Guadeloupe',
|
|
|
|
|
u'latitude': 51.396953,
|
|
|
|
|
u'license_class': u'3',
|
|
|
|
|
u'locator': u'FO51sj',
|
|
|
|
|
u'longitude': -68.41959,
|
|
|
|
|
u'lotw': False,
|
2017-08-20 16:26:37 +02:00
|
|
|
u'moddate': datetime(2016, 4, 21, 19, 19, 6, tzinfo=UTC),
|
2016-01-11 23:38:28 +01:00
|
|
|
u'mqsl': False,
|
|
|
|
|
u'name': u'DO NOT QSL',
|
|
|
|
|
u'qslmgr': u'Via BURO or AA7BQ',
|
|
|
|
|
u'state': u'JJ',
|
2017-08-20 16:26:37 +02:00
|
|
|
# u'user': u'KF7WIS',
|
2016-01-11 23:38:28 +01:00
|
|
|
u'zipcode': u'00033'
|
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
|
|
|
|
2015-04-06 20:00:30 +02:00
|
|
|
data = fix_qrz._lookup_qrz_callsign("XX2XX", fix_qrz._apikey)
|
|
|
|
|
data.pop('u_views', None)
|
|
|
|
|
assert data == response_XX2XX
|
|
|
|
|
assert len(data) == len(response_XX2XX)
|
2016-01-11 23:38:28 +01:00
|
|
|
|
2015-04-06 20:00:30 +02:00
|
|
|
data = fix_qrz._lookup_qrz_callsign("XX3XX", fix_qrz._apikey)
|
|
|
|
|
data.pop('u_views', None)
|
|
|
|
|
assert data == response_XX3XX
|
|
|
|
|
assert len(data) == len(response_XX3XX)
|
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):
|
|
|
|
|
data = fix_qrz._lookup_qrz_callsign("XX2XX", fix_qrz._apikey)
|
|
|
|
|
data.pop('u_views', None)
|
|
|
|
|
assert data == response_XX2XX
|
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
|