1 | # -*- coding: utf-8 -*- |
||
2 | import os |
||
3 | import unittest |
||
4 | |||
5 | import pretend |
||
6 | from cornice import Service |
||
7 | from pyramid.httpexceptions import HTTPBadRequest, HTTPNotFound |
||
8 | from sqlalchemy import engine_from_config |
||
9 | from sqlalchemy.orm import sessionmaker |
||
10 | |||
11 | from oe_utils.data.data_managers import DataManager |
||
12 | from oe_utils.views import get_resource, get_json_from_request, RouteConfigArgs |
||
13 | from tests import init_test_db, DummyDossier, DummySchema |
||
14 | |||
15 | from zope.sqlalchemy import ZopeTransactionExtension |
||
16 | |||
17 | try: |
||
18 | from unittest.mock import Mock, patch, MagicMock |
||
19 | except ImportError: |
||
20 | from mock import Mock, patch, MagicMock |
||
21 | try: |
||
22 | import configparser |
||
23 | except ImportError: |
||
24 | import ConfigParser as configparser |
||
25 | |||
26 | service = Service('dummy_service', path='/test') |
||
27 | |||
28 | |||
29 | class ViewsTests(unittest.TestCase): |
||
30 | View Code Duplication | @classmethod |
|
0 ignored issues
–
show
Duplication
introduced
by
![]() |
|||
31 | def setUpClass(cls): |
||
32 | config = configparser.ConfigParser() |
||
33 | config.read(os.path.join(os.path.dirname(__file__), 'test.ini')) |
||
34 | settings = config.items('app:oe_utils') |
||
35 | settings = dict((s[0], s[1]) for s in settings) |
||
36 | cls.engine = engine_from_config(settings, prefix='sqlalchemy.') |
||
37 | cls.session_maker = sessionmaker( |
||
38 | bind=cls.engine, |
||
39 | extension=ZopeTransactionExtension() |
||
40 | ) |
||
41 | init_test_db(cls.engine) |
||
42 | |||
43 | def setUp(self): |
||
44 | self.session = self.session_maker() |
||
45 | self.data_manager = DataManager(self.session, DummyDossier) |
||
46 | |||
47 | def tearDown(self): |
||
48 | self.session.rollback() |
||
49 | self.session.close() |
||
50 | |||
51 | def test_get_resource_not_found(self): |
||
52 | self.assertRaises(HTTPNotFound, get_resource, self.data_manager, 987654321) |
||
53 | |||
54 | def test_get_resource(self): |
||
55 | get_resource(self.data_manager, 1) |
||
56 | |||
57 | def test_get_json_from_request_no_json_body(self): |
||
58 | request = pretend.stub(method="POST", path='/test') |
||
59 | with self.assertRaises(HTTPBadRequest) as e: |
||
60 | get_json_from_request(request) |
||
61 | self.assertIn('Request bevat geen json body. \n', e.exception.__str__()) |
||
62 | |||
63 | def test_get_json_from_request(self): |
||
64 | request = pretend.stub(method='POST', path='/test', json_body={'test': 'test'}) |
||
65 | res = get_json_from_request(request) |
||
66 | self.assertEqual({'test': 'test'}, res) |
||
67 | |||
68 | |||
69 | class RouteConfigArgsTests(unittest.TestCase): |
||
70 | def setUp(self): |
||
71 | self.route_config_args = RouteConfigArgs([], DummySchema, tags=['Test']) |
||
72 | |||
73 | def tearDown(self): |
||
74 | del self.route_config_args |
||
75 | |||
76 | def test_init(self): |
||
77 | self.assertEquals(['Test'], self.route_config_args.tags) |
||
78 | self.assertEquals([], self.route_config_args.response_schemas) |
||
79 | self.assertEquals(DummySchema, self.route_config_args.schema) |
||
80 |