From 46d535027b5247a9f1b242b4944e2ee8d65b4fad Mon Sep 17 00:00:00 2001 From: David Medina Date: Fri, 10 Feb 2012 00:59:01 +0100 Subject: Resources tests resources.core * Also detected and fixed memory bug --- pygithub3/resources/base.py | 13 +++++--- pygithub3/tests/resources/__init__.py | 0 pygithub3/tests/resources/test_core.py | 54 ++++++++++++++++++++++++++++++++++ pygithub3/tests/utils/resources.py | 21 +++++++++++++ 4 files changed, 84 insertions(+), 4 deletions(-) create mode 100644 pygithub3/tests/resources/__init__.py create mode 100644 pygithub3/tests/resources/test_core.py create mode 100644 pygithub3/tests/utils/resources.py (limited to 'pygithub3') diff --git a/pygithub3/resources/base.py b/pygithub3/resources/base.py index ae038bd..dfa285f 100644 --- a/pygithub3/resources/base.py +++ b/pygithub3/resources/base.py @@ -22,6 +22,9 @@ class Resource(object): for attr in self._attrs: setattr(self, attr, self._attrs[attr]) + def __str__(self): + return "<%s>" % self.__class__.__name__ + def __repr__(self): return self.__str__() @@ -59,19 +62,21 @@ class Resource(object): elif hasattr(raw_resources, '__iter__'): return [resource.__load(raw_resource) for raw_resource in raw_resources] - raw_resource.update( + + new_resource = raw_resource.copy() + new_resource.update( {attr: parse_date(raw_resource[attr]) for attr in self._dates if attr in raw_resource}) - raw_resource.update( + new_resource.update( {attr: parse_map(resource, raw_resource[attr]) for attr, resource in self._maps.items() if attr in raw_resource}) - raw_resource.update( + new_resource.update( {attr: parse_collection_map(resource, raw_resource[attr]) for attr, resource in self._collection_maps.items() if attr in raw_resource}) - return self(raw_resource) + return self(new_resource) class Raw(Resource): diff --git a/pygithub3/tests/resources/__init__.py b/pygithub3/tests/resources/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/pygithub3/tests/resources/test_core.py b/pygithub3/tests/resources/test_core.py new file mode 100644 index 0000000..276c92f --- /dev/null +++ b/pygithub3/tests/resources/test_core.py @@ -0,0 +1,54 @@ +#!/usr/bin/env python +# -*- encoding: utf-8 -*- + +from unittest import TestCase +from datetime import datetime + +from pygithub3.resources.base import Resource, Raw, json +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) + ) + + +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') + + +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)) diff --git a/pygithub3/tests/utils/resources.py b/pygithub3/tests/utils/resources.py new file mode 100644 index 0000000..364cc6f --- /dev/null +++ b/pygithub3/tests/utils/resources.py @@ -0,0 +1,21 @@ +#!/usr/bin/env python +# -*- encoding: utf-8 -*- + +from pygithub3.resources.base import Resource + + +class Simple(Resource): + pass + + +class HasSimple(Resource): + _maps = {'simple': Simple} + + +class Nested(Resource): + _dates = ('date', ) + _maps = {'simple': Simple} + _collection_maps = { + 'list_collection': HasSimple, + 'items_collections': HasSimple + } -- cgit v1.2.3-59-g8ed1b