Skip to content

Commit

Permalink
tests: fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jrcastro2 committed Jun 12, 2023
1 parent d5caca6 commit 72e31a6
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 33 deletions.
2 changes: 1 addition & 1 deletion invenio_users_resources/ext.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,4 +85,4 @@ def _after_rollback(session):
# rollbacked. In a case (e.g. nested transactions) when an
# exception is caught and then the session is commited,
# the sets might not reflect all the users that were changed.
current_db_change_history._clear_dirty_sets(session)
current_db_change_history.clear_dirty_sets(session)
2 changes: 1 addition & 1 deletion invenio_users_resources/records/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ class GroupAggregate(Record):
"""The group's name."""

description = DictField("description")
"""The group's name."""
"""The group's description."""

is_managed = DictField("is_managed")
"""If the group is managed manually."""
Expand Down
44 changes: 21 additions & 23 deletions invenio_users_resources/records/hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ def pre_commit(sender, session):
# it seems that the {dirty,new,deleted} sets aren't populated
# in after_commit anymore, that's why we need to collect the
# information here.
# session is a scoped_session and does not have _model_changes
# so we have rely only on .dirty
# session is a scoped_session and does not have _model_changes,
# so we have to rely only on .dirty
updated = session.dirty.union(session.new)
deleted = session.deleted
sid = id(session)
Expand All @@ -31,19 +31,17 @@ def pre_commit(sender, session):

# users need to be reindexed if their user model was updated, or
# their profile was changed (or even possibly deleted)
current_db_change_history.updated_users[sid].extend(
[u.id for u in updated if isinstance(u, User)]
)
current_db_change_history.updated_roles[sid].extend(
[r.id for r in updated if isinstance(r, Role)]
)
for item in updated:
if isinstance(item, User):
current_db_change_history.add_updated_user(sid, item.id)
if isinstance(item, Role):
current_db_change_history.add_updated_role(sid, item.id)

current_db_change_history.deleted_users[sid].extend(
[u.id for u in deleted if isinstance(u, User)]
)
current_db_change_history.deleted_roles[sid].extend(
[r.id for r in deleted if isinstance(r, Role)]
)
for item in deleted:
if isinstance(item, User):
current_db_change_history.add_deleted_user(sid, item.id)
if isinstance(item, Role):
current_db_change_history.add_deleted_role(sid, item.id)


def post_commit(sender, session):
Expand All @@ -52,15 +50,15 @@ def post_commit(sender, session):
# DB operations are allowed here, not even lazy-loading of
# properties!
sid = id(session)
for user_id in current_db_change_history.updated_users[sid]:
reindex_user.delay(user_id)
if current_db_change_history.sessions.get(sid):
for user_id in current_db_change_history.sessions[sid].updated_users:
reindex_user.delay(user_id)

for role_id in current_db_change_history.updated_roles[sid]:
reindex_group.delay(role_id)
for role_id in current_db_change_history.sessions[sid].updated_roles:
reindex_group.delay(role_id)

for user_id in current_db_change_history.deleted_users[sid]:
unindex_user.delay(user_id)
for role_id in current_db_change_history.deleted_roles[sid]:
unindex_group.delay(role_id)
for user_id in current_db_change_history.sessions[sid].deleted_users:
unindex_user.delay(user_id)

current_db_change_history._clear_dirty_sets(session)
for role_id in current_db_change_history.sessions[sid].deleted_roles:
unindex_group.delay(role_id)
19 changes: 18 additions & 1 deletion run-tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,23 @@ set -o errexit
# Quit on unbound symbols
set -o nounset

keep_services=0
pytest_args=()
for arg in $@; do
# from the CLI args, filter out some known values and forward the rest to "pytest"
# note: we don't use "getopts" here b/c of some limitations (e.g. long options),
# which means that we can't combine short options (e.g. "./run-tests -Kk pattern")
case ${arg} in
-K|--keep-services)
keep_services=1
;;
*)
pytest_args+=( ${arg} )
;;
esac
done


# Always bring down docker services
function cleanup() {
eval "$(docker-services-cli down --env)"
Expand All @@ -29,6 +46,6 @@ python -m check_manifest
python -m setup extract_messages --output-file /dev/null
python -m sphinx.cmd.build -qnNW docs docs/_build/html
eval "$(docker-services-cli up --db ${DB:-postgresql} --search ${SEARCH:-opensearch} --cache ${CACHE:-redis} --mq ${MQ:-rabbitmq} --env)"
python -m pytest
python -m pytest ${pytest_args[@]+"${pytest_args[@]}"}
tests_exit_code=$?
exit "$tests_exit_code"
34 changes: 27 additions & 7 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -213,9 +213,11 @@ def users(UserFixture, app, database, users_data):
return users


def _create_group(name, description, database):
def _create_group(id, name, description, is_managed, database):
"""Creates a Role/Group."""
r = current_datastore.create_role(name=name, description=description)
r = current_datastore.create_role(
id=id, name=name, description=description, is_managed=is_managed
)
current_datastore.commit()

return r
Expand All @@ -224,20 +226,38 @@ def _create_group(name, description, database):
@pytest.fixture(scope="module")
def group(database):
"""A single group."""
r = _create_group(name="it-dep", description="IT Department", database=database)
r = _create_group(
id="it-dep",
name="it-dep",
description="IT Department",
is_managed=True,
database=database,
)

GroupAggregate.index.refresh()
return r


@pytest.fixture(scope="module")
def groups(database, group):
def group2(database):
"""A single group."""
roles = [group] # it-dep
roles.append(
_create_group(name="hr-dep", description="HR Department", database=database)
r = _create_group(
id="hr-dep",
name="hr-dep",
description="HR Department",
is_managed=True,
database=database,
)

GroupAggregate.index.refresh()
return r


@pytest.fixture(scope="module")
def groups(database, group, group2):
"""A single group."""
roles = [group, group2]

GroupAggregate.index.refresh()
return roles

Expand Down

0 comments on commit 72e31a6

Please sign in to comment.