diff --git a/backend/tests/factories.py b/backend/tests/factories.py new file mode 100644 index 00000000..e7548a9e --- /dev/null +++ b/backend/tests/factories.py @@ -0,0 +1,88 @@ +import factory + +from login.models import OsmUser +from core.models import Dataset, AOI, Label, Model, Training, Feedback, FeedbackAOI + + +class OsmUserFactory(factory.django.DjangoModelFactory): + class Meta: + model = OsmUser + + username = "Test User" + osm_id = 123456 + + +class DatasetFactory(factory.django.DjangoModelFactory): + class Meta: + model = Dataset + + name = "Test Dataset" + created_by = factory.SubFactory(OsmUserFactory) + status = -1 + + +class AoiFactory(factory.django.DjangoModelFactory): + class Meta: + model = AOI + + geom = "POLYGON ((0 0, 0 1, 1 1, 1 0, 0 0))" + dataset = factory.SubFactory(DatasetFactory) + label_status = -1 + + +class LabelFactory(factory.django.DjangoModelFactory): + + class Meta: + model = Label + + aoi = factory.SubFactory(AoiFactory) + geom = "POLYGON ((0 0, 0 1, 1 1, 1 0, 0 0))" + + +class ModelFactory(factory.django.DjangoModelFactory): + + class Meta: + model = Model + + dataset = factory.SubFactory(DatasetFactory) + name = "Test Model" + created_by = factory.SubFactory(OsmUserFactory) + status = -1 + + +class TrainingFactory(factory.django.DjangoModelFactory): + + class Meta: + model = Training + + model = factory.SubFactory(ModelFactory) + status = "SUBMITTED" + zoom_level = [20, 21] + created_by = factory.SubFactory(OsmUserFactory) + epochs = 3 + batch_size = 24 + + +class FeedbackFactory(factory.django.DjangoModelFactory): + + class Meta: + model = Feedback + + geom = "POLYGON ((0 0, 0 1, 1 1, 1 0, 0 0))" + training = factory.SubFactory(TrainingFactory) + zoom_level = 19 + feedback_type = "TP" + user = factory.SubFactory(OsmUserFactory) + source_imagery = "https://test_data/hotosm/fAIr/" + + +class FeedbackAoiFactory(factory.django.DjangoModelFactory): + + class Meta: + model = FeedbackAOI + + training = factory.SubFactory(TrainingFactory) + geom = "POLYGON ((0 0, 0 1, 1 1, 1 0, 0 0))" + label_status = -1 + source_imagery = "https://test_data/hotosm/" + user = factory.SubFactory(OsmUserFactory) diff --git a/backend/tests/test_core_model.py b/backend/tests/test_core_model.py new file mode 100644 index 00000000..ce719797 --- /dev/null +++ b/backend/tests/test_core_model.py @@ -0,0 +1,63 @@ +from django.test import TestCase + +from .factories import ( + DatasetFactory, + OsmUserFactory, + AoiFactory, + LabelFactory, + ModelFactory, + TrainingFactory, + FeedbackFactory, + FeedbackAoiFactory, +) + + +class TestCoreModels(TestCase): + + def setUp(self): + self.user = OsmUserFactory(username="Test User 2", osm_id=123) + self.dataset = DatasetFactory(created_by=self.user) + self.aoi = AoiFactory(dataset=self.dataset) + self.label = LabelFactory(aoi=self.aoi) + self.model = ModelFactory(dataset=self.dataset, created_by=self.user) + self.training = TrainingFactory(model=self.model, created_by=self.user) + self.feedback = FeedbackFactory(training=self.training, user=self.user) + self.feedbackAoi = FeedbackAoiFactory(training=self.training, user=self.user) + + def test_dataset_creation(self): + self.assertEqual(self.dataset.name, "Test Dataset") + self.assertEqual(self.dataset.created_by, self.user) + + def test_aoi_creation(self): + self.assertEqual(self.aoi.dataset, self.dataset) + self.assertEqual(self.aoi.label_status, -1) + + def test_label_creation(self): + self.assertEqual(self.label.aoi, self.aoi) + + def test_model_creation(self): + self.assertEqual(self.model.name, "Test Model") + self.assertEqual(self.model.dataset, self.dataset) + self.assertEqual(self.model.created_by, self.user) + self.assertEqual(self.model.status, -1) + + def test_training_creation(self): + self.assertEqual(self.training.model, self.model) + self.assertEqual(self.training.status, "SUBMITTED") + self.assertEqual(self.training.zoom_level, [20, 21]) + self.assertEqual(self.training.created_by, self.user) + self.assertEqual(self.training.epochs, 3) + self.assertEqual(self.training.batch_size, 24) + + def test_feedback_creation(self): + self.assertEqual(self.feedback.training, self.training) + self.assertEqual(self.feedback.zoom_level, 19) + self.assertEqual(self.feedback.feedback_type, "TP") + self.assertEqual(self.feedback.user, self.user) + self.assertEqual(self.feedback.source_imagery, "https://test_data/hotosm/fAIr/") + + def test_feedbackAoi_creation(self): + self.assertEqual(self.feedbackAoi.training, self.training) + self.assertEqual(self.feedbackAoi.label_status, -1) + self.assertEqual(self.feedbackAoi.source_imagery, "https://test_data/hotosm/") + self.assertEqual(self.feedbackAoi.user, self.user) diff --git a/backend/tests/test_login_model.py b/backend/tests/test_login_model.py new file mode 100644 index 00000000..8ddc4136 --- /dev/null +++ b/backend/tests/test_login_model.py @@ -0,0 +1,12 @@ +from django.test import TestCase + +from .factories import OsmUserFactory + + +class TestLoginModels(TestCase): + + def test_OsmUser_creation(self): + osm_user = OsmUserFactory() + + self.assertEqual(str(osm_user), "Test User") + self.assertEqual(osm_user.osm_id, 123456)