|
2 | 2 | from datetime import datetime, timezone, timedelta |
3 | 3 | from decimal import Decimal |
4 | 4 | from pprint import pprint |
5 | | -from typing import Generator |
| 5 | +from typing import Generator, MutableMapping, Any |
6 | 6 |
|
7 | 7 | import pytest |
8 | 8 | from assertpy import assert_that |
@@ -45,107 +45,109 @@ def test_sample_jsonb_orm_create(delete_all_fixture: None, test_client: TestClie |
45 | 45 | assert_created_response_entity(response_entity, request_entity) |
46 | 46 |
|
47 | 47 |
|
48 | | -# @pytest.mark.parametrize( |
49 | | -# "raise_server_exceptions, expected_status", |
50 | | -# [(False, 409)], |
51 | | -# indirect=["raise_server_exceptions"], |
52 | | -# ) |
53 | | -# def test_sample_jsonb_orm_create_with_duplicate_json_id_returns_409( |
54 | | -# raise_server_exceptions: bool, |
55 | | -# delete_all_fixture: None, |
56 | | -# test_client: TestClient, |
57 | | -# expected_status: int, |
58 | | -# captured_logs: list[MutableMapping[str, Any]], |
59 | | -# ): |
60 | | -# sample = create_random_sample(test_client) |
61 | | -# |
62 | | -# request_entity: SampleDocumentCreate = SampleDocumentCreate( |
63 | | -# sample_id=sample.id, |
64 | | -# json_data=DocumentMetadata(id=uuid.uuid4(), tags=tuple(["tag1", "tag2"])), |
65 | | -# secondary_json_dict={"key": "value"}, |
66 | | -# ) |
67 | | -# http_response = test_client.put( |
68 | | -# SAMPLE_JSONB_SQL_ENDPOINT, json=jsonable_encoder(request_entity) |
69 | | -# ) |
70 | | -# assert_that(http_response.status_code).is_equal_to(200) |
71 | | -# request_entity_with_duplicate_id: SampleDocumentCreate = SampleDocumentCreate( |
72 | | -# sample_id=sample.id, |
73 | | -# json_data=DocumentMetadata( |
74 | | -# id=request_entity.json_data.id, tags=tuple(["tag1", "tag2"]) |
75 | | -# ), |
76 | | -# secondary_json_dict={"key": "value"}, |
77 | | -# ) |
78 | | -# http_response_with_duplicate_id = test_client.put( |
79 | | -# SAMPLE_JSONB_SQL_ENDPOINT, |
80 | | -# json=jsonable_encoder(request_entity_with_duplicate_id), |
81 | | -# ) |
82 | | -# assert_that(http_response_with_duplicate_id.status_code).is_equal_to( |
83 | | -# expected_status |
84 | | -# ) |
85 | | -# expected_body: dict[str, Any] = { |
86 | | -# "detail": {"key": "$.json_id.id", "value": str(request_entity.json_data.id)} |
87 | | -# } |
88 | | -# assert_that(http_response_with_duplicate_id.json()).is_equal_to(expected_body) |
89 | | -# assert_that(len(captured_logs)).is_greater_than(0) |
90 | | -# assert_that( |
91 | | -# list( |
92 | | -# filter( |
93 | | -# lambda entry: str(entry["event"]).startswith("IntegrityError!"), |
94 | | -# captured_logs, |
95 | | -# ) |
96 | | -# ) |
97 | | -# ).is_length(1) |
98 | | -# assert_that( |
99 | | -# list( |
100 | | -# filter( |
101 | | -# lambda entry: str(entry["event"]).startswith("An HTTP error!"), |
102 | | -# captured_logs, |
103 | | -# ) |
104 | | -# ) |
105 | | -# ).is_length(1) |
106 | | -# |
107 | | -# |
108 | | -# @pytest.mark.parametrize( |
109 | | -# "raise_server_exceptions", |
110 | | -# [False], |
111 | | -# indirect=True, |
112 | | -# ) |
113 | | -# def test_sample_jsonb_orm_create_with_non_existent_parent_sample_returns_422( |
114 | | -# raise_server_exceptions: bool, |
115 | | -# delete_all_fixture: None, |
116 | | -# test_client: TestClient, |
117 | | -# captured_logs: list[MutableMapping[str, Any]], |
118 | | -# ): |
119 | | -# request_entity: SampleDocumentCreate = SampleDocumentCreate( |
120 | | -# sample_id=uuid.uuid4(), |
121 | | -# json_data=DocumentMetadata(id=uuid.uuid4(), tags=tuple(["tag1", "tag2"])), |
122 | | -# secondary_json_dict={"key": "value"}, |
123 | | -# ) |
124 | | -# http_response = test_client.put( |
125 | | -# SAMPLE_JSONB_SQL_ENDPOINT, json=jsonable_encoder(request_entity) |
126 | | -# ) |
127 | | -# assert_that(http_response.status_code).is_equal_to(422) |
128 | | -# expected_body: dict[str, Any] = { |
129 | | -# "detail": {"key": "$.sample_id", "value": str(request_entity.sample_id)} |
130 | | -# } |
131 | | -# assert_that(http_response.json()).is_equal_to(expected_body) |
132 | | -# assert_that(len(captured_logs)).is_greater_than(0) |
133 | | -# assert_that( |
134 | | -# list( |
135 | | -# filter( |
136 | | -# lambda entry: str(entry["event"]).startswith("IntegrityError!"), |
137 | | -# captured_logs, |
138 | | -# ) |
139 | | -# ) |
140 | | -# ).is_length(1) |
141 | | -# assert_that( |
142 | | -# list( |
143 | | -# filter( |
144 | | -# lambda entry: str(entry["event"]).startswith("An HTTP error!"), |
145 | | -# captured_logs, |
146 | | -# ) |
147 | | -# ) |
148 | | -# ).is_length(1) |
| 48 | +@pytest.mark.parametrize( |
| 49 | + "raise_server_exceptions, expected_status", |
| 50 | + [(False, 409)], |
| 51 | + indirect=["raise_server_exceptions"], |
| 52 | +) |
| 53 | +def test_sample_jsonb_orm_create_with_duplicate_json_id_returns_409( |
| 54 | + raise_server_exceptions: bool, |
| 55 | + delete_all_fixture: None, |
| 56 | + test_client: TestClient, |
| 57 | + expected_status: int, |
| 58 | + captured_logs: list[MutableMapping[str, Any]], |
| 59 | +): |
| 60 | + sample = create_random_sample(test_client) |
| 61 | + |
| 62 | + request_entity: SampleDocumentCreate = SampleDocumentCreate( |
| 63 | + sample_id=sample.id, |
| 64 | + json_data=DocumentMetadata(id=uuid.uuid4(), tags=tuple(["tag1", "tag2"])), |
| 65 | + secondary_json_dict={"key": "value"}, |
| 66 | + ) |
| 67 | + http_response = test_client.put( |
| 68 | + SAMPLE_JSONB_SQL_ENDPOINT, json=jsonable_encoder(request_entity) |
| 69 | + ) |
| 70 | + assert_that(http_response.status_code).is_equal_to(200) |
| 71 | + request_entity_with_duplicate_id: SampleDocumentCreate = SampleDocumentCreate( |
| 72 | + sample_id=sample.id, |
| 73 | + json_data=DocumentMetadata( |
| 74 | + id=request_entity.json_data.id, tags=tuple(["tag1", "tag2"]) |
| 75 | + ), |
| 76 | + secondary_json_dict={"key": "value"}, |
| 77 | + ) |
| 78 | + http_response_with_duplicate_id = test_client.put( |
| 79 | + SAMPLE_JSONB_SQL_ENDPOINT, |
| 80 | + json=jsonable_encoder(request_entity_with_duplicate_id), |
| 81 | + ) |
| 82 | + assert_that(http_response_with_duplicate_id.status_code).is_equal_to( |
| 83 | + expected_status |
| 84 | + ) |
| 85 | + expected_body: dict[str, Any] = { |
| 86 | + "detail": {"key": "$.json_id.id", "value": str(request_entity.json_data.id)} |
| 87 | + } |
| 88 | + assert_that(http_response_with_duplicate_id.json()).is_equal_to(expected_body) |
| 89 | + assert_that(len(captured_logs)).is_greater_than(0) |
| 90 | + assert_that( |
| 91 | + list( |
| 92 | + filter( |
| 93 | + lambda entry: str(entry["event"]).startswith("UniqueViolationError!"), |
| 94 | + captured_logs, |
| 95 | + ) |
| 96 | + ) |
| 97 | + ).is_length(1) |
| 98 | + assert_that( |
| 99 | + list( |
| 100 | + filter( |
| 101 | + lambda entry: str(entry["event"]).startswith("An HTTP error!"), |
| 102 | + captured_logs, |
| 103 | + ) |
| 104 | + ) |
| 105 | + ).is_length(1) |
| 106 | + |
| 107 | + |
| 108 | +@pytest.mark.parametrize( |
| 109 | + "raise_server_exceptions", |
| 110 | + [False], |
| 111 | + indirect=True, |
| 112 | +) |
| 113 | +def test_sample_jsonb_orm_create_with_non_existent_parent_sample_returns_422( |
| 114 | + raise_server_exceptions: bool, |
| 115 | + delete_all_fixture: None, |
| 116 | + test_client: TestClient, |
| 117 | + captured_logs: list[MutableMapping[str, Any]], |
| 118 | +): |
| 119 | + request_entity: SampleDocumentCreate = SampleDocumentCreate( |
| 120 | + sample_id=uuid.uuid4(), |
| 121 | + json_data=DocumentMetadata(id=uuid.uuid4(), tags=tuple(["tag1", "tag2"])), |
| 122 | + secondary_json_dict={"key": "value"}, |
| 123 | + ) |
| 124 | + http_response = test_client.put( |
| 125 | + SAMPLE_JSONB_SQL_ENDPOINT, json=jsonable_encoder(request_entity) |
| 126 | + ) |
| 127 | + assert_that(http_response.status_code).is_equal_to(422) |
| 128 | + expected_body: dict[str, Any] = { |
| 129 | + "detail": {"key": "$.sample_id", "value": str(request_entity.sample_id)} |
| 130 | + } |
| 131 | + assert_that(http_response.json()).is_equal_to(expected_body) |
| 132 | + assert_that(len(captured_logs)).is_greater_than(0) |
| 133 | + assert_that( |
| 134 | + list( |
| 135 | + filter( |
| 136 | + lambda entry: str(entry["event"]).startswith( |
| 137 | + "ForeignKeyViolationError!" |
| 138 | + ), |
| 139 | + captured_logs, |
| 140 | + ) |
| 141 | + ) |
| 142 | + ).is_length(1) |
| 143 | + assert_that( |
| 144 | + list( |
| 145 | + filter( |
| 146 | + lambda entry: str(entry["event"]).startswith("An HTTP error!"), |
| 147 | + captured_logs, |
| 148 | + ) |
| 149 | + ) |
| 150 | + ).is_length(1) |
149 | 151 |
|
150 | 152 |
|
151 | 153 | def test_sample_jsonb_orm_read_all(delete_all_fixture: None, test_client: TestClient): |
|
0 commit comments