Skip to content

Commit

Permalink
Add test for custom editors
Browse files Browse the repository at this point in the history
  • Loading branch information
Aadesh-Baral committed Sep 1, 2022
1 parent c024a77 commit 11ef8d4
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 0 deletions.
10 changes: 10 additions & 0 deletions backend/models/postgis/custom_editors.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,15 @@ class CustomEditor(db.Model):
description = db.Column(db.String)
url = db.Column(db.String, nullable=False)

def create(self):
"""Creates and saves the current model to the DB"""
db.session.add(self)
db.session.commit()

def save(self):
"""Save changes to db"""
db.session.commit()

@staticmethod
def get_by_project_id(project_id: int):
""" Get custom editor by it's project id """
Expand All @@ -29,6 +38,7 @@ def update_editor(self, dto: CustomEditorDTO):
self.name = dto.name
self.description = dto.description
self.url = dto.url
self.save()

def delete(self):
""" Deletes the current model from the DB """
Expand Down
93 changes: 93 additions & 0 deletions tests/backend/unit/models/postgis/test_custom_editor.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
from tests.backend.base import BaseTestCase
from tests.backend.helpers.test_helpers import create_canned_project
from backend.models.postgis.custom_editors import CustomEditor, CustomEditorDTO


class TestCustomEditor(BaseTestCase):
def setUp(self):
super().setUp()
self.test_project, self.author_id = create_canned_project()
self.custom_editor = CustomEditor()
self.custom_editor.name = "Test custom editor"
self.custom_editor.description = "Test descrption"
self.custom_editor.url = "Test URL"
self.test_project.custom_editor = self.custom_editor
self.custom_editor.create()

def test_get_by_project_id(self):
# Act
custom_editor = CustomEditor.get_by_project_id(self.test_project.id)

# Assert
self.assertEqual(custom_editor.project_id, self.test_project.id)
self.assertEqual(custom_editor.name, self.custom_editor.name)
self.assertEqual(custom_editor.description, self.custom_editor.description)
self.assertEqual(custom_editor.url, self.custom_editor.url)

def test_create_from_dto(self):
# Arrange
self.custom_editor.delete()
custom_editor_dto = CustomEditorDTO()
custom_editor_dto.name = self.custom_editor.name
custom_editor_dto.description = self.custom_editor.description
custom_editor_dto.url = self.custom_editor.url

# Act
custom_editor = CustomEditor.create_from_dto(
self.test_project.id, custom_editor_dto
)

# Assert
self.assertEqual(custom_editor.project_id, self.test_project.id)
self.assertEqual(custom_editor.name, self.custom_editor.name)
self.assertEqual(custom_editor.description, self.custom_editor.description)
self.assertEqual(custom_editor.url, self.custom_editor.url)

def test_update_editor(self):
# Arrange
self.test_project.custom_editor = self.custom_editor
custom_editor_dto = CustomEditorDTO()
custom_editor_dto.name = "Updated Name"
custom_editor_dto.description = "Updated Description"
custom_editor_dto.url = "Updated URL"

# Act
self.custom_editor.update_editor(custom_editor_dto)

# Assert
self.assertEqual(self.custom_editor.project_id, self.test_project.id)
self.assertEqual(self.custom_editor.name, custom_editor_dto.name)
self.assertEqual(self.custom_editor.description, custom_editor_dto.description)
self.assertEqual(self.custom_editor.url, custom_editor_dto.url)

def test_delete(self):
# Act
self.custom_editor.delete()

# Assert
custom_editor = CustomEditor.get_by_project_id(self.test_project.id)
self.assertIsNone(custom_editor)

def test_as_dto(self):
# Act
custom_editor_dto = self.custom_editor.as_dto()

# Assert
self.assertIsInstance(custom_editor_dto, CustomEditorDTO)
self.assertEqual(self.custom_editor.project_id, self.test_project.id)
self.assertEqual(self.custom_editor.name, custom_editor_dto.name)
self.assertEqual(self.custom_editor.description, custom_editor_dto.description)
self.assertEqual(self.custom_editor.url, custom_editor_dto.url)

def test_clone_to_project(self):
# Act
test_new_project_id = 123456
cloned_custom_editor = self.custom_editor.clone_to_project(test_new_project_id)

# Assert
self.assertEqual(test_new_project_id, cloned_custom_editor.project_id)
self.assertEqual(self.custom_editor.name, cloned_custom_editor.name)
self.assertEqual(
self.custom_editor.description, cloned_custom_editor.description
)
self.assertEqual(self.custom_editor.url, cloned_custom_editor.url)

0 comments on commit 11ef8d4

Please sign in to comment.