Skip to content

Commit

Permalink
PB-227: Add additional tests
Browse files Browse the repository at this point in the history
  • Loading branch information
LukasJoss committed Aug 28, 2024
1 parent 3e5073c commit 363ded7
Show file tree
Hide file tree
Showing 14 changed files with 195 additions and 393 deletions.
1 change: 0 additions & 1 deletion Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ nose2 = "*"
pylint = "*"
pylint-flask = "*"
cairosvg = "*"
pandas3 = "*"

[requires]
python_version = "3.9"
421 changes: 69 additions & 352 deletions Pipfile.lock

Large diffs are not rendered by default.

18 changes: 18 additions & 0 deletions app/helpers/check_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

from flask import abort

from app.helpers.description import find_descripton_file
from app.icon_set import get_icon_set

logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -88,3 +89,20 @@ def get_and_check_icon(icon_set, icon_name):
logger.error("The icon doesn't exist: %s", path)
abort(404, "Icon not found in icon set")
return icon


def check_if_descripton_file_exists(icon_set):
"""
Checks that the icon set has a corresponding dictionary containing description for all available
languages.
if not raises a flask error and abort the current request.
Args:
icon_set: (IconSet) the icon set in which belongs the icon we want
icon_name: (str) the name of the icon
"""
# checking that the icon exists in the icon set's folder
path = find_descripton_file(icon_set)
if not os.path.isfile(path):
logger.error("The description dictionary doesn't exist: %s", path)
abort(404, "Description dictionary not found")
24 changes: 21 additions & 3 deletions app/helpers/description.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@


def get_icon_set_description(icon_set=''):
path = os.path.abspath(os.path.join(DESCRIPTION_FOLDER, icon_set + '-dictionary.json'))
'''
Return json containing the description in all available languages for all icons of the
provided icon set
'''
path = find_descripton_file(icon_set)
if not os.path.isfile(path):
return None

Expand All @@ -16,12 +20,26 @@ def get_icon_set_description(icon_set=''):


def get_icon_description(icon_name='', icon_set=''):
path = os.path.abspath(os.path.join(DESCRIPTION_FOLDER, icon_set + '-dictionary.json'))
'''
Return json containing the description in all available languages for the specified icon of the
provided icon set
'''
path = find_descripton_file(icon_set)
if not os.path.isfile(path):
return False
return None

with open(path, encoding='utf-8') as f:
df = json.load(f)

icon_description = df[icon_name]
return icon_description


def find_descripton_file(icon_set):
'''
Return file path of description file if it exists
'''
path = os.path.abspath(os.path.join(DESCRIPTION_FOLDER, icon_set)) + '-dictionary.json'
if os.path.isfile(path):
return path
return False
12 changes: 8 additions & 4 deletions app/icon_set.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from flask import url_for

from app.helpers.description import find_descripton_file
from app.helpers.description import get_icon_set_description
from app.helpers.icons import get_icon_set_template_url
from app.helpers.url import get_base_url
Expand Down Expand Up @@ -83,9 +84,12 @@ def get_icon_set_description_url(self):
of all available icons of this icon set.
Returns:
the URL to the description in all available languages of all icons in this icon set
the URL to the description in all available languages of all icons in this icon set if
it exists, otherwise return None
"""
return url_for('description_from_icon_set', icon_set_name=self.name, _external=True)
if find_descripton_file(self.name):
return url_for('description_from_icon_set', icon_set_name=self.name, _external=True)
return None

def get_icon(self, icon_name):
"""
Expand Down Expand Up @@ -129,8 +133,8 @@ def get_description(self):
belonging to this icon set.
Returns:
A dictionary of all icon description in all available languages from this icon set, or
None if no json is found in the folder "json/"
A dictionary of all icon description in all available languages from this icon set it
it exists, otherwise return None
"""
if not self.is_valid():
return None
Expand Down
2 changes: 2 additions & 0 deletions app/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

from app import app
from app.helpers.check_functions import check_color_channels
from app.helpers.check_functions import check_if_descripton_file_exists
from app.helpers.check_functions import check_scale
from app.helpers.check_functions import get_and_check_icon
from app.helpers.check_functions import get_and_check_icon_set
Expand Down Expand Up @@ -61,6 +62,7 @@ def icons_from_icon_set(icon_set_name):
@app.route('/sets/<string:icon_set_name>/description', methods=['GET'])
def description_from_icon_set(icon_set_name):
icon_set = get_and_check_icon_set(icon_set_name)
check_if_descripton_file_exists(icon_set_name)
return make_api_compliant_response(icon_set.get_description())


Expand Down
2 changes: 1 addition & 1 deletion metadata/description/babs-D-dictionary.json
Original file line number Diff line number Diff line change
Expand Up @@ -579,4 +579,4 @@
"fr": "Point de rassemblement d'urgence",
"it": "Punto di raccolta d'urgenza"
}
}
}
4 changes: 2 additions & 2 deletions metadata/description/babs-F-dictionary.json
Original file line number Diff line number Diff line change
Expand Up @@ -574,9 +574,9 @@
"fr": "Interdiction de survoler",
"it": "Divieto di sorvolo"
},
"116-F-Point-de-rassemblement-durgence": {
"116-F-Point-de-rassemblement-d-urgence": {
"de": "Notfalltreffpunkt",
"fr": "Point de rassemblement d'urgence",
"it": "Punto di raccolta d'urgenza"
}
}
}
4 changes: 2 additions & 2 deletions metadata/description/babs-I-dictionary.json
Original file line number Diff line number Diff line change
Expand Up @@ -574,9 +574,9 @@
"fr": "Interdiction de survoler",
"it": "Divieto di sorvolo"
},
"116-I-Punto-di-raccolta-durgenza": {
"116-I-Punto-di-raccolta-d-urgenza": {
"de": "Notfalltreffpunkt",
"fr": "Point de rassemblement d'urgence",
"it": "Punto di raccolta d'urgenza"
}
}
}
1 change: 1 addition & 0 deletions scripts/generate_babs_dic.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import argparse
import json

# pylint: disable=import-error
import pandas as pd


Expand Down
7 changes: 6 additions & 1 deletion tests/unit_tests/test_all_icons.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,12 @@ def test_all_icon_sets_metadata_endpoint(self):
self.assertIn('name', icon_set_metadata)
self.assertEqual(icon_set_name, icon_set_metadata['name'])
self.assertIn('colorable', icon_set_metadata)
self.assertIn('description', icon_set_metadata)
self.assertIn('description_url', icon_set_metadata)
if icon_set_metadata['description_url']:
r = self.app.get(
icon_set_metadata['description_url'], headers=self.default_header
)
self.assertEqual(r.status_code, 200)
self.assertIn('icons_url', icon_set_metadata)
self.assertIsNotNone(icon_set_metadata['icons_url'])
self.assertEqual(
Expand Down
57 changes: 57 additions & 0 deletions tests/unit_tests/test_description.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import json
import os

from flask import url_for

from app.settings import DESCRIPTION_FOLDER
from tests.unit_tests.base_test import ServiceIconsUnitTests


def validate_json(json_file):
try:
json.loads(json_file)
except ValueError as err:
return False
return True


class IconsTests(ServiceIconsUnitTests):

def test_validate_json_description_files(self):
files = list(os.listdir(DESCRIPTION_FOLDER))
for file in files:
for root, dirs, files in os.walk(os.path.join(DESCRIPTION_FOLDER)):
for name in files:
p = os.path.join(root, name)
with open(p, encoding='utf-8') as f:
json_file = f.read()
self.assertTrue(
validate_json(json_file), "validation failed of json file: " + file
)

def test_get_icon_set_description_valid(self):
response = self.app.get(
url_for(
'description_from_icon_set',
icon_set_name='babs-I',
),
headers={"Origin": 'www.example.com'}
)
self.assertEqual(response.status_code, 200)

def test_get_icon_set_description_invalid(self):
response = self.app.get(
url_for(
'description_from_icon_set',
icon_set_name='default',
),
headers={"Origin": 'www.example.com'}
)
self.assertEqual(
response.json, {
"error": {
"code": 404, "message": "Description dictionary not found"
},
"success": False
}
)
8 changes: 8 additions & 0 deletions tests/unit_tests/test_endpoints_compliance.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,14 @@ def test_icon_set_metadata(self):
)
)

def test_icon_set_description(self):
self.check_response_compliance(
self.app.get(
url_for('description_from_icon_set', icon_set_name='babs-I'),
headers=self.default_header
)
)

def test_icons_list(self):
self.check_response_compliance(
self.app.get(
Expand Down
27 changes: 0 additions & 27 deletions tests/unit_tests/test_json.py

This file was deleted.

0 comments on commit 363ded7

Please sign in to comment.