Пример #1
0
    def test_resource_init_by_properties(self):
        r = Resource(MagicMock(), properties={
            'href': 'test/resource',
            'createdAt': '2014-07-16T13:48:22.378Z',
            'modifiedAt': '2014-07-16T13:48:22.378+01:00',
            'name': 'Test Resource',
            'someProperty': 'value'
        })

        # it's not new (has href)
        self.assertFalse(r.is_new())
        # it knows what it is
        self.assertEqual(r.href, 'test/resource')
        # we can access the attributes
        self.assertEqual(r.name, 'Test Resource')
        # there is created_at attribute
        self.assertEqual(
            r.created_at,
            datetime(2014, 7, 16, 13, 48, 22, 378000, tzinfo=tzutc()))
        # there is modified_at attribute
        self.assertEqual(
            r.modified_at,
            datetime(
                2014, 7, 16, 13, 48, 22, 378000, tzinfo=tzoffset(None, 3600)))
        # attribute name was correctly converted
        self.assertEqual(r.some_property, 'value')
        # there are no writable attributes
        with self.assertRaises(AttributeError):
            r.name = 5
        with self.assertRaises(AttributeError):
            r.created_at = 'whatever'
        with self.assertRaises(AttributeError):
            r.modified_at = 'whatever'
Пример #2
0
 def test_getting_resource_property_names(self):
     r = Resource(MagicMock(),
                  properties={
                      'href': 'reshref',
                      '_some_property': 'should not show up'
                  })
     self.assertEqual(['href'], r._get_property_names())
Пример #3
0
    def test_resource_dir_method(self):
        r = Resource(
                MagicMock(),
                properties={'href': 'reshref', '_some_property': 'should not show up'})
        r._ensure_data = lambda : None

        self.assertEqual(['href'], r.__dir__())
Пример #4
0
 def test_resource_str_method(self):
     r = Resource(
             MagicMock(),
             properties={'href': 'reshref'})
     self.assertEqual('<Resource href=reshref>', r.__str__())
     r = Resource(
             MagicMock(),
             properties={'name': 'name'})
     self.assertEqual('name', r.__str__())
Пример #5
0
    def test_resource_dir_method(self):
        r = Resource(MagicMock(),
                     properties={
                         'href': 'reshref',
                         '_some_property': 'should not show up'
                     })
        r._ensure_data = lambda: None

        self.assertEqual(['href'], r.__dir__())
Пример #6
0
    def test_resource_init_by_href(self):
        r = Resource(MagicMock(), href='test/resource')

        # it's not new (has href)
        self.assertFalse(r.is_new())
        # it know what it is
        self.assertEqual(r.href, 'test/resource')
        # href is not writable
        with self.assertRaises(AttributeError):
            r.href = 'abc'
        # non-existing attribute access is handled correctly
        with self.assertRaises(AttributeError):
            r.foo
Пример #7
0
    def test_resource_init_by_href(self):
        r = Resource(MagicMock(), href='test/resource')

        # it's not new (has href)
        self.assertFalse(r.is_new())
        # it know what it is
        self.assertEqual(r.href, 'test/resource')
        # href is not writable
        with self.assertRaises(AttributeError):
            r.href = 'abc'
        # non-existing attribute access is handled correctly
        with self.assertRaises(AttributeError):
            r.foo
Пример #8
0
    def test_wrapping_resource_attrs(self):
        r = Resource(MagicMock(), properties={})
        to_wrap = Resource(MagicMock(), properties={})
        ret = r._wrap_resource_attr(Application, to_wrap)
        self.assertEqual(to_wrap, ret)

        ret = r._wrap_resource_attr(Application, {'name': 'some app'})
        self.assertEqual('some app', ret.name)
        self.assertTrue(isinstance(ret, Application))

        ret = r._wrap_resource_attr(Application, None)
        self.assertIsNone(ret)

        self.assertRaises(TypeError, r._wrap_resource_attr, Application, 'Unsupported Conversion')
    def test_resource_init_by_properties(self):
        r = Resource(MagicMock(), properties={
            'href': 'test/resource',
            'name': 'Test Resource',
            'someProperty': 'value'
        })

        # it's not new (has href)
        self.assertFalse(r.is_new())
        # it knows what it is
        self.assertEqual(r.href, 'test/resource')
        # we can access the attributes
        self.assertEqual(r.name, 'Test Resource')
        # attribute name was correctly converted
        self.assertEqual(r.some_property, 'value')
        # there are no writable attributes
        with self.assertRaises(AttributeError):
            r.name = 5
Пример #10
0
    def test_resource_materialization(self):
        ds = MagicMock()
        ds.get_resource.return_value = {
            'href': 'test/resource',
            'name': 'Test Resource'
        }

        r = Resource(MagicMock(data_store=ds), href='test/resource')
        name = r.name

        ds.get_resource.assert_called_once_with('test/resource', params=None)
        self.assertEqual(name, 'Test Resource')
Пример #11
0
    def test_resource_init_by_properties(self):
        r = Resource(MagicMock(),
                     properties={
                         'href': 'test/resource',
                         'name': 'Test Resource',
                         'someProperty': 'value'
                     })

        # we have the resource data
        self.assertTrue(r.is_materialized())
        # it's not new (has href)
        self.assertFalse(r.is_new())
        # it knows what it is
        self.assertEqual(r.href, 'test/resource')
        # we can access the attributes
        self.assertEqual(r.name, 'Test Resource')
        # attribute name was correctly converted
        self.assertEqual(r.some_property, 'value')
        # there are no writable attributes
        with self.assertRaises(AttributeError):
            r.name = 5
Пример #12
0
    def test_resource_init_with_partial_href(self, session):
        session.return_value.request.return_value = MagicMock(
            status_code=200,
            json=MagicMock(return_value={'name': 'My Application'}))

        self.client = Client(api_key={
            'id': 'MyId',
            'secret': 'Shush!'
        },
                             base_url='https://enterprise.stormpath.io/v1')
        r = Resource(self.client, href='/application/APP_UID')

        self.assertEqual(r.name, 'My Application')
        session.return_value.request.assert_called_once_with(
            'GET',
            'https://enterprise.stormpath.io/v1/application/APP_UID',
            params=None,
            allow_redirects=False,
            data=None)
Пример #13
0
    def test_wrapping_resource_attrs(self):
        r = Resource(MagicMock(), properties={})
        to_wrap = Resource(MagicMock(), properties={})
        ret = r._wrap_resource_attr(Application, to_wrap)
        self.assertEqual(to_wrap, ret)

        ret = r._wrap_resource_attr(Application, {'name': 'some app'})
        self.assertEqual('some app', ret.name)
        self.assertTrue(isinstance(ret, Application))

        ret = r._wrap_resource_attr(Application, None)
        self.assertIsNone(ret)

        self.assertRaises(TypeError, r._wrap_resource_attr, Application,
                          'Unsupported Conversion')
Пример #14
0
    def test_resource_init_by_properties(self):
        r = Resource(MagicMock(),
                     properties={
                         'href': 'test/resource',
                         'createdAt': '2014-07-16T13:48:22.378Z',
                         'modifiedAt': '2014-07-16T13:48:22.378+01:00',
                         'name': 'Test Resource',
                         'someProperty': 'value'
                     })

        # it's not new (has href)
        self.assertFalse(r.is_new())
        # it knows what it is
        self.assertEqual(r.href, 'test/resource')
        # we can access the attributes
        self.assertEqual(r.name, 'Test Resource')
        # there is created_at attribute
        self.assertEqual(
            r.created_at,
            datetime(2014, 7, 16, 13, 48, 22, 378000, tzinfo=tzutc()))
        # there is modified_at attribute
        self.assertEqual(
            r.modified_at,
            datetime(2014,
                     7,
                     16,
                     13,
                     48,
                     22,
                     378000,
                     tzinfo=tzoffset(None, 3600)))
        # attribute name was correctly converted
        self.assertEqual(r.some_property, 'value')
        # there are no writable attributes
        with self.assertRaises(AttributeError):
            r.name = 5
        with self.assertRaises(AttributeError):
            r.created_at = 'whatever'
        with self.assertRaises(AttributeError):
            r.modified_at = 'whatever'
Пример #15
0
 def test_getting_resource_property_names(self):
     r = Resource(
             MagicMock(),
             properties={'href': 'reshref', '_some_property': 'should not show up'})
     self.assertEqual(['href'], r._get_property_names())
Пример #16
0
 def test_sanitize_property(self):
     ret = Resource._sanitize_property({'full_name': 'full name'})
     self.assertEqual({'fullName': 'full name'}, ret)
Пример #17
0
 def test_sanitize_property(self):
     ret = Resource._sanitize_property({'full_name': 'full name'})
     self.assertEqual({'fullName': 'full name'}, ret)
Пример #18
0
 def test_resource_repr_method(self):
     r = Resource(MagicMock(), properties={'href': 'reshref'})
     self.assertEqual('<Resource href=reshref>', r.__repr__())