Introduce some unit tests in lemoncurry.tests - only testing a few of the really easy things so far ;)

This commit is contained in:
Danielle McLean 2018-02-02 15:08:41 +11:00
parent dffa2d9d50
commit 39b2e40e32
Signed by: 00dani
GPG Key ID: 5A5D2D1AFF12EEC5
3 changed files with 74 additions and 0 deletions

View File

View File

@ -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

26
lemoncurry/tests/utils.py Normal file
View File

@ -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'