forked from RedHatInsights/insights-rbac
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request RedHatInsights#1175 from coderbydesign/add-self-re…
…ferencial-fk-on-workspace-model Add self-referencial FK on Workspace model
- Loading branch information
Showing
9 changed files
with
178 additions
and
39 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,26 @@ | ||
# Generated by Django 4.2.14 on 2024-08-29 19:42 | ||
|
||
from django.db import migrations, models | ||
import django.db.models.deletion | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
("management", "0048_outbox"), | ||
] | ||
|
||
operations = [ | ||
migrations.AlterField( | ||
model_name="workspace", | ||
name="parent", | ||
field=models.ForeignKey( | ||
blank=True, | ||
null=True, | ||
on_delete=django.db.models.deletion.PROTECT, | ||
related_name="children", | ||
to="management.workspace", | ||
to_field="uuid", | ||
), | ||
), | ||
] |
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
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,47 @@ | ||
# | ||
# Copyright 2024 Red Hat, Inc. | ||
# | ||
# This program is free software: you can redistribute it and/or modify | ||
# it under the terms of the GNU Affero General Public License as | ||
# published by the Free Software Foundation, either version 3 of the | ||
# License, or (at your option) any later version. | ||
# | ||
# This program is distributed in the hope that it will be useful, | ||
# but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
# GNU Affero General Public License for more details. | ||
# | ||
# You should have received a copy of the GNU Affero General Public License | ||
# along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
# | ||
"""Test the workspace model.""" | ||
from management.models import Workspace | ||
from tests.identity_request import IdentityRequest | ||
|
||
from django.db.models import ProtectedError | ||
|
||
|
||
class WorkspaceModelTests(IdentityRequest): | ||
"""Test the workspace model.""" | ||
|
||
def setUp(self): | ||
"""Set up the workspace model tests.""" | ||
super().setUp() | ||
|
||
def tearDown(self): | ||
"""Tear down workspace model tests.""" | ||
Workspace.objects.update(parent=None) | ||
Workspace.objects.all().delete() | ||
|
||
def test_child_parent_relations(self): | ||
"""Test that workspaces can add/have parents as well as children""" | ||
parent = Workspace.objects.create(name="Parent", tenant=self.tenant) | ||
child = Workspace.objects.create(name="Child", tenant=self.tenant, parent=parent) | ||
self.assertEqual(child.parent, parent) | ||
self.assertEqual(list(parent.children.all()), [child]) | ||
|
||
def test_delete_fails_when_children(self): | ||
"""Test that workspaces will not be deleted when children exist""" | ||
parent = Workspace.objects.create(name="Parent", tenant=self.tenant) | ||
child = Workspace.objects.create(name="Child", tenant=self.tenant, parent=parent) | ||
self.assertRaises(ProtectedError, parent.delete) |
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,65 @@ | ||
# | ||
# Copyright 2024 Red Hat, Inc. | ||
# | ||
# This program is free software: you can redistribute it and/or modify | ||
# it under the terms of the GNU Affero General Public License as | ||
# published by the Free Software Foundation, either version 3 of the | ||
# License, or (at your option) any later version. | ||
# | ||
# This program is distributed in the hope that it will be useful, | ||
# but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
# GNU Affero General Public License for more details. | ||
# | ||
# You should have received a copy of the GNU Affero General Public License | ||
# along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
# | ||
from django.test import TestCase | ||
from unittest.mock import Mock | ||
from api.models import Tenant | ||
from management.models import Workspace | ||
from management.workspace.serializer import WorkspaceSerializer | ||
import uuid | ||
|
||
|
||
class WorkspaceSerializerTest(TestCase): | ||
"""Test the workspace serializer""" | ||
|
||
def setUp(self): | ||
"""Set up workspace serializer tests.""" | ||
tenant = Tenant.objects.get(tenant_name="public") | ||
self.parent = Workspace.objects.create( | ||
name="Parent", description="Parent desc", tenant=tenant, uuid=uuid.uuid4() | ||
) | ||
self.child = Workspace.objects.create( | ||
name="Child", description="Child desc", tenant=tenant, parent=self.parent, uuid=uuid.uuid4() | ||
) | ||
|
||
def tearDown(self): | ||
"""Tear down workspace serializer tests.""" | ||
Workspace.objects.update(parent=None) | ||
Workspace.objects.all().delete() | ||
|
||
def test_get_workspace_detail_child(self): | ||
"""Return GET /workspace/<uuid>/ serializer response for child""" | ||
serializer = WorkspaceSerializer(self.child) | ||
expected_data = { | ||
"uuid": str(self.child.uuid), | ||
"name": self.child.name, | ||
"description": self.child.description, | ||
"parent_id": str(self.parent.uuid), | ||
} | ||
|
||
self.assertDictEqual(serializer.data, expected_data) | ||
|
||
def test_get_workspace_detail_parent(self): | ||
"""Return GET /workspace/<uuid>/ serializer response for parent""" | ||
serializer = WorkspaceSerializer(self.parent) | ||
expected_data = { | ||
"uuid": str(self.parent.uuid), | ||
"name": self.parent.name, | ||
"description": self.parent.description, | ||
"parent_id": None, | ||
} | ||
|
||
self.assertDictEqual(serializer.data, expected_data) |
Oops, something went wrong.