def test_make_request(self): action = SirenAction('action', 'http://blah.com', 'application/json') mck = mock.Mock(send=mock.Mock(return_value=True)) resp = action.make_request(_session=mck, x=1, y=2) self.assertTrue(resp) self.assertEqual(mck.send.call_count, 1) self.assertIsInstance(mck.send.call_args[0][0], PreparedRequest)
def test_add_field(self): action = SirenAction('action', 'blah', 'application/json') self.assertEqual(action.fields, []) action.add_field('field') self.assertEqual(len(action.fields), 1) self.assertDictEqual(action.fields[0], dict(name='field', type=None, value=None))
def test_as_request_get(self): action = SirenAction('action', 'http://blah.com', 'application/json') resp = action.as_request(x=1, y=2) self.assertIsInstance(resp, PreparedRequest) self.assertEqual(resp.method, 'GET') self.assertIn('y=2', resp.path_url) self.assertIn('x=1', resp.path_url)
def test_get_bound_href(self): action = SirenAction('action', 'blah', 'application/json') bound_href, request_fields = action._get_bound_href(TemplatedString, x=1, y=2) self.assertEqual(bound_href, 'blah') self.assertDictEqual(request_fields, dict(x=1, y=2))
def test_get_fields_dict(self): action = SirenAction( 'action', 'blah', 'application/json', fields=[dict(name='field', type=None, value='whocares')]) field_dict = action.get_fields_as_dict() self.assertDictEqual(dict(field='whocares'), field_dict)
def test_get_bound_href_with_template(self): action = SirenAction('action', 'http://host.com/{id}/{id}', 'application/json') bound_href, request_fields = action._get_bound_href(TemplatedString, x=1, y=2, id=3) self.assertEqual(bound_href, 'http://host.com/3/3') self.assertDictEqual(dict(x=1, y=2), request_fields)
def test_as_request_delete(self): action = SirenAction('action', 'http://blah.com', 'application/json', method='DELETE') resp = action.as_request(x=1, y=2) self.assertIsInstance(resp, PreparedRequest) self.assertEqual(resp.method, 'DELETE') self.assertEqual('/', resp.path_url)
def test_as_python_object(): """Integration test of using hard-coded siren to create the hypermedia rest-client. This will attempt to contact the url.""" base_url = 'http://127.0.0.1:5000/codex/views' classnames = ['view'] properties = { 'view_id': '1', 'url': 'http://slashdot.org', 'time_fetched': '1409067477' } actions = [ SirenAction(name='update-view', href='{}/1/'.format(base_url), type='application/json', fields=[ { 'type': 'text', 'name': 'url' }, { "type": "text", "name": "time_fetched" }, ], title=None, method='PUT'), SirenAction(name='create-view', href=base_url, type='application/json', fields=[ { 'type': 'text', 'name': 'url' }, { "type": "text", "name": "time_fetched" }, ], title=None, method='POST'), ] links = [SirenLink(rel=['self'], href='http://127.0.0.1/views/1')] so = SirenEntity(classnames=classnames, properties=properties, actions=actions, links=links) view = so.as_python_object() assert type(view).__name__ == 'view' assert view.view_id == '1' assert view.url == 'http://slashdot.org' assert view.time_fetched == '1409067477' view.update_view(url='blank', time_fetched='2014-08-26 14:05:26', body='<html>TEST</html>')
def test_as_siren(self): action = SirenAction('action', 'blah', 'application/json') siren_action = action.as_siren() expected = { 'href': u'blah', 'name': u'action', 'title': u'action', 'fields': [], 'type': u'application/json', 'method': u'GET' } self.assertDictEqual(siren_action, expected)
def test_get_bound_href_unboud_variables(self): action = SirenAction('action', 'http://host.com/{id}/{id}', 'application/json') self.assertRaises(ValueError, action._get_bound_href, TemplatedString, x=1, y=2)
def test_as_json(self): action = SirenAction('action', 'blah', 'application/json') siren_action = action.as_json() self.assertIsInstance(siren_action, six.string_types)
def test_as_siren(self): action = SirenAction('action', 'blah', 'application/json') siren_action = action.as_siren() expected = {'href': u'blah', 'name': u'action', 'title': u'action', 'fields': [], 'type': u'application/json', 'method': u'GET'} self.assertDictEqual(siren_action, expected)
def test_get_fields_dict(self): action = SirenAction('action', 'blah', 'application/json', fields=[dict(name='field', type=None, value='whocares')]) field_dict = action.get_fields_as_dict() self.assertDictEqual(dict(field='whocares'), field_dict)