From 4b41dad890a2cf8678302a479ecc007e28da545a Mon Sep 17 00:00:00 2001 From: Holger Frey Date: Thu, 12 Oct 2017 23:51:45 +0200 Subject: [PATCH] added first tests --- tests/__init__.py | 4 +--- tests/resources/__init__.py | 45 +++++++++++++++++++++++++++++++++++++ tests/test_ordr2.py | 3 --- tests/views/__init__.py | 1 + tests/views/errors.py | 14 ++++++++++++ tests/views/public.py | 10 +++++++++ 6 files changed, 71 insertions(+), 6 deletions(-) create mode 100644 tests/resources/__init__.py create mode 100644 tests/views/__init__.py create mode 100644 tests/views/errors.py create mode 100644 tests/views/public.py diff --git a/tests/__init__.py b/tests/__init__.py index ed05283..2cf08d1 100644 --- a/tests/__init__.py +++ b/tests/__init__.py @@ -1,3 +1 @@ -# -*- coding: utf-8 -*- - -''' Unit test package for ordr2. ''' +''' Test package for ordr2. ''' diff --git a/tests/resources/__init__.py b/tests/resources/__init__.py new file mode 100644 index 0000000..c903484 --- /dev/null +++ b/tests/resources/__init__.py @@ -0,0 +1,45 @@ +''' Test package for ordr2.resources ''' + +import pytest + + +def test_base_resource_init(): + ''' test __init__ function of base resource ''' + from ordr2.resources import BaseResource, RootResource + root = RootResource('request object') + + resource = BaseResource('resource name', root) + + assert resource.__name__ == 'resource name' + assert resource.__parent__ == root + assert resource.request == 'request object' + + +@pytest.mark.parametrize( + 'segment', [ + 'known', + pytest.mark.xfail('unknown', raises=KeyError) + ] + ) +def test_base_resource_getitem(segment): + ''' test the __getitem__ function of base resource ''' + from ordr2.resources import BaseResource, RootResource + root = RootResource('request object') + root.nodes = {'known': BaseResource} + + resource = root[segment] + + assert resource.__name__ == segment + assert resource.__parent__ == root + assert resource.request == 'request object' + + +def test_root_resource_init(): + ''' test __init__ function of root resource ''' + from ordr2.resources import RootResource + + resource = RootResource('request object') + + assert resource.__name__ == None + assert resource.__parent__ == None + assert resource.request == 'request object' diff --git a/tests/test_ordr2.py b/tests/test_ordr2.py index 547038d..96b2182 100644 --- a/tests/test_ordr2.py +++ b/tests/test_ordr2.py @@ -1,6 +1,3 @@ -#!/usr/bin/env python -# -*- coding: utf-8 -*- - ''' Tests for `ordr2` package. ''' import pytest diff --git a/tests/views/__init__.py b/tests/views/__init__.py new file mode 100644 index 0000000..d931ab6 --- /dev/null +++ b/tests/views/__init__.py @@ -0,0 +1 @@ +''' Test package for ordr2.views ''' diff --git a/tests/views/errors.py b/tests/views/errors.py new file mode 100644 index 0000000..d66f236 --- /dev/null +++ b/tests/views/errors.py @@ -0,0 +1,14 @@ +''' Tests for ordr2.views.error ''' + + +def test_index_view(): + ''' test the not found view ''' + from pyramid.testing import DummyRequest + from ordr2.views.errors import notfound_view + request = DummyRequest() + + result = notfound_view(None, request) + + assert result == {} + assert request.response.status == '404 Not Found' + diff --git a/tests/views/public.py b/tests/views/public.py new file mode 100644 index 0000000..e608bd2 --- /dev/null +++ b/tests/views/public.py @@ -0,0 +1,10 @@ +''' Tests for ordr2.views.public ''' + + +def test_index_view(): + ''' test the default view for a root resource ''' + from ordr2.views.public import index + + result = index(None, None) + + assert result == {}