Skip to content
Open
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
Empty file removed .github/.keep
Empty file.
33 changes: 33 additions & 0 deletions .github/workflows/python-app.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# This workflow will install Python dependencies, run tests and lint with a single version of Python
# For more information see: https://help.github.com/actions/language-and-framework-guides/using-python-with-github-actions

name: Python application

on:
push:
branches: [ main ]
pull_request:
branches: [ main ]

jobs:
build:

runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2
- name: Set up Python 3.9
uses: actions/setup-python@v2
with:
python-version: "3.9"
- name: Install dependencies
run: |
python -m pip install --upgrade pip
python -m pip install -r requirements.txt
- name: Lint with flake8
run: |
# stop the build if there are Python syntax errors or undefined names
python -m flake8 track tests
- name: Test with pytest
run: |
python -m pytest -v tests
9 changes: 9 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
.hypothesis
.pytest_cache
__pycache__
venv

# news/changed (on disk, untracked for new files)
# git add -> (staging area)
# git commit -> (history)
# git push -> (history on server)
5 changes: 5 additions & 0 deletions prod-requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Production requirements

matplotlib==3.5.1
numpy==1.22.0
pandas==1.3.5
17 changes: 17 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Development requirements

-r prod-requirements.txt

flake8==4.0.1
flake8-bugbear==22.1.11
hypothesis==6.36.0
pandera==0.8.1
pytest==6.2.5
PyYAML==6.0

# To check for python, run in console
# python -m pytest --version

# From terminal
# python3.9 -m venv venv
# ./venv/bin/python -m pip install -r requirements.txt
12 changes: 12 additions & 0 deletions tests/dist_cases.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
- lat1: 0
lng1: 0
lat2: 1
lng2: 1
distance: 144.1700384962146
# checking same location
- lat1: 0
lng1: 0
lat2: 0
lng2: 0
distance: 0
name: "same location"
95 changes: 95 additions & 0 deletions tests/test_track.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
import math
from pathlib import Path

import yaml
from hypothesis import given
from hypothesis.strategies import floats

import track
import pytest
from track.schema import track_schema


def test_smoke():
pass


def test_distance():
dist = track.distance(0, 0, 1, 1)
assert 144.1700384962146 == dist


distance_cases = [
# coord1, coord2, distance
[(0, 0), (1, 1), 144.1700384962146],
# [(1, 1), (1, 1), 0],
pytest.param((1, 1), (1, 1), 0, id='same location'),
# ...
]

# Exercise: Instead of reading tests from distance_cases, read then from
# dist_cases.yml
# You'll need to install PyYAML to read YAML files
# import yaml
# with open('tests/dist_cases.yml') as fp:
# data = yaml.safe_load(fp) # data is a list of dicts

tests_dir = Path(__file__).absolute().parent


def load_dist_cases():
with open(tests_dir / 'dist_cases.yml') as fp:
data = yaml.safe_load(fp)

cases = []
for case in data:
coord1 = (case['lat1'], case['lng1'])
coord2 = (case['lat2'], case['lng2'])
cases.append([coord1, coord2, case['distance']])
return cases


@pytest.mark.parametrize('coord1, coord2, expected', load_dist_cases())
def test_distance_many(coord1, coord2, expected):
lat1, lng1 = coord1
lat2, lng2 = coord2
dist = track.distance(lat1, lng1, lat2, lng2)
assert expected == dist


def test_distance_type_error():
with pytest.raises(TypeError):
track.distance('1', 1, 0, 0)

# try:
# track.distance('1', 1, 0, 0)
# assert False, 'did not raise'
# except TypeError:
# pass


# from hypothesis import given
# from hypothesis.strategies import floats

@given(floats(), floats(), floats(), floats())
def test_distance_fuzz(lat1, lng1, lat2, lng2):
# print(lat1, lng1, lat2, lng2)
# run: python -m pytest -s
dist = track.distance(lat1, lng1, lat2, lng2)
if math.isnan(dist):
return
assert dist >= 0


# from schema import track_schema
def test_load_track():
csv_file = tests_dir / 'track.csv'
df = track.load_track(csv_file)
track_schema.validate(df)


"""
Running tests:
- python -m flake8 lib.py tests
- python -m pytest -v
"""
File renamed without changes.
Loading