diff --git a/lemoncurry/tests/__init__.py b/lemoncurry/tests/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/lemoncurry/tests/breadcrumbs.py b/lemoncurry/tests/breadcrumbs.py new file mode 100644 index 0000000..c11ffd6 --- /dev/null +++ b/lemoncurry/tests/breadcrumbs.py @@ -0,0 +1,48 @@ +from collections import namedtuple +import pytest + +from .. import breadcrumbs as b + + +@pytest.fixture +def nested_crumbs(): + x = {'route': 'nc.x', 'label': 'x', 'parent': None} + y = {'route': 'nc.y', 'label': 'y', 'parent': 'nc.x'} + z = {'route': 'nc.z', 'label': 'z', 'parent': 'nc.y'} + crumbs = (x, y, z) + + for crumb in crumbs: + b.breadcrumbs[crumb['route']] = crumb + yield namedtuple('NestedCrumbs', 'x y z')(*crumbs) + for crumb in crumbs: + del b.breadcrumbs[crumb['route']] + + +class TestAdd: + def test_inserts_a_breadcrumb_without_parent(self): + route = 'tests.add.insert' + assert route not in b.breadcrumbs + b.add(route, 'some label') + assert route in b.breadcrumbs + assert b.breadcrumbs[route] == { + 'route': route, 'label': 'some label', 'parent': None + } + + def test_inserts_a_breadcrumb_with_parent(self): + route = 'tests.add.with_parent' + parent = 'tests.add.insert' + assert route not in b.breadcrumbs + b.add(route, 'child label', parent) + assert route in b.breadcrumbs + assert b.breadcrumbs[route] == { + 'route': route, 'label': 'child label', 'parent': parent + } + + +class TestFind: + def test_finds_chain_of_crumbs(self, nested_crumbs): + crumbs = b.find(nested_crumbs.z['route']) + assert len(crumbs) == 3 + assert crumbs[0] == nested_crumbs.x + assert crumbs[1] == nested_crumbs.y + assert crumbs[2] == nested_crumbs.z diff --git a/lemoncurry/tests/utils.py b/lemoncurry/tests/utils.py new file mode 100644 index 0000000..5745087 --- /dev/null +++ b/lemoncurry/tests/utils.py @@ -0,0 +1,26 @@ +from unittest.mock import Mock +from .. import utils + + +class TestOrigin: + def test_simple_http(self): + """should return the correct origin for a vanilla HTTP site""" + req = Mock(scheme='http', site=Mock(domain='lemoncurry.test')) + assert utils.origin(req) == 'http://lemoncurry.test' + + def test_simple_https(self): + """should return the correct origin for a vanilla HTTPS site""" + req = Mock(scheme='https', site=Mock(domain='secure.lemoncurry.test')) + assert utils.origin(req) == 'https://secure.lemoncurry.test' + + +class TestUri: + def test_siteroot(self): + """should return correct full URI for requests to the site root""" + req = Mock(scheme='https', path='/', site=Mock(domain='l.test')) + assert utils.uri(req) == 'https://l.test/' + + def test_path(self): + """should return correct full URI for requests with a path""" + req = Mock(scheme='https', path='/notes/23', site=Mock(domain='l.tst')) + assert utils.uri(req) == 'https://l.tst/notes/23'