Beispiel #1
0
 def test_unknown_resource_equality(self, reqmock):
     reqmock.get('https://api/foo/abc123', status_code=200,
                 text='{"id": "abc123", "foo": "bar"}')
     href = 'https://api/foo/abc123'
     r1 = Reference(href)
     r2 = Reference(href)
     assert r1 == r2
Beispiel #2
0
    def test_reference_save(self, reqmock):
        reqmock.get('https://api/foos/abc123',
                    status_code=200,
                    text='{"id": "abc123", "server_generated": "foo"}')
        r = Reference('https://api/foos/abc123')
        assert r.id == 'abc123'
        assert r._id == 'abc123'

        # Check to ensure Class attribute is not touched.
        assert Reference._id is None
        assert Reference._href is None
        assert Reference._resolved is False

        # check that _id and _href are not updated when simply calling an
        # attribute.
        reqmock.put('https://api/foos/def123',
                    status_code=200,
                    text='{"id": "def123", "server_generated": "bar"}')
        r.id = 'def123'
        r.server_generated
        assert r._id == 'abc123'
        assert Reference._id is None
        assert Reference._href is None
        assert Reference._resolved is False

        r.save()
        assert r.id == r._id
        assert r._href == 'https://api/foos/def123'
        assert Reference._id is None
        assert Reference._href is None
        assert Reference._resolved is False
Beispiel #3
0
    def test_reference_retrieve(self, reqmock):
        reqmock.get('https://api/foos/abc123', status_code=200,
                    text='{"id": "abc123", "server_generated": "foo"}')
        r = Reference('https://api/foos/abc123')
        assert r.id == 'abc123'
        assert r._id == 'abc123'

        # The retrieve method should not result in a change to the _id or _href
        # attributes
        reqmock.get('https://api/foos/def123', status_code=200,
                    text='{"id": "def123", "server_generated": "bar"}')
        r.retrieve('def123')
        assert r._id == 'abc123'
        assert r._href == 'https://api/foos/abc123'
Beispiel #4
0
    def test_simple(self):
        d = datetime.today()
        s = AnalyzeReObject(num=1,
                            str='foo',
                            date=d,
                            list=[1, 2, 3],
                            dict={
                                'foo': 'bar'
                            },
                            ref=Reference('https://api/layers/abc123'),
                            res=Resource(id='abc123', foo='bar')).to_dict()

        assert s == {
            'num': 1,
            'str': 'foo',
            'date': d,
            'list': [1, 2, 3],
            'dict': {
                'foo': 'bar'
            },
            'ref': {
                'ref_id': 'abc123'
            },
            'res': {
                'ref_id': 'abc123'
            }
        }
Beispiel #5
0
 def test_deepcopy_unresolved(self, reqmock):
     reqmock.get('https://api/foos/abc123', status_code=200,
                 text='{"id": "abc123"}')
     a = Reference('https://api/foos/abc123')
     b = copy.deepcopy(a)
     assert b._id == 'abc123'
     assert reqmock.call_count == 0
Beispiel #6
0
 def test_copy_resolved(self, reqmock):
     reqmock.get('https://api/foos/abc123', status_code=200,
                 text='{"id": "abc123", "list": [1, 2, 3]}')
     a = Reference('https://api/foos/abc123')
     assert a.list == [1, 2, 3]  # Forces evaluation
     b = copy.copy(a)
     assert b.list is a.list  # Assert copy was shallow
     assert reqmock.call_count == 1
Beispiel #7
0
 def test_unknown_resource(self, reqmock):
     reqmock.get('https://api/foo/abc123', status_code=200,
                 text='{"id": "abc123", "foo": "bar"}')
     href = 'https://api/foo/abc123'
     r = Reference(href)
     assert r._id == 'abc123'
     assert r.foo == 'bar'
     assert isinstance(r, Resource)
Beispiel #8
0
    def test_known_resource(self, reqmock):
        reqmock.get('https://api/layers/abc123', status_code=200,
                    text='{"id": "abc123", "foo": "bar"}')
        href = 'https://api/layers/abc123'
        r = Reference(href)

        # Test laziness
        assert r._id == 'abc123'
        assert reqmock.call_count == 0

        # Force evaluation
        assert r.id == 'abc123'
        assert r.foo == 'bar'
        assert reqmock.call_count == 1
        assert isinstance(r, Layer)
Beispiel #9
0
 def test_nested(self):
     d = datetime.today()
     num = AnalyzeReObject(num=1)
     str_ = AnalyzeReObject(str='foo')
     date = AnalyzeReObject(date=d)
     list_ = AnalyzeReObject(list=[1, 2, 3])
     dict_ = AnalyzeReObject(dict={'foo': 'bar'})
     ref = AnalyzeReObject(ref=Reference('https://api/layers/abc123'))
     res = AnalyzeReObject(res=Resource(id='abc123', foo='bar'))
     s = AnalyzeReObject(num=num,
                         str=str_,
                         date=date,
                         list=list_,
                         dict=dict_,
                         ref=ref,
                         res=res).to_dict()
     assert s == {
         'num': {
             'num': 1
         },
         'str': {
             'str': 'foo'
         },
         'date': {
             'date': d
         },
         'list': {
             'list': [1, 2, 3]
         },
         'dict': {
             'dict': {
                 'foo': 'bar'
             }
         },
         'ref': {
             'ref': {
                 'ref_id': 'abc123'
             }
         },
         'res': {
             'res': {
                 'ref_id': 'abc123'
             }
         }
     }