Skip to content

Commit 3d4c17e

Browse files
committed
Delete sample endpoint
1 parent b3adf49 commit 3d4c17e

File tree

5 files changed

+139
-7
lines changed

5 files changed

+139
-7
lines changed

openapi_server/controllers/sample_controller.py

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,4 +121,25 @@ def samples_id_patch(id, new_sample=None): # noqa: E501
121121
except IntegrityError:
122122
return Error(409, 'Already existed'), 409
123123
else:
124-
return old_sample.to_model(), 200
124+
return old_sample.to_model(), 200
125+
126+
127+
def samples_id_delete(id): # noqa: E501
128+
"""samples_id_delete
129+
130+
Delete a sample. # noqa: E501
131+
132+
:param id:
133+
:type id:
134+
135+
:rtype: Sample
136+
"""
137+
138+
inst = orm.Sample.query.get(id)
139+
if not inst:
140+
return Error(404, 'Not found'), 404
141+
142+
db.session.delete(inst)
143+
db.session.commit()
144+
145+
return inst.to_model(), 200

openapi_server/openapi/openapi.yaml

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,39 @@ paths:
119119
- sample
120120
x-openapi-router-controller: openapi_server.controllers.sample_controller
121121
/samples/{id}:
122+
delete:
123+
description: Delete a sample.
124+
operationId: samples_id_delete
125+
parameters:
126+
- explode: false
127+
in: path
128+
name: id
129+
required: true
130+
schema:
131+
$ref: '#/components/schemas/SampleID'
132+
style: simple
133+
responses:
134+
"200":
135+
content:
136+
application/json:
137+
schema:
138+
$ref: '#/components/schemas/Sample'
139+
description: Deleted
140+
"404":
141+
content:
142+
application/json:
143+
schema:
144+
$ref: '#/components/schemas/Error'
145+
description: Not found
146+
default:
147+
content:
148+
application/json:
149+
schema:
150+
$ref: '#/components/schemas/Error'
151+
description: Unexpected error
152+
tags:
153+
- sample
154+
x-openapi-router-controller: openapi_server.controllers.sample_controller
122155
get:
123156
description: Return a sample by its ID.
124157
operationId: samples_id_get
@@ -818,6 +851,7 @@ components:
818851
SampleID:
819852
format: uuid
820853
pattern: ^[0-9a-fA-F]{8}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{12}$
854+
title: id
821855
type: string
822856
Sample:
823857
example:
@@ -826,13 +860,17 @@ components:
826860
experiment-id: experiment-id
827861
properties:
828862
experiment-id:
863+
title: experiment-id
829864
type: string
830865
isolate-id:
866+
title: isolate-id
831867
type: string
832868
id:
833869
format: uuid
834870
pattern: ^[0-9a-fA-F]{8}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{12}$
871+
title: id
835872
type: string
873+
title: Sample
836874
type: object
837875
File:
838876
example:
@@ -841,18 +879,22 @@ components:
841879
md5sum: md5sum
842880
properties:
843881
md5sum:
882+
title: md5sum
844883
type: string
845884
filename:
885+
title: filename
846886
type: string
847887
file-type:
848888
enum:
849889
- fastq
850890
- vcf
891+
title: file-type
851892
type: string
852893
required:
853894
- file-type
854895
- filename
855896
- md5sum
897+
title: File
856898
type: object
857899
Event:
858900
example:
@@ -865,6 +907,7 @@ components:
865907
command: command
866908
properties:
867909
id:
910+
title: id
868911
type: integer
869912
name:
870913
enum:
@@ -874,17 +917,23 @@ components:
874917
- prediction
875918
- bigsi-building
876919
- distance-calculation
920+
title: name
877921
type: string
878922
software:
923+
title: software
879924
type: string
880925
software-version:
926+
title: software-version
881927
type: string
882928
start-time:
929+
title: start-time
883930
type: number
884931
duration:
885932
format: int32
933+
title: duration
886934
type: integer
887935
command:
936+
title: command
888937
type: string
889938
required:
890939
- command
@@ -893,6 +942,7 @@ components:
893942
- software
894943
- software-version
895944
- start-time
945+
title: Event
896946
type: object
897947
QcResult:
898948
example:
@@ -902,19 +952,23 @@ components:
902952
properties:
903953
coverage:
904954
format: float
955+
title: coverage
905956
type: number
906957
number_of_het_snps:
907958
format: int32
959+
title: number_of_het_snps
908960
type: integer
909961
decision:
910962
enum:
911963
- passed
912964
- failed
965+
title: decision
913966
type: string
914967
required:
915968
- coverage
916969
- decision
917970
- number_of_het_snps
971+
title: QcResult
918972
type: object
919973
Status:
920974
example:
@@ -932,48 +986,55 @@ components:
932986
- started
933987
- complete
934988
- failed
989+
title: de-contamination
935990
type: string
936991
qc:
937992
enum:
938993
- pending
939994
- started
940995
- complete
941996
- failed
997+
title: qc
942998
type: string
943999
variant-calling:
9441000
enum:
9451001
- pending
9461002
- started
9471003
- complete
9481004
- failed
1005+
title: variant-calling
9491006
type: string
9501007
prediction:
9511008
enum:
9521009
- pending
9531010
- started
9541011
- complete
9551012
- failed
1013+
title: prediction
9561014
type: string
9571015
bigsi-building:
9581016
enum:
9591017
- pending
9601018
- started
9611019
- complete
9621020
- failed
1021+
title: bigsi-building
9631022
type: string
9641023
distance-calculation:
9651024
enum:
9661025
- pending
9671026
- started
9681027
- complete
9691028
- failed
1029+
title: distance-calculation
9701030
type: string
9711031
stage:
9721032
enum:
9731033
- accepted
9741034
- qc-failed
9751035
- live
9761036
- deprecated
1037+
title: stage
9771038
type: string
9781039
required:
9791040
- bigsi-building
@@ -982,15 +1043,19 @@ components:
9821043
- prediction
9831044
- qc
9841045
- variant-calling
1046+
title: Status
9851047
type: object
9861048
Error:
9871049
properties:
9881050
code:
9891051
format: int32
1052+
title: code
9901053
type: integer
9911054
message:
1055+
title: message
9921056
type: string
9931057
required:
9941058
- code
9951059
- message
1060+
title: Error
9961061
type: object

openapi_server/test/conftest.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -202,11 +202,9 @@ def _(sample_id):
202202

203203

204204
@fixture
205-
def delete_sample():
206-
def _(sample_id):
207-
inst = Sample.query.get(sample_id)
208-
db.session.delete(inst)
209-
db.session.commit()
205+
def delete_sample(make_request):
206+
def _(sample_id, *args, **kwargs):
207+
return make_request(f'/api/v1/samples/{sample_id}', 'DELETE', *args, **kwargs)
210208
return _
211209

212210

openapi_server/test/test_sample_controller.py

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,4 +208,23 @@ def test_patching_samples(old_sample, new_sample, patch_sample, create_sample, g
208208
assert getattr(patched, prop) == getattr(new_sample, prop)
209209

210210
retrieved = Sample.from_dict(get_sample(created.id, ensure=True).json)
211-
assert retrieved == patched
211+
assert retrieved == patched
212+
213+
214+
@given(sample_id=sample_ids())
215+
def test_deleting_non_existent_samples(sample_id, delete_sample):
216+
response = delete_sample(sample_id)
217+
assert response.status_code == 404
218+
219+
220+
@given(sample=samples())
221+
def test_deleting_samples(sample, create_sample, delete_sample):
222+
with managed_db():
223+
response = create_sample(sample)
224+
created = Sample.from_dict(response.json)
225+
226+
response = delete_sample(created.id)
227+
deleted = Sample.from_dict(response.json)
228+
229+
assert response.status_code == 200
230+
assert deleted == created

swagger.yaml

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,35 @@ paths:
212212
application/json:
213213
schema:
214214
$ref: '#/components/schemas/Error'
215+
delete:
216+
tags:
217+
- sample
218+
description: Delete a sample.
219+
parameters:
220+
- name: id
221+
in: path
222+
required: true
223+
schema:
224+
$ref: '#/components/schemas/SampleID'
225+
responses:
226+
200:
227+
description: Deleted
228+
content:
229+
application/json:
230+
schema:
231+
$ref: '#/components/schemas/Sample'
232+
404:
233+
description: Not found
234+
content:
235+
application/json:
236+
schema:
237+
$ref: '#/components/schemas/Error'
238+
default:
239+
description: Unexpected error
240+
content:
241+
application/json:
242+
schema:
243+
$ref: '#/components/schemas/Error'
215244
/samples/{id}/files:
216245
get:
217246
tags:

0 commit comments

Comments
 (0)