modify conftest & add testcase

This commit is contained in:
2024-12-07 14:22:24 +09:00
parent 7e9654ab4c
commit 29501f785f
3 changed files with 109 additions and 20 deletions

View File

@@ -15,7 +15,7 @@ engine = create_engine(SQLALCHEMY_DATABASE_URI,echo=True)
test_session_maker = sessionmaker(autocommit=False, autoflush=False, bind=engine)
@pytest.fixture(scope="function")
@pytest.fixture(scope="session")
def test_db():
connection = engine.connect()
transaction = connection.begin()
@@ -25,7 +25,7 @@ def test_db():
transaction.rollback()
connection.close()
@pytest.fixture(scope="function")
@pytest.fixture(scope="session")
def test_client(test_db):
def get_test_db():
try:
@@ -56,9 +56,24 @@ def test_user(test_db):
dicUser["password"] = password
return dicUser
@pytest.fixture(scope="function")
def test_admin(test_db):
password ="admin"
@pytest.fixture(scope="session")
def password():
return "password"
@pytest.fixture(scope="session")
def user(password):
user = models.User(
email = "user@test.com",
first_name = "user",
last_name = "abc",
hashed_password = security.get_password_hash(password),
is_active = True,
is_superuser = False
)
return user
@pytest.fixture(scope="session")
def admin(password):
user = models.User(
email = "admin@test.com",
first_name = "admin",
@@ -67,12 +82,23 @@ def test_admin(test_db):
is_active = True,
is_superuser = True
)
return user
@pytest.fixture(scope="session")
def login_user(test_db,test_client,user,password):
test_db.add(user)
test_db.commit()
test_db.refresh(user)
dicUser = user.__dict__
dicUser["password"] = password
return dicUser
response = test_client.post("/api/token", data={"username": user.email, "password":password })
return response.json()["access_token"]
@pytest.fixture(scope="session")
def login_admin(test_db,test_client,admin,password):
test_db.add(admin)
test_db.commit()
test_db.refresh(admin)
response = test_client.post("/api/token", data={"username": admin.email, "password":password })
return response.json()["access_token"]
# @pytest.fixture
# def test_password() -> str: