|
12 | 12 | from marshmallow_jsonapi import fields
|
13 | 13 |
|
14 | 14 | from flask_rest_jsonapi import Api, ResourceList, ResourceDetail, ResourceRelationship, JsonApiException
|
| 15 | +from flask_rest_jsonapi.exceptions import RelationNotFound, InvalidSort |
15 | 16 | from flask_rest_jsonapi.querystring import QueryStringManager as QSManager
|
16 | 17 | from flask_rest_jsonapi.data_layers.alchemy import SqlalchemyDataLayer
|
17 | 18 | from flask_rest_jsonapi.data_layers.base import BaseDataLayer
|
@@ -304,6 +305,21 @@ def register_routes(client, app, api_blueprint, person_list, person_detail, pers
|
304 | 305 | api.init_app(app)
|
305 | 306 |
|
306 | 307 |
|
| 308 | +@pytest.fixture(scope="module") |
| 309 | +def get_object_mock(): |
| 310 | + class get_object(object): |
| 311 | + foo = type('foo', (object,), { |
| 312 | + 'property': type('prop', (object,), { |
| 313 | + 'mapper': type('map', (object,), { |
| 314 | + 'class_': 'test' |
| 315 | + })() |
| 316 | + })() |
| 317 | + })() |
| 318 | + def __init__(self, kwargs): |
| 319 | + pass |
| 320 | + return get_object |
| 321 | + |
| 322 | + |
307 | 323 | # test good cases
|
308 | 324 | def test_get_list(client, register_routes, person, person_2):
|
309 | 325 | with client:
|
@@ -760,42 +776,105 @@ def test_get_list_field_error(client, register_routes):
|
760 | 776 |
|
761 | 777 | def test_sqlalchemy_data_layer_without_session(person_model, person_list):
|
762 | 778 | with pytest.raises(Exception):
|
763 |
| - SqlalchemyDataLayer(model=person_model, resource=person_list) |
| 779 | + SqlalchemyDataLayer(dict(model=person_model, resource=person_list)) |
764 | 780 |
|
765 | 781 |
|
766 | 782 | def test_sqlalchemy_data_layer_without_model(session, person_list):
|
767 | 783 | with pytest.raises(Exception):
|
768 |
| - SqlalchemyDataLayer(session=session, resource=person_list) |
| 784 | + SqlalchemyDataLayer(dict(session=session, resource=person_list)) |
| 785 | + |
| 786 | + |
| 787 | +def test_sqlalchemy_data_layer_create_object_error(session, person_model, person_list): |
| 788 | + with pytest.raises(JsonApiException): |
| 789 | + dl = SqlalchemyDataLayer(dict(session=session, model=person_model, resource=person_list)) |
| 790 | + dl.create_object(dict(), dict()) |
769 | 791 |
|
770 | 792 |
|
771 | 793 | def test_sqlalchemy_data_layer_get_object_error(session, person_model):
|
772 | 794 | with pytest.raises(Exception):
|
773 |
| - dl = SqlalchemyDataLayer(session=session, model=person_model, id_field='error') |
774 |
| - dl.get_object(**dict()) |
| 795 | + dl = SqlalchemyDataLayer(dict(session=session, model=person_model, id_field='error')) |
| 796 | + dl.get_object(dict()) |
| 797 | + |
| 798 | + |
| 799 | +def test_sqlalchemy_data_layer_update_object_error(session, person_model, person_list, monkeypatch): |
| 800 | + def commit_mock(): |
| 801 | + raise JsonApiException() |
| 802 | + with pytest.raises(JsonApiException): |
| 803 | + dl = SqlalchemyDataLayer(dict(session=session, model=person_model, resource=person_list)) |
| 804 | + monkeypatch.setattr(dl.session, 'commit', commit_mock) |
| 805 | + dl.update_object(dict(), dict(), dict()) |
| 806 | + |
| 807 | + |
| 808 | +def test_sqlalchemy_data_layer_delete_object_error(session, person_model, person_list, monkeypatch): |
| 809 | + def commit_mock(): |
| 810 | + raise JsonApiException() |
| 811 | + def delete_mock(obj): |
| 812 | + pass |
| 813 | + with pytest.raises(JsonApiException): |
| 814 | + dl = SqlalchemyDataLayer(dict(session=session, model=person_model, resource=person_list)) |
| 815 | + monkeypatch.setattr(dl.session, 'commit', commit_mock) |
| 816 | + monkeypatch.setattr(dl.session, 'delete', delete_mock) |
| 817 | + dl.delete_object(dict(), dict()) |
775 | 818 |
|
776 | 819 |
|
777 | 820 | def test_sqlalchemy_data_layer_create_relationship_field_not_found(session, person_model):
|
778 | 821 | with pytest.raises(Exception):
|
779 |
| - dl = SqlalchemyDataLayer(session=session, model=person_model) |
780 |
| - dl.create_relationship(dict(), 'error', '', **{'id': 1}) |
| 822 | + dl = SqlalchemyDataLayer(dict(session=session, model=person_model)) |
| 823 | + dl.create_relationship(dict(), 'error', '', dict(id=1)) |
| 824 | + |
| 825 | + |
| 826 | +def test_sqlalchemy_data_layer_create_relationship_error(session, person_model, get_object_mock, monkeypatch): |
| 827 | + def commit_mock(): |
| 828 | + raise JsonApiException() |
| 829 | + with pytest.raises(JsonApiException): |
| 830 | + dl = SqlalchemyDataLayer(dict(session=session, model=person_model)) |
| 831 | + monkeypatch.setattr(dl.session, 'commit', commit_mock) |
| 832 | + monkeypatch.setattr(dl, 'get_object', get_object_mock) |
| 833 | + dl.create_relationship(dict(data=None), 'foo', '', dict(id=1)) |
781 | 834 |
|
782 | 835 |
|
783 | 836 | def test_sqlalchemy_data_layer_get_relationship_field_not_found(session, person_model):
|
784 |
| - with pytest.raises(Exception): |
785 |
| - dl = SqlalchemyDataLayer(session=session, model=person_model) |
786 |
| - dl.get_relationship('error', '', '', **{'id': 1}) |
| 837 | + with pytest.raises(RelationNotFound): |
| 838 | + dl = SqlalchemyDataLayer(dict(session=session, model=person_model)) |
| 839 | + dl.get_relationship('error', '', '', dict(id=1)) |
787 | 840 |
|
788 | 841 |
|
789 | 842 | def test_sqlalchemy_data_layer_update_relationship_field_not_found(session, person_model):
|
790 | 843 | with pytest.raises(Exception):
|
791 |
| - dl = SqlalchemyDataLayer(session=session, model=person_model) |
792 |
| - dl.update_relationship(dict(), 'error', '', **{'id': 1}) |
| 844 | + dl = SqlalchemyDataLayer(dict(session=session, model=person_model)) |
| 845 | + dl.update_relationship(dict(), 'error', '', dict(id=1)) |
| 846 | + |
| 847 | + |
| 848 | +def test_sqlalchemy_data_layer_update_relationship_error(session, person_model, get_object_mock, monkeypatch): |
| 849 | + def commit_mock(): |
| 850 | + raise JsonApiException() |
| 851 | + with pytest.raises(JsonApiException): |
| 852 | + dl = SqlalchemyDataLayer(dict(session=session, model=person_model)) |
| 853 | + monkeypatch.setattr(dl.session, 'commit', commit_mock) |
| 854 | + monkeypatch.setattr(dl, 'get_object', get_object_mock) |
| 855 | + dl.update_relationship(dict(data=None), 'foo', '', dict(id=1)) |
793 | 856 |
|
794 | 857 |
|
795 | 858 | def test_sqlalchemy_data_layer_delete_relationship_field_not_found(session, person_model):
|
796 | 859 | with pytest.raises(Exception):
|
797 |
| - dl = SqlalchemyDataLayer(session=session, model=person_model) |
798 |
| - dl.delete_relationship(dict(), 'error', '', **{'id': 1}) |
| 860 | + dl = SqlalchemyDataLayer(dict(session=session, model=person_model)) |
| 861 | + dl.delete_relationship(dict(), 'error', '', dict(id=1)) |
| 862 | + |
| 863 | + |
| 864 | +def test_sqlalchemy_data_layer_delete_relationship_error(session, person_model, get_object_mock, monkeypatch): |
| 865 | + def commit_mock(): |
| 866 | + raise JsonApiException() |
| 867 | + with pytest.raises(JsonApiException): |
| 868 | + dl = SqlalchemyDataLayer(dict(session=session, model=person_model)) |
| 869 | + monkeypatch.setattr(dl.session, 'commit', commit_mock) |
| 870 | + monkeypatch.setattr(dl, 'get_object', get_object_mock) |
| 871 | + dl.delete_relationship(dict(data=None), 'foo', '', dict(id=1)) |
| 872 | + |
| 873 | + |
| 874 | +def test_sqlalchemy_data_layer_sort_query_error(session, person_model, monkeypatch): |
| 875 | + with pytest.raises(InvalidSort): |
| 876 | + dl = SqlalchemyDataLayer(dict(session=session, model=person_model)) |
| 877 | + dl.sort_query(None, [dict(field='test')]) |
799 | 878 |
|
800 | 879 |
|
801 | 880 | def test_post_list_incorrect_type(client, register_routes, computer):
|
@@ -1277,6 +1356,44 @@ def test_base_data_layer():
|
1277 | 1356 | base_dl.update_relationship(None, None, None, dict())
|
1278 | 1357 | with pytest.raises(NotImplementedError):
|
1279 | 1358 | base_dl.delete_relationship(None, None, None, dict())
|
| 1359 | + with pytest.raises(NotImplementedError): |
| 1360 | + base_dl.query(dict()) |
| 1361 | + with pytest.raises(NotImplementedError): |
| 1362 | + base_dl.before_create_object(None, dict()) |
| 1363 | + with pytest.raises(NotImplementedError): |
| 1364 | + base_dl.after_create_object(None, None, dict()) |
| 1365 | + with pytest.raises(NotImplementedError): |
| 1366 | + base_dl.before_get_object(dict()) |
| 1367 | + with pytest.raises(NotImplementedError): |
| 1368 | + base_dl.after_get_object(None, dict()) |
| 1369 | + with pytest.raises(NotImplementedError): |
| 1370 | + base_dl.before_get_collection(None, dict()) |
| 1371 | + with pytest.raises(NotImplementedError): |
| 1372 | + base_dl.after_get_collection(None, None, dict()) |
| 1373 | + with pytest.raises(NotImplementedError): |
| 1374 | + base_dl.before_update_object(None, None, dict()) |
| 1375 | + with pytest.raises(NotImplementedError): |
| 1376 | + base_dl.after_update_object(None, None, dict()) |
| 1377 | + with pytest.raises(NotImplementedError): |
| 1378 | + base_dl.before_delete_object(None, dict()) |
| 1379 | + with pytest.raises(NotImplementedError): |
| 1380 | + base_dl.after_delete_object(None, dict()) |
| 1381 | + with pytest.raises(NotImplementedError): |
| 1382 | + base_dl.before_create_relationship(None, None, None, dict()) |
| 1383 | + with pytest.raises(NotImplementedError): |
| 1384 | + base_dl.after_create_relationship(None, None, None, None, None, dict()) |
| 1385 | + with pytest.raises(NotImplementedError): |
| 1386 | + base_dl.before_get_relationship(None, None, None, dict()) |
| 1387 | + with pytest.raises(NotImplementedError): |
| 1388 | + base_dl.after_get_relationship(None, None, None, None, None, dict()) |
| 1389 | + with pytest.raises(NotImplementedError): |
| 1390 | + base_dl.before_update_relationship(None, None, None, dict()) |
| 1391 | + with pytest.raises(NotImplementedError): |
| 1392 | + base_dl.after_update_relationship(None, None, None, None, None, dict()) |
| 1393 | + with pytest.raises(NotImplementedError): |
| 1394 | + base_dl.before_delete_relationship(None, None, None, dict()) |
| 1395 | + with pytest.raises(NotImplementedError): |
| 1396 | + base_dl.after_delete_relationship(None, None, None, None, None, dict()) |
1280 | 1397 |
|
1281 | 1398 |
|
1282 | 1399 | def test_qs_manager():
|
|
0 commit comments