|
10 | 10 | from flask import Blueprint, make_response
|
11 | 11 | from marshmallow_jsonapi.flask import Schema, Relationship
|
12 | 12 | from marshmallow_jsonapi import fields
|
| 13 | +from marshmallow import ValidationError |
13 | 14 |
|
14 | 15 | from flask_rest_jsonapi import Api, ResourceList, ResourceDetail, ResourceRelationship, JsonApiException
|
15 | 16 | from flask_rest_jsonapi.pagination import add_pagination_links
|
16 |
| -from flask_rest_jsonapi.exceptions import RelationNotFound, InvalidSort, InvalidFilters |
| 17 | +from flask_rest_jsonapi.exceptions import RelationNotFound, InvalidSort, InvalidFilters, InvalidInclude, BadRequest |
17 | 18 | from flask_rest_jsonapi.querystring import QueryStringManager as QSManager
|
18 | 19 | from flask_rest_jsonapi.data_layers.alchemy import SqlalchemyDataLayer
|
19 | 20 | from flask_rest_jsonapi.data_layers.base import BaseDataLayer
|
20 | 21 | from flask_rest_jsonapi.data_layers.filtering.alchemy import Node
|
| 22 | +from flask_rest_jsonapi.schema import get_relationships |
21 | 23 | import flask_rest_jsonapi.decorators
|
| 24 | +import flask_rest_jsonapi.resource |
| 25 | +import flask_rest_jsonapi.schema |
22 | 26 |
|
23 | 27 |
|
24 | 28 | @pytest.fixture(scope="module")
|
@@ -364,13 +368,67 @@ def test_Node(person_model, person_schema, monkeypatch):
|
364 | 368 |
|
365 | 369 |
|
366 | 370 | def test_check_method_requirements(monkeypatch):
|
367 |
| - class Self(object): |
368 |
| - def __init__(self): |
369 |
| - pass |
370 |
| - request = type('request', (object,), dict(method=None)) |
| 371 | + self = type('self', (object,), dict()) |
| 372 | + request = type('request', (object,), dict(method='GET')) |
| 373 | + monkeypatch.setattr(flask_rest_jsonapi.decorators, 'request', request) |
| 374 | + with pytest.raises(Exception): |
| 375 | + flask_rest_jsonapi.\ |
| 376 | + decorators.check_method_requirements(lambda: 1)(self()) |
| 377 | + |
| 378 | + |
| 379 | +def test_json_api_exception(): |
| 380 | + JsonApiException(None, None, title='test', status='test') |
| 381 | + |
| 382 | + |
| 383 | +def test_query_string_manager(person_schema): |
| 384 | + query_string = {'page[slumber]': '3'} |
| 385 | + qsm = QSManager(query_string, person_schema) |
| 386 | + with pytest.raises(BadRequest): |
| 387 | + qsm.pagination |
| 388 | + qsm.qs['sort'] = 'computers' |
| 389 | + with pytest.raises(InvalidSort): |
| 390 | + qsm.sorting |
| 391 | + |
| 392 | + |
| 393 | +def test_resource(person_model, person_schema, session, monkeypatch): |
| 394 | + def schema_load_mock(*args): |
| 395 | + raise ValidationError(dict(errors=[dict(status=None, title=None)])) |
| 396 | + query_string = {'page[slumber]': '3'} |
| 397 | + app = type('app', (object,), dict(config=dict(DEBUG=True))) |
| 398 | + headers = {'Content-Type': 'application/vnd.api+json'} |
| 399 | + request = type('request', (object,), dict(method='POST', |
| 400 | + headers=headers, |
| 401 | + get_json=dict, |
| 402 | + args=query_string)) |
| 403 | + dl = SqlalchemyDataLayer(dict(session=session, model=person_model)) |
| 404 | + rl = ResourceList() |
| 405 | + rd = ResourceDetail() |
| 406 | + rl._data_layer = dl |
| 407 | + rl.schema = person_schema |
| 408 | + rd._data_layer = dl |
| 409 | + rd.schema = person_schema |
| 410 | + monkeypatch.setattr(flask_rest_jsonapi.resource, 'request', request) |
| 411 | + monkeypatch.setattr(flask_rest_jsonapi.resource, 'current_app', app) |
371 | 412 | monkeypatch.setattr(flask_rest_jsonapi.decorators, 'request', request)
|
| 413 | + monkeypatch.setattr(rl.schema, 'load', schema_load_mock) |
| 414 | + r = super(flask_rest_jsonapi.resource.Resource, ResourceList)\ |
| 415 | + .__new__(ResourceList) |
372 | 416 | with pytest.raises(Exception):
|
373 |
| - flask_rest_jsonapi.decorators.check_method_requirements(lambda: 1)(Self()) |
| 417 | + r.dispatch_request() |
| 418 | + rl.post() |
| 419 | + rd.patch() |
| 420 | + |
| 421 | + |
| 422 | +def test_compute_schema(person_schema): |
| 423 | + query_string = {'page[number]': '3', 'fields[person]': list()} |
| 424 | + qsm = QSManager(query_string, person_schema) |
| 425 | + with pytest.raises(InvalidInclude): |
| 426 | + flask_rest_jsonapi.schema.compute_schema( |
| 427 | + person_schema, dict(), qsm, ['id'] |
| 428 | + ) |
| 429 | + s = flask_rest_jsonapi.schema.compute_schema( |
| 430 | + person_schema, dict(only=list()), qsm, list() |
| 431 | + ) |
374 | 432 |
|
375 | 433 |
|
376 | 434 | # test good cases
|
|
0 commit comments