Skip to content

Commit

Permalink
Fix database fixture and password unit test (#1120)
Browse files Browse the repository at this point in the history
  • Loading branch information
sea-kelp authored Aug 2, 2024
1 parent 43f3ac9 commit d7260c6
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 21 deletions.
31 changes: 14 additions & 17 deletions OpenOversight/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -300,30 +300,27 @@ def db(app):
with app.app_context():
_db.app = app
_db.create_all()
connection = _db.engine.connect()
session = scoped_session(session_factory=sessionmaker(bind=connection))
_db.session = session
add_mockdata(session)
session.commit()
connection.close()
session.remove()
with _db.engine.begin() as connection:
_db.session = scoped_session(sessionmaker(bind=connection))
add_mockdata(_db.session)
_db.session.remove()

yield _db


@pytest.fixture(scope="function")
def session(db):
"""Creates a new database session for a test."""
with db.engine.connect() as connection:
with connection.begin():
session = scoped_session(session_factory=sessionmaker(bind=connection))
db.session = session

try:
yield session
finally:
session.remove()
connection.close()
with db.engine.connect() as connection, connection.begin() as tx:
db.session = scoped_session(sessionmaker(bind=connection))

try:
yield db.session
finally:
db.session.remove()
# If an error was raised during test, tx is already rolled back
if tx.is_active:
tx.rollback()


@pytest.fixture
Expand Down
5 changes: 1 addition & 4 deletions OpenOversight/tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,11 +173,8 @@ def test_salary_repr(mockdata):
def test_password_not_printed(mockdata):
"""Validate that password fields cannot be directly accessed."""
user = User(password="bacon")
try:
with raises(AttributeError):
print(user.password)
except Exception as e:
assert isinstance(e, AttributeError)
assert str(e) == "password is not a readable attribute"


def test_password_set_success(mockdata):
Expand Down

0 comments on commit d7260c6

Please sign in to comment.