Skip to content

Commit

Permalink
Added schema tests
Browse files Browse the repository at this point in the history
  • Loading branch information
rgmacalintal committed Dec 9, 2024
1 parent a402a5e commit 38edd6b
Show file tree
Hide file tree
Showing 2 changed files with 149 additions and 0 deletions.
73 changes: 73 additions & 0 deletions tests/metagenomics_batch_item_schema_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
# This file contains tests relating to MetagenomicBatchItemAPI.
# Currently only contains tests for the MetagenomicBatchItemSchema (serialization and deserialization tests).
# API mock call tests should be added.

import unittest

from marshmallow.exceptions import ValidationError

import os
import sys

# Add the root directory of the project to the Python path
project_root = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.insert(0, project_root)

from dinapy.schemas.metagenomics_batch_item_schema import MetagenomicsBatchItemSchema
from dinapy.entities.MetagenomicsBatchItem import *

VALID_METAGENOMICS_BATCH_ITEM_DATA = {
"data": {
"id": "019398b3-6251-7dfd-8479-02cf0f2b8f84",
"type": "metagenomics-batch-item",
"attributes": {
"createdBy": "dina-admin",
"createdOn": "2024-12-05T21:21:53.219363Z"
},
"relationships": {
"metagenomicsBatch": {
"data": {
"id": "019398b3-6219-73b9-a822-e12c0ff1e9c6",
"type": "metagenomics-batch"
}
}
}
}
}

class MetagenomicsBatchItemSchemaTest(unittest.TestCase):
def test_deserialize_metagenomics_batch_item(self):
schema = MetagenomicsBatchItemSchema()
try:
result = schema.load(VALID_METAGENOMICS_BATCH_ITEM_DATA)
print(result.__dict__)
self.assertIsInstance(result, MetagenomicsBatchItemDTO)
except ValidationError as e:
self.fail(f"Validation failed with error: {e.messages}")

def test_serialize_metagenomics_batch(self):
schema = MetagenomicsBatchItemSchema()
metagenomics_batch_item_attributes = MetagenomicsBatchItemAttributesDTOBuilder(
).set_name("test").set_createdBy("dina-admin").build()
metagenomics_batch_item = MetagenomicsBatchItemDTOBuilder(
).set_attributes(metagenomics_batch_item_attributes).build()

try:
serialized_metagenomics_batch_item = schema.dump(metagenomics_batch_item)
expected = {
"data": {
"type": "metagenomics-batch-item",
"attributes": {
"name": "test"
}
}
}

print(serialized_metagenomics_batch_item)
self.assertIsInstance(serialized_metagenomics_batch_item, dict)
self.assertDictEqual(serialized_metagenomics_batch_item, expected)
except ValidationError as e:
self.fail(f"Validation failed with error: {e.messages}")

if __name__ == "__main__":
unittest.main()
76 changes: 76 additions & 0 deletions tests/metagenomics_batch_schema_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
# This file contains tests relating to MetagenomicBatchAPI.
# Currently only contains tests for the MetagenomicBatchSchema (serialization and deserialization tests).
# API mock call tests should be added.

import unittest

from marshmallow.exceptions import ValidationError

import os
import sys

# Add the root directory of the project to the Python path
project_root = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.insert(0, project_root)

from dinapy.schemas.metagenomics_batch_schema import MetagenomicsBatchSchema
from dinapy.entities.MetagenomicsBatch import *

VALID_METAGENOMICS_BATCH_DATA = {
"data": {
"id": "019398b3-6219-73b9-a822-e12c0ff1e9c6",
"type": "metagenomics-batch",
"attributes": {
"createdBy": "dina-admin",
"createdOn": "2024-12-05T21:21:53.156284Z",
"group": "aafc",
"name": "metagenomics-batch-test"
},
"relationships": {
"indexSet": {
"data": {
"id": "456036bc-6fa6-4779-b028-ec26a7b7e6dd",
"type": "index-set"
}
}
}
}
}

class MetagenomicsBatchSchemaTest(unittest.TestCase):
def test_deserialize_metagenomics_batch(self):
schema = MetagenomicsBatchSchema()
try:
result = schema.load(VALID_METAGENOMICS_BATCH_DATA)
print(result.__dict__)
self.assertIsInstance(result, MetagenomicsBatchDTO)
except ValidationError as e:
self.fail(f"Validation failed with error: {e.messages}")

def test_serialize_metagenomics_batch(self):
schema = MetagenomicsBatchSchema()
metagenomics_batch_attributes = MetagenomicsBatchAttributesDTOBuilder(
).set_createdBy("dina-admin").set_group("aafc").set_name("metagenomics-batch-test").build()
metagenomics_batch = MetagenomicsBatchDTOBuilder(
).set_attributes(metagenomics_batch_attributes).build()

try:
serialized_metagenomics_batch = schema.dump(metagenomics_batch)
expected = {
"data": {
"type": "metagenomics-batch",
"attributes": {
"group": "aafc",
"name": "metagenomics-batch-test"
}
}
}

print(serialized_metagenomics_batch)
self.assertIsInstance(serialized_metagenomics_batch, dict)
self.assertDictEqual(serialized_metagenomics_batch, expected)
except ValidationError as e:
self.fail(f"Validation failed with error: {e.messages}")

if __name__ == "__main__":
unittest.main()

0 comments on commit 38edd6b

Please sign in to comment.