-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e652514
commit abedb46
Showing
14 changed files
with
411 additions
and
17 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
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 |
---|---|---|
|
@@ -9,3 +9,5 @@ pydantic[dotenv] | |
SQLAlchemy | ||
uvicorn | ||
pydantic-settings | ||
event_schema_profcomff | ||
confluent_kafka |
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
Empty file.
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,110 @@ | ||
import pytest | ||
import sqlalchemy.exc | ||
from event_schema.auth import UserLogin | ||
|
||
from userdata_api.models.db import Category, Info, Param, Source | ||
from userdata_api.utils.utils import random_string | ||
from worker.user import patch_user_info | ||
|
||
|
||
@pytest.fixture() | ||
def category(dbsession): | ||
name = f"test{random_string()}" | ||
dbsession.add( | ||
_cat := Category( | ||
name=name, read_scope=f"testscope.{random_string()}", update_scope=f"testscope.{random_string()}" | ||
) | ||
) | ||
dbsession.commit() | ||
yield _cat | ||
dbsession.delete(_cat) | ||
dbsession.commit() | ||
|
||
|
||
@pytest.fixture() | ||
def param(dbsession, category): | ||
time_ = f"test{random_string()}" | ||
dbsession.add( | ||
_par := Param(name=f"test{time_}", category_id=category.id, type="last", changeable=True, is_required=True) | ||
) | ||
dbsession.commit() | ||
yield _par | ||
dbsession.delete(_par) | ||
dbsession.commit() | ||
|
||
|
||
@pytest.fixture() | ||
def source(dbsession): | ||
time_ = f"test{random_string()}" | ||
__source = Source(name=f"test{time_}", trust_level=8) | ||
dbsession.add(__source) | ||
dbsession.commit() | ||
yield __source | ||
dbsession.delete(__source) | ||
dbsession.commit() | ||
|
||
|
||
@pytest.fixture() | ||
def info(param, source, dbsession): | ||
time_ = f"test{random_string()}" | ||
__info = Info(value=f"test{time_}", source_id=source.id, param_id=param.id, owner_id=1) | ||
dbsession.add(__info) | ||
dbsession.commit() | ||
yield __info | ||
try: | ||
dbsession.delete(__info) | ||
dbsession.commit() | ||
except sqlalchemy.exc.Any: | ||
pass | ||
|
||
|
||
def test_create(param, source, dbsession): | ||
with pytest.raises(sqlalchemy.exc.NoResultFound): | ||
dbsession.query(Info).filter(Info.param_id == param.id, Info.source_id == source.id, Info.value == "test").one() | ||
patch_user_info( | ||
UserLogin.model_validate( | ||
{"items": [{"category": param.category.name, "param": param.name, "value": "test"}], "source": source.name} | ||
), | ||
1, | ||
session=dbsession, | ||
) | ||
info = ( | ||
dbsession.query(Info).filter(Info.param_id == param.id, Info.source_id == source.id, Info.value == "test").one() | ||
) | ||
assert info | ||
dbsession.delete(info) | ||
dbsession.commit() | ||
|
||
|
||
def test_update(info, dbsession): | ||
assert info.value != "updated" | ||
patch_user_info( | ||
UserLogin.model_validate( | ||
{ | ||
"items": [{"category": info.category.name, "param": info.param.name, "value": "updated"}], | ||
"source": info.source.name, | ||
} | ||
), | ||
1, | ||
session=dbsession, | ||
) | ||
|
||
dbsession.expire(info) | ||
assert info.value == "updated" | ||
|
||
|
||
def test_delete(info, dbsession): | ||
assert info.is_deleted is False | ||
patch_user_info( | ||
UserLogin.model_validate( | ||
{ | ||
"items": [{"category": info.category.name, "param": info.param.name, "value": None}], | ||
"source": info.source.name, | ||
} | ||
), | ||
1, | ||
session=dbsession, | ||
) | ||
|
||
dbsession.expire(info) | ||
assert info.is_deleted is True |
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,7 +1,25 @@ | ||
import argparse | ||
|
||
import uvicorn | ||
|
||
from userdata_api.routes.base import app | ||
from worker.consumer import process | ||
|
||
|
||
def get_args(): | ||
parser = argparse.ArgumentParser() | ||
subparsers = parser.add_subparsers(dest='command') | ||
|
||
start = subparsers.add_parser("start") | ||
start.add_argument('--instance', type=str, required=True) | ||
|
||
return parser.parse_args() | ||
|
||
|
||
if __name__ == '__main__': | ||
uvicorn.run(app) | ||
args = get_args() | ||
match args.instance: | ||
case "api": | ||
uvicorn.run(app) | ||
case "worker": | ||
process() |
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
Empty file.
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,40 @@ | ||
import logging | ||
from typing import Any | ||
|
||
import pydantic | ||
from event_schema.auth import UserLogin, UserLoginKey | ||
from sqlalchemy import create_engine | ||
from sqlalchemy.orm import sessionmaker | ||
|
||
from settings import get_settings | ||
from worker.kafka import KafkaConsumer | ||
|
||
from .user import patch_user_info | ||
|
||
|
||
log = logging.getLogger(__name__) | ||
settings = get_settings() | ||
consumer = KafkaConsumer() | ||
|
||
_engine = create_engine(str(settings.DB_DSN), pool_pre_ping=True, isolation_level="AUTOCOMMIT") | ||
_Session = sessionmaker(bind=_engine) | ||
|
||
|
||
def process_models(key: Any, value: Any) -> tuple[UserLoginKey | None, UserLogin | None]: | ||
try: | ||
return UserLoginKey.model_validate(key), UserLogin.model_validate(value) | ||
except pydantic.ValidationError: | ||
log.error(f"Validation error occurred, {key=}, {value=}", exc_info=False) | ||
return None, None | ||
|
||
|
||
def process_message(message: tuple[Any, Any]) -> None: | ||
processed_k, processed_v = process_models(*message) | ||
if not (processed_k and processed_v): | ||
return | ||
patch_user_info(processed_v, processed_k.user_id, session=_Session()) | ||
|
||
|
||
def process(): | ||
for message in consumer.listen(): | ||
process_message(message) |
Oops, something went wrong.