-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(data): add user enrollment data table
- Loading branch information
Showing
5 changed files
with
248 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,54 @@ | ||
from sqlalchemy import BigInteger, Column, Float, Integer, String | ||
from sqlalchemy.engine import Engine, create_engine | ||
from sqlalchemy.orm import DeclarativeBase, Session | ||
|
||
from .exceptions import UserEnrollmentNotFoundError | ||
from .models import ParticipantId, UserEnrollment | ||
from .settings import GlobalSettings | ||
|
||
|
||
class Base(DeclarativeBase): | ||
... | ||
|
||
|
||
class UserEnrollmentT(Base): | ||
__tablename__ = "user_enrollment" | ||
participant_id = Column(String, primary_key=True) | ||
enrollment_state = Column(String) | ||
successful_rating_needed_to_earn_in = Column(Integer) | ||
timestamp_of_last_state_change = Column(BigInteger) | ||
timestamp_of_last_earn_out = Column(BigInteger) | ||
modeling_population = Column(String) | ||
modeling_group = Column(Float) | ||
|
||
|
||
class Storage: | ||
def __init__(self, engine: Engine) -> None: | ||
self._engine = engine | ||
|
||
def __delete__(self) -> None: | ||
self._engine.dispose() | ||
|
||
@property | ||
def engine(self) -> Engine: | ||
return self._engine | ||
|
||
def get_user_enrollment_by_participant_id(self, participant_id: ParticipantId) -> UserEnrollment: | ||
raise NotImplementedError | ||
with Session(self.engine) as sess: | ||
row = sess.get(UserEnrollmentT, str(participant_id)) | ||
if row is None: | ||
raise UserEnrollmentNotFoundError(participant_id=participant_id) | ||
return UserEnrollment( | ||
participant_id=ParticipantId.from_str(str(row.participant_id)), | ||
enrollment_state=row.enrollment_state, | ||
successful_rating_needed_to_earn_in=row.successful_rating_needed_to_earn_in, | ||
timestamp_of_last_state_change=row.timestamp_of_last_state_change, | ||
timestamp_of_last_earn_out=row.timestamp_of_last_earn_out, | ||
modeling_population=row.modeling_population, | ||
modeling_group=row.modeling_group, | ||
) | ||
|
||
|
||
def gen_storage(settings: GlobalSettings) -> Storage: | ||
return Storage() | ||
engine = create_engine(settings.storage_settings.sqlalchemy_database_url.unicode_string()) | ||
return Storage(engine=engine) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
version: '3.1' | ||
services: | ||
db: | ||
image: postgres | ||
volumes: | ||
- bx-db:/var/lib/postgresql/data | ||
environment: | ||
- POSTGRES_PASSWORD=postgres | ||
restart: unless-stopped | ||
ports: | ||
- 5432:5432 | ||
|
||
volumes: | ||
bx-db: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
from typing import Dict, List, Union | ||
|
||
from birdxplorer.models import ParticipantId, UserEnrollment | ||
from birdxplorer.settings import GlobalSettings, PostgresStorageSettings | ||
from birdxplorer.storage import gen_storage | ||
|
||
|
||
def test_get_user_enrollment_by_participant_id_returns_data( | ||
user_enrollment_data_list: List[Dict[str, Union[int, str, float]]], | ||
user_enrollment_records: int, | ||
settings_for_test: PostgresStorageSettings, | ||
) -> None: | ||
s = GlobalSettings() | ||
s.storage_settings = settings_for_test | ||
storage = gen_storage(settings=s) | ||
for data in user_enrollment_data_list: | ||
pid = ParticipantId.from_str(str(data["participantId"])) | ||
result = storage.get_user_enrollment_by_participant_id(pid) | ||
assert isinstance(result, UserEnrollment) | ||
assert result.participant_id == data["participantId"] | ||
assert result.enrollment_state == data["enrollmentState"] | ||
assert result.successful_rating_needed_to_earn_in == data["successfulRatingNeededToEarnIn"] | ||
assert result.timestamp_of_last_state_change == data["timestampOfLastStateChange"] | ||
assert result.timestamp_of_last_earn_out == data["timestampOfLastEarnOut"] | ||
assert result.modeling_population == data["modelingPopulation"] | ||
assert result.modeling_group == data["modelingGroup"] |