diff --git a/toardb/timeseries/crud.py b/toardb/timeseries/crud.py index 08131dd9aabeecedeb4b5063c5ad858b6b9e7a87..0988f9ab33c19f579f43c34a846c11a0fab25aae 100644 --- a/toardb/timeseries/crud.py +++ b/toardb/timeseries/crud.py @@ -70,33 +70,28 @@ def create_timeseries(db: Session, timeseries: TimeseriesCreate): timeseries_dict = timeseries.dict() roles_data = timeseries_dict.pop('roles', None) db_timeseries = models.Timeseries(**timeseries_dict) - # there's also a mismatch with additional_metadata --> BUT: this should not be switched back! - # in upload command, we have now: "additional_metadata": "{}" - # but return from this method gives: "additional_metadata": {} - # ==> there is a mismatch between model(JSONB) and schema(JSON) - db_timeseries.additional_metadata = str(db_timeseries.additional_metadata) db.add(db_timeseries) result = db.commit() db.refresh(db_timeseries) # get timeseries_id timeseries_id = db_timeseries.id - # store roles (and update associaton table?!) - for r in roles_data: - # try which format is used - try: + # store roles and update associaton table + if roles_data: + for r in roles_data: db_role = models.TimeseriesRole(**r) - except: - print ("human readable format: ", roles_data) - # check whether role is already present in database - db_object = get_unique_timeseries_role(db, db_role.role, db_role.person_id, db_role.status) - if db_object: - role_id = db_object.id - else: - db.add(db_role) + # check whether role is already present in database + db_object = get_unique_timeseries_role(db, db_role.role, db_role.person_id, db_role.status) + if db_object: + role_id = db_object.id + else: + db.add(db_role) + db.commit() + db.refresh(db_role) + role_id = db_role.id + db.execute(insert(timeseries_timeseries_roles_table).values(timeseries_id=timeseries_id, role_id=role_id)) db.commit() - db.refresh(db_role) - role_id = db_role.id - # now into associaton table! --> This should be done automatically?! - db.execute(insert(timeseries_timeseries_roles_table).values(timeseries_id=timeseries_id, role_id=role_id)) - db.commit() + # there is a mismatch with additional_metadata + # in upload command, we have now: "additional_metadata": "{}" + # but return from this method gives (=database): "additional_metadata": {} + db_timeseries.additional_metadata = str(db_timeseries.additional_metadata) return db_timeseries diff --git a/toardb/timeseries/models.py b/toardb/timeseries/models.py index 956351353f397c20677b9ef995975a72eee2abb7..21a4dc4dc3fc33d5d22b7133ad9723f06f0c1349 100644 --- a/toardb/timeseries/models.py +++ b/toardb/timeseries/models.py @@ -42,3 +42,4 @@ RC_enum = Table("rc_vocabulary", Column("enum_str", String), Column("enum_display_str", String) ) + diff --git a/toardb/timeseries/models_core.py b/toardb/timeseries/models_core.py index cfac0b61713c96a7fbbeff16ef758187cfc567b8..04360b5839f540e1901dd3a30c8a5edad0d80524 100644 --- a/toardb/timeseries/models_core.py +++ b/toardb/timeseries/models_core.py @@ -16,27 +16,12 @@ from toardb.contacts.models import Person Base = declarative_base() metadata = Base.metadata -# the problem is pytest!!! -# pytest does not accept strings within ForeignKey, relationship, ... -# forward declarations -# (because of now commented line with relationship -#class TimeseriesRole(Base): -# __tablename__ = 'timeseries_roles' -# id = Column(Integer, primary_key=True, server_default=text("nextval('timeseries_roles_id_seq'::regclass)")) - -#class Timeseries(Base): -# __tablename__ = 'timeseries' -# id = Column(Integer, primary_key=True, server_default=text("nextval('timeseries_id_seq'::regclass)")) - +# many-to-many relationships timeseries_timeseries_roles_table = Table('timeseries_timeseries_roles', Base.metadata, Column('timeseries_id', Integer, ForeignKey('timeseries.id')), Column('role_id', Integer, ForeignKey('timeseries_roles.id')) ) -#timeseries_timeseries_roles = Table('timeseries_timeseries_roles', Base.metadata, -# Column('timeseries_id', Integer, ForeignKey(Timeseries.id)), -# Column('role_id', Integer, ForeignKey(TimeseriesRole.id)) -#) class TimeseriesRole(Base): """ Table "public.timeseries_roles" @@ -78,18 +63,13 @@ class TimeseriesRole(Base): # use the explicit class name here, # see: https://groups.google.com/forum/#!topic/sqlalchemy/YjGhE4d6K4U person_id = Column(ForeignKey(Person.id, deferrable=True, initially='DEFERRED'), nullable=False, index=True) -# how to reactivate the following line person = relationship(Person) -# how to reactivate the following line?! -# timeseries_id = Column(Integer, ForeignKey(Timeseries.id), nullable=False) -# timeseries = relationship(Timeseries, secondary=timeseries_timeseries_roles, backref="timeseries") timeseries = relationship("Timeseries", secondary=timeseries_timeseries_roles_table, backref="roles") - class Timeseries(Base): """ Table "public.timeseries" diff --git a/toardb/timeseries/schemas.py b/toardb/timeseries/schemas.py index ec24f36b529a6dd2b85727b5250912b9f3e26e76..59aea875e41b4100aa48f56529d39fd842d1f328 100644 --- a/toardb/timeseries/schemas.py +++ b/toardb/timeseries/schemas.py @@ -102,13 +102,13 @@ class TimeseriesProgramme(TimeseriesProgrammeBase): # ======== for nested view/upload ========= class TimeseriesBase(TimeseriesCoreBase): - roles: List[TimeseriesRole] + roles: List[TimeseriesRole] = None class Config: orm_mode = True class TimeseriesCreate(TimeseriesCoreBase): - roles: List[TimeseriesRoleBase] + roles: List[TimeseriesRoleBase] = None class Config: orm_mode = True diff --git a/toardb/timeseries/test_timeseries.py b/toardb/timeseries/test_timeseries.py index 0c1dd03595c40dc8307f81a0923c476dbd351254..18633bf893a275813468ed0ea972baec068f23ec 100644 --- a/toardb/timeseries/test_timeseries.py +++ b/toardb/timeseries/test_timeseries.py @@ -128,7 +128,7 @@ class TestApps: 'measurement_method': 'UV absorption', 'sampling_height': 7.0, 'date_added': '2020-05-15T15:30:00+02:00', 'date_modified': '2020-05-16T09:30:00+02:00', 'station_id': 2, 'variable_id': 7, - 'additional_metadata':{}}] + 'additional_metadata':{}, 'roles': []}] assert response.json() == expected_resp @@ -150,7 +150,7 @@ class TestApps: 'measurement_method': 'UV absorption', 'sampling_height': 7.0, 'date_added': '2020-05-15T15:30:00+02:00', 'date_modified': '2020-05-16T09:30:00+02:00', 'station_id': 2, 'variable_id': 7, - 'additional_metadata':{}} + 'additional_metadata':{}, 'roles': []} assert response.json() == expected_resp @@ -192,7 +192,35 @@ class TestApps: 'measurement_method': 'UV absorption', 'sampling_height': 7.0, 'date_added': '2020-05-15T15:30:00+02:00', 'date_modified': '2020-05-16T09:30:00+02:00', 'station_id': 2, 'variable_id': 7, - 'additional_metadata':{}} + 'additional_metadata':{}, 'roles': []} + assert response.json() == expected_resp + + + def test_insert_new_with_roles(self, client, db): + response = client.post("/timeseries/", + json={"timeseries": + {"label": "CMA2", "order": 1, "access_rights": 0, + "sampling_frequency": 0, "aggregation": 0, + "data_start_date": "2003-09-07T15:30:00+02:00", + "data_end_date": "2016-12-31T14:30:00+01:00", + "measurement_method": "UV absorption", "sampling_height": 7.0, + "date_added": "2020-05-15T15:30:00+02:00", "date_modified": "2020-05-16T09:30:00+02:00", + "station_id": 2, "variable_id": 7, + "additional_metadata":"{}", + "roles": [{"role": 0, "person_id": 3, "status": 0},{"role": 1, "person_id": 3, "status": 0}]} + } + ) + print("I got:", response.json()) + expected_status_code = 200 + assert response.status_code == expected_status_code + expected_resp = {'id': 2, 'label': 'CMA2', 'order': 1, 'access_rights': 0, + 'sampling_frequency': 0, 'aggregation': 0, + 'data_start_date': '2003-09-07T15:30:00+02:00', 'data_end_date': '2016-12-31T14:30:00+01:00', + 'measurement_method': 'UV absorption', 'sampling_height': 7.0, + 'date_added': '2020-05-15T15:30:00+02:00', 'date_modified': '2020-05-16T09:30:00+02:00', + 'station_id': 2, 'variable_id': 7, + 'additional_metadata':{}, + 'roles': [{'id': 1, 'person_id': 3, 'role': 0, 'status': 0}, {'id': 2, 'person_id': 3, 'role': 1, 'status': 0}]} assert response.json() == expected_resp diff --git a/toardb/timeseries/timeseries.py b/toardb/timeseries/timeseries.py index 8c0fa82db69367c7fd4cd96b86545eb82f0ed1ba..de1819b099dfee8c05097e3993f96031626813ff 100644 --- a/toardb/timeseries/timeseries.py +++ b/toardb/timeseries/timeseries.py @@ -41,7 +41,6 @@ def get_timeseries(timeseries_id: int, db: Session = Depends(get_db)): # # -# still to be tested: @router.post('/timeseries/', response_model=schemas.Timeseries) def create_timeseries(timeseries: schemas.TimeseriesCreate = Body(..., embed = True), db: Session = Depends(get_db)): db_timeseries = crud.get_timeseries_by_unique_constraints(db, station_id=timeseries.station_id, @@ -49,3 +48,4 @@ def create_timeseries(timeseries: schemas.TimeseriesCreate = Body(..., embed = T if db_timeseries: raise HTTPException(status_code=400, detail="Timeseries already registered.") return crud.create_timeseries(db=db, timeseries=timeseries) +