aboutsummaryrefslogtreecommitdiffstats
path: root/pygithub3/tests/resources/test_core.py
blob: d8b8541cc7f118f79a22c48a660c8190d3fdef8e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#!/usr/bin/env python
# -*- encoding: utf-8 -*-

from datetime import datetime

from pygithub3.tests.utils.core import TestCase
from pygithub3.resources.base import Raw
from pygithub3.tests.utils.resources import Nested, Simple, HasSimple

simple_resource = dict(type='simple')
has_simple = dict(type='has_simple', simple=simple_resource)
github_return = dict(
    id=1,
    name='name_test',
    date='2008-01-14T04:33:35Z',
    simple=simple_resource,
    list_collection=[has_simple] * 2,
    items_collections=dict(arg1=has_simple, arg2=has_simple)
)
github_return_nested = github_return.copy()
github_return.update({'self_nested': github_return_nested})
github_return.update({'self_nested_list': [github_return_nested] * 2})
github_return.update({'self_nested_dict': dict(arg1=github_return_nested)})


class TestResourceMapping(TestCase):

    def setUp(self):
        self.r = Nested.loads(github_return)

    def test_attrs_map(self):
        self.assertEqual(self.r.id, 1)
        self.assertEqual(self.r.name, 'name_test')
        self.assertEqual(self.r.date, datetime(2008, 1, 14, 4, 33, 35))

    def test_MAPS(self):
        self.assertIsInstance(self.r.simple, Simple)
        self.assertEqual(self.r.simple.type, 'simple')

    def test_LIST_collection_map(self):
        has_simple_objects = filter(lambda x: isinstance(x, HasSimple),
                                    self.r.list_collection)
        self.assertEqual(len(has_simple_objects), 2)
        self.assertEqual(self.r.list_collection[0].type, 'has_simple')
        self.assertEqual(self.r.list_collection[0].simple.type, 'simple')

    def test_DICT_collection_map(self):
        arg1_has_simple = self.r.items_collections['arg1']
        self.assertEqual(arg1_has_simple.type, 'has_simple')
        self.assertEqual(arg1_has_simple.simple.type, 'simple')

    def test_SELF_nested(self):
        self.assertIsInstance(self.r.self_nested, Nested)
        self.assertIsInstance(self.r.self_nested.simple, Simple)
        self.assertIsInstance(self.r.list_collection[0], HasSimple)
        self.assertIsInstance(self.r.items_collections['arg1'], HasSimple)

    def test_SELF_nested_in_collections(self):
        self.assertIsInstance(self.r.self_nested_list[0], Nested)
        self.assertIsInstance(self.r.self_nested_dict['arg1'], Nested)


class TestRawResource(TestCase):
    """ Litle obvious :P """

    def test_return_original_copy(self):
        self.r = Raw.loads(github_return)
        self.assertEqual(id(self.r), id(github_return))