Skip to content

Commit

Permalink
feat(add tests): Make it wasier to make future changes by providing s…
Browse files Browse the repository at this point in the history
…uite of tests
  • Loading branch information
MikeSchapp committed Oct 1, 2022
2 parents 39d2a2c + 2792d61 commit 505c159
Show file tree
Hide file tree
Showing 21 changed files with 473 additions and 76 deletions.
8 changes: 5 additions & 3 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
name: Release
on:
push:
branches:
- main
workflow_run:
workflows: ["Test"]
branches: [main]
types:
- completed
jobs:
release:
name: Release
Expand Down
31 changes: 31 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
name: Test
on:
push

jobs:
release:
name: Unit Test
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v2
with:
fetch-depth: 0
- name: Install dependencies and run tests
run: |
git clone https://github.com/pyenv/pyenv.git ~/.pyenv
export PYENV_ROOT="$HOME/.pyenv"
command -v pyenv >/dev/null || export PATH="$PYENV_ROOT/bin:$PATH"
eval "$(pyenv init -)"
pyenv install micropython-1.19.1
pyenv local micropython-1.19.1
micropython -m upip install -r requirements.txt -p 'firmware/'
- name: Setup Python 3.10
uses: actions/setup-python@v4
with:
python-version: '3.10'
- name: Install dependencies
run: pip install -r test-requirements.txt
- name: Run tests
run: make run_tests

163 changes: 162 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,163 @@
env.json
secrets.json
secrets.json

# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class

# C extensions
*.so

# Distribution / packaging
.Python
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
share/python-wheels/
*.egg-info/
.installed.cfg
*.egg
MANIFEST

# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec

# Installer logs
pip-log.txt
pip-delete-this-directory.txt

# Unit test / coverage reports
htmlcov/
.tox/
.nox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
*.py,cover
.hypothesis/
.pytest_cache/
cover/

# Translations
*.mo
*.pot

# Django stuff:
*.log
local_settings.py
db.sqlite3
db.sqlite3-journal

# Flask stuff:
instance/
.webassets-cache

# Scrapy stuff:
.scrapy

# Sphinx documentation
docs/_build/

# PyBuilder
.pybuilder/
target/

# Jupyter Notebook
.ipynb_checkpoints

# IPython
profile_default/
ipython_config.py

# pyenv
# For a library or package, you might want to ignore these files since the code is
# intended to run in multiple environments; otherwise, check them in:
# .python-version

# pipenv
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
# However, in case of collaboration, if having platform-specific dependencies or dependencies
# having no cross-platform support, pipenv may install dependencies that don't work, or not
# install all needed dependencies.
#Pipfile.lock

# poetry
# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
# This is especially recommended for binary packages to ensure reproducibility, and is more
# commonly ignored for libraries.
# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
#poetry.lock

# pdm
# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
#pdm.lock
# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it
# in version control.
# https://pdm.fming.dev/#use-with-ide
.pdm.toml

# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
__pypackages__/

# Celery stuff
celerybeat-schedule
celerybeat.pid

# SageMath parsed files
*.sage.py

# Environments
.env
.venv
env/
venv/
ENV/
env.bak/
venv.bak/

# Spyder project settings
.spyderproject
.spyproject

# Rope project settings
.ropeproject

# mkdocs documentation
/site

# mypy
.mypy_cache/
.dmypy.json
dmypy.json

# Pyre type checker
.pyre/

# pytype static type analyzer
.pytype/

# Cython debug symbols
cython_debug/

# PyCharm
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
# and can be added to the global gitignore or merged into this file. For a more nuclear
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
#.idea/
2 changes: 1 addition & 1 deletion .python-version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
micropython-1.19.1
3.10.7
7 changes: 7 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
install_test_requirements:
pip install -r test-requirements.txt

run_tests:
pytest --cov=firmware/lib
coverage xml
coverage report --fail-under=80
17 changes: 17 additions & 0 deletions conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# conftest.py
import sys
import json
import requests
import os

class Pin:
def __init__():
pass

machine = type(sys)('machine')
machine.Pin = Pin

sys.modules['ujson'] = json
sys.modules['urequests'] = requests
sys.modules['uos'] = os
sys.modules['machine'] = machine
59 changes: 0 additions & 59 deletions firmware/lib/connection.py

This file was deleted.

8 changes: 1 addition & 7 deletions firmware/lib/scout.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import utils
from lib import utils
import urequests as requests
import ujson as json
import uos as os
from pin import onboard_led
import time


class Scout:
Expand Down Expand Up @@ -112,10 +110,6 @@ def connect_to_baby_buddy(base_url):
baby_buddy_reachable = True
except OSError:
print("Failed to connect to BabyBuddy")
onboard_led()
time.sleep(1)
onboard_led()
onboard_led(0)
print("Connected to BabyBuddy")
return baby_buddy

Expand Down
2 changes: 1 addition & 1 deletion firmware/lib/webpage.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import uos as os
import ujson as json
import machine
import template
from lib import template


def load_webpage(location):
Expand Down
4 changes: 2 additions & 2 deletions firmware/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import uos as os
import _thread
from lib.utils import retrieve_auth_variables, join_path, auth_variables_valid
from lib.connection import PicoConnection
from picoconnection import PicoConnection
from lib.webpage import config_route, default_route
from lib.scout import connect_to_baby_buddy
from picowebrouter import WebRouter
Expand Down Expand Up @@ -80,7 +80,7 @@ def ensure_connection():
ap_mode = True
if ap_mode:
print("No matching wifi, falling back to webpage based setup.")
ap = pico_connection.access_point_wifi_setup()
ap = pico_connection.access_point_wifi_setup("BabyScout", "BabyBuddy")
ip = ap.ifconfig()[0]
app = WebRouter(ip, 80, default_route, "webpages/static")
app.route("/config")(config_route)()
Expand Down
2 changes: 1 addition & 1 deletion firmware/secrets.json.placeholder
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"SSIDS_PASSWORD": {
"MYSSID": "MYPASSWORD
"MYSSID": "MYPASSWORD"
},
"BASE_URL": "http://baby.example.com/api/",
"AUTHORIZATION": {
Expand Down
2 changes: 2 additions & 0 deletions pytest.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[pytest]
pythonpath = firmware/
3 changes: 2 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
micropython-picowebrouter==0.0.2
micropython-picowebrouter==0.0.2
micropython-picoconnection==0.0.0
4 changes: 4 additions & 0 deletions test-requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
pytest
requests
pytest-cov
coverage
11 changes: 11 additions & 0 deletions tests/resources/fake_auth.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"SSIDS_PASSWORD": {
"MYSSID": "MYPASSWORD"
},
"BASE_URL": "http://baby.example.com/api/",
"AUTHORIZATION": {
"CF-Access-Client-Id": "my cloudfront tunnel id",
"CF-Access-Client-Secret": "my cloudfront tunnel secret",
"Authorization": "Token my baby buddy token "
}
}
10 changes: 10 additions & 0 deletions tests/resources/fake_false_auth.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"SSIDS_PASSWORD": {
"MYSSID": "MYPASSWORD"
},
"AUTHORIZATION": {
"CF-Access-Client-Id": "my cloudfront tunnel id",
"CF-Access-Client-Secret": "my cloudfront tunnel secret",
"Authorization": "Token my baby buddy token "
}
}
Loading

0 comments on commit 505c159

Please sign in to comment.