|
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 | | -from flask_rest_jsonapi.exceptions import RelationNotFound, InvalidSort |
| 16 | +from flask_rest_jsonapi.pagination import add_pagination_links |
| 17 | +from flask_rest_jsonapi.exceptions import RelationNotFound, InvalidSort, InvalidFilters, InvalidInclude, BadRequest |
16 | 18 | from flask_rest_jsonapi.querystring import QueryStringManager as QSManager |
17 | 19 | from flask_rest_jsonapi.data_layers.alchemy import SqlalchemyDataLayer |
18 | 20 | from flask_rest_jsonapi.data_layers.base import BaseDataLayer |
| 21 | +from flask_rest_jsonapi.data_layers.filtering.alchemy import Node |
| 22 | +from flask_rest_jsonapi.schema import get_relationships |
| 23 | +import flask_rest_jsonapi.decorators |
| 24 | +import flask_rest_jsonapi.resource |
| 25 | +import flask_rest_jsonapi.schema |
19 | 26 |
|
20 | 27 |
|
21 | 28 | @pytest.fixture(scope="module") |
@@ -315,11 +322,115 @@ class get_object(object): |
315 | 322 | })() |
316 | 323 | })() |
317 | 324 | })() |
| 325 | + |
318 | 326 | def __init__(self, kwargs): |
319 | 327 | pass |
320 | 328 | return get_object |
321 | 329 |
|
322 | 330 |
|
| 331 | +def test_add_pagination_links(): |
| 332 | + qs = {'page[number]': '15', 'page[size]': '10'} |
| 333 | + qsm = QSManager(qs, None) |
| 334 | + add_pagination_links(dict(), 1000, qsm, str()) |
| 335 | + |
| 336 | + |
| 337 | +def test_Node(person_model, person_schema, monkeypatch): |
| 338 | + from copy import deepcopy |
| 339 | + filt = { |
| 340 | + 'val': '0000', |
| 341 | + 'field': True, |
| 342 | + 'not': dict(), |
| 343 | + 'name': 'name', |
| 344 | + 'op': 'eq', |
| 345 | + 'strip': lambda: 's' |
| 346 | + } |
| 347 | + filt['not'] = deepcopy(filt) |
| 348 | + del filt['not']['not'] |
| 349 | + n = Node(person_model, |
| 350 | + filt, |
| 351 | + None, |
| 352 | + person_schema) |
| 353 | + with pytest.raises(TypeError): |
| 354 | + # print(n.val is None and n.field is None) |
| 355 | + # # n.column |
| 356 | + n.resolve() |
| 357 | + with pytest.raises(AttributeError): |
| 358 | + n.model = None |
| 359 | + n.column |
| 360 | + with pytest.raises(InvalidFilters): |
| 361 | + n.model = person_model |
| 362 | + n.filter_['op'] = '' |
| 363 | + n.operator |
| 364 | + with pytest.raises(InvalidFilters): |
| 365 | + n.related_model |
| 366 | + with pytest.raises(InvalidFilters): |
| 367 | + n.related_schema |
| 368 | + |
| 369 | + |
| 370 | +def test_check_method_requirements(monkeypatch): |
| 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) |
| 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) |
| 416 | + with pytest.raises(Exception): |
| 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 | + ) |
| 432 | + |
| 433 | + |
323 | 434 | # test good cases |
324 | 435 | def test_get_list(client, register_routes, person, person_2): |
325 | 436 | with client: |
|
0 commit comments