Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rename ExampleWorkspace to ResourceWorkspace #272

Merged
merged 3 commits into from
Nov 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion config/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,7 @@
"primed.cdsa.adapters.CDSAWorkspaceAdapter",
"primed.miscellaneous_workspaces.adapters.ConsortiumDevelWorkspaceAdapter",
"primed.miscellaneous_workspaces.adapters.DataPrepWorkspaceAdapter",
"primed.miscellaneous_workspaces.adapters.ExampleWorkspaceAdapter",
"primed.miscellaneous_workspaces.adapters.ResourceWorkspaceAdapter",
"primed.miscellaneous_workspaces.adapters.SimulatedDataWorkspaceAdapter",
"primed.miscellaneous_workspaces.adapters.OpenAccessWorkspaceAdapter",
]
Expand Down
16 changes: 7 additions & 9 deletions primed/miscellaneous_workspaces/adapters.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,18 +36,16 @@ class ConsortiumDevelWorkspaceAdapter(BaseWorkspaceAdapter):
workspace_detail_template_name = "anvil_consortium_manager/workspace_detail.html"


class ExampleWorkspaceAdapter(BaseWorkspaceAdapter):
"""Adapter for ExampleWorkspaces."""
class ResourceWorkspaceAdapter(BaseWorkspaceAdapter):
"""Adapter for ResourceWorkspaces."""

type = "example"
name = "Example workspace"
description = (
"Workspaces containing examples of using AnVIL, working with data, etc."
)
type = "resource"
name = "Resource workspace"
description = "Workspaces containing consortium resources (e.g., examples of using AnVIL, data inventories)"
list_table_class = DefaultWorkspaceTable
workspace_form_class = WorkspaceForm
workspace_data_model = models.ExampleWorkspace
workspace_data_form_class = forms.ExampleWorkspaceForm
workspace_data_model = models.ResourceWorkspace
workspace_data_form_class = forms.ResourceWorkspaceForm
workspace_detail_template_name = "anvil_consortium_manager/workspace_detail.html"


Expand Down
6 changes: 3 additions & 3 deletions primed/miscellaneous_workspaces/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,11 @@ class Meta:
}


class ExampleWorkspaceForm(Bootstrap5MediaFormMixin, forms.ModelForm):
"""Form for a ExampleWorkspace object."""
class ResourceWorkspaceForm(Bootstrap5MediaFormMixin, forms.ModelForm):
"""Form for a ResourceWorkspace object."""

class Meta:
model = models.ExampleWorkspace
model = models.ResourceWorkspace
fields = (
"workspace",
"requested_by",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# Generated by Django 3.2.19 on 2023-11-13 20:11

from django.conf import settings
from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('anvil_consortium_manager', '0015_add_new_permissions'),
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
('miscellaneous_workspaces', '0008_dataprepworkspace_historicaldataprepworkspace'),
]

operations = [
migrations.RenameModel(
old_name='HistoricalExampleWorkspace',
new_name='HistoricalResourceWorkspace',
),
migrations.RenameModel(
old_name='ExampleWorkspace',
new_name='ResourceWorkspace',
),
migrations.AlterModelOptions(
name='historicalresourceworkspace',
options={'get_latest_by': ('history_date', 'history_id'), 'ordering': ('-history_date', '-history_id'), 'verbose_name': 'historical resource workspace', 'verbose_name_plural': 'historical resource workspaces'},
),
migrations.AlterField(
model_name='dataprepworkspace',
name='is_active',
field=models.BooleanField(default=True, help_text='Indicator of whether this workspace is currently being used to prepare data.'),
),
migrations.AlterField(
model_name='historicaldataprepworkspace',
name='is_active',
field=models.BooleanField(default=True, help_text='Indicator of whether this workspace is currently being used to prepare data.'),
),
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Generated by Django 3.2.19 on 2023-11-13 20:12

from django.db import migrations


def workspace_type_example_to_resource(apps, schema_editor):
"""Change the workspace type from "example" to "resource"."""
Workspace = apps.get_model("anvil_consortium_manager", "Workspace")

for row in Workspace.objects.filter(workspace_type="example"):
row.workspace_type = "resource"
row.save(update_fields=["workspace_type"])


def workspace_type_resource_to_example(apps, schema_editor):
"""Change the workspace type from "resource" to "example"."""
Workspace = apps.get_model("anvil_consortium_manager", "Workspace")

for row in Workspace.objects.filter(workspace_type="resource"):
row.workspace_type = "example"
row.save(update_fields=["workspace_type"])


class Migration(migrations.Migration):

dependencies = [
('miscellaneous_workspaces', '0009_rename_example_to_resource_workspace'),
]

operations = [
migrations.RunPython(workspace_type_example_to_resource, reverse_code=workspace_type_resource_to_example),
]
4 changes: 2 additions & 2 deletions primed/miscellaneous_workspaces/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ class ConsortiumDevelWorkspace(RequesterModel, TimeStampedModel, BaseWorkspaceDa
"""A model to track shared consortium development workspaces."""


class ExampleWorkspace(RequesterModel, TimeStampedModel, BaseWorkspaceData):
"""A model to track example workspaces."""
class ResourceWorkspace(RequesterModel, TimeStampedModel, BaseWorkspaceData):
"""A model to track resource workspaces."""


class TemplateWorkspace(TimeStampedModel, BaseWorkspaceData):
Expand Down
8 changes: 4 additions & 4 deletions primed/miscellaneous_workspaces/tests/factories.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,17 @@ class Meta:
model = models.ConsortiumDevelWorkspace


class ExampleWorkspaceFactory(DjangoModelFactory):
"""A factory for the ExampleWorkspace model."""
class ResourceWorkspaceFactory(DjangoModelFactory):
"""A factory for the ResourceWorkspace model."""

workspace = SubFactory(
WorkspaceFactory,
workspace_type=adapters.ExampleWorkspaceAdapter().get_type(),
workspace_type=adapters.ResourceWorkspaceAdapter().get_type(),
)
requested_by = SubFactory(UserFactory)

class Meta:
model = models.ExampleWorkspace
model = models.ResourceWorkspace


class TemplateWorkspaceFactory(DjangoModelFactory):
Expand Down
8 changes: 4 additions & 4 deletions primed/miscellaneous_workspaces/tests/test_forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,9 @@ def test_invalid_missing_requester(self):
self.assertIn("required", form.errors["requested_by"][0])


class ExampleWorkspaceFormTest(TestCase):
class ResourceWorkspaceFormTest(TestCase):

form_class = forms.ExampleWorkspaceForm
form_class = forms.ResourceWorkspaceForm

def setUp(self):
"""Create a workspace for use in the form."""
Expand Down Expand Up @@ -152,15 +152,15 @@ def test_valid(self):
"""Form is valid with necessary input."""
form_data = {
"workspace": self.workspace,
"intended_workspace_type": "example",
"intended_workspace_type": "resource",
}
form = self.form_class(data=form_data)
self.assertTrue(form.is_valid())

def test_invalid_missing_workspace(self):
"""Form is invalid when missing workspace."""
form_data = {
"intended_workspace_type": "example",
"intended_workspace_type": "resource",
}
form = self.form_class(data=form_data)
self.assertFalse(form.is_valid())
Expand Down
171 changes: 171 additions & 0 deletions primed/miscellaneous_workspaces/tests/test_migrations.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@
"""Tests for migrations in the miscellaneous_workspaces app."""

from anvil_consortium_manager.tests.factories import BillingProjectFactory, WorkspaceFactory
from django_test_migrations.contrib.unittest_case import MigratorTestCase
from primed.users.tests.factories import UserFactory
import factory

from . import factories

class ExampleToResourceWorkspaceForwardMigrationTest(MigratorTestCase):
"""Tests for the migrations associated with renaming the ExampleWorkspace to ResourceWorkspace."""

migrate_from = ("miscellaneous_workspaces", "0008_dataprepworkspace_historicaldataprepworkspace")
migrate_to = ("miscellaneous_workspaces", "0010_update_workspace_type_field")

def prepare(self):
"""Prepare some example workspaces to be migrated."""
# Get model definition from the old state.
BillingProject = self.old_state.apps.get_model("anvil_consortium_manager", "BillingProject")
Workspace = self.old_state.apps.get_model("anvil_consortium_manager", "Workspace")
ExampleWorkspace = self.old_state.apps.get_model("miscellaneous_workspaces", "ExampleWorkspace")
User = self.old_state.apps.get_model("users", "User")
# Create required fks.
# requester = User.objects.create()
billing_project = BillingProject.objects.create(name="bp", has_app_as_user=True)
user = User.objects.create(name="Test User")
# Create some example workspaces for testing.
self.workspace_1 = Workspace.objects.create(
billing_project=billing_project,
name="example-workspace-1",
workspace_type="example",
)
self.example_workspace_1 = ExampleWorkspace.objects.create(
workspace=self.workspace_1,
requested_by=user,
)
self.workspace_2 = Workspace.objects.create(
billing_project=billing_project,
name="example-workspace-2",
workspace_type="example",
)
self.example_workspace_2 = ExampleWorkspace.objects.create(
workspace=self.workspace_2,
requested_by=user,
)
# Create a workspace with a different type.
self.other_workspace = Workspace.objects.create(
billing_project=billing_project,
name="other-workspace",
workspace_type="dbgap",
)

def test_workspace_updates(self):
"""Test updates to the workspace model."""
Workspace = self.new_state.apps.get_model("anvil_consortium_manager", "Workspace")
ResourceWorkspace = self.new_state.apps.get_model("miscellaneous_workspaces", "ResourceWorkspace")
workspace = Workspace.objects.get(pk=self.workspace_1.pk)
self.assertEqual(workspace.workspace_type, "resource")
workspace.full_clean()
workspace = Workspace.objects.get(pk=self.workspace_2.pk)
self.assertEqual(workspace.workspace_type, "resource")
workspace.full_clean()
# Check the other workspace.
other_workspace = Workspace.objects.get(pk=self.other_workspace.pk)
self.assertEqual(other_workspace.workspace_type, "dbgap")

def test_resource_workspace_updates(self):
"""Test updates to the ResourceWorkspace model."""
Workspace = self.new_state.apps.get_model("anvil_consortium_manager", "Workspace")
ResourceWorkspace = self.new_state.apps.get_model("miscellaneous_workspaces", "ResourceWorkspace")
resource_workspace = ResourceWorkspace.objects.get(pk=self.example_workspace_1.pk)
resource_workspace.full_clean()
resource_workspace = ResourceWorkspace.objects.get(pk=self.example_workspace_2.pk)
resource_workspace.full_clean()

def test_relationships(self):
"""relationships and reverse relationships are correct after migration."""
Workspace = self.new_state.apps.get_model("anvil_consortium_manager", "Workspace")
ResourceWorkspace = self.new_state.apps.get_model("miscellaneous_workspaces", "ResourceWorkspace")
workspace = Workspace.objects.get(pk=self.workspace_1.pk)
resource_workspace = ResourceWorkspace.objects.get(pk=self.example_workspace_1.pk)
self.assertTrue(hasattr(workspace, "resourceworkspace"))
self.assertIsInstance(workspace.resourceworkspace, ResourceWorkspace)
self.assertEqual(workspace.resourceworkspace, resource_workspace)
workspace = Workspace.objects.get(pk=self.workspace_2.pk)
resource_workspace = ResourceWorkspace.objects.get(pk=self.example_workspace_2.pk)
self.assertTrue(hasattr(workspace, "resourceworkspace"))
self.assertIsInstance(workspace.resourceworkspace, ResourceWorkspace)
self.assertEqual(workspace.resourceworkspace, resource_workspace)


class ExampleToResourceWorkspaceReverseMigrationTest(MigratorTestCase):
"""Tests for the reverse migrations associated with renaming the ExampleWorkspace to ResourceWorkspace."""

migrate_from = ("miscellaneous_workspaces", "0010_update_workspace_type_field")
migrate_to = ("miscellaneous_workspaces", "0008_dataprepworkspace_historicaldataprepworkspace")

def prepare(self):
"""Prepare some example workspaces to be migrated."""
# Get model definition from the old state.
BillingProject = self.old_state.apps.get_model("anvil_consortium_manager", "BillingProject")
Workspace = self.old_state.apps.get_model("anvil_consortium_manager", "Workspace")
ResourceWorkspace = self.old_state.apps.get_model("miscellaneous_workspaces", "ResourceWorkspace")
User = self.old_state.apps.get_model("users", "User")
# Create required fks.
# requester = User.objects.create()
billing_project = BillingProject.objects.create(name="bp", has_app_as_user=True)
user = User.objects.create(name="Test User")
# Create some example workspaces for testing.
self.workspace_1 = Workspace.objects.create(
billing_project=billing_project,
name="resource-workspace-1",
workspace_type="resource",
)
self.resource_workspace_1 = ResourceWorkspace.objects.create(
workspace=self.workspace_1,
requested_by=user,
)
self.workspace_2 = Workspace.objects.create(
billing_project=billing_project,
name="resource-workspace-2",
workspace_type="resource",
)
self.resource_workspace_2 = ResourceWorkspace.objects.create(
workspace=self.workspace_2,
requested_by=user,
)
# Create a workspace with a different type.
self.other_workspace = Workspace.objects.create(
billing_project=billing_project,
name="other-workspace",
workspace_type="dbgap",
)

def test_workspace_updates(self):
"""Test updates to the workspace model."""
Workspace = self.new_state.apps.get_model("anvil_consortium_manager", "Workspace")
ExampleWorkspace = self.new_state.apps.get_model("miscellaneous_workspaces", "ExampleWorkspace")
workspace = Workspace.objects.get(pk=self.workspace_1.pk)
self.assertEqual(workspace.workspace_type, "example")
workspace.full_clean()
workspace = Workspace.objects.get(pk=self.workspace_2.pk)
self.assertEqual(workspace.workspace_type, "example")
workspace.full_clean()
# Check the other workspace.
other_workspace = Workspace.objects.get(pk=self.other_workspace.pk)
self.assertEqual(other_workspace.workspace_type, "dbgap")

def test_resource_workspace_updates(self):
"""Test updates to the ResourceWorkspace model."""
Workspace = self.new_state.apps.get_model("anvil_consortium_manager", "Workspace")
ExampleWorkspace = self.new_state.apps.get_model("miscellaneous_workspaces", "ExampleWorkspace")
example_workspace = ExampleWorkspace.objects.get(pk=self.resource_workspace_1.pk)
example_workspace.full_clean()
example_workspace = ExampleWorkspace.objects.get(pk=self.resource_workspace_2.pk)
example_workspace.full_clean()

def test_relationships(self):
"""relationships and reverse relationships are correct after migration."""
Workspace = self.new_state.apps.get_model("anvil_consortium_manager", "Workspace")
ExampleWorkspace = self.new_state.apps.get_model("miscellaneous_workspaces", "ExampleWorkspace")
workspace = Workspace.objects.get(pk=self.workspace_1.pk)
example_workspace = ExampleWorkspace.objects.get(pk=self.resource_workspace_1.pk)
self.assertTrue(hasattr(workspace, "exampleworkspace"))
self.assertIsInstance(workspace.exampleworkspace, ExampleWorkspace)
self.assertEqual(workspace.exampleworkspace, example_workspace)
workspace = Workspace.objects.get(pk=self.workspace_2.pk)
example_workspace = ExampleWorkspace.objects.get(pk=self.resource_workspace_2.pk)
self.assertTrue(hasattr(workspace, "exampleworkspace"))
self.assertIsInstance(workspace.exampleworkspace, ExampleWorkspace)
self.assertEqual(workspace.exampleworkspace, example_workspace)
10 changes: 5 additions & 5 deletions primed/miscellaneous_workspaces/tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,22 +53,22 @@ def test_str_method(self):
self.assertEqual(str(instance), "test-bp/test-ws")


class ExampleWorkspaceTest(TestCase):
"""Tests for the ExampleWorkspace model."""
class ResourceWorkspaceTest(TestCase):
"""Tests for the ResourceWorkspace model."""

def test_model_saving(self):
"""Creation using the model constructor and .save() works."""
workspace = WorkspaceFactory.create()
user = UserFactory.create()
instance = models.ExampleWorkspace(workspace=workspace, requested_by=user)
instance = models.ResourceWorkspace(workspace=workspace, requested_by=user)
instance.save()
self.assertIsInstance(instance, models.ExampleWorkspace)
self.assertIsInstance(instance, models.ResourceWorkspace)

def test_str_method(self):
workspace = WorkspaceFactory.create(
billing_project__name="test-bp", name="test-ws"
)
instance = factories.ExampleWorkspaceFactory.create(workspace=workspace)
instance = factories.ResourceWorkspaceFactory.create(workspace=workspace)
self.assertIsInstance(str(instance), str)
self.assertEqual(str(instance), "test-bp/test-ws")

Expand Down
Loading