Skip to content

Commit

Permalink
added tests for representations.py still work in progress
Browse files Browse the repository at this point in the history
  • Loading branch information
CharliesPants committed Mar 8, 2024
1 parent b5af26b commit 5bf22ca
Show file tree
Hide file tree
Showing 2 changed files with 232 additions and 2 deletions.
1 change: 1 addition & 0 deletions fractal_database_matrix/representations.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ async def create_representation(
)

# accept invites to room
print(target.matrixcredentials_set.all())
for account in target.matrixcredentials_set.all():
await account.accept_invite(room_id)

Expand Down
233 changes: 231 additions & 2 deletions tests/test_representations.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,231 @@
async def test_it_works():
pass
from unittest.mock import AsyncMock, MagicMock, patch

import pytest
from asgiref.sync import sync_to_async
from django.contrib.contenttypes.fields import GenericForeignKey, GenericRelation
from fractal.cli.controllers.auth import AuthController, AuthenticatedController
from fractal.matrix.async_client import FractalAsyncClient
from fractal_database.models import (
ReplicatedModel,
ReplicationTarget,
RepresentationLog,
)
from fractal_database.representations import Representation
from fractal_database_matrix.models import MatrixCredentials, MatrixReplicationTarget
from fractal_database_matrix.representations import (
MatrixRepresentation,
MatrixRoom,
MatrixSpace,
MatrixSubSpace,
)

pytestmark = pytest.mark.django_db(transaction=True)


async def test_put_state_no_creds():
matrix_representation = MatrixRepresentation()

room_id = "room_id"
target = MagicMock()
state_type = "state_type"
content = {}

with patch(
"fractal.cli.controllers.auth.AuthenticatedController.get_creds", return_value=None
):
with pytest.raises(Exception) as e:
await matrix_representation.put_state(room_id, target, state_type, content)
assert str(e.value) == "You must be logged in to put state"


async def test_create_room_no_creds():
matrix_representation = MatrixRepresentation()

target = MagicMock()
name = "Test Room"
initial_state = []
public = False
invite = []

with patch(
"fractal.cli.controllers.auth.AuthenticatedController.get_creds", return_value=None
):
with pytest.raises(Exception) as e:
await matrix_representation.create_room(
target, name, initial_state=initial_state, public=public, invite=invite
)
assert str(e.value) == "You must be logged in to create a room"


async def test_create_room_invalid_invite_uppercase(
logged_in_db_auth_controller: AuthenticatedController,
):
matrix_representation = MatrixRepresentation()
creds = AuthenticatedController().get_creds()

target = MagicMock()
name = "Test Room"
initial_state = []
public = False
# Simulate uppercase Matrix IDs in the invite list
invite = ["@Test:Upper.com"]

with patch(
"fractal.cli.controllers.auth.AuthenticatedController.get_creds", return_value=creds
):
with pytest.raises(Exception) as e:
await matrix_representation.create_room(
target, name, initial_state=initial_state, public=public, invite=invite
)
assert str(e.value) == "Matrix IDs must be lowercase"


async def test_create_room_representation_success():
matrix_representation = MatrixRepresentation()
target = MagicMock()
room_id = "test room"

with patch(
"fractal.cli.controllers.auth.AuthenticatedController.get_creds",
return_value=("access_token", "homeserver_url", "matrix_id"),
), patch(
"fractal.matrix.async_client.FractalAsyncClient.room_create",
return_value=MagicMock(room_id="room_id"),
), patch(
"builtins.print"
) as mocked_print:
await matrix_representation.create_room(target, room_id, invite=["@test:room.com"])

mocked_print.assert_called_with(
f"Successfully created representation of {room_id} in Matrix: room_id"
)


async def test_add_subspace_no_creds():
matrix_representation = MatrixRepresentation()

target = MagicMock()
parent_room_id = "parent_room_id"
child_room_id = "child_room_id"

with patch(
"fractal.cli.controllers.auth.AuthenticatedController.get_creds", return_value=None
):
with pytest.raises(Exception) as e:
await matrix_representation.add_subspace(target, parent_room_id, child_room_id)
assert str(e.value) == "You must be logged in to add a subspace"


async def test_add_subspace_success_print():
# Mock required objects
matrix_representation = MatrixRepresentation()
target = MagicMock()
parent_room_id = "parent_room_id"
child_room_id = "child_room_id"

# Patch the necessary method
with patch(
"fractal.cli.controllers.auth.AuthenticatedController.get_creds",
return_value=("access_token", "homeserver_url", "matrix_id"),
), patch(
"fractal.matrix.async_client.FractalAsyncClient.room_put_state"
) as mocked_room_put_state, patch(
"builtins.print"
) as mocked_print:
# Invoke the method
await matrix_representation.add_subspace(target, parent_room_id, child_room_id)

# Assert that print was called with the correct message
mocked_print.assert_called_with(
f"Successfully added child space {child_room_id} to parent space {parent_room_id}"
)


async def test_create_representation_no_name():
mock_repr_log = MagicMock()
mock_repr_log.metadata = {} # Empty metadata without "name"
mock_target = MagicMock()
matrix_room = MatrixRoom()

with patch.object(matrix_room, "create_room") as mock_create_room:

with pytest.raises(Exception) as e:
await matrix_room.create_representation(mock_repr_log, target_id=mock_target)

assert str(e.value) == "name must be specified in metadata"


async def test_create_representation_success(
logged_in_db_auth_controller: AuthenticatedController, test_database, test_device
):
instance = test_database
target = await MatrixReplicationTarget.objects.acreate(name="test_target")
# target.matrixcredentials_set = MagicMock()
creds = await test_device.matrixcredentials_set.aget()
await sync_to_async(target.matrixcredentials_set.add)(creds)
method = "some_method" # method not used in tested function

# Creating representation log for testing
repr_log = await RepresentationLog.objects.acreate(
instance=target,
method=method,
target=target,
metadata=target.repr_metadata_props(),
)

matrix_room = MatrixRoom()
matrix_room.create_room = AsyncMock(return_value="some_room_id")
with patch("fractal_database.signals._accept_invite", new=AsyncMock()) as mock_accept_invite:
meta_data = await matrix_room.create_representation(repr_log, "not_used")
assert meta_data["room_id"] == "some_room_id"
mock_accept_invite.assert_called()


async def test_create_representation_matrix_space(
logged_in_db_auth_controller: AuthenticatedController, test_database, test_device
):
instance = test_database
target = await MatrixReplicationTarget.objects.acreate(name="test_target")
creds = await test_device.matrixcredentials_set.aget()
await sync_to_async(target.matrixcredentials_set.add)(creds)
method = "some_method" # method not used in tested function

# Creating representation log for testing
repr_log = await RepresentationLog.objects.acreate(
instance=target,
method=method,
target=target,
metadata=target.repr_metadata_props(),
)

matrix_space = MatrixSpace()

# target.matrixcredentials_set.all.return_value = [] # Mocking empty matrix credentials

result = await matrix_space.create_representation(repr_log, target)

assert "room_id" in result


# Work in progress below
# create_representation logs is listed as a def in repesentations
# should that mean this funtions is a def and not async?
def test_create_representation_logs(
logged_in_db_auth_controller: AuthenticatedController, test_database, test_device
):
# not sure what cls is doing
# In the class Representation I see it used for method=cls.representaion_module
# cls

# the relationship b/w instance and Replication mode || target and Replication target
# look like a similar relationship as repr_log and RepresentationLog in create_representation
instance: "ReplicatedModel" = GenericForeignKey("target_type", "target_id") # abstract model
# I assume something like this should be correct
# instance = ReplicatedModel.objects.create()

# how does ReplicationTarget
# differ from MatrixReplicationTarget which
# I was using in the previoius funtions?
target: "ReplicationTarget" # abstract model

create_subspace = MatrixSpace.create_representation_logs(instance, target)

0 comments on commit 5bf22ca

Please sign in to comment.