Skip to content

Commit fa6ac90

Browse files
authored
Merge pull request #112 from mapillary/Rubix982/Hotfix-For-Tests
[Testing] Hotfix For Tests
2 parents a4c27af + 5cf378d commit fa6ac90

File tree

9 files changed

+183
-102
lines changed

9 files changed

+183
-102
lines changed

Makefile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,6 @@
22

33
style:
44
black src/mapillary && flake8 src/mapillary
5+
6+
test:
7+
@ pytest --log-cli-level=20

Pipfile.lock

Lines changed: 0 additions & 15 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pytest.ini

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
[pytest]
2+
log_format = %(asctime)s %(levelname)s %(message)s
3+
log_date_format = %Y-%m-%d %H:%M:%S
4+
filterwarnings = default
5+
ignore:.*is deprecated.*:Warning
6+
error::DeprecationWarning:importlib.*
7+
log_file = ./tests/log/tests.log
8+
markers =
9+
smoke: for smoke testing

tests/__init__.py

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,20 @@
1-
# Copyright (c) Facebook, Inc. and its affiliates. (http://www.facebook.com)
1+
# Copyright (c) Facebook, Inc. and its affiliates. (http://www.facebook.com)
2+
# -*- coding: utf-8 -*-
3+
4+
"""
5+
tests.__init__
6+
7+
This module loads the test modules under tests/
8+
9+
:copyright: (c) 2021 Facebook
10+
:license: MIT LICENSE
11+
"""
12+
13+
# Configuration
14+
from . import conftest # noqa: F401
15+
16+
# Utils testing
17+
from . import utils as tests_utils # noqa: F401
18+
19+
# Helper testing
20+
from . import helper as tests_helper # noqa: F401

tests/conftest.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# Copyright (c) Facebook, Inc. and its affiliates. (http://www.facebook.com)
2+
# -*- coding: utf-8 -*-
3+
4+
"""
5+
tests.conftest.py
6+
7+
This module loads variables needed to run the test
8+
9+
:copyright: (c) 2021 Facebook
10+
:license: MIT LICENSE
11+
"""
12+
13+
# Package imports
14+
import pytest
15+
import requests
16+
import mercantile
17+
from vt2geojson.tools import vt_bytes_to_geojson
18+
19+
@pytest.fixture(autouse=True)
20+
def mly_access_token():
21+
"""Specify the access token here"""
22+
23+
token = "MLY|YYY"
24+
25+
if "MLY|YYY" in token or "MLY|XXX" in token:
26+
raise ValueError("[tests.conftest]: MLY Access Token is not specified")
27+
28+
return token
29+
30+
31+
@pytest.fixture(autouse=True)
32+
def data(mly_access_token):
33+
34+
zoom, longitude, latitude = 14, 31, 30
35+
36+
tile = mercantile.tile(lng=longitude, lat=latitude, zoom=zoom)
37+
38+
data = vt_bytes_to_geojson(
39+
b_content=requests.get(
40+
f"https://tiles.mapillary.com/maps/vtp/mly1_public/2/14/{tile[0]}/{tile[1]}/"
41+
f"?access_token={mly_access_token}"
42+
).content,
43+
x=tile.x,
44+
y=tile.y,
45+
z=tile.z,
46+
layer="image",
47+
)
48+
49+
return {"data": data, "tile": tile, "longitude": longitude, "latitude": latitude}

tests/helper/client.py

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,10 @@
3838
"UNDERLINE": "\033[4m",
3939
}
4040

41+
4142
def list_fetched_functions(fetched_functions: list) -> None:
4243
"""Simply lists out the functions described in fetch.py along with their DocString
43-
44+
4445
:param fetched_functions: List of function names along with their references in a tuple set
4546
:type fetched_functions: list
4647
@@ -51,16 +52,15 @@ def list_fetched_functions(fetched_functions: list) -> None:
5152
# Through each tuple in the format (name, reference)
5253
for index, function in enumerate(fetched_functions):
5354

54-
# Print out ...
55+
# Print out ...
5556
print(
56-
5757
# ... an index for the function, then the name of the function in cyan color ...
5858
f"{index + 1}. {colors['OKCYAN']}{function[0]}{colors['ENDC']} "
59-
6059
# ... then print the docstring as well
6160
f"- {function[1].__doc__}"
6261
)
6362

63+
6464
def save_as_geojson(data: dict) -> None:
6565
"""Saves the resulting geojson dictionary in a file tagged .geojson
6666
@@ -74,11 +74,14 @@ def save_as_geojson(data: dict) -> None:
7474
# TODO: Test if this works perfectly fine in Windows, since it uses the `os` package
7575

7676
# Save the file at the given path, create it if it does not exist
77-
with open(f'{os.path.abspath(".")}/data/{sys.argv[2]}.geojson', mode='w') as geojson_file:
78-
77+
with open(
78+
f'{os.path.abspath(".")}/data/{sys.argv[2]}.geojson', mode="w"
79+
) as geojson_file:
80+
7981
# Save the dictionary as a json in a clean formatted way
8082
json.dump(data, geojson_file, sort_keys=True, indent=4)
8183

84+
8285
def main():
8386
"""Main logic for the CLI"""
8487

@@ -88,18 +91,14 @@ def main():
8891
# If there are insufficient arguments
8992
if len(sys.argv) < 3:
9093

91-
# Log an error ...
94+
# Log an error ...
9295
logging.error(
93-
9496
# ... indicating the source of the error ...
9597
" - client.py\n"
96-
9798
# ... the reason for the error ...
9899
f"{colors['FAIL']}Invalid format!{colors['ENDC']}\n"
99-
100100
# ... giving the user a format for the colors ...
101101
f"Try: python client.py {colors['BOLD']}'MLY|XXX'{colors['ENDC']} "
102-
103102
# ... format of the function called
104103
f"{colors['OKCYAN']}fetch_function_here{colors['ENDC']}\n"
105104
)
@@ -127,21 +126,18 @@ def main():
127126
# If the results ends up empty
128127
if result == {}:
129128

130-
# Log an error ...
129+
# Log an error ...
131130
logging.error(
132-
133-
# ... indicating the source of the error ...
131+
# ... indicating the source of the error ...
134132
" - client.py\n"
135-
136-
# ... the reason for the error
133+
# ... the reason for the error
137134
f"{colors['FAIL']}Unrecognized function call!{colors['ENDC']}\n"
138135
)
139136

140137
# Finally, list the functions for the user
141138
list_fetched_functions(fetched_functions=fetched_functions)
142139

143140
# Saving the data
144-
# TODO: Assumes user is working in Linux!
145141
save_as_geojson(result)
146142

147143

tests/utils/__init__.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Copyright (c) Facebook, Inc. and its affiliates. (http://www.facebook.com)
2+
# -*- coding: utf-8 -*-
3+
4+
"""
5+
tests.utils.__init__
6+
7+
This module loads the modules under src/mapillary/utils for tests
8+
9+
:copyright: (c) 2021 Facebook
10+
:license: MIT LICENSE
11+
"""
12+
13+
# Exraction testing
14+
from . import test_extract # noqa: F401
15+
16+
# Filter testing
17+
from . import test_filter # noqa: F401

tests/utils/test_extract.py

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -11,32 +11,30 @@
1111
:license: MIT LICENSE
1212
"""
1313

14-
import requests
15-
import mercantile
16-
from vt2geojson.tools import vt_bytes_to_geojson
17-
from mapillary.utils.extract import extract_properties
14+
# Package imports
15+
import pytest
16+
import logging # Logger
1817

19-
def test_extract_properties(properties: list) -> dict:
18+
# Local imports
19+
from mapillary.utils.extract import extract_properties # Function to test
20+
from tests.conftest import data # Data as fixture
2021

21-
tile = mercantile.tile(lng=31, lat=30, zoom=14)
22+
logger = logging.getLogger(__name__)
2223

23-
data = vt_bytes_to_geojson(
24-
b_content=requests.get(
25-
f"https://tiles.mapillary.com/maps/vtp/mly1_public/2/14/{tile[0]}/{tile[1]}/?access_token=MLY|4352045404840373|b35c62c790e9b52c8476cfd08eb58704"
26-
).content,
27-
x=tile.x,
28-
y=tile.y,
29-
z=tile.z,
30-
layer="image",
31-
)
3224

33-
extracted = extract_properties(data, properties)
25+
@pytest.mark.parametrize(
26+
"operation, expected", [("extract_properties(data['data'], ['id'])", 0)]
27+
)
28+
def test_extract_properties(data: dict, operation, expected):
3429

35-
assert('id' in extracted)
30+
# Operation to test
31+
test_that = f"{operation} != {expected}"
3632

37-
assert(len(extracted['id']) != 0)
33+
# Logging the intended operation to be tested
34+
logger.info(f"\n[test_extract_properties] Test that {test_that}")
3835

39-
return {"Test": "Success"}
36+
# Actual data on left, operation on right
37+
actual = extract_properties(data["data"], ["id"])
4038

41-
if __name__ == '__main__':
42-
test_extract_properties(['id'])
39+
# What was expected left, what was actual on the right
40+
assert len(actual["id"]) != 0, f"{test_that} failed, got {actual}"

0 commit comments

Comments
 (0)