読み方のヒント
矢印ボタン(または ← → キー)でページを移動できます。
テストの前ごとに、データを入れ直します。
@pytest.fixture(autouse=True)
def reset_data(create_tables):
with engine.begin() as connection:
connection.execute(text(
"TRUNCATE reservation_requests, reservation_items, reservations, products, users"
))
run_sql_file("komorebi_data.sql")
with Session(engine) as session:
for user in session.query(User).all():
user.password_hash = create_tables[user.username]
session.commit()
yield
TRUNCATE は、表の中身をすべて消すSQLです。外部キーでつながった表は、まとめて指定します。そのあとで、初期データ(利用者4人、商品8つ、予約3件)を入れ直します。
scope を書かない fixture は、テストごとに実行されます。どのテストも、予約が3件ある状態から始まります。
def test_each_test_starts_from_initial_data():
assert count_reservations() == 3
このテストは、予約を作るテストのあとに実行されても成功します。
1回目で作った予約が残っているため、2回目は5件になり、テストが失敗します。テストの結果が、前に実行したテストに左右されてしまいます。
7 / 20