Skip to content

Commit

Permalink
testing testing
Browse files Browse the repository at this point in the history
  • Loading branch information
Guts committed Mar 28, 2018
1 parent e8037e6 commit 2b84516
Show file tree
Hide file tree
Showing 3 changed files with 118 additions and 55 deletions.
62 changes: 28 additions & 34 deletions isogeo_pysdk/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ class IsogeoUtils(object):
"""
API_URLS = {"prod": "api",
"qa": "api.qa",
# "int": "api.int.hq.isogeo.fr"
}

WEBAPPS = {"oc": {"args": ("md_id", "share_id", "oc_token"),
Expand Down Expand Up @@ -76,7 +75,7 @@ def convert_uuid(self, in_uuid=str, mode=0):
* 2 to URN (Isogeo specific style)
"""
# quick parameters check
# parameters check
if not isinstance(in_uuid, string_types):
raise TypeError("'in_uuid' expected a str value.")
else:
Expand All @@ -85,8 +84,8 @@ def convert_uuid(self, in_uuid=str, mode=0):
raise ValueError("{} is not a correct UUID".format(in_uuid))
else:
pass
if not isinstance(mode, int) or mode not in (0, 1, 2):
raise TypeError("'mode' expected an integer value: 0 or 1")
if not isinstance(mode, int):
raise TypeError("'mode' expects an integer value")
else:
pass
# handle Isogeo specific UUID in XML exports
Expand All @@ -104,7 +103,7 @@ def convert_uuid(self, in_uuid=str, mode=0):
urn = uuid.UUID(in_uuid).urn
return "urn:isogeo:metadata:uuid:{}".format(urn.split(":")[2])
else:
pass
raise ValueError("'mode' must be one of: 0 | 1 | 2")

def get_isogeo_version(self, component="api", prot="https"):
"""Get Isogeo components versions.
Expand All @@ -131,9 +130,6 @@ def get_isogeo_version(self, component="api", prot="https"):
version_url = "https://app.isogeo.com/about"
elif component == "app" and self.platform == "qa":
version_url = "https://qa-isogeo-app.azurewebsites.net/about"
elif component == "app" and self.platform == "int":
version_url = "{}://v1.api.int.hq.isogeo.fr/about"\
.format(prot)
else:
raise ValueError("Component value is one theses values:"
"api [default], db, app.")
Expand Down Expand Up @@ -194,8 +190,8 @@ def get_edit_url(self, md_id=str, md_type=str, owner_id=str, tab="identification
raise ValueError("One of md_id or owner_id is not a correct UUID.")
else:
pass
if not checker.check_edit_tab(tab, md_type=md_type):
raise ValueError("Bad tab name")
if checker.check_edit_tab(tab, md_type=md_type):
pass
# construct URL
return "https://app.isogeo.com/" \
"groups/{}" \
Expand All @@ -219,9 +215,7 @@ def get_view_url(self, webapp="oc", **kwargs):

return url.format(**kwargs)
else:
webapp_args = self.WEBAPPS.get(webapp).get("args")
raise TypeError("'{}' webapp expects {} argument(s):"
" {}"
raise TypeError("'{}' webapp expects {} argument(s): {}."
" Args passed: {}"
.format(webapp,
len(webapp_args),
Expand Down Expand Up @@ -254,27 +248,27 @@ def register_webapp(self, webapp_name, webapp_args, webapp_url):
"url": webapp_url}

# -- API AUTH ------------------------------------------------------------
def credentials_loader(self, f_json, f_ini, e_vars):
"""Loads API credentials from a file or environment variables.
:param str f_json: path to the credentials JSON file:
:param str f_ini: path to the credentials INI file
:param dict e_vars: dict of environment variables names
"""
if f_ini:
with open('client_secrets.json', "r") as j:
api = json.loads(j.read()).get("installed")
return api
elif f_json:
with open('client_secrets.json', "r") as j:
api = json.loads(j.read()).get("installed")
return api
elif e_vars:
with open('client_secrets.json', "r") as j:
api = json.loads(j.read()).get("installed")
return api
else:
raise ValueError()
# def credentials_loader(self, f_json, f_ini, e_vars):
# """Loads API credentials from a file or environment variables.

# :param str f_json: path to the credentials JSON file:
# :param str f_ini: path to the credentials INI file
# :param dict e_vars: dict of environment variables names
# """
# if f_ini:
# with open('client_secrets.json', "r") as j:
# api = json.loads(j.read()).get("installed")
# return api
# elif f_json:
# with open('client_secrets.json', "r") as j:
# api = json.loads(j.read()).get("installed")
# return api
# elif e_vars:
# with open('client_secrets.json', "r") as j:
# api = json.loads(j.read()).get("installed")
# return api
# else:
# raise ValueError()


# ##############################################################################
Expand Down
27 changes: 27 additions & 0 deletions test_coverage.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#################################################################
#
# Script to package and upload Isogeo Python package.
#
#################################################################

"-- STEP -- Creating virtualenv"
py -3 -m venv env3_tests
./env3_tests/Scripts/activate

"-- STEP -- Install and display dependencies within the virtualenv"
python -m pip install -U pip
pip install --upgrade setuptools wheel
pip install --upgrade -r .\tests\requirements_test.txt

"-- STEP -- Run unit tests"
python -m unittest discover

"-- STEP -- Run coverage"
coverage run -m unittest discover -s tests/

"-- STEP -- Build and open coverage report"
coverage html
Invoke-Item htmlcov/index.html

"-- STEP -- Exit virtualenv"
deactivate
84 changes: 63 additions & 21 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,22 +53,43 @@ def tearDown(self):

# - Isogeo components versions -----------------------------------------
def test_get_isogeo_version_api(self):
""""""
version_api = self.utils.get_isogeo_version(component="api")
version_api_naive = self.utils.get_isogeo_version()
self.assertIsInstance(version_api, str)
self.assertIsInstance(version_api_naive, str)
self.assertEqual(version_api, version_api_naive)
"""Check API version"""
# prod
version_api_prod = self.utils.get_isogeo_version(component="api")
version_api_naive_prod = self.utils.get_isogeo_version()
# qa
platform, base_url = self.utils.set_base_url(platform="qa")
version_api_qa = self.utils.get_isogeo_version(component="api")
version_api_naive_qa = self.utils.get_isogeo_version()
# check
self.assertIsInstance(version_api_prod, str)
self.assertIsInstance(version_api_naive_prod, str)
self.assertIsInstance(version_api_qa, str)
self.assertIsInstance(version_api_naive_qa, str)
self.assertEqual(version_api_prod, version_api_naive_prod)
self.assertEqual(version_api_qa, version_api_naive_prod)

def test_get_isogeo_version_app(self):
""""""
version_app = self.utils.get_isogeo_version(component="app")
self.assertIsInstance(version_app, str)
"""Check APP version"""
# prod
version_app_prod = self.utils.get_isogeo_version(component="app")
# qa
platform, base_url = self.utils.set_base_url(platform="qa")
version_app_qa = self.utils.get_isogeo_version(component="app")
# check
self.assertIsInstance(version_app_prod, str)
self.assertIsInstance(version_app_qa, str)

def test_get_isogeo_version_db(self):
"""Check res"""
version_db = self.utils.get_isogeo_version(component="db")
self.assertIsInstance(version_db, str)
"""Check DB version"""
# prod
version_db_prod = self.utils.get_isogeo_version(component="db")
# qa
platform, base_url = self.utils.set_base_url(platform="qa")
version_db_qa = self.utils.get_isogeo_version(component="db")
# check
self.assertIsInstance(version_db_prod, str)
self.assertIsInstance(version_db_qa, str)

def test_get_isogeo_version_bad_parameter(self):
"""Raise error if component parameter is bad."""
Expand All @@ -77,10 +98,19 @@ def test_get_isogeo_version_bad_parameter(self):

# -- Base URLs -----------------------------------------------------------
def test_set_base_url(self):
""""""
"""Set base URLs"""
# by default platform = prod
platform, base_url = self.utils.set_base_url()
self.assertIsInstance(platform, str)
self.assertIsInstance(base_url, str)
self.assertEqual(platform, "prod")
self.assertEqual(base_url, self.utils.API_URLS.get("prod"))
# qa
platform, base_url = self.utils.set_base_url(platform="qa")
self.assertIsInstance(platform, str)
self.assertIsInstance(base_url, str)
self.assertEqual(platform, "qa")
self.assertEqual(base_url, self.utils.API_URLS.get("qa"))

def test_set_base_url_bad_parameter(self):
"""Raise error if platform parameter is bad."""
Expand Down Expand Up @@ -130,6 +160,11 @@ def test_get_edit_url_bad_tab(self):
md_type="raster-dataset",
owner_id="32f7e95ec4e94ca3bc1afda960003882",
tab="attributes")
with self.assertRaises(ValueError):
self.utils.get_edit_url(md_id="0269803d50c446b09f5060ef7fe3e22b",
md_type="raster-dataset",
owner_id="32f7e95ec4e94ca3bc1afda960003882",
tab="what_a_tab_name")

# -- URLs Builders - view on web app -------------------------------------
def test_get_view_url_ok(self):
Expand All @@ -152,6 +187,10 @@ def test_get_view_url_bad(self):
self.utils.get_view_url(md_id="0269803d50c446b09f5060ef7fe3e22b",
webapp="my_imaginary_webapp",
portal_url="demo.isogeo.net")
with self.assertRaises(TypeError):
self.utils.get_view_url(md_id="0269803d50c446b09f5060ef7fe3e22b",
webapp="pixup_portal",
my_nice_arg="a_nice_arg")

def test_register_webapp_custom_ok(self):
"""Test register a custom webapp and use it ot build view url."""
Expand Down Expand Up @@ -248,16 +287,19 @@ def test_urnIsogeo_to_urnIsogeo(self):
self.assertEqual(uuid_out, self.uuid_urnIsogeo)
self.assertIn("isogeo:metadata", uuid_out)

# UUID converter
def test_uuid_converter_bad_parameter(self):
"""Raise error if one parameter is bad."""
# UUID converter - bad boys
def test_uuid_converter_bad_value(self):
"""Raise error if one parameter value is bad."""
with self.assertRaises(ValueError):
self.utils.convert_uuid(in_uuid="oh_my_bad_i_m_not_a_correct_uuid")
with self.assertRaises(ValueError):
self.utils.convert_uuid(in_uuid=self.uuid_urnIsogeo,
mode=4)

def test_uuid_converter_bad_type(self):
"""Raise error if one parameter type is bad."""
with self.assertRaises(TypeError):
self.utils.convert_uuid(in_uuid=2)
self.utils.convert_uuid(in_uuid="0269803d50c446b09f5060ef7fe3e22b",
with self.assertRaises(TypeError):
self.utils.convert_uuid(in_uuid=self.uuid_urnIsogeo,
mode="ups_not_an_int")
self.utils.convert_uuid(in_uuid="0269803d50c446b09f5060ef7fe3e22b",
mode=3)
self.utils.convert_uuid(in_uuid="0269803d50c446b09f5060ef7fe3e22b",
mode=True)

0 comments on commit 2b84516

Please sign in to comment.