diff --git a/.idea/misc.xml b/.idea/misc.xml index 6754db7..a99db41 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -1,4 +1,7 @@ + + \ No newline at end of file diff --git a/.idea/straw2analysis.iml b/.idea/straw2analysis.iml index 5b6552c..594e7e8 100644 --- a/.idea/straw2analysis.iml +++ b/.idea/straw2analysis.iml @@ -1,7 +1,9 @@ - + + + diff --git a/config/models.py b/config/models.py index bdcd81d..676af22 100644 --- a/config/models.py +++ b/config/models.py @@ -37,27 +37,27 @@ class Participant(Base): return ''.format(self.username) -class AWAREsensor(Base): +class AWAREsensor(object): id = Column(BigInteger, primary_key=True, nullable=False) _id = Column(BigInteger, nullable=False) timestamp = Column(BigInteger, nullable=False) device_id = Column(String(length=36), nullable=False) @declared_attr - def __tablename__(cls): - return cls.__name__.lower() + def __tablename__(self): + return self.__name__.lower() @declared_attr - def participant_id(cls): + def participant_id(self): return Column(Integer, ForeignKey('participants.id'), nullable=False, index=True) @declared_attr - def participants(cls): - return relationship('Participant', lazy="select", backref=cls.__tablename__) + def participant(self): + return relationship('Participant', lazy="select", backref=self.__tablename__) @declared_attr - def __table_args__(cls): - return UniqueConstraint('device_id', '_id', name=cls.__tablename__ + "_twice"), + def __table_args__(self): + return UniqueConstraint('device_id', '_id', name=self.__tablename__ + "_twice"), # I think it makes more sense to create a Constraint on device_id and _id rather than relate it to participant_id. # _id is a primary key, auto incremented by AWARE. # However, I would expect it to reset back to 1 if the application was reinstalled, @@ -145,6 +145,7 @@ class ESM(Base, AWAREsensor): class Imperfection(Base): + __tablename__ = "imperfection" id = Column(BigInteger, primary_key=True, nullable=False) timestamp = Column(BigInteger, nullable=False) error = Column(String) diff --git a/test/test_database.py b/test/test_database.py index 0ab23b1..b9242c8 100644 --- a/test/test_database.py +++ b/test/test_database.py @@ -1,18 +1,33 @@ import unittest from sqlalchemy import create_engine +from sqlalchemy.orm import sessionmaker +from config.models import Participant, LightSensor from setup import db_uri class DatabaseConnection(unittest.TestCase): def setUp(self): self.engine = create_engine(db_uri, echo=True) + Session = sessionmaker(bind=self.engine) + self.session = Session() def tearDown(self): self.engine.dispose() + self.session.close() def test_connection(self): with self.engine.connect() as connection: self.assertIsNotNone(connection) connection.close() + + def test_get_participant(self): + self.participant_0 = self.session.query(Participant).first() + self.assertIsNotNone(self.participant_0) + print(self.participant_0) + + def test_get_light_data(self): + light_0 = self.session.query(Participant).join(LightSensor).first() + self.assertIsNotNone(light_0) + print(light_0) \ No newline at end of file