Skip to content

Admin

Admin for population data package.

OpenCloseSystemAdmin

Bases: ModelAdmin

Admin page for OpenCloseSystem model

PopulationEstimateCategoryAdmin

Bases: ModelAdmin

Admin page for PopulationEstimateCategory model

PopulationStatusAdmin

Bases: ModelAdmin

Admin page for PopulationStatus model

SamplingEffortCoverageAdmin

Bases: ModelAdmin

Admin page for SamplingEffortCoverage model

Factories

Test factories for population data package.

AnnualPopulationAbstractFactory

Bases: DjangoModelFactory

Population count abstract factory.

AnnualPopulationF

Bases: AnnualPopulationAbstractFactory

Population count factory.

AnnualPopulationPerActivityFactory

Bases: AnnualPopulationAbstractFactory

Population count per activity factory.

CertaintyF

Bases: DjangoModelFactory

Certainty factory.

Meta

meta

OpenCloseSystemF

Bases: DjangoModelFactory

Open Close System factory.

Meta

meta

PopulationEstimateCategoryF

Bases: DjangoModelFactory

Population Status factory.

Meta

meta

PopulationStatusF

Bases: DjangoModelFactory

Population Status factory.

Meta

meta

SamplingEffortCoverageF

Bases: DjangoModelFactory

SamplingEffortCoverage factory.

Meta

meta

Models

Models for population data package.

AnnualPopulation

Bases: AnnualPopulationAbstract

Annual Population model.

clean

clean()

Custom validation to ensure area_available_to_species is not greater than property_size_ha.

Source code in django_project/population_data/models.py
def clean(self):
    """
    Custom validation to ensure area_available_to_species is
    not greater than property_size_ha.
    """
    if self.area_available_to_species and self.property.property_size_ha:
        if float(
            self.area_available_to_species
        ) > float(
            self.property.property_size_ha
        ):
            raise ValidationError({
                'area_available_to_species': AREA_AVAILABLE_ERROR_MESSAGE
            })

    super().clean()

AnnualPopulationAbstract

Bases: Model

"Annual Population model.

clean

clean()

Custom validation to ensure the sum of adult_male and adult_female is not greater than total.

Source code in django_project/population_data/models.py
def clean(self):
    """
    Custom validation to ensure the sum of adult_male and adult_female
    is not greater than total.
    """
    if self.adult_male is not None or self.adult_female is not None:
        adult_male = self.adult_male if self.adult_male else 0
        adult_female = self.adult_female if self.adult_female else 0
        if adult_male + adult_female > self.total:
            raise ValidationError({
                'adult_male': TOTAL_POPULATION_ERROR_MESSAGE,
                'adult_female': TOTAL_POPULATION_ERROR_MESSAGE,
            })

    super().clean()

AnnualPopulationPerActivity

Bases: AnnualPopulationAbstract

Annual Population per activity model.

Certainty

Bases: Model

Certainty model.

OpenCloseSystem

Bases: Model

Open Close System model.

PopulationEstimateCategory

Bases: Model

Population Estimate Category model.

PopulationStatus

Bases: Model

Population status model.

SamplingEffortCoverage

Bases: Model

Sampling Effort Coverage model.

Serializers

Serializers for population data package.

CertaintySerializer

Bases: ModelSerializer

Certainty Serializer

OpenCloseSystemSerializer

Bases: ModelSerializer

OpenClose System Serializer

PopulationEstimateCategorySerializer

Bases: ModelSerializer

PopulationEstimateCategory Serializer

PopulationStatusSerializer

Bases: ModelSerializer

PopulationStatus Serializer

SamplingEffortCoverageSerializer

Bases: ModelSerializer

SamplingEffortCoverage Serializer

Test Case

Test case for population data models.

AnnualPopulationPerActivityTestCase

Bases: TestCase

Population count test case.

setUpTestData classmethod

setUpTestData()

SetUpTestData for population count test case.

Source code in django_project/population_data/tests/test_population_data_models.py
@classmethod
def setUpTestData(cls):
    """SetUpTestData for population count test case."""
    taxon = Taxon.objects.create(
        scientific_name='taxon_0',
        common_name_verbatim='taxon_0',
        colour_variant=False,
        taxon_rank=TaxonRankFactory(),
    )
    user = User.objects.create_user(username='testuser', password='12345')
    population = AnnualPopulationF(
        taxon=taxon,
        user=user,
        total=120,
        adult_male=19,
        adult_female=100
    )
    cls.population_count = AnnualPopulationPerActivityFactory(
        annual_population=population,
        intake_permit='1',
        offtake_permit='1'
    )

test_create_population_count

test_create_population_count()

Test create population count.

Source code in django_project/population_data/tests/test_population_data_models.py
def test_create_population_count(self):
    """Test create population count."""
    self.assertTrue(
        isinstance(self.population_count, AnnualPopulationPerActivity)
    )
    self.assertTrue(
        AnnualPopulationPerActivity.objects.filter(
            id=self.population_count.id
        ).exists()
    )

test_delete_population_count

test_delete_population_count()

Test delete population count.

Source code in django_project/population_data/tests/test_population_data_models.py
def test_delete_population_count(self):
    """Test delete population count."""
    initial_count = AnnualPopulationPerActivity.objects.count()
    self.population_count.delete()
    with self.assertRaises(AnnualPopulationPerActivity.DoesNotExist):
        AnnualPopulationPerActivity.objects.get(pk=self.population_count.pk)
    self.assertEqual(
        AnnualPopulationPerActivity.objects.count(),
        initial_count - 1,
        msg="The count of AnnualPopulationPerActivity"
        "instances did not decrease by 1 after deletion."
    )

test_update_population_count

test_update_population_count()

Test update population count.

Source code in django_project/population_data/tests/test_population_data_models.py
def test_update_population_count(self):
    """Test update population count."""
    self.population_count.total = 100
    self.population_count.save()
    self.assertEqual(
        AnnualPopulationPerActivity.objects.get(year=self.population_count.year).total, 100
    )

test_year_activity_type_fields_unique_toghter_constraint

test_year_activity_type_fields_unique_toghter_constraint()

Test year, annual population, and activity_type are unique togther.

Source code in django_project/population_data/tests/test_population_data_models.py
def test_year_activity_type_fields_unique_toghter_constraint(self):
    """Test year, annual population, and activity_type are unique togther."""
    with self.assertRaises(IntegrityError) as raised:
        AnnualPopulationPerActivityFactory(
            annual_population=self.population_count.annual_population,
            year=self.population_count.year,
            activity_type=self.population_count.activity_type,
            intake_permit='1',
            offtake_permit='1'
        )

PopulationCountTestCase

Bases: TestCase

Population count test case.

setUpTestData classmethod

setUpTestData()

SetUpTestData for population count test case.

Source code in django_project/population_data/tests/test_population_data_models.py
@classmethod
def setUpTestData(cls):
    """SetUpTestData for population count test case."""
    taxon = TaxonFactory.create(
        scientific_name='taxon_0',
        common_name_verbatim='taxon_0',
        colour_variant=False,
        taxon_rank=TaxonRankFactory(),
    )
    user = User.objects.create_user(username='testuser', password='12345')
    cls.population_count = AnnualPopulationF(
        taxon=taxon,
        user=user,
        total=120,
        adult_male=19,
        adult_female=100,
        adult_total=119
    )

test_adult_population_validation

test_adult_population_validation()

Test that a ValidationError is raised when the sum of adult_male and adult_female exceeds the total.

Source code in django_project/population_data/tests/test_population_data_models.py
def test_adult_population_validation(self):
    """
    Test that a ValidationError is raised when the sum of adult_male and
    adult_female exceeds the total.
    """
    data = {
        'year': 2023,
        'total': 100,
        'adult_male': 60,
        'adult_female': 50,
    }
    with self.assertRaises(ValidationError):
        population_instance = AnnualPopulation(**data)
        population_instance.clean()

test_area_available_to_species

test_area_available_to_species()

Test that a ValidationError is raised when area available to species exceeds property size.

Source code in django_project/population_data/tests/test_population_data_models.py
def test_area_available_to_species(self):
    """
    Test that a ValidationError is raised when area available to species
    exceeds property size.
    """
    property_obj = self.population_count.property
    data = {
        'year': 2023,
        'total': 100,
        'area_available_to_species': 100000,
        'property': property_obj
    }
    with self.assertRaises(ValidationError):
        population_instance = AnnualPopulation(**data)
        population_instance.clean()

test_create_population_count

test_create_population_count()

Test create population count.

Source code in django_project/population_data/tests/test_population_data_models.py
def test_create_population_count(self):
    """Test create population count."""
    self.assertTrue(
        isinstance(self.population_count, AnnualPopulation)
    )
    self.assertTrue(
        AnnualPopulation.objects.filter(
            id=self.population_count.id
        ).exists()
    )

test_delete_population_count

test_delete_population_count()

Test delete population count.

Source code in django_project/population_data/tests/test_population_data_models.py
def test_delete_population_count(self):
    """Test delete population count."""
    self.population_count.delete()
    self.assertFalse(
        AnnualPopulation.objects.filter(id=self.population_count.id).exists()
    )

TestCertainty

Bases: TestCase

Test for certainty model.

setUp

setUp()

setup test data

Source code in django_project/population_data/tests/test_population_data_models.py
def setUp(self) -> None:
    """setup test data"""
    self.Certainty = CertaintyF(name='name', description='text')

test_create_certainty

test_create_certainty()

test create certainty.

Source code in django_project/population_data/tests/test_population_data_models.py
def test_create_certainty(self):
    """test create certainty."""
    self.assertEqual(self.Certainty.name, 'name')
    self.assertEqual(self.Certainty.description, 'text')
    self.assertEqual(Certainty.objects.count(), 1)
    self.assertEqual(str(self.Certainty), self.Certainty.name)

test_delete_certainty

test_delete_certainty()

test delete certainty.

Source code in django_project/population_data/tests/test_population_data_models.py
def test_delete_certainty(self):
    """test delete certainty."""
    self.Certainty.delete()
    self.assertEqual(Certainty.objects.count(), 0)

test_update_Certainty

test_update_Certainty()

test update certainty.

Source code in django_project/population_data/tests/test_population_data_models.py
def test_update_Certainty(self):
    """test update certainty."""
    self.Certainty.name = 'Certainty'
    self.Certainty.description = 'Certainty description'
    self.Certainty.save()
    self.assertEqual(self.Certainty.name, 'Certainty')
    self.assertEqual(self.Certainty.description, 'Certainty description')

TestOpenCloseSystem

Bases: TestCase

Test for open close system model.

setUp

setUp()

setup test data

Source code in django_project/population_data/tests/test_population_data_models.py
def setUp(self) -> None:
    """setup test data"""
    self.open_close_sustem = OpenCloseSystemF(name='name')

test_create_open_close_system

test_create_open_close_system()

test create open close system.

Source code in django_project/population_data/tests/test_population_data_models.py
def test_create_open_close_system(self):
    """test create open close system."""
    self.assertEqual(self.open_close_sustem.name, 'name')
    self.assertEqual(OpenCloseSystem.objects.count(), 1)

test_delete_open_close_system

test_delete_open_close_system()

test delete open close system.

Source code in django_project/population_data/tests/test_population_data_models.py
def test_delete_open_close_system(self):
    """test delete open close system."""
    self.open_close_sustem.delete()
    self.assertEqual(OpenCloseSystem.objects.count(), 0)

test_update_open_close_system

test_update_open_close_system()

test update open close system.

Source code in django_project/population_data/tests/test_population_data_models.py
def test_update_open_close_system(self):
    """test update open close system."""
    self.open_close_sustem.name = 'OpenCloseSystem'
    self.open_close_sustem.save()
    self.assertEqual(self.open_close_sustem.name, 'OpenCloseSystem')

TestPopulationEstimateCategory

Bases: TestCase

Test for population estimate category model.

setUp

setUp()

setup test data.

Source code in django_project/population_data/tests/test_population_data_models.py
def setUp(self) -> None:
    """setup test data."""
    self.population_estimate_category = PopulationEstimateCategoryF(
        name='name'
        )

test_create_population_estimate

test_create_population_estimate()

test create population estimate category.

Source code in django_project/population_data/tests/test_population_data_models.py
def test_create_population_estimate(self):
    """test create population estimate category."""
    self.assertEqual(self.population_estimate_category.name, 'name')
    self.assertEqual(str(self.population_estimate_category), 'name')
    self.assertEqual(PopulationEstimateCategory.objects.count(), 1)

test_delete_population_estimate

test_delete_population_estimate()

test delete population estimate category.

Source code in django_project/population_data/tests/test_population_data_models.py
def test_delete_population_estimate(self):
    """test delete population estimate category."""
    self.population_estimate_category.delete()
    self.assertEqual(PopulationEstimateCategory.objects.count(), 0)

test_population_estimate_name_constraint

test_population_estimate_name_constraint()

Test population estimate category name contraint.

Source code in django_project/population_data/tests/test_population_data_models.py
def test_population_estimate_name_constraint(self):
    """Test population estimate category name contraint."""
    another = PopulationEstimateCategoryF(name='Population estimate')
    self.assertEqual(PopulationEstimateCategory.objects.count(), 2)
    self.assertNotEqual(
        self.population_estimate_category.name,
        another.name
    )
    with self.assertRaises(Exception) as raised:
        PopulationEstimateCategoryF(name='name')

test_update_population_estimate

test_update_population_estimate()

test update population estimate category.

Source code in django_project/population_data/tests/test_population_data_models.py
def test_update_population_estimate(self):
    """test update population estimate category."""
    self.population_estimate_category.name = 'PopulationEstimateCategory'
    self.population_estimate_category.save()
    self.assertEqual(
        self.population_estimate_category.name,
        'PopulationEstimateCategory'
    )

TestPopulationSatatus

Bases: TestCase

Test for population status model.

setUp

setUp()

setup test data.

Source code in django_project/population_data/tests/test_population_data_models.py
def setUp(self) -> None:
    """setup test data."""
    self.population_status = PopulationStatusF(
        name='name'
        )

test_create_population_status

test_create_population_status()

test create population status.

Source code in django_project/population_data/tests/test_population_data_models.py
def test_create_population_status(self):
    """test create population status."""
    self.assertEqual(self.population_status.name, 'name')
    self.assertEqual(str(self.population_status), 'name')
    self.assertEqual(PopulationStatus.objects.count(), 1)

test_delete_population_status

test_delete_population_status()

test delete population status.

Source code in django_project/population_data/tests/test_population_data_models.py
def test_delete_population_status(self):
    """test delete population status."""
    self.population_status.delete()
    self.assertEqual(PopulationStatus.objects.count(), 0)

test_population_status_name_constraint

test_population_status_name_constraint()

Test population status name contraint.

Source code in django_project/population_data/tests/test_population_data_models.py
def test_population_status_name_constraint(self):
    """Test population status name contraint."""
    another = PopulationStatusF(name='Population Status')
    self.assertEqual(PopulationStatus.objects.count(), 2)
    self.assertNotEqual(self.population_status.name, another.name)

    with self.assertRaises(Exception) as raised:
        PopulationStatusF(name='name')

test_update_population_status

test_update_population_status()

test update population status.

Source code in django_project/population_data/tests/test_population_data_models.py
def test_update_population_status(self):
    """test update population status."""
    self.population_status.name = 'PopulationSatatus'
    self.population_status.save()
    self.assertEqual(
        self.population_status.name,
        'PopulationSatatus'
    )

TestSamplingEffortCoverage

Bases: TestCase

Test for sampling effort coverage model.

setUp

setUp()

setup test data.

Source code in django_project/population_data/tests/test_population_data_models.py
def setUp(self) -> None:
    """setup test data."""
    self.coverage = SamplingEffortCoverageF(
        name='name'
        )

test_create_sampling_effort_cov

test_create_sampling_effort_cov()

test create sampling effort coverage.

Source code in django_project/population_data/tests/test_population_data_models.py
def test_create_sampling_effort_cov(self):
    """test create sampling effort coverage."""
    self.assertEqual(self.coverage.name, 'name')
    self.assertEqual(str(self.coverage), 'name')
    self.assertEqual(SamplingEffortCoverage.objects.count(), 1)

test_delete_sampling_effort_cov

test_delete_sampling_effort_cov()

test delete sampling effort coverage.

Source code in django_project/population_data/tests/test_population_data_models.py
def test_delete_sampling_effort_cov(self):
    """test delete sampling effort coverage."""
    self.coverage.delete()
    self.assertEqual(SamplingEffortCoverage.objects.count(), 0)

test_sampling_effort_cov_name_constraint

test_sampling_effort_cov_name_constraint()

Test sampling effort coverage name contraint.

Source code in django_project/population_data/tests/test_population_data_models.py
def test_sampling_effort_cov_name_constraint(self):
    """Test sampling effort coverage name contraint."""
    another = SamplingEffortCoverageF(name='Coverage2')
    self.assertEqual(SamplingEffortCoverage.objects.count(), 2)
    self.assertNotEqual(self.coverage.name, another.name)

    with self.assertRaises(Exception) as raised:
        SamplingEffortCoverageF(name='name')

test_update_sampling_effort_cov

test_update_sampling_effort_cov()

test update sampling effort coverage.

Source code in django_project/population_data/tests/test_population_data_models.py
def test_update_sampling_effort_cov(self):
    """test update sampling effort coverage."""
    self.coverage.name = 'Test1'
    self.coverage.save()
    self.assertEqual(
        self.coverage.name,
        'Test1'
    )