From 37b8da10fef575f2808bd429d87b983cbd2815ae Mon Sep 17 00:00:00 2001 From: manuelseeger Date: Thu, 6 Jun 2024 21:07:38 +0200 Subject: [PATCH] Move to pyodmongo 1.0 --- README.md | 4 ++-- replays/db.py | 8 ++++---- tests/integration/test_db.py | 10 ++++++---- 3 files changed, 12 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index 546f57c..91acc14 100644 --- a/README.md +++ b/README.md @@ -187,10 +187,10 @@ This is invoked at the very start of an SC2 game (when the in-game clock hits 1 - Summarize past strategies of the opponent - Ask for follow up questions -You can configure which events AICoach should react to with the `coach_events` option. - ### Configure coach events +You can configure which events AICoach should react to with the `coach_events` option. + ```yaml # config.yourname.yml diff --git a/replays/db.py b/replays/db.py index 93eb2d9..1acc5c8 100644 --- a/replays/db.py +++ b/replays/db.py @@ -3,7 +3,7 @@ from pydantic_core import ValidationError from pymongo.collection import Collection from pyodmongo import DbEngine, DbModel -from pyodmongo.models.responses import SaveResponse +from pyodmongo.models.responses import DbResponse from pyodmongo.queries import eq, sort from typing_extensions import override @@ -23,17 +23,17 @@ def __init__(self): self.replays: Collection = self.db._db["replays"] self.meta: Collection = self.db._db["replays.meta"] - def upsert(self, model: SC2Model) -> SaveResponse: + def upsert(self, model: SC2Model) -> DbResponse: ModelClass = model.__class__ try: return self.db.save(model, query=eq(ModelClass.id, model.id)) except ValidationError as e: # On INSERT, pyodm forces the returned ID into mongo ObjectId and throws since we use a custom ID field # Return SaveResponse without validation instead - return SaveResponse.model_construct( + return DbResponse.model_construct( **{ "acknowledged": True, - "upserted_id": model.id, + "upserted_ids": {0: model.id}, "matched_count": 0, "modified_count": 0, } diff --git a/tests/integration/test_db.py b/tests/integration/test_db.py index 7ea80b2..d9e55d8 100644 --- a/tests/integration/test_db.py +++ b/tests/integration/test_db.py @@ -2,6 +2,7 @@ import pytest +from config import config from replays import ReplayReader, replaydb from replays.db import eq from replays.types import AssistantMessage, Metadata, Replay, Role @@ -63,8 +64,8 @@ def test_upsert_existing_replay(replay_file): def test_upsert_new_replay(): - # random 64 character id - new_id = "a" * 64 + + new_id = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" replay = Replay( id=new_id, @@ -76,9 +77,9 @@ def test_upsert_new_replay(): result = replaydb.upsert(replay) assert result.acknowledged - assert result.upserted_id == new_id + assert any(new_id == v for k, v in result.upserted_ids.items()) - del_result = replaydb.db.delete_one(Replay, query=eq(Replay.id, new_id)) + del_result = replaydb.db.delete(Replay, query=eq(Replay.id, new_id)) assert del_result.deleted_count == 1 assert del_result.acknowledged @@ -86,4 +87,5 @@ def test_upsert_new_replay(): def test_get_most_recent(): replay: Replay = replaydb.get_most_recent() + assert any(config.student.name in player.name for player in replay.players) assert replay is not None