Add shopping cart and related models
This commit is contained in:
@@ -1,14 +1,38 @@
|
||||
import os
|
||||
from unittest import mock
|
||||
|
||||
import pytest
|
||||
from fastapi.testclient import TestClient
|
||||
from sqlalchemy import create_engine
|
||||
from sqlalchemy import StaticPool, create_engine
|
||||
from sqlalchemy.orm import sessionmaker
|
||||
|
||||
from allmende_payment_system.api.dependencies import get_session
|
||||
from allmende_payment_system.app import app
|
||||
from allmende_payment_system.models import Base
|
||||
|
||||
|
||||
def make_db():
|
||||
engine = create_engine(
|
||||
"sqlite:///:memory:",
|
||||
connect_args={"check_same_thread": False},
|
||||
poolclass=StaticPool,
|
||||
)
|
||||
Base.metadata.create_all(bind=engine) # Create tables
|
||||
return sessionmaker(autocommit=False, autoflush=False, bind=engine)
|
||||
|
||||
|
||||
def make_in_memory_session():
|
||||
db = make_db()
|
||||
session = db()
|
||||
try:
|
||||
yield session
|
||||
finally:
|
||||
session.close()
|
||||
|
||||
|
||||
app.dependency_overrides[get_session] = make_in_memory_session
|
||||
|
||||
|
||||
@pytest.fixture(scope="session")
|
||||
def client():
|
||||
os.environ["APS_username"] = "test"
|
||||
@@ -23,13 +47,5 @@ def unauthorized_client():
|
||||
|
||||
@pytest.fixture
|
||||
def test_db():
|
||||
engine = create_engine("sqlite:///:memory:")
|
||||
Base.metadata.create_all(bind=engine) # Create tables
|
||||
TestingSessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
|
||||
|
||||
# Provide a session and the engine
|
||||
db = TestingSessionLocal()
|
||||
try:
|
||||
yield db
|
||||
finally:
|
||||
db.close()
|
||||
db = make_db()
|
||||
return db()
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
from allmende_payment_system.models import Account
|
||||
|
||||
|
||||
def test_unauthorized_access(unauthorized_client):
|
||||
def test_unauthorized_access(unauthorized_client, test_db):
|
||||
response = unauthorized_client.get("/")
|
||||
assert response.status_code == 401
|
||||
|
||||
|
||||
def test_authorized_access(client):
|
||||
def test_authorized_access(client, test_db):
|
||||
response = client.get("/")
|
||||
assert response.status_code == 200
|
||||
|
||||
@@ -1,16 +1,28 @@
|
||||
import pytest
|
||||
|
||||
from allmende_payment_system.models import Account, User
|
||||
|
||||
|
||||
def test_user_model(test_db):
|
||||
@pytest.fixture(scope="function")
|
||||
def test_user(test_db):
|
||||
user = User(username="test", display_name="Test User")
|
||||
test_db.add(user)
|
||||
test_db.commit()
|
||||
test_db.flush()
|
||||
return user
|
||||
|
||||
assert user.id is not None
|
||||
|
||||
def test_user_model(test_db, test_user):
|
||||
assert test_user.id is not None
|
||||
|
||||
account = Account(name="Test Account")
|
||||
account.users.append(user)
|
||||
account.users.append(test_user)
|
||||
test_db.add(account)
|
||||
test_db.commit()
|
||||
test_db.flush()
|
||||
|
||||
assert len(user.accounts) == 1
|
||||
assert len(test_user.accounts) == 1
|
||||
|
||||
|
||||
def test_user_shopping_cart_new(test_db, test_user):
|
||||
cart = test_user.shopping_cart
|
||||
|
||||
assert len(cart.items) == 0
|
||||
|
||||
Reference in New Issue
Block a user