Example #1
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']