Skip to content
This repository has been archived by the owner on Feb 23, 2022. It is now read-only.

Commit

Permalink
Merge pull request #367 from multinet-app/fix_caching
Browse files Browse the repository at this point in the history
Fix Caching
  • Loading branch information
jjnesbitt committed Apr 28, 2020
2 parents 7bc7620 + 903b22d commit ddd6347
Show file tree
Hide file tree
Showing 2 changed files with 140 additions and 1 deletion.
11 changes: 10 additions & 1 deletion multinet/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,8 @@ def workspace_mapping(name: str) -> Union[Dict[str, str], None]:

def workspace_exists(name: str) -> bool:
"""Convinience wrapper for checking if a workspace exists."""
return bool(workspace_mapping(name))
# Use un-cached underlying function
return bool(workspace_mapping.__wrapped__(name))


def workspace_exists_internal(name: str) -> bool:
Expand All @@ -129,6 +130,10 @@ def create_workspace(name: str) -> None:
# Could only happen if there's a name collisison
raise InternalServerError()

# Invalidate the cache for things changed by this function
workspace_mapping.cache_clear()
get_workspace_db.cache_clear()


def rename_workspace(old_name: str, new_name: str) -> None:
"""Rename a workspace."""
Expand Down Expand Up @@ -160,6 +165,10 @@ def delete_workspace(name: str) -> None:
sysdb.delete_database(doc["internal"])
coll.delete(doc["_id"])

# Invalidate the cache for things changed by this function
get_workspace_db.cache_clear()
workspace_mapping.cache_clear()


def get_workspace(name: str) -> WorkspaceSpec:
"""Return a single workspace, if it exists."""
Expand Down
130 changes: 130 additions & 0 deletions test/test_workspace.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
"""Test that workspace operations act like we expect them to."""
import pytest

from uuid import uuid4
from multinet.db import (
create_workspace,
delete_workspace,
rename_workspace,
workspace_exists,
workspace_mapping,
)


@pytest.fixture
def generated_workspace():
"""Create a workspace, and yield the name of the workspace."""

workspace_name = uuid4().hex

create_workspace(workspace_name)
return workspace_name


@pytest.fixture
def handled_workspace(generated_workspace):
"""
Create a workspace, and yield the name of the workspace.
On teardown, deletes the workspace.
"""
yield generated_workspace

delete_workspace(generated_workspace)


# BEGIN TESTS


@pytest.mark.skip()
def test_present_workspace(handled_workspace):
"""Test that workspace caching works as expected on present workspaces."""

# Assert that the cached response matches the actual response
assert workspace_mapping.__wrapped__(handled_workspace) == workspace_mapping(
handled_workspace
)
workspace_mapping.cache_clear()

first_resp = workspace_mapping(handled_workspace)
second_resp = workspace_mapping(handled_workspace)

# Assert that cached response is idempotent
assert first_resp == second_resp


@pytest.mark.skip()
def test_absent_workspace():
"""Test that workspace caching works as expected on absent workspaces."""

# Test that random workspace doesn't exist
assert workspace_mapping.__wrapped__(uuid4().hex) is None
workspace_mapping.cache_clear()

workspace_name = uuid4().hex
first_resp = workspace_mapping(workspace_name)
second_resp = workspace_mapping(workspace_name)

# Test that fake workspace doesn't exist,
# and that calls are idempotent
assert first_resp is None
assert second_resp is None


@pytest.mark.skip()
def test_workspace_create():
"""Test that creating a workspace doesn't result in invalid caching."""
workspace_name = uuid4().hex

pre_create = workspace_mapping(workspace_name)
create_workspace(workspace_name)
post_create = workspace_mapping(workspace_name)
post_create_exists = workspace_exists(workspace_name)

# Teardown
delete_workspace(workspace_name)

# Asserts
assert pre_create is None
assert post_create is not None
assert post_create_exists


@pytest.mark.skip()
def test_workspace_delete(generated_workspace):
"""Tests that deleting a workspace doesn't result in invalid caching."""

pre_delete = workspace_mapping(generated_workspace)
delete_workspace(generated_workspace)
post_delete = workspace_mapping(generated_workspace)
exists_post_delete = workspace_exists(generated_workspace)

# Asserts
assert pre_delete is not None
assert post_delete is None
assert not exists_post_delete


@pytest.mark.skip()
def test_workspace_rename(generated_workspace):
"""Test that renaming a workspace doesn't result in invalid caching."""
new_workspace_name = uuid4().hex

pre_rename = workspace_mapping(generated_workspace)
rename_workspace(generated_workspace, new_workspace_name)
post_rename_old = workspace_mapping(generated_workspace)
post_rename_new = workspace_mapping(new_workspace_name)

new_exists = workspace_exists(new_workspace_name)
old_exists = workspace_exists(generated_workspace)

# Teardown
delete_workspace(new_workspace_name)

# Asserts
assert pre_rename is not None
assert post_rename_old is None
assert post_rename_new is not None

assert new_exists
assert not old_exists

0 comments on commit ddd6347

Please sign in to comment.