Commit e59a6f57 authored by Trond Aasan's avatar Trond Aasan
Browse files

Merge branch 'bugfix/tox-poetry' into 'master'

Don't use `poetry` in `tox.ini`

See merge request !1
parents 04dcbae0 038aa6d9
Pipeline #86664 passed with stage
in 37 seconds
......@@ -4,7 +4,13 @@ stages:
- test
before_script:
- pip install tox poetry
- pip install tox
pre-commit:
image: python
stage: test
script:
- tox -e pre-commit-ci
python38:
image: python:3.8
......@@ -17,6 +23,6 @@ python39:
stage: test
script:
- tox -e py39
# artifacts:
# reports:
# cobertura: coverage.xml
artifacts:
reports:
cobertura: coverage.xml
......@@ -9,20 +9,18 @@ repos:
- id: check-yaml
- id: check-added-large-files
- repo: local
- repo: https://github.com/psf/black
# Must match version in pyproject.toml
rev: 21.6b0
hooks:
- id: black
name: black
entry: poetry run black
language: python
language_version: python3
minimum_pre_commit_version: 2.9.2
require_serial: true
types_or: [ python, pyi ]
- repo: local
hooks:
- id: pytest
name: pytest
entry: poetry run pytest
language: python
language_version: python3
pass_filenames: false
types_or: [ python, pyi ]
......@@ -44,12 +44,19 @@ class Tree(Generic[T]):
yield v
def __repr__(self):
return f"Tree(data={self.data}, children={self.children})"
children = ", ".join(list(map(repr, self.children)))
return (
f"Tree(data={self.data}, id_key={repr(self.id_key)}, children=[{children}])"
)
def __eq__(self, other):
if not isinstance(other, Tree):
return False
return self.data == other.data and self.children == other.children
return all(
[
self.id_key == other.id_key,
self.data == other.data,
self.children == other.children,
]
)
def values(self) -> Iterator[T]:
for x in self:
......
......@@ -22,6 +22,7 @@ pytest = "^5.2"
black = { version = "21.6b0", allow-prereleases = true }
pytest-black = "^0.3.12"
pre-commit = "^2.13.0"
coverage = "^5.5"
[build-system]
requires = ["poetry-core>=1.0.0"]
......
import itertools
from typing import NamedTuple, Optional, List
import pytest
......@@ -66,6 +67,87 @@ def node_with_id_equals_parent_id():
return Node(id=20001, parent_id=20001)
@pytest.fixture
def nodes(
node_1,
node_2,
node_3,
node_4,
node_5,
):
return [
node_1,
node_2,
node_3,
node_4,
node_5,
]
@pytest.fixture
def nodes_x(
node_x_1,
node_x_2,
):
return [
node_x_1,
node_x_2,
]
@pytest.fixture
def tree_1(
node_1,
node_2,
node_3,
node_4,
node_5,
):
t_5 = Tree(data=node_5, id_key="id", children=[])
t_4 = Tree(data=node_4, id_key="id", children=[t_5])
t_3 = Tree(data=node_3, id_key="id", children=[])
t_2 = Tree(data=node_2, id_key="id", children=[t_4])
t_1 = Tree(data=node_1, id_key="id", children=[t_2, t_3])
t_2.parent = t_1
t_3.parent = t_1
t_4.parent = t_2
t_5.parent = t_4
return [t_1, t_2, t_3, t_4, t_5]
@pytest.fixture
def tree_x1(
node_x_1,
node_x_2,
):
t_x_2 = Tree(data=node_x_2, id_key="id", children=[])
t_x_1 = Tree(data=node_x_1, id_key="id", children=[t_x_2])
t_x_2.parent = t_x_1
return [t_x_1, t_x_2]
@pytest.fixture
def tree_with_loop(
node_x_1,
node_x_2,
):
t_x_2 = Tree(data=node_x_2, id_key="id", children=[])
t_x_1 = Tree(data=node_x_1, id_key="id", children=[t_x_2])
t_x_2.parent = t_x_1
t_x_1.parent = t_x_2
return [t_x_1, t_x_2]
@pytest.fixture
def forest(tree_1, tree_x1):
return {x.id: x for x in [*tree_1, *tree_x1]}
def test_build_forest_loop_parent_equals_key(
node_1,
node_with_id_equals_parent_id,
......@@ -117,57 +199,34 @@ def test_build_forest_parent_not_found(
_build_forest(items)
def test_build_forest(
node_1,
node_2,
node_3,
node_4,
node_5,
node_x_1,
node_x_2,
):
items = [
node_1,
node_2,
node_3,
node_4,
node_5,
node_x_1,
node_x_2,
]
t_5 = Tree(data=node_5, id_key="id", children=[])
t_4 = Tree(data=node_4, id_key="id", children=[t_5])
t_3 = Tree(data=node_3, id_key="id", children=[])
t_2 = Tree(data=node_2, id_key="id", children=[t_4])
t_1 = Tree(data=node_1, id_key="id", children=[t_2, t_3])
t_x_2 = Tree(data=node_x_2, id_key="id", children=[])
t_x_1 = Tree(data=node_x_1, id_key="id", children=[t_x_2])
def test_build_forest(nodes, nodes_x, forest):
forest = _build_forest(items)
x = _build_forest([*nodes, *nodes_x])
assert forest == {x.id: x for x in [t_1, t_2, t_3, t_4, t_5, t_x_1, t_x_2]}
assert x == forest
def test_tree_iter(
node_1,
node_2,
node_3,
node_4,
node_5,
node_x_1,
node_x_2,
):
items = [
node_1,
node_2,
node_3,
node_4,
node_5,
node_x_1,
node_x_2,
]
def test_tree_iter(tree_1):
xs = list(sorted(tree_1[0], key=lambda x: x.id))
assert xs == tree_1
def test_tree_values(tree_1, nodes):
xs = list(sorted(tree_1[0].values(), key=lambda x: x.id))
assert xs == nodes
def test_depth(tree_1):
assert tree_1[0].depth == 1
assert tree_1[1].depth == 2
def test_depth_throws_loop_detected(tree_with_loop):
with pytest.raises(LoopDetected):
tree_with_loop[0].depth # noqa
forest = _build_forest(items)
forest_data = sorted(map(lambda x: x.data, forest.values()), key=lambda x: x.id)
assert items == forest_data
def test___repr__(tree_1):
assert eval(repr(tree_1)) == tree_1
[tox]
isolated_build = true
envlist = py3{8,9}
envlist = py3{8,9},pre-commit
# We want to run all pre-commit hooks when we run tox
[testenv:pre-commit]
deps =
pre-commit
pytest
commands = pre-commit run --all-files --show-diff-on-failure
# We skip the pytest hook in CI as pytest runs in the other environments
[testenv:pre-commit-ci]
deps = pre-commit
setenv =
SKIP=pytest
commands = {[testenv:pre-commit]commands}
[testenv]
whitelist_externals = poetry
skip_install = True
deps =
coverage
pytest
commands =
poetry install -v
poetry run pre-commit run --all
coverage erase
coverage run --branch --source=bottint_tree -m pytest
coverage xml
coverage report
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment