Skip to content

Commit 93c22ab

Browse files
authored
Merge pull request #10 from CIAT-DAPA/develop
Develop
2 parents 07b63dc + 4913f93 commit 93c22ab

File tree

7 files changed

+110
-5
lines changed

7 files changed

+110
-5
lines changed

src/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from .ormgap.models.accession import Accession
22
from .ormgap.models.crop import Crop
33
from .ormgap.models.country import Country
4-
from .ormgap.models.group import Group
4+
from .ormgap.models.group import Group
5+
from .ormgap.models.project import Project

src/ormgap/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@
22
from .models.accession import Accession
33
from .models.country import Country
44
from .models.crop import Crop
5-
from .models.group import Group
5+
from .models.group import Group
6+
from .models.project import Project

src/ormgap/models/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from .accession import Accession
22
from .country import Country
33
from .crop import Crop
4-
from .group import Group
4+
from .group import Group
5+
from .project import Project

src/ormgap/models/accession.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
from .crop import Crop
33
from .group import Group
44
from .country import Country
5+
from .project import Project
56

67
class Accession(Document):
78

@@ -32,6 +33,8 @@ class Accession(Document):
3233
External identifier for the accession. Mandatory and unique.
3334
other_attributes: dict
3435
Additional attributes of the accession. Optional.
36+
project: Project
37+
Project object, project to which the accession is linked. Optional.
3538
3639
Methods:
3740
-------
@@ -46,12 +49,13 @@ class Accession(Document):
4649
}
4750
species_name = StringField(max_length=150)
4851
crop = ReferenceField(Crop, required=True)
49-
landrace_group = ReferenceField(Group, required=True)
52+
landrace_group = ReferenceField(Group, required=False)
5053
institution_name = StringField(max_length=255)
5154
source_database = StringField(max_length=255)
5255
country = ReferenceField(Country, required=True)
5356
latitude = FloatField(required=True)
5457
longitude = FloatField(required=True)
5558
accession_id = StringField(max_length=255)
5659
ext_id = StringField(max_length=255, required=True, unique=True)
60+
project = ReferenceField(Project, required=False)
5761
other_attributes = DictField()

src/ormgap/models/project.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
from mongoengine import Document, StringField
2+
3+
class Project(Document):
4+
"""
5+
Represents a project in the database.
6+
7+
Attributes:
8+
----------
9+
ext_id: str
10+
External identifier for the project. Mandatory and unique.
11+
name: str
12+
Name of the project. Mandatory.
13+
14+
Methods:
15+
-------
16+
save()
17+
Saves the Project object to the database.
18+
delete()
19+
Deletes the Project object from the database.
20+
"""
21+
22+
meta = {
23+
'collection': 'project'
24+
}
25+
ext_id = StringField(max_length=100, unique=True, required=True)
26+
name = StringField(max_length=150, required=True)

src/tests/test_accession.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
from ormgap.models.crop import Crop
1212
from ormgap.models.group import Group
1313
from ormgap.models.country import Country
14+
from ormgap.models.project import Project
1415

1516
class AccessionTestCase(unittest.TestCase):
1617
@classmethod
@@ -24,6 +25,8 @@ def setUp(self):
2425
self.assertIsNotNone(self.group.id)
2526
self.country = Country(iso_2='AR', name='Argentina').save()
2627
self.assertIsNotNone(self.country.id)
28+
self.project = Project(name='Test Project', ext_id='P001').save()
29+
self.assertIsNotNone(self.project.id)
2730
self.accession = Accession(
2831
species_name='Test Species',
2932
crop=self.crop,
@@ -34,7 +37,8 @@ def setUp(self):
3437
latitude=40.7128,
3538
longitude=-74.0060,
3639
accession_id='12345',
37-
ext_id='123'
40+
ext_id='123',
41+
project=self.project
3842
)
3943

4044
def test_create_accession(self):
@@ -47,6 +51,7 @@ def test_create_accession(self):
4751
self.assertEqual(accession.landrace_group.id, self.group.id)
4852
self.assertEqual(accession.latitude, 40.7128)
4953
self.assertEqual(accession.longitude, -74.0060)
54+
self.assertEqual(accession.project.id, self.project.id)
5055

5156

5257
def test_get_accession_by_crop(self):
@@ -114,6 +119,7 @@ def tearDown(self):
114119
self.crop.delete()
115120
self.group.delete()
116121
self.country.delete()
122+
self.project.delete()
117123
Accession.objects.delete()
118124

119125
if __name__ == '__main__':

src/tests/test_project.py

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
import unittest
2+
import sys
3+
import os
4+
5+
dir_path = os.path.dirname(os.path.realpath(__file__))
6+
orm_dir_path = os.path.abspath(os.path.join(dir_path, '..'))
7+
sys.path.append(orm_dir_path)
8+
9+
from mongoengine import connect, disconnect
10+
from ormgap.models.project import Project
11+
12+
class ProjectTestCase(unittest.TestCase):
13+
@classmethod
14+
def setUpClass(cls):
15+
""" Connect to a mock MongoDB instance. """
16+
connect('test_gap_analysis', host='mongomock://localhost')
17+
18+
def setUp(self):
19+
""" Clean up the database before each test. """
20+
Project.objects.delete()
21+
self.project = Project(ext_id='P001', name='Test Project')
22+
23+
def test_create_project(self):
24+
""" Test if a project is created successfully. """
25+
self.project.save()
26+
self.assertIsNotNone(self.project.id)
27+
28+
def test_duplicate_ext_id(self):
29+
""" Test that a project with duplicate ext_id is not allowed. """
30+
self.project.save()
31+
duplicate_project = Project(ext_id='P001', name='Duplicate Project')
32+
with self.assertRaises(Exception) as context:
33+
duplicate_project.save()
34+
self.assertTrue('E11000 Duplicate Key Error' in str(context.exception))
35+
36+
def test_get_project_by_ext_id(self):
37+
""" Test retrieving a project by ext_id. """
38+
self.project.save()
39+
project = Project.objects(ext_id='P001').first()
40+
self.assertIsNotNone(project)
41+
self.assertEqual(project.name, 'Test Project')
42+
43+
def test_update_project_name(self):
44+
""" Test updating the project's name. """
45+
self.project.save()
46+
self.project.name = 'Updated Project'
47+
self.project.save()
48+
49+
updated_project = Project.objects(ext_id='P001').first()
50+
self.assertEqual(updated_project.name, 'Updated Project')
51+
52+
def test_delete_project(self):
53+
""" Test deleting a project. """
54+
self.project.save()
55+
self.project.delete()
56+
57+
project = Project.objects(ext_id='P001').first()
58+
self.assertIsNone(project)
59+
60+
@classmethod
61+
def tearDownClass(cls):
62+
""" Disconnect the mock database after all tests. """
63+
disconnect()
64+
65+
if __name__ == '__main__':
66+
unittest.main()

0 commit comments

Comments
 (0)