updated tests & docstrings of LookupLib

This commit is contained in:
dh1tw 2014-04-25 23:46:44 +02:00
parent 644306bea0
commit 4e4e94091d
19 changed files with 329 additions and 137 deletions

View file

@ -1,6 +1,9 @@
import pytest
import tempfile
import os
from apikey import APIKEY
#
# @pytest.fixture()
# def cleandir():
@ -44,10 +47,10 @@ def fixNone(request):
API_KEY = ""
@pytest.fixture(scope="session")
def fixApiKey(request):
return(API_KEY)
return(APIKEY)
@pytest.fixture(scope="module", params=["clublogapi", "clublogxml", "countryfile"])
def fixGeneralApi(request, fixApiKey):

163881
test/fixtures/cty.plist vendored Normal file

File diff suppressed because it is too large Load diff

178983
test/fixtures/cty.xml vendored Normal file

File diff suppressed because it is too large Load diff

View file

@ -10,12 +10,8 @@ def fixAnyValue(request):
class TestlookupLib:
def test_construction_without_kwargs(self):
"""Load with non without any args & kwargs"""
with pytest.raises(APIKeyMissingError):
LookupLib()
def test_construction_with_invalid_kwargs(self, fixAnyValue):
"""Load with non without any args & kwargs"""
with pytest.raises(AttributeError):

View file

@ -3,7 +3,7 @@ from datetime import datetime
from pyhamtools.lookuplib import LookupLib
from pyhamtools.exceptions import APIKeyMissingError, LookupError, NoResult
from pyhamtools.exceptions import APIKeyMissingError
#Fixtures
#===========================================================
@ -93,27 +93,27 @@ class TestclublogApi_Getters:
assert fixClublogApi.lookup_callsign("DH1TW/MM") == response_Exception_DH1TW_MM
assert fixClublogApi.lookup_callsign("DH1TW/AM") == response_Exception_DH1TW_AM
with pytest.raises(NoResult):
with pytest.raises(KeyError):
fixClublogApi.lookup_callsign("QRM")
with pytest.raises(NoResult):
with pytest.raises(KeyError):
fixClublogApi.lookup_callsign("")
#lookup_prefix(prefix, [date])
#===============================
def test_lookup_callsign(self, fixClublogApi):
with pytest.raises(NoResult):
with pytest.raises(KeyError):
fixClublogApi.lookup_prefix("DH")
#is_invalid_operation(callsign, [date])
#===============================
def test_is_invalid_operation(self, fixClublogApi):
with pytest.raises(NoResult):
with pytest.raises(KeyError):
fixClublogApi.is_invalid_operation("5W1CFN")
#lookup_zone_exception(callsign, [date])
#====================================
def test_lookup_zone_exception(self, fixClublogApi):
with pytest.raises(NoResult):
with pytest.raises(KeyError):
fixClublogApi.lookup_zone_exception("dp0gvn")

View file

@ -1,9 +1,10 @@
import pytest
from datetime import datetime
import pytz
import os
from pyhamtools.lookuplib import LookupLib
from pyhamtools.exceptions import APIKeyMissingError, LookupError, NoResult
from pyhamtools.exceptions import APIKeyMissingError
UTC = pytz.UTC
@ -117,8 +118,11 @@ response_Prefix_ZD5_1964_to_1971 = {
}
@pytest.fixture(scope="function")
def fixCtyXmlFile(request):
return "/Users/user/projects/pyhamtools/pyhamtools/cty.xml"
def fix_cty_xml_file(request):
dir = os.path.dirname(__file__)
cty_file_rel = "./fixtures/cty.xml"
cty_file_abs = os.path.join(dir, cty_file_rel)
return cty_file_abs
#TESTS
@ -128,16 +132,16 @@ class TestClublogXML_Constructor:
def test_with_invalid_api_key(self):
with pytest.raises(APIKeyMissingError):
lib = LookupLib(apikey="foo")
lib = LookupLib(lookuptype="clublogxml", apikey="foo")
lib.lookup_entity(230)
def test_with_no_api_key(self):
with pytest.raises(APIKeyMissingError):
lib = LookupLib()
lib = LookupLib(lookuptype="clublogxml")
lib.lookup_entity(230)
def test_with_file(self, fixCtyXmlFile):
lib = LookupLib(filename=fixCtyXmlFile)
def test_with_file(self, fix_cty_xml_file):
lib = LookupLib(lookuptype="clublogxml", filename=fix_cty_xml_file)
assert lib.lookup_entity(230) == response_Entity_230
class TestclublogXML_Getters:
@ -149,13 +153,13 @@ class TestclublogXML_Getters:
assert fixClublogXML.lookup_entity(230) == response_Entity_230
assert fixClublogXML.lookup_entity("230") == response_Entity_230
with pytest.raises(NoResult):
with pytest.raises(KeyError):
fixClublogXML.lookup_entity("foo")
with pytest.raises(NoResult):
with pytest.raises(KeyError):
fixClublogXML.lookup_entity(1000)
with pytest.raises(NoResult):
with pytest.raises(KeyError):
fixClublogXML.lookup_entity(999)
@ -177,7 +181,7 @@ class TestclublogXML_Getters:
#timestamp < startdate
timestamp = datetime(year=1962, month=7, day=5, tzinfo=UTC)
with pytest.raises(NoResult):
with pytest.raises(KeyError):
fixClublogXML.lookup_callsign("vk9xo", timestamp)
def test_lookup_callsign_exception_only_with_end_date(self, fixClublogXML):
@ -187,11 +191,11 @@ class TestclublogXML_Getters:
assert fixClublogXML.lookup_callsign("vk9xx", timestamp) == response_Exception_VK9XX_with_end_date
# timestamp > enddate
with pytest.raises(NoResult):
with pytest.raises(KeyError):
fixClublogXML.lookup_callsign("vk9xx")
timestamp = datetime(year=1975, month=9, day=16, tzinfo=UTC)
with pytest.raises(NoResult):
with pytest.raises(KeyError):
fixClublogXML.lookup_callsign("vk9xx", timestamp)
def test_lookup_callsign_exception_no_start_nor_end_date(self, fixClublogXML):
@ -208,10 +212,10 @@ class TestclublogXML_Getters:
def test_lookup_prefix(self, fixClublogXML):
assert fixClublogXML.lookup_prefix("DH") == response_Prefix_DH
with pytest.raises(NoResult):
with pytest.raises(KeyError):
fixClublogXML.lookup_prefix("QRM")
with pytest.raises(NoResult):
with pytest.raises(KeyError):
fixClublogXML.lookup_prefix("")
@ -223,7 +227,7 @@ class TestclublogXML_Getters:
#return empty dict - Prefix was not assigned at that time
timestamp = datetime(year=1975, month=9, day=16).replace(tzinfo=UTC)
with pytest.raises(NoResult):
with pytest.raises(KeyError):
fixClublogXML.lookup_prefix("VK9", timestamp)
#return new entity (Norfolk Island)
@ -233,14 +237,14 @@ class TestclublogXML_Getters:
def test_lookup_prefix_with_entities_having_start_and_stop(self, fixClublogXML):
timestamp_before = datetime(year=1964, month=11, day=1).replace(tzinfo=UTC)
with pytest.raises(NoResult):
with pytest.raises(KeyError):
fixClublogXML.lookup_prefix("ZD5", timestamp_before)
timestamp_valid = datetime(year=1964, month=12, day=2).replace(tzinfo=UTC)
assert fixClublogXML.lookup_prefix("ZD5", timestamp_valid) == response_Prefix_ZD5_1964_to_1971
timestamp_after = datetime(year=1971, month=8, day=1).replace(tzinfo=UTC)
with pytest.raises(NoResult):
with pytest.raises(KeyError):
fixClublogXML.lookup_prefix("ZD5", timestamp_after)
@ -251,30 +255,30 @@ class TestclublogXML_Getters:
def test_is_invalid_operations(self, fixClublogXML):
#No dataset --> default Operation is True
with pytest.raises(NoResult):
with pytest.raises(KeyError):
fixClublogXML.is_invalid_operation("dh1tw")
#Invalid Operation with start and end date
timestamp_before = datetime(year=1993, month=12, day=30).replace(tzinfo=UTC)
timestamp = datetime(year=1994, month=12, day=30).replace(tzinfo=UTC)
with pytest.raises(NoResult):
with pytest.raises(KeyError):
fixClublogXML.is_invalid_operation("vk0mc")
assert fixClublogXML.is_invalid_operation("vk0mc", timestamp)
with pytest.raises(NoResult):
with pytest.raises(KeyError):
fixClublogXML.is_invalid_operation("vk0mc", timestamp_before)
#Invalid Operation with start date
timestamp_before = datetime(year=2012, month=1, day=31).replace(tzinfo=UTC)
assert fixClublogXML.is_invalid_operation("5W1CFN")
with pytest.raises(NoResult):
with pytest.raises(KeyError):
fixClublogXML.is_invalid_operation("5W1CFN", timestamp_before)
#Invalid Operation with end date
timestamp_before = datetime(year=2004, month=04, day=02).replace(tzinfo=UTC)
with pytest.raises(NoResult):
with pytest.raises(KeyError):
fixClublogXML.is_invalid_operation("T33C")
assert fixClublogXML.is_invalid_operation("T33C", timestamp_before)
@ -287,7 +291,7 @@ class TestclublogXML_Getters:
def test_lookup_zone_exception(self, fixClublogXML):
#No dataset --> default answer: None
with pytest.raises(NoResult):
with pytest.raises(KeyError):
fixClublogXML.lookup_zone_exception("dh1tw")
#zone exception with no date
@ -299,15 +303,15 @@ class TestclublogXML_Getters:
timestamp_after = datetime(year=1993, month=03, day=1).replace(tzinfo=UTC)
assert fixClublogXML.lookup_zone_exception("dl1kvc/p", timestamp) == 38
with pytest.raises(NoResult):
with pytest.raises(KeyError):
fixClublogXML.lookup_zone_exception("dl1kvc/p", timestamp_before)
with pytest.raises(NoResult):
with pytest.raises(KeyError):
fixClublogXML.lookup_zone_exception("dl1kvc/p", timestamp_after)
#zone exception with start date
timestamp_before = datetime(year=2013, month=12, day=26).replace(tzinfo=UTC)
assert fixClublogXML.lookup_zone_exception("dh1hb/p")
with pytest.raises(NoResult):
with pytest.raises(KeyError):
fixClublogXML.lookup_zone_exception("dh1hb/p", timestamp_before)

View file

@ -1,8 +1,9 @@
import pytest
import os
from datetime import datetime
from pyhamtools.lookuplib import LookupLib
from pyhamtools.exceptions import APIKeyMissingError, NoResult, LookupError
from pyhamtools.exceptions import APIKeyMissingError
#Fixtures
#===========================================================
@ -29,8 +30,11 @@ response_Exception_3D2RI = {
}
@pytest.fixture(scope="function")
def fixPlistFile(request):
return "/Users/user/projects/pyhamtools/pyhamtools/cty.plist"
def fix_plist_file(request):
dir = os.path.dirname(__file__)
cty_file_rel = "./fixtures/cty.plist"
cty_file_abs = os.path.join(dir, cty_file_rel)
return cty_file_abs
#TESTS
@ -39,25 +43,22 @@ def fixPlistFile(request):
#@pytest.mark.skipif(True, reason="slow test")
class Test_Countryfile_Constructor:
def test_object_construction_with_invalid_files(self):
with pytest.raises(AttributeError):
LookupLib("countryfile", download=False)
with pytest.raises(AttributeError):
LookupLib("countryfile", filename="", download=False)
with pytest.raises(AttributeError):
LookupLib("countryfile", filename="foo bar", download=False)
def test_constructor_with_file_instead_of_downlad(self, fix_plist_file):
lib = LookupLib("countryfile", filename=fix_plist_file)
assert lib.lookup_callsign("3D2RI") == response_Exception_3D2RI
def test_constructor_with_invalid_file(self):
with pytest.raises(IOError):
lib = LookupLib("countryfile", filename="foo bar")
lib.lookup_callsign("GB0BVL")
class Test_countryfile_Getter_Setter:
#lookup_entity(adif)
#===============================
def test_getException(self, fixCountryFile):
with pytest.raises(NoResult):
with pytest.raises(KeyError):
fixCountryFile.lookup_entity(230)
@ -66,10 +67,10 @@ class Test_countryfile_Getter_Setter:
def test_getException(self, fixCountryFile):
assert fixCountryFile.lookup_callsign("3D2RI") == response_Exception_3D2RI
with pytest.raises(NoResult):
with pytest.raises(KeyError):
fixCountryFile.lookup_callsign("QRM")
with pytest.raises(NoResult):
with pytest.raises(KeyError):
fixCountryFile.lookup_callsign("")
@ -78,20 +79,20 @@ class Test_countryfile_Getter_Setter:
def test_lookup_prefix(self, fixCountryFile):
assert fixCountryFile.lookup_prefix("DH") == response_Prefix_DH
with pytest.raises(NoResult):
with pytest.raises(KeyError):
fixCountryFile.lookup_prefix("QRM")
with pytest.raises(NoResult):
with pytest.raises(KeyError):
fixCountryFile.lookup_prefix("")
#is_invalid_operation(callsign, [date])
#===============================
def test_is_invalid_operation(self, fixCountryFile):
with pytest.raises(NoResult):
with pytest.raises(KeyError):
fixCountryFile.is_invalid_operation("5W1CFN")
#lookup_zone_exception(callsign, [date])
#====================================
def test_lookup_zone_exception(self, fixCountryFile):
with pytest.raises(NoResult):
with pytest.raises(KeyError):
fixCountryFile.lookup_zone_exception("dp0gvn")

View file

@ -4,8 +4,6 @@ import os
from datetime import datetime
from pyhamtools.exceptions import NoResult
#Fixtures
#===========================================================
@ -91,7 +89,7 @@ class Test_Getter_Setter_Api_Types_for_all_sources:
assert type(entity[attr] is datetime)
count +=1
assert len(entity) == count
except NoResult:
except KeyError:
pass
def test_lookup_callsign(self, fixGeneralApi, fixExceptions):
@ -130,7 +128,7 @@ class Test_Getter_Setter_Api_Types_for_all_sources:
#all attributes checked?
assert len(ex) == count
except NoResult:
except KeyError:
pass
except AttributeError:
pass
@ -172,7 +170,7 @@ class Test_Getter_Setter_Api_Types_for_all_sources:
#all attributes checked?
assert len(prefix) == count
except NoResult:
except KeyError:
pass
except AttributeError:
pass
@ -182,7 +180,7 @@ class Test_Getter_Setter_Api_Types_for_all_sources:
try:
invOp = fixGeneralApi.is_invalid_operation(fixInvalidOperations)
assert type(invOp) is bool
except NoResult:
except KeyError:
pass
except AttributeError:
pass
@ -191,7 +189,7 @@ class Test_Getter_Setter_Api_Types_for_all_sources:
try:
zEx = fixGeneralApi.lookup_zone_exception(fixZoneExceptions)
assert type(zEx) is int
except NoResult:
except KeyError:
pass
except AttributeError:
pass
@ -201,7 +199,7 @@ class Test_Getter_Setter_Api_Types_for_all_sources:
response = fixGeneralApi.setException(fixSetExceptions)
assert type(response) is bool
assert fixGeneralApi.lookup_callsign(fixSetExceptions.keys()[0]) == fixSetExceptions[fixSetExceptions.keys()[0]]
except NoResult:
except KeyError:
pass
except AttributeError:
pass