Skip to content

Commit

Permalink
Update MongoDb tests to support parallel runs
Browse files Browse the repository at this point in the history
Remove created test collections after unit test run
  • Loading branch information
NeonDaniel committed Nov 5, 2024
1 parent de259b4 commit a50cf2d
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
4 changes: 2 additions & 2 deletions neon_users_service/databases/mongodb.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ def __init__(self, db_host: str, db_port: int, db_user: str, db_pass: str,
db_name: str = "neon-users", collection_name: str = "users"):
connection_string = f"mongodb://{db_user}:{db_pass}@{db_host}:{db_port}"
self.client = MongoClient(connection_string)
db = self.client[db_name]
self.collection = db[collection_name]
self.db = self.client[db_name]
self.collection = self.db[collection_name]

def _db_create_user(self, user: User) -> User:
self.collection.insert_one({**user.model_dump(),
Expand Down
6 changes: 6 additions & 0 deletions tests/test_databases.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ def test_delete_user(self):

class TestMongoDb(TestCase):
test_config = json.loads(environ.get("MONGO_TEST_CONFIG"))
test_config['collection_name'] = f"{test_config['collection_name']}{time()}"
from neon_users_service.databases.mongodb import MongoDbUserDatabase
database = MongoDbUserDatabase(**test_config)
test_user = User(username="test_user", password_hash="password")
Expand All @@ -127,6 +128,11 @@ def tearDown(self):
except UserNotFoundError:
pass

@classmethod
def tearDownClass(cls):
cls.database.db.drop_collection(cls.test_config['collection_name'])
cls.database.shutdown()

def test_create_user(self):
# Create User
user = self.database.create_user(self.test_user)
Expand Down

0 comments on commit a50cf2d

Please sign in to comment.