Beispiel #1
0
    def test_get_batch(self):
        root_resource = self.create_root_resource_with_children(
            r'^api/v2/(?P<base_resource>.*)$',
            methods=['GET'],
            result={'name': 'value'})
        request_data = {
            "data": [
                self._generate_batch_partial(
                    'get', 'http://localhost:8081/api/v2/child/grandchild',
                    {'business_id': 12345})
            ]
        }
        response = savory_dispatch_batch(root_resource,
                                         full_host='localhost:8081',
                                         method='POST',
                                         body=json.dumps(request_data))
        self.assertEqual(response.status_code, 200)

        response_json = json.loads(response.content)
        data = response_json['data']

        self.assertEqual(len(data), 1)
        self.assertEqual(data[0]['status'], 200)
        self.assertEqual(data[0]['uri'],
                         'http://localhost:8081/api/v2/child/grandchild')
        self.assertEqual(data[0]['data'], {u'name': u'value'})

        ctx = mock_context()
        ctx.formatter = JSONFormatter()

        self.assertEqual(data[0]['etag'], get_sha1(ctx, {u'name': u'value'}))
Beispiel #2
0
    def test_mutable_parameters(self):
        dct = {'a': 'http://one/two/three/four'}
        ctx = mock_context()
        ctx.formatter = JSONFormatter()

        get_sha1(ctx, dct)
        # Make sure that the dct that we pass in is the same dict that gets returned
        self.assertEqual(dct, {'a': 'http://one/two/three/four'})
 def test_haystack_field(self):
     FooResource = mock.Mock()
     FooResource.return_value = frv = mock.Mock()
     frv.get.return_value = {'a': 'b'}
     api = HaystackField(
         formatter=JSONFormatter(),
         resource=FooResource)
     self.assertEqual(api.prepare(None), '{"a": "b"}')
Beispiel #4
0
def mock_context():
    @contextlib.contextmanager
    def target(*args):
        ctx.push(*args)
        yield
        ctx.pop()

    ctx = Mock(name='context', spec=['push', 'pop', 'peek'])
    ctx.formatter = JSONFormatter()
    ctx.build_resource_uri = lambda resource: 'uri://' + resource.resource_path
    ctx.target = target
    return ctx
Beispiel #5
0
def compute_context(resource_path, request, root_resource):
    full_path = _strip_query_string(request.get_full_path())
    if len(resource_path) == 0:
        base_path = full_path
    else:
        base_path = full_path[:-len(resource_path)]

    ctx = APIContext(base_uri=request.build_absolute_uri(base_path),
                     root_resource=root_resource,
                     formatter=JSONFormatter(),
                     request=request)

    return ctx
Beispiel #6
0
 def test_parameter_data_types(self):
     # get_param_value should assume unparsable data remains a string
     ctx = mock_context()
     ctx.formatter = JSONFormatter()
     foofilter = filters.ParameterizedFilter('foo', 'bar')
     params = Params({'bar': 'unparsable'})
     values = foofilter.get_param_values('bar', ctx, params)
     self.assertEquals(1, len(values))
     self.assertEqual(set(['unparsable']), set(values))
     # parsable data should be parsed as a correct type
     now = datetime.datetime.now(tz=pytz.UTC).replace(microsecond=0)
     for value, svalue in [(11, '11'), (3.14159, '3.14159'),
                           (now, now.isoformat("T"))]:
         params = Params({'bar': svalue})
         othervalues = foofilter.get_param_values('bar', ctx, params)
         self.assertEqual(set([value]), set(othervalues))
         self.assertEqual(type(value), type(othervalues[0]))
Beispiel #7
0
 def test_has_dictionary(self):
     dct = {'a': 'b', 'c': 'd'}
     ctx = mock_context()
     ctx.formatter = JSONFormatter()
     self.assertEqual(get_sha1(ctx, dct),
                      '855e751b12bf88bce273d5e1d93a31af9e4945d6')