From f03f532862cb2e61f7657b68cd7958b463f658e8 Mon Sep 17 00:00:00 2001 From: Vishal Tanwar Date: Fri, 4 Nov 2022 21:33:59 +0530 Subject: [PATCH] create unique index for redeem code as it should be unique --- gcmanager/dependencies.py | 5 ++++- tests/unit/test_container.py | 12 ++++++------ 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/gcmanager/dependencies.py b/gcmanager/dependencies.py index cc3c9a6..dcc818a 100644 --- a/gcmanager/dependencies.py +++ b/gcmanager/dependencies.py @@ -2,6 +2,7 @@ from urllib.parse import quote from kink import Container +from pymongo import ASCENDING from pymongo import MongoClient from pymongo.collection import Collection from pymongo.database import Database @@ -61,7 +62,9 @@ def _build_mongodb_database(container: Container) -> None: def _build_mongodb_collection(container: Container) -> None: database = container[Database] settings = container[Settings] - container[Collection] = lambda c: database[settings["MONGODB_GC_COLLECTION_NAME"]] + collection = database[settings["MONGODB_GC_COLLECTION_NAME"]] + collection.create_index([("redeem_code", ASCENDING)], unique=True) + container[Collection] = lambda c: collection def _build_mongodb_repository(container: Container) -> None: diff --git a/tests/unit/test_container.py b/tests/unit/test_container.py index ee9b53d..fceb631 100644 --- a/tests/unit/test_container.py +++ b/tests/unit/test_container.py @@ -24,12 +24,12 @@ def test_raises_exception_when_required_env_variables_absent(self) -> None: def test_returns_container_when_required_env_variables_present(self) -> None: when(os.environ).get("APP_ENV").thenReturn("PROD") - when(os.environ).get("MONGODB_USERNAME").thenReturn("someuser") - when(os.environ).get("MONGODB_PASSWORD").thenReturn("somepassword") - when(os.environ).get("MONGODB_HOST").thenReturn("somehost") - when(os.environ).get("MONGODB_PORT", "27017").thenReturn("27017") - when(os.environ).get("MONGODB_DBNAME", "gcmanager").thenReturn("somedb") + when(os.environ).get("MONGODB_USERNAME").thenReturn("testing_user") + when(os.environ).get("MONGODB_PASSWORD").thenReturn("testing") + when(os.environ).get("MONGODB_HOST").thenReturn("localhost") + when(os.environ).get("MONGODB_PORT", "27017").thenReturn("27020") + when(os.environ).get("MONGODB_DBNAME", "gcmanager").thenReturn("testdb") when(os.environ).get("MONGODB_GC_COLLECTION_NAME", "giftcards").thenReturn( - "somecollection", + "testcollection", ) self.assertIsInstance(build_dependency_container(), Container)