Example #1
0
 def test_from_api_response(self):
     """
     Tests for a requests.Response object.
     """
     entity = {'class': ['blah']}
     resp = Response()
     resp.status_code = 200
     resp._content = six.binary_type(json.dumps(entity).encode('utf8'))
     builder = SirenBuilder()
     siren = builder.from_api_response(resp)
     self.assertIsInstance(siren, SirenEntity)
Example #2
0
 def test_from_api_response(self):
     """
     Tests for a requests.Response object.
     """
     entity = {'class': ['blah']}
     resp = Response()
     resp.status_code = 200
     resp._content = six.binary_type(json.dumps(entity).encode('utf8'))
     builder = SirenBuilder()
     siren = builder.from_api_response(resp)
     self.assertIsInstance(siren, SirenEntity)
Example #3
0
 def test_construct_link_bad(self):
     """
     Tests constructing a link.
     """
     builder = SirenBuilder()
     self.assertRaises(KeyError, builder._construct_link,
                       dict(rel=['blah']))
Example #4
0
 def test_construct_entity_missing_non_essential(self):
     """Tests that non-essential pieces are ignored."""
     entity = {'class': ['blah']}
     builder = SirenBuilder()
     resp = builder._construct_entity(entity)
     self.assertIsInstance(resp, SirenEntity)
Example #5
0
 def test_construct_link(self):
     builder = SirenBuilder()
     link = builder._construct_link(dict(rel=['rel'], href='whocares'))
     self.assertIsInstance(link, SirenLink)
Example #6
0
 def test_from_api_response_bad_type(self):
     builder = SirenBuilder()
     self.assertRaises(TypeError, builder.from_api_response, [])
Example #7
0
 def test_bad_text_from_api_response(self):
     builder = SirenBuilder()
     self.assertRaises(MalformedSirenError, builder.from_api_response,
                       'asdfgsjdfg')
Example #8
0
 def test_construct_entity_missing_non_essential(self):
     """Tests that non-essential pieces are ignored."""
     entity = {'class': ['blah']}
     builder = SirenBuilder()
     resp = builder._construct_entity(entity)
     self.assertIsInstance(resp, SirenEntity)
Example #9
0
 def test_construct_entity_missing_class(self):
     entity = dict(properties={}, actions=[], links=[], entities=[])
     builder = SirenBuilder()
     self.assertRaises(KeyError, builder._construct_entity, entity)
Example #10
0
 def test_construct_link(self):
     builder = SirenBuilder()
     link = builder._construct_link(dict(rel=['rel'], href='whocares'))
     self.assertIsInstance(link, SirenLink)
Example #11
0
def test_from_api_response():
    """Test the construction of objects from a json string."""
    json = """
    {
      "links": [
        {
          "href": "/resources?url=http://slashdot.org",
          "rel": [
            "self", "double-self"
          ]
        },
        {
          "href": "/views/5e88ecafcfe0520766cede7ef76dc16b2d869f5f6ce37141fde4224780a839c5dac26014336a22fabcf475873a5d254245e954fd9646e84c8e6ab087934eb873",
          "rel": [
            "view"
          ]
        }
      ],
      "class": [
        "Resource"
      ],
      "actions": [
        {
          "name": "get_with_url",
          "title": "get resource with url",
          "fields": [
            {
              "type": "text",
              "name": "url"
            }
          ],
          "href": "/resources",
          "type": "application/json",
          "method": "GET"
        }
      ],
      "properties": {
        "url": "http://slashdot.org",
        "time_fetched": 1409067477,
        "view_id": "5e88ecafcfe0520766cede7ef76dc16b2d869f5f6ce37141fde4224780a839c5dac26014336a22fabcf475873a5d254245e954fd9646e84c8e6ab087934eb873",
        "body_hash": "a3bf7ed65f40731cc33eb806476f3810883fc609b7dab609802fc844aaf06a4ec1836c0b21969acabe3c66b7d5dbd75fa664efad355eaf67d1055aa388f8b989"
      }
    }"""

    sb = SirenBuilder()
    sb.verify = False
    sb.request_factory = None
    so = sb.from_api_response(json)
    assert 'Resource' in so.classnames
    assert so.properties['url']
    assert so.properties['time_fetched']
    assert so.properties['view_id']
    assert so.properties['body_hash']

    links = so.links
    assert links and len(links) == 2
    assert so.get_links('self').href == '/resources?url=http://slashdot.org'
    assert so.get_links('double-self').href == '/resources?url=http://slashdot.org'
    assert so.get_links('view').href == '/views/5e88ecafcfe0520766cede7ef76dc16b2d869f5f6ce37141fde4224780a839c5dac26014336a22fabcf475873a5d254245e954fd9646e84c8e6ab087934eb873'

    actions = so.actions
    assert actions and len(actions) == 1

    action = actions[0]
    assert action.name
    assert action.href
    assert action.type
    assert action.method

    fields = action.fields
    assert fields and len(fields) == 1

    for f in fields:
        assert f['name']
        assert f['type']
def test_from_api_response():
    """Test the construction of objects from a json string."""
    json = """
    {
      "links": [
        {
          "href": "/resources?url=http://slashdot.org",
          "rel": [
            "self", "double-self"
          ]
        },
        {
          "href": "/views/5e88ecafcfe0520766cede7ef76dc16b2d869f5f6ce37141fde4224780a839c5dac26014336a22fabcf475873a5d254245e954fd9646e84c8e6ab087934eb873",
          "rel": [
            "view"
          ]
        }
      ],
      "class": [
        "Resource"
      ],
      "actions": [
        {
          "name": "get_with_url",
          "title": "get resource with url",
          "fields": [
            {
              "type": "text",
              "name": "url"
            }
          ],
          "href": "/resources",
          "type": "application/json",
          "method": "GET"
        }
      ],
      "properties": {
        "url": "http://slashdot.org",
        "time_fetched": 1409067477,
        "view_id": "5e88ecafcfe0520766cede7ef76dc16b2d869f5f6ce37141fde4224780a839c5dac26014336a22fabcf475873a5d254245e954fd9646e84c8e6ab087934eb873",
        "body_hash": "a3bf7ed65f40731cc33eb806476f3810883fc609b7dab609802fc844aaf06a4ec1836c0b21969acabe3c66b7d5dbd75fa664efad355eaf67d1055aa388f8b989"
      }
    }"""

    sb = SirenBuilder()
    sb.verify = False
    sb.request_factory = None
    so = sb.from_api_response(json)
    assert 'Resource' in so.classnames
    assert so.properties['url']
    assert so.properties['time_fetched']
    assert so.properties['view_id']
    assert so.properties['body_hash']

    links = so.links
    assert links and len(links) == 2
    assert so.get_links('self').href == '/resources?url=http://slashdot.org'
    assert so.get_links(
        'double-self').href == '/resources?url=http://slashdot.org'
    assert so.get_links(
        'view'
    ).href == '/views/5e88ecafcfe0520766cede7ef76dc16b2d869f5f6ce37141fde4224780a839c5dac26014336a22fabcf475873a5d254245e954fd9646e84c8e6ab087934eb873'

    actions = so.actions
    assert actions and len(actions) == 1

    action = actions[0]
    assert action.name
    assert action.href
    assert action.type
    assert action.method

    fields = action.fields
    assert fields and len(fields) == 1

    for f in fields:
        assert f['name']
        assert f['type']