auth service completed and tested

This commit is contained in:
2025-01-14 19:16:24 +03:00
parent 08b1815156
commit 486fadbfb3
33 changed files with 1325 additions and 248 deletions

13
Ztest/fixtures.py Normal file
View File

@@ -0,0 +1,13 @@
"""Test fixtures and models."""
from sqlalchemy import Column, String
from Services.PostgresDb.Models.mixins import CrudCollection
class TestUser(CrudCollection):
"""Test user model for PostgreSQL tests."""
__tablename__ = "test_users"
username = Column(String, unique=True, index=True)
email = Column(String, unique=True, index=True)

13
Ztest/models.py Normal file
View File

@@ -0,0 +1,13 @@
"""Test models."""
from sqlalchemy import Column, String
from Services.PostgresDb.Models.mixins import CrudCollection
class UserModel(CrudCollection):
"""User model for PostgreSQL tests."""
__tablename__ = "test_users"
username = Column(String, unique=True, index=True)
email = Column(String, unique=True, index=True)

View File

@@ -7,7 +7,7 @@ from Services.MongoDb.Models.actions import MongoActions
from Services.MongoDb.Models.action_models.domain import (
DomainData,
DomainDocumentCreate,
DomainDocumentUpdate
DomainDocumentUpdate,
)
from AllConfigs.NoSqlDatabase.configs import MongoConfig
@@ -17,7 +17,7 @@ def mongo_client():
"""Create MongoDB test client."""
# Connect using configured credentials
client = MongoClient(MongoConfig.URL)
client.admin.command('ping') # Test connection
client.admin.command("ping") # Test connection
yield client
client.close()
@@ -27,12 +27,12 @@ def mongo_actions(mongo_client):
"""Create MongoActions instance for testing."""
if not mongo_client:
pytest.skip("MongoDB connection not available")
actions = MongoActions(
client=mongo_client,
database=MongoConfig.DATABASE_NAME,
company_uuid="test_company",
storage_reason="domains"
storage_reason="domains",
)
yield actions
try:
@@ -45,37 +45,36 @@ def mongo_actions(mongo_client):
def test_mongo_crud_operations(mongo_actions: MongoActions):
"""Test CRUD operations with MongoActions."""
# Create test data
domain_data = DomainData(
user_uu_id="test_user",
main_domain="example.com",
other_domains_list=["old.com"]
other_domains_list=["old.com"],
)
create_doc = DomainDocumentCreate(data=domain_data)
# Test create
result = mongo_actions.insert_one(create_doc.model_dump())
assert result.inserted_id is not None
# Test read
doc = mongo_actions.find_one({"data.main_domain": "example.com"})
assert doc is not None
assert doc["data"]["main_domain"] == "example.com"
# Test update
update_data = DomainData(
user_uu_id="test_user",
main_domain="new.com",
other_domains_list=["example.com", "old.com"]
other_domains_list=["example.com", "old.com"],
)
update_doc = DomainDocumentUpdate(data=update_data)
result = mongo_actions.update_one(
{"_id": doc["_id"]},
{"$set": update_doc.model_dump()}
{"_id": doc["_id"]}, {"$set": update_doc.model_dump()}
)
assert result.modified_count == 1
# Test delete
result = mongo_actions.delete_one({"_id": doc["_id"]})
assert result.deleted_count == 1
@@ -83,23 +82,18 @@ def test_mongo_crud_operations(mongo_actions: MongoActions):
def test_mongo_aggregate(mongo_actions: MongoActions):
"""Test aggregate operations with MongoActions."""
# Insert test documents
docs = [
DomainDocumentCreate(
data=DomainData(
user_uu_id="user1",
main_domain=f"domain{i}.com"
)
data=DomainData(user_uu_id="user1", main_domain=f"domain{i}.com")
).model_dump()
for i in range(3)
]
mongo_actions.insert_many(docs)
# Test aggregation
pipeline = [
{"$group": {"_id": "$data.user_uu_id", "count": {"$sum": 1}}}
]
pipeline = [{"$group": {"_id": "$data.user_uu_id", "count": {"$sum": 1}}}]
result = mongo_actions.aggregate(pipeline)
result_list = list(result)
assert len(result_list) == 1
@@ -107,4 +101,4 @@ def test_mongo_aggregate(mongo_actions: MongoActions):
if __name__ == "__main__":
pytest.main([__file__, "-v"])
pytest.main([__file__, "-v"])

View File

@@ -1,21 +1,12 @@
"""Test PostgreSQL database operations."""
import pytest
from sqlalchemy import Column, String, create_engine, text
from sqlalchemy import create_engine, text
from sqlalchemy.orm import Session
from Services.PostgresDb.database import Base, get_db
from Services.PostgresDb.Models.mixins import CrudCollection
from AllConfigs.SqlDatabase.configs import WagDatabase
class TestUser(CrudCollection):
"""Test user model for PostgreSQL tests."""
__tablename__ = "test_users"
username = Column(String, unique=True, index=True)
email = Column(String, unique=True, index=True)
from Ztest.models import UserModel
@pytest.fixture(scope="session")
@@ -24,12 +15,12 @@ def db_engine():
# Use the same database URL but with test database
test_db_url = WagDatabase.DATABASE_URL
engine = create_engine(test_db_url, echo=True)
# Create all tables
Base.metadata.create_all(bind=engine)
yield engine
# Drop all tables after tests
Base.metadata.drop_all(bind=engine)
@@ -40,9 +31,9 @@ def db_session(db_engine):
connection = db_engine.connect()
transaction = connection.begin()
session = Session(bind=connection)
yield session
# Rollback the transaction after each test
transaction.rollback()
connection.close()
@@ -52,12 +43,12 @@ def db_session(db_engine):
def test_create_user(db_session):
"""Test creating a user in the database."""
# Create user using CrudMixin methods
user = TestUser(username="testuser", email="test@example.com")
user = UserModel(username="testuser", email="test@example.com")
db_session.add(user)
db_session.commit()
# Verify user was created
db_user = db_session.query(TestUser).filter_by(username="testuser").first()
db_user = db_session.query(UserModel).filter_by(username="testuser").first()
assert db_user is not None
assert db_user.email == "test@example.com"
assert db_user.created_at is not None
@@ -68,16 +59,16 @@ def test_create_user(db_session):
def test_update_user(db_session):
"""Test updating a user in the database."""
# Create user
user = TestUser(username="updateuser", email="update@example.com")
user = UserModel(username="updateuser", email="update@example.com")
db_session.add(user)
db_session.commit()
# Update user using CrudMixin methods
user.update(session=db_session, email="newemail@example.com")
user.update(db=db_session, email="newemail@example.com")
db_session.commit()
# Verify update
updated_user = db_session.query(TestUser).filter_by(username="updateuser").first()
updated_user = db_session.query(UserModel).filter_by(username="updateuser").first()
assert updated_user.email == "newemail@example.com"
assert updated_user.updated_at is not None
@@ -85,16 +76,16 @@ def test_update_user(db_session):
def test_soft_delete_user(db_session):
"""Test soft deleting a user from the database."""
# Create user
user = TestUser(username="deleteuser", email="delete@example.com")
user = UserModel(username="deleteuser", email="delete@example.com")
db_session.add(user)
db_session.commit()
# Soft delete by updating deleted and active flags
user.update(session=db_session, deleted=True, active=False)
user.update(db=db_session, deleted=True, active=False)
db_session.commit()
# Verify soft deletion
deleted_user = db_session.query(TestUser).filter_by(username="deleteuser").first()
deleted_user = db_session.query(UserModel).filter_by(username="deleteuser").first()
assert deleted_user is not None
assert deleted_user.deleted
assert not deleted_user.active
@@ -105,4 +96,4 @@ def test_get_db_context_manager():
with get_db() as session:
# Verify we can execute a simple query
result = session.execute(text("SELECT 1"))
assert result.scalar() == 1
assert result.scalar() == 1