-
Notifications
You must be signed in to change notification settings - Fork 77
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update all fields on AdvisorData upsert (#155)
* update all fields on upsert * add a test for zeroing out lastAuthenticated, silence logs in tests, rename fixtures * add docstrings to fixtures * post-rebase fixes
- Loading branch information
1 parent
191adf3
commit db8746b
Showing
4 changed files
with
255 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import pytest | ||
from flask_sqlalchemy import SQLAlchemy | ||
|
||
|
||
@pytest.fixture(scope="function") | ||
def app_config(): | ||
"""Return a Flask configuration object for testing. The returned configuration is intended to be a good base for | ||
testing and can be customized for specific testing needs. | ||
""" | ||
from flask import Config | ||
c = Config('.') | ||
c.from_mapping({ | ||
"SQLALCHEMY_DATABASE_URI": "sqlite:///:memory:", | ||
"SQLALCHEMY_TRACK_MODIFICATIONS": False, | ||
"DEBUG": False, | ||
"LOG_CFG": {'version': 1, 'handlers': []}, # silence logging | ||
}) | ||
return c | ||
|
||
|
||
@pytest.fixture(scope="function") | ||
def mock_database(app_config): | ||
"""Yield an instance of flask_sqlalchemy.SQLAlchemy associated with the base model class used in aardvark.model. | ||
This is almost certainly not safe for parallel/multi-threaded use. | ||
""" | ||
from aardvark.app import db | ||
mock_db = SQLAlchemy(model_class=db.Model) | ||
|
||
from aardvark.app import create_app | ||
app = create_app(config_override=app_config) | ||
with app.app_context(): | ||
mock_db.create_all() | ||
yield mock_db | ||
mock_db.drop_all() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,146 @@ | ||
def test_advisor_data_create(mock_database): | ||
from aardvark.model import AdvisorData, db | ||
AdvisorData.create_or_update( | ||
9999, | ||
1111, | ||
"Swifties United", | ||
"swift", | ||
"taylor", | ||
1, | ||
) | ||
db.session.commit() | ||
|
||
record: AdvisorData = AdvisorData.query.filter( | ||
AdvisorData.item_id == 9999, | ||
AdvisorData.serviceNamespace == "swift", | ||
).scalar() | ||
assert record | ||
assert record.id | ||
assert record.item_id == 9999 | ||
assert record.lastAuthenticated == 1111 | ||
assert record.lastAuthenticatedEntity == "taylor" | ||
assert record.serviceName == "Swifties United" | ||
assert record.serviceNamespace == "swift" | ||
assert record.totalAuthenticatedEntities == 1 | ||
|
||
|
||
def test_advisor_data_update(mock_database): | ||
from aardvark.model import AdvisorData, db | ||
AdvisorData.create_or_update( | ||
9999, | ||
0, | ||
"Pink Pony Club", | ||
"roan", | ||
None, | ||
0, | ||
) | ||
db.session.commit() | ||
|
||
record: AdvisorData = db.session.query(AdvisorData).filter( | ||
AdvisorData.id == 1, | ||
).scalar() | ||
assert record | ||
assert record.lastAuthenticated == 0 | ||
|
||
AdvisorData.create_or_update( | ||
9999, | ||
1111, | ||
"Pink Pony Club", | ||
"roan", | ||
"chappell", | ||
1, | ||
) | ||
db.session.commit() | ||
|
||
record: AdvisorData = db.session.query(AdvisorData).filter( | ||
AdvisorData.id == 1, | ||
).scalar() | ||
assert record | ||
assert record.item_id == 9999 | ||
assert record.lastAuthenticated == 1111 | ||
assert record.lastAuthenticatedEntity == "chappell" | ||
assert record.serviceName == "Pink Pony Club" | ||
assert record.serviceNamespace == "roan" | ||
assert record.totalAuthenticatedEntities == 1 | ||
|
||
|
||
def test_advisor_data_update_older_last_authenticated(mock_database): | ||
from aardvark.model import AdvisorData, db | ||
AdvisorData.create_or_update( | ||
9999, | ||
1111, | ||
"Pink Pony Club", | ||
"roan", | ||
"chappell", | ||
1, | ||
) | ||
db.session.commit() | ||
|
||
record: AdvisorData = db.session.query(AdvisorData).filter( | ||
AdvisorData.id == 1, | ||
).scalar() | ||
assert record | ||
assert record.lastAuthenticated == 1111 | ||
|
||
# Calling create_or_update with a lower lastAuthenticated value should NOT update lastAuthenticated in the DB | ||
AdvisorData.create_or_update( | ||
9999, | ||
1000, | ||
"Pink Pony Club", | ||
"roan", | ||
"chappell", | ||
1, | ||
) | ||
db.session.commit() | ||
|
||
record: AdvisorData = db.session.query(AdvisorData).filter( | ||
AdvisorData.id == 1, | ||
).scalar() | ||
assert record | ||
assert record.item_id == 9999 | ||
assert record.lastAuthenticated == 1111 | ||
assert record.lastAuthenticatedEntity == "chappell" | ||
assert record.serviceName == "Pink Pony Club" | ||
assert record.serviceNamespace == "roan" | ||
assert record.totalAuthenticatedEntities == 1 | ||
|
||
|
||
def test_advisor_data_update_zero_last_authenticated(mock_database): | ||
from aardvark.model import AdvisorData, db | ||
AdvisorData.create_or_update( | ||
9999, | ||
1111, | ||
"Pink Pony Club", | ||
"roan", | ||
"chappell", | ||
1, | ||
) | ||
db.session.commit() | ||
|
||
record: AdvisorData = db.session.query(AdvisorData).filter( | ||
AdvisorData.id == 1, | ||
).scalar() | ||
assert record | ||
assert record.lastAuthenticated == 1111 | ||
|
||
# Calling create_or_update with a zero lastAuthenticated value SHOULD update lastAuthenticated in the DB | ||
AdvisorData.create_or_update( | ||
9999, | ||
0, | ||
"Pink Pony Club", | ||
"roan", | ||
"", | ||
0, | ||
) | ||
db.session.commit() | ||
|
||
record: AdvisorData = db.session.query(AdvisorData).filter( | ||
AdvisorData.id == 1, | ||
).scalar() | ||
assert record | ||
assert record.item_id == 9999 | ||
assert record.lastAuthenticated == 0 | ||
assert record.lastAuthenticatedEntity == "" | ||
assert record.serviceName == "Pink Pony Club" | ||
assert record.serviceNamespace == "roan" | ||
assert record.totalAuthenticatedEntities == 0 |