|
| 1 | +#!/usr/bin/env python |
| 2 | +# -*- encoding: utf-8 -*- |
| 3 | + |
| 4 | +from unittest import TestCase |
| 5 | +from mock import Mock |
| 6 | + |
| 7 | +from pygithub3.requests import Factory, Body, json, Request |
| 8 | +from pygithub3.exceptions import UriInvalid, DoesNotExists, ValidationError |
| 9 | +from pygithub3.tests.utils.requests import ( |
| 10 | + RequestWithArgs, RequestCleanedUri, RequestBodyWithSchema, mock_json_dumps, |
| 11 | + DummyRequest, RequestCleanedBody) |
| 12 | + |
| 13 | +json.dumps = Mock(side_effect=mock_json_dumps) |
| 14 | + |
| 15 | + |
| 16 | +class TestFactory(TestCase): |
| 17 | + |
| 18 | + def setUp(self): |
| 19 | + self.f = Factory() |
| 20 | + |
| 21 | + def test_BUILDER_with_invalid_action(self): |
| 22 | + self.assertRaises(UriInvalid, self.f, 'invalid') |
| 23 | + self.assertRaises(UriInvalid, self.f, 'invalid.') |
| 24 | + self.assertRaises(UriInvalid, self.f, '.invalid') |
| 25 | + |
| 26 | + def test_BUILDER_with_fake_action(self): |
| 27 | + self.assertRaises(DoesNotExists, self.f, 'users.fake') |
| 28 | + self.assertRaises(DoesNotExists, self.f, 'fake.users') |
| 29 | + |
| 30 | + def test_BUILDER_builds_users(self): |
| 31 | + """ Users.get as real test because it wouldn't be useful mock |
| 32 | + the import-jit process """ |
| 33 | + request = self.f('users.get') |
| 34 | + self.assertIsInstance(request, Request) |
| 35 | + |
| 36 | +class TestRequestUri(TestCase): |
| 37 | + |
| 38 | + def test_SIMPLE_with_correct_args(self): |
| 39 | + request = RequestWithArgs(arg1='arg1', arg2='arg2') |
| 40 | + self.assertEqual(str(request), 'URI/arg1/arg2') |
| 41 | + |
| 42 | + def test_SIMPLE_without_needed_args(self): |
| 43 | + request = RequestWithArgs() |
| 44 | + self.assertRaises(ValidationError, str, request) |
| 45 | + |
| 46 | + def test_with_cleaned_uri(self): |
| 47 | + """ Its real uri has args but I override `clean_uri` method, so |
| 48 | + if `nomatters` arg exists, change uri to `URI` """ |
| 49 | + request = RequestCleanedUri(notmatters='test') |
| 50 | + self.assertEqual(str(request), 'URI') |
| 51 | + |
| 52 | + |
| 53 | +class TestRequestBody(TestCase): |
| 54 | + |
| 55 | + def test_with_schema_with_valid(self): |
| 56 | + request = RequestBodyWithSchema(body=dict( |
| 57 | + arg1='only', fake='t', fake1='t')) |
| 58 | + self.assertEqual(request.get_body(), dict(arg1='only')) |
| 59 | + |
| 60 | + def test_with_schema_with_invalid(self): |
| 61 | + request = RequestBodyWithSchema(body='invalid_data') |
| 62 | + self.assertRaises(ValidationError, request.get_body) |
| 63 | + |
| 64 | + def test_with_schema_without_body(self): |
| 65 | + request = RequestBodyWithSchema() |
| 66 | + self.assertIsNone(request.get_body()) |
| 67 | + |
| 68 | + def test_without_schema(self): |
| 69 | + request = DummyRequest(body=dict(arg1='test')) |
| 70 | + self.assertEqual(request.get_body(), dict(arg1='test')) |
| 71 | + |
| 72 | + def test_without_schema_without_body(self): |
| 73 | + request = DummyRequest() |
| 74 | + self.assertIsNone(request.get_body()) |
| 75 | + |
| 76 | + def test_with_clean_body(self): |
| 77 | + self.assertRaises(ValidationError, RequestCleanedBody) |
| 78 | + |
| 79 | + |
| 80 | +class TestBodyParsers(TestCase): |
| 81 | + |
| 82 | + def setUp(self): |
| 83 | + self.b = Body( |
| 84 | + dict(arg1='arg1', arg2='arg2', arg3='arg3', arg4='arg4'), |
| 85 | + ('arg1', 'arg3', 'arg4')) |
| 86 | + |
| 87 | + def test_RETURN_only_valid_keys(self): |
| 88 | + get_body_returns = self.b.parse() |
| 89 | + self.assertEqual(get_body_returns, dict(arg1='arg1', arg3='arg3', |
| 90 | + arg4='arg4')) |
0 commit comments