Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mock HTTP-Requests and django file storage in tests #17

Merged
merged 1 commit into from
Nov 24, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 38 additions & 26 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ svgwrite = "*" # http://github.com/mozman/svgwrite.git
geopy = "*" # https://github.com/geopy/geopy
lxml = "*"
django-dbbackup = "*" # https://github.com/django-dbbackup/django-dbbackup
requests = "*" # https://github.com/psf/requests

# We can't install v5 until https://github.com/python-poetry/poetry/issues/3320 is fixed!
bx_py_utils = "0.0.4" # https://github.com/boxine/bx_py_utils
Expand Down Expand Up @@ -85,6 +86,7 @@ autopep8 = "*"
pyupgrade = "*"
model_bakery = "*" # https://github.com/model-bakers/model_bakery
requests-mock = "*"
django-override-storage = "*" # https://github.com/danifus/django-override-storage

[tool.poetry.scripts]
manage = "for_runners_project.manage:main"
Expand Down
42 changes: 37 additions & 5 deletions src/for_runners/geo.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,14 @@
import collections
import logging

from django.core.cache import cache
from geopy.geocoders import Nominatim


log = logging.getLogger(__name__)

WGS84_NDIGITS = 5

Address = collections.namedtuple("Address", ("short, full"))


Expand Down Expand Up @@ -46,14 +50,42 @@ def reverse_geo(lat, lon):
full : string
The "full" Address
"""
log.debug(f'reverse_geo lat={lat} lon={lon}')
geolocator = Nominatim(user_agent="django-for-runners")
location = geolocator.reverse(f"{lat}, {lon}")
lat2 = round(lat, ndigits=WGS84_NDIGITS)
if lat2 != lat:
log.debug(f'round lat={lat} -> {lat2}')

lon2 = round(lon, ndigits=WGS84_NDIGITS)
if lon2 != lon:
log.debug(f'round lat={lon} -> {lon2}')

log.debug(f'reverse_geo lat={lat2} lon={lon2}')

cache_key = f'reverse_geo_{lat2}_{lon2}'
address = cache.get(cache_key)
if address:
log.debug('reverse geo from cache')
full_address, raw_address = address
else:
geolocator = Nominatim(user_agent="django-for-runners")

# https://nominatim.org/release-docs/develop/api/Reverse/
location = geolocator.reverse(
query=f"{lat2}, {lon2}",
# TODO: language={user language}
zoom=17 # major and minor streets
)
full_address = location.address
raw_address = location.raw["address"]
address = (full_address, raw_address)
cache.set(
cache_key, address,
timeout=None # cache forever
)

short_address = construct_short_address(address=location.raw["address"])
short_address = construct_short_address(address=raw_address)
log.info(f'short_address={short_address}')

return Address(short_address, location.address)
return Address(short_address, full_address)


if __name__ == "__main__":
Expand Down
1 change: 1 addition & 0 deletions src/for_runners/services/gpx_create.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ def add_from_file(*, gpx_file_file_path, user):
"""
assert_is_file(gpx_file_file_path)

log.info(f'Add GPX file: {gpx_file_file_path}')
with gpx_file_file_path.open("r") as f:
gpx_content = f.read()

Expand Down
13 changes: 13 additions & 0 deletions src/for_runners/tests/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#
# Hardcore Monkey patch to find not mocked requests ;)
# But this will also break selenium tests, too.
#
# import socket
#
# class SocketMock:
# def __call__(self, *args, **kwargs):
# raise NotImplementedError(
# 'Socket is monkeypatched in Tests!'
# )
#
# socket.socket = SocketMock()
13 changes: 13 additions & 0 deletions src/for_runners/tests/fixture_files/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from pathlib import Path

from django_tools.unittest_utils.assertments import assert_is_file


FIXTURES_PATH = Path(__file__).parent


def fixture_content(file_name, mode='rb'):
file_path = FIXTURES_PATH / file_name
assert_is_file(file_path)
with file_path.open(mode) as f:
return f.read()
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[{"distance":95674,"title":"Zurich","location_type":"City","woeid":784794,"latt_long":"47.377060,8.539550"},{"distance":128881,"title":"Geneva","location_type":"City","woeid":782538,"latt_long":"46.208351,6.142700"},{"distance":209620,"title":"Torino","location_type":"City","woeid":725003,"latt_long":"45.070290,7.667680"},{"distance":212151,"title":"Milan","location_type":"City","woeid":718345,"latt_long":"45.468941,9.181030"},{"distance":239723,"title":"Stuttgart","location_type":"City","woeid":698064,"latt_long":"48.767670,9.171920"},{"distance":239997,"title":"Lyon","location_type":"City","woeid":609125,"latt_long":"45.759392,4.828980"},{"distance":337153,"title":"Munich","location_type":"City","woeid":676757,"latt_long":"48.136410,11.577530"},{"distance":361207,"title":"Nice","location_type":"City","woeid":614274,"latt_long":"43.701530,7.278240"},{"distance":363090,"title":"Frankfurt","location_type":"City","woeid":650272,"latt_long":"50.112080,8.683410"},{"distance":387044,"title":"Nuremberg","location_type":"City","woeid":680564,"latt_long":"49.454342,11.07349"}]
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[{"distance":16638,"title":"Essen","location_type":"City","woeid":648820,"latt_long":"51.451809,7.0106"},{"distance":21601,"title":"Düsseldorf","location_type":"City","woeid":646099,"latt_long":"51.215630,6.776040"},{"distance":48430,"title":"Dortmund","location_type":"City","woeid":645458,"latt_long":"51.516609,7.458290"},{"distance":53455,"title":"Cologne","location_type":"City","woeid":667931,"latt_long":"50.941662,6.955180"},{"distance":167915,"title":"Amsterdam","location_type":"City","woeid":727232,"latt_long":"52.373119,4.893190"},{"distance":180601,"title":"Brussels","location_type":"City","woeid":968019,"latt_long":"50.848381,4.349680"},{"distance":185233,"title":"The Hague","location_type":"City","woeid":726874,"latt_long":"52.083988,4.31741"},{"distance":196720,"title":"Frankfurt","location_type":"City","woeid":650272,"latt_long":"50.112080,8.683410"},{"distance":229306,"title":"Hanover","location_type":"City","woeid":657169,"latt_long":"52.372269,9.73815"},{"distance":230669,"title":"Bremen","location_type":"City","woeid":641142,"latt_long":"53.075089,8.8047"}]
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[{"distance":27084,"title":"Essen","location_type":"City","woeid":648820,"latt_long":"51.451809,7.0106"},{"distance":27185,"title":"Düsseldorf","location_type":"City","woeid":646099,"latt_long":"51.215630,6.776040"},{"distance":58638,"title":"Dortmund","location_type":"City","woeid":645458,"latt_long":"51.516609,7.458290"},{"distance":60096,"title":"Cologne","location_type":"City","woeid":667931,"latt_long":"50.941662,6.955180"},{"distance":157369,"title":"Amsterdam","location_type":"City","woeid":727232,"latt_long":"52.373119,4.893190"},{"distance":171378,"title":"Brussels","location_type":"City","woeid":968019,"latt_long":"50.848381,4.349680"},{"distance":173776,"title":"The Hague","location_type":"City","woeid":726874,"latt_long":"52.083988,4.31741"},{"distance":206864,"title":"Frankfurt","location_type":"City","woeid":650272,"latt_long":"50.112080,8.683410"},{"distance":234707,"title":"Bremen","location_type":"City","woeid":641142,"latt_long":"53.075089,8.8047"},{"distance":237529,"title":"Hanover","location_type":"City","woeid":657169,"latt_long":"52.372269,9.73815"}]
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[{"distance":482,"title":"Berlin","location_type":"City","woeid":638242,"latt_long":"52.516071,13.376980"},{"distance":147245,"title":"Leipzig","location_type":"City","woeid":671072,"latt_long":"51.3452,12.38594"},{"distance":164826,"title":"Dresden","location_type":"City","woeid":645686,"latt_long":"51.053631,13.74081"},{"distance":247187,"title":"Hanover","location_type":"City","woeid":657169,"latt_long":"52.372269,9.73815"},{"distance":253780,"title":"Hamburg","location_type":"City","woeid":656958,"latt_long":"53.553341,9.992450"},{"distance":280937,"title":"Prague","location_type":"City","woeid":796597,"latt_long":"50.079079,14.433220"},{"distance":313483,"title":"Bremen","location_type":"City","woeid":641142,"latt_long":"53.075089,8.8047"},{"distance":354694,"title":"Copenhagen","location_type":"City","woeid":554890,"latt_long":"55.676311,12.569350"},{"distance":376899,"title":"Nuremberg","location_type":"City","woeid":680564,"latt_long":"49.454342,11.07349"},{"distance":419902,"title":"Dortmund","location_type":"City","woeid":645458,"latt_long":"51.516609,7.458290"}]

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"place_id":283373754,"licence":"Data © OpenStreetMap contributors, ODbL 1.0. https://osm.org/copyright","osm_type":"node","osm_id":3815077900,"lat":"0","lon":"0","display_name":"Soul Buoy","address":{"man_made":"Soul Buoy"},"boundingbox":["-5.0E-5","5.0E-5","-5.0E-5","5.0E-5"]}
Loading