def test_decode_instance(self):
        client = Client('http://example.com', fetch_schema=False)

        User = client.resource_factory('user', {
            "type": "object",
            "properties": {
                "$uri": {
                    "type": "string",
                    "readOnly": True
                },
                "name": {
                    "type": "string"
                }
            },
            "links": [
                {
                    "rel": "instances",
                    "method": "GET",
                    "href": "/user"
                }
            ]
        })

        result = json.loads(json.dumps({
            "$uri": "/user/123",
            "name": "foo"
        }), cls=PotionJSONDecoder, client=client)

        self.assertEqual(client.instance('/user/123'), result)
        self.assertEqual("foo", result.name)
        self.assertEqual(123, result.id)
    def test_instance_cache(self):
        responses.add(responses.GET, 'http://example.com/schema', json={
            "properties": {}
        })

        client = Client('http://example.com')

        foo_a = client.instance('/foo')
        foo_b = client.instance('/foo')
        self.assertIs(foo_a, foo_b)
    def test_fetch_instance(self):
        responses.add(responses.GET, 'http://example.com/api/schema', json={
            "properties": {
                "user": {"$ref": "/api/user/schema#"}
            }
        })

        responses.add(responses.GET, 'http://example.com/api/user/schema', json={
            "type": "object",
            "properties": {
                "name": {"type": "string"}
            },
            "links": [
                {
                    "rel": "self",
                    "href": "/api/user/{id}",
                    "method": "GET"
                }
            ]
        })

        responses.add(responses.GET, 'http://example.com/api/user/123', json={
            "$uri": "/api/user/123",
            "name": "foo"
        })

        client = Client('http://example.com/api')
        user = client.User.fetch(123)

        self.assertEqual({
            "$uri": "/api/user/123",
            "name": "foo"
        }, dict(user))

        self.assertEqual(client.instance('/api/user/123'), user)
        self.assertEqual("foo", user.name)
        self.assertEqual(123, user.id)

        self.assertEqual(user, client.User(123)._self())