Skip to content

Commit

Permalink
Merge pull request #52 from digitalaotearoa/add-poetry
Browse files Browse the repository at this point in the history
Add poetry
  • Loading branch information
piacandrews authored Jul 27, 2023
2 parents 840eca2 + ffb99da commit 9de3d7a
Show file tree
Hide file tree
Showing 22 changed files with 1,762 additions and 144 deletions.
106 changes: 88 additions & 18 deletions .github/workflows/python.yaml
Original file line number Diff line number Diff line change
@@ -1,31 +1,101 @@
name: Python

on: [push]
on:
push:
branches: [main]
pull_request:
types: [assigned, opened, reopened, synchronize, ready_for_review]
workflow_dispatch:

jobs:
tests:
build:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3
- name: Checkout
uses: actions/checkout@v3

- name: Install Poetry
run: pipx install poetry

- name: Set up Python
uses: actions/setup-python@v3
uses: actions/setup-python@v4
with:
python-version: 3.9.13
- name: Install OpenFisca-Aotearoa
run: make install
- name: Run tests
run: make test
pythonlint:

- name: Cache tests
uses: actions/cache@v3
with:
path: |
Makefile
openfisca_aotearoa/tests
poetry.lock
tox.ini
key: tests-${{ github.sha }}

- name: Cache build
id: cache-build
uses: actions/cache@v3
with:
path: dist
key: build-${{ hashFiles('poetry.lock') }}-${{ github.sha }}

- name: Build package
if: steps.cache-build.outputs.cache-hit != 'true'
run: poetry build

lint:
runs-on: ubuntu-latest
needs: [build]

steps:
- uses: actions/checkout@v3
- run: pip install --upgrade flake8
- run: flake8
yamllint:
- name: Checkout
uses: actions/checkout@v3

- name: Install Poetry
run: pipx install poetry

- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: 3.9.13
cache: poetry
cache-dependency-path: poetry.lock

- name: Install dependencies
run: make install

- name: Lint files
run: make lint

test:
runs-on: ubuntu-latest
needs: [build]

steps:
- uses: actions/checkout@v3
- run: pip install --upgrade yamllint
- run: yamllint --version
- name: YAML lint
run: yamllint --format=github .
- name: Install Tox
run: pipx install tox

- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: 3.9.13

- name: Restore tests
uses: actions/cache@v3
with:
path: |
Makefile
openfisca_aotearoa/tests
poetry.lock
tox.ini
key: tests-${{ github.sha }}

- name: Restore build
uses: actions/cache@v3
with:
path: dist
key: build-${{ hashFiles('poetry.lock') }}-${{ github.sha }}

- name: Run the test suite
run: tox
1 change: 0 additions & 1 deletion MANIFEST.in

This file was deleted.

36 changes: 12 additions & 24 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,48 +1,36 @@
all: lint test

uninstall:
pip freeze | grep -v "^-e" | xargs pip uninstall -y
pip freeze | grep -v "^-e" | sed "s/@.*//" | xargs pip uninstall -y

clean:
rm -rf build dist
find . -name '*.pyc' -exec rm \{\} \;

deps:
pip install --upgrade pip

install: deps
install:
@# Install OpenFisca-Aotearoa for development.
@# `make install` installs the editable version of OpenFisca-Aotearoa.
@# This allows contributors to test as they code.
pip install --upgrade --editable .[dev]

build: clean deps
@# Install OpenFisca-Aotearoa for deployment and publishing.
@# `make build` allows us to be be sure tests are run against the packaged version
@# of OpenFisca-Aotearoa, the same we put in the hands of users and reusers.
python -m build
find dist -name "*.whl" -exec pip install --force-reinstall {}[dev] \;
pip install --upgrade pip
poetry install --sync

check-syntax-errors:
python -m compileall -q .

format-style:
format:
@# Do not analyse .gitignored files.
@# `make` needs `$$` to output `$`. Ref: http://stackoverflow.com/questions/2382764.
isort `git ls-files | grep "\.py$$"`
autopep8 `git ls-files | grep "\.py$$"`
pyupgrade --py39-plus `git ls-files | grep "\.py$$"`

poetry run isort `git ls-files | grep "\.py$$"`
poetry run autopep8 `git ls-files | grep "\.py$$"`
poetry run pyupgrade --py39-plus `git ls-files | grep "\.py$$"`

check-style:
lint: clean check-syntax-errors format
@# Do not analyse .gitignored files.
@# `make` needs `$$` to output `$`. Ref: http://stackoverflow.com/questions/2382764.
flake8 `git ls-files | grep "\.py$$"`
pylint `git ls-files | grep "\.py$$"`
yamllint `git ls-files | grep "\.yaml$$"`

poetry run flake8 `git ls-files | grep "\.py$$"`
poetry run pylint `git ls-files | grep "\.py$$"`
poetry run yamllint `git ls-files | grep "\.yaml$$"`

lint: clean check-syntax-errors format-style check-style
test: clean check-syntax-errors
ifdef yaml
openfisca test -c openfisca_aotearoa openfisca_aotearoa/tests/$(yaml)
Expand Down
1 change: 0 additions & 1 deletion Procfile

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,10 @@
sole_parent_support__benefit:
- 304.05
- 0
- name: Sole Parent Support Act 2018 — Eligibility Marriage or Civil Union Dissolved Family and $200 income and childcare costs TODO, check outputs manually
- name: >
Sole Parent Support Act 2018 — Eligibility Marriage or Civil Union
Dissolved Family and $200 income and childcare costs
TODO, check outputs manually
period: 2019-W05
absolute_error_margin: 0
input:
Expand Down Expand Up @@ -85,7 +88,10 @@
sole_parent_support__benefit:
- 310.05
- 0
- name: Sole Parent Support Act 2018 — Eligibility Marriage or Civil Union Dissolved Family and $200 income and childcare costs TODO, check outputs manually
- name: >
Sole Parent Support Act 2018 — Eligibility Marriage or Civil Union
Dissolved Family and $200 income and childcare costs
TODO, check outputs manually
period: 2023-W05
absolute_error_margin: 0
input:
Expand Down
92 changes: 92 additions & 0 deletions openfisca_aotearoa/tests/test_api.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
"""Smoke-test to ensure the API runs and returns a valid response."""

import json
import random
import subprocess
from subprocess import SubprocessError, TimeoutExpired
from urllib import request

import pytest


@pytest.fixture
def host():
"""Return the host to use for the API."""

return "127.0.0.1"


@pytest.fixture
def port():
"""Return a random port to use for the API."""

return random.randint(5000, 5999)


@pytest.fixture
def endpoint():
"""Return the endpoint to use for the API test."""

return "spec"


@pytest.fixture
def timeout():
"""Return the timeout to use for the API."""

return 2.5


@pytest.fixture
def payload(timeout, host, port, endpoint):
"""Return the payload to use for the API test."""

return {"url": f"http://{host}:{port}/{endpoint}", "timeout": timeout}


@pytest.fixture
def pipe():
"""Return a descriptor to use for the server subprocess."""

return subprocess.PIPE


@pytest.fixture
def server(host, port, pipe, timeout):
"""Return a server subprocess."""

cmd = [
"openfisca",
"serve",
"--country-package",
"openfisca_aotearoa",
"--port",
str(port),
]

with subprocess.Popen(cmd, stdout = pipe, stderr = pipe) as proc:
try:
_, out = proc.communicate(timeout = timeout)

except TimeoutExpired as error:
if error.stderr is not None:
out = error.stderr

else:
out = f"Timed out after {timeout}s ({proc.pid})".encode()

if f"Listening at: http://{host}:{port} ({proc.pid})" in str(out):
yield
proc.terminate()

else:
proc.terminate()
raise SubprocessError(f"Failed to start!\n{out.decode()}")


def test_openfisca_server(server, payload):
"""Test the OpenFisca API serves the /spec endpoint."""

with request.urlopen(**payload) as response:
data = json.loads(response.read().decode("utf-8"))
assert data["info"]["title"] == "Openfisca-Aotearoa Web API"
6 changes: 4 additions & 2 deletions openfisca_aotearoa/variables/acts/citizenship/citizenship.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
"""TODO: Add missing doctring."""
from openfisca_core.periods import DateUnit, DAY, ETERNITY
from openfisca_core.periods import DAY, ETERNITY, DateUnit
from openfisca_core.variables import Variable

from openfisca_aotearoa.entities import Person
from openfisca_aotearoa.variables.demographics.residence import days_since_n_years_ago
from openfisca_aotearoa.variables.demographics.residence import (
days_since_n_years_ago,
)


class citizenship__citizen(Variable):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@

# Import from openfisca-core the common python objects used to code the legislation in OpenFisca

from openfisca_core import holders
from openfisca_core import periods, variables
from openfisca_core import holders, periods, variables

from openfisca_aotearoa import entities

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
"""TODO: Add missing doctring."""

from openfisca_core import holders
from openfisca_core import periods, variables
from openfisca_core import holders, periods, variables

from openfisca_aotearoa import entities

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

from openfisca_core import indexed_enums, periods, variables

import openfisca_aotearoa
from openfisca_aotearoa import entities


Expand Down Expand Up @@ -40,8 +41,9 @@ class accommodation_supplement__area_of_residence(variables.Variable):

def formula_2018_11_26(people, period, _params):
area_of_residence = AccommodationSupplement__AreaOfResidence
params_path = "openfisca_aotearoa/parameters"
file_path = "social_security/accommodation_supplement"
module_path = Path(openfisca_aotearoa.__file__).parent
params_path = Path(f"{module_path}/parameters")
file_path = Path("social_security/accommodation_supplement")
area_path = Path(f"{params_path}/{file_path}/area.csv").resolve()

# We read locations from a database.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@

import numpy

from openfisca_core import holders
from openfisca_core import periods, variables
from openfisca_core import holders, periods, variables

from openfisca_aotearoa import entities

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
"""TODO: Add missing doctring."""

from openfisca_core import holders
from openfisca_core import periods, variables
from openfisca_core import holders, periods, variables

from openfisca_aotearoa import entities

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
"""This module provides eligibility and amount for Jobseeker Support."""

from openfisca_core import holders
from openfisca_core import periods, variables
from openfisca_core import holders, periods, variables

from openfisca_aotearoa import entities

Expand Down
Loading

0 comments on commit 9de3d7a

Please sign in to comment.