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())
def test_read_only_properties(self): responses.add( responses.GET, "http://example.com/schema", json={"properties": {"user": {"$ref": "/user/schema#"}}} ) responses.add( responses.GET, "http://example.com/user/schema", json={ "type": "object", "additionalProperties": False, "properties": { "$uri": {"type": "string", "readOnly": True}, "name": {"type": "string", "readOnly": True}, "age": {"type": "integer"}, }, "links": [ {"rel": "self", "href": "/user/{id}", "method": "GET"}, {"rel": "update", "href": "/user/{id}", "method": "PATCH", "schema": {"$ref": "#"}}, ], }, ) client = Client("http://example.com") def request_callback(request): request_data = json.loads(request.body) self.assertEqual(1, len(request_data)) self.assertTrue("age" in request_data) self.assertTrue(request_data["age"] in (20, 21)) return 201, {}, json.dumps({"$uri": "/user/1", "age": request_data["age"]}) responses.add_callback( responses.PATCH, "http://example.com/user/1", callback=request_callback, content_type="application/json" ) user = client.User(1, name="Foo") with self.assertRaises(AttributeError): user.name = "Bar" user.age = 20 user.save() user.update(name="Bar", age=21) self.assertEqual(user.age, 21)