From 47586cb6820c144520427f4bce7948b86a3c586e Mon Sep 17 00:00:00 2001 From: James Kent Date: Thu, 16 Nov 2023 14:47:58 -0600 Subject: [PATCH] make openapi more permissive and style --- store/neurostore/models/data.py | 4 +--- store/neurostore/models/event_listeners.py | 1 - store/neurostore/openapi | 2 +- store/neurostore/resources/data.py | 11 ++++++----- store/neurostore/schemas/data.py | 3 ++- store/neurostore/tests/api/test_studies.py | 4 ++++ store/neurostore/tests/api/test_studysets.py | 20 ++++++++++++-------- store/neurostore/tests/conftest.py | 17 +++++++++++++---- 8 files changed, 39 insertions(+), 23 deletions(-) diff --git a/store/neurostore/models/data.py b/store/neurostore/models/data.py index c5733a2bc..b309131ba 100644 --- a/store/neurostore/models/data.py +++ b/store/neurostore/models/data.py @@ -149,9 +149,7 @@ class BaseStudy(BaseMixin, db.Model): user = relationship("User", backref=backref("base_studies")) # retrieve versions of same study - versions = relationship( - "Study", backref=backref("base_study") - ) + versions = relationship("Study", backref=backref("base_study")) def update_has_images_and_points(self): # Calculate has_images and has_coordinates for the BaseStudy diff --git a/store/neurostore/models/event_listeners.py b/store/neurostore/models/event_listeners.py index cb9cd799e..ae18e53f0 100644 --- a/store/neurostore/models/event_listeners.py +++ b/store/neurostore/models/event_listeners.py @@ -186,5 +186,4 @@ def get_base_study(obj): ): continue - base_study.update_has_images_and_points() diff --git a/store/neurostore/openapi b/store/neurostore/openapi index 3153a832a..aa3c8a874 160000 --- a/store/neurostore/openapi +++ b/store/neurostore/openapi @@ -1 +1 @@ -Subproject commit 3153a832add571434fbb884dc8d3ec16623cc6c9 +Subproject commit aa3c8a87463cef0e6ef7fa09e064c0be455ca93d diff --git a/store/neurostore/resources/data.py b/store/neurostore/resources/data.py index 588a88eb2..b8d7c791b 100644 --- a/store/neurostore/resources/data.py +++ b/store/neurostore/resources/data.py @@ -227,12 +227,13 @@ def post(self): record = ( BaseStudy.query.filter_by(**filter_params) .options( - joinedload(BaseStudy.versions) - .options(joinedload(Study.studyset_studies) - .joinedload(StudysetStudy.studyset), - joinedload(Study.user), + joinedload(BaseStudy.versions).options( + joinedload(Study.studyset_studies).joinedload( + StudysetStudy.studyset + ), + joinedload(Study.user), ), - joinedload(BaseStudy.user) + joinedload(BaseStudy.user), ) .one_or_none() ) diff --git a/store/neurostore/schemas/data.py b/store/neurostore/schemas/data.py index 23c12e930..aa043b8b1 100644 --- a/store/neurostore/schemas/data.py +++ b/store/neurostore/schemas/data.py @@ -62,7 +62,8 @@ def _serialize(self, value, attr, obj, **ser_kwargs): "has_images", ] nested_schema = self.nested( - context=self.context, only=info_fields, + context=self.context, + only=info_fields, ) return nested_schema.dump(value, many=self.many) else: diff --git a/store/neurostore/tests/api/test_studies.py b/store/neurostore/tests/api/test_studies.py index e2bbbfda1..2a07f7d68 100644 --- a/store/neurostore/tests/api/test_studies.py +++ b/store/neurostore/tests/api/test_studies.py @@ -148,6 +148,10 @@ def test_post_studies(auth_client, ingest_neurosynth, session): auth_client.post("/api/studies/", data=my_study) + my_second_study = {"name": "asdfasfa", "pmid": "100000", "doi": "asdf;lds"} + + auth_client.post("/api/studies/", data=my_second_study) + def test_delete_studies(auth_client, ingest_neurosynth, session): study_db = Study.query.first() diff --git a/store/neurostore/tests/api/test_studysets.py b/store/neurostore/tests/api/test_studysets.py index 6ceb7c53c..016090de9 100644 --- a/store/neurostore/tests/api/test_studysets.py +++ b/store/neurostore/tests/api/test_studysets.py @@ -23,6 +23,7 @@ def test_post_and_get_studysets(auth_client, ingest_neurosynth, session): == post_resp.json() ) + # @add_event_listeners def test_add_many_studies_to_studyset(auth_client, ingest_neurosynth, session): existing_studies = Study.query.all() @@ -38,26 +39,29 @@ def generate_doi(): # List comprehension to generate the desired structure made_up_studies = [ { - "pmid": random.randint(100000, 999999), + "pmid": str(random.randint(100000, 999999)), "doi": generate_doi(), - "name": ''.join(random.choices(string.ascii_letters, k=10)), - } for _ in range(1) + "name": "".join(random.choices(string.ascii_letters, k=10)), + } + for _ in range(1) ] # create empty studyset ss = auth_client.post("/api/studysets/", data={"name": "mixed_studyset"}) assert ss.status_code == 200 - - ss_id = ss.json()['id'] - # combine made_up and created studies - all_studies = existing_study_ids# + made_up_studies + ss_id = ss.json()["id"] + # combine made_up and created studies + all_studies = existing_study_ids + made_up_studies - ss_update = auth_client.put(f"/api/studysets/{ss_id}", data={"studies": all_studies}) + ss_update = auth_client.put( + f"/api/studysets/{ss_id}", data={"studies": all_studies} + ) assert ss_update.status_code == 200 + def test_add_study_to_studyset(auth_client, ingest_neurosynth, session): payload = auth_client.get("/api/studies/").json() study_ids = [study["id"] for study in payload["results"]] diff --git a/store/neurostore/tests/conftest.py b/store/neurostore/tests/conftest.py index 4f58a5441..d42f3e79e 100644 --- a/store/neurostore/tests/conftest.py +++ b/store/neurostore/tests/conftest.py @@ -30,23 +30,32 @@ """ Record sql queries """ -def add_event_listeners(f): + +def add_event_listeners(f): @functools.wraps(f) def event_listeners(*args, **kwargs): @sa.event.listens_for(_db.engine, "before_cursor_execute") - def _record_query_start(conn, cursor, statement, parameters, context, executemany): + def _record_query_start( + conn, cursor, statement, parameters, context, executemany + ): conn.info["query_start"] = datetime.now() @sa.event.listens_for(_db.engine, "after_cursor_execute") - def _calculate_query_run_time(conn, cursor, statement, parameters, context, executemany): + def _calculate_query_run_time( + conn, cursor, statement, parameters, context, executemany + ): LOGGER.warning(f"\n\n{statement}") - LOGGER.warning("this query took {}".format(datetime.now() - conn.info["query_start"])) + LOGGER.warning( + "this query took {}".format(datetime.now() - conn.info["query_start"]) + ) print("here!") + return f(*args, **kwargs) return event_listeners + """ Test selection arguments """