diff options
author | Matthew Lemon <matt@matthewlemon.com> | 2020-02-19 21:22:21 +0000 |
---|---|---|
committer | Matthew Lemon <matt@matthewlemon.com> | 2020-02-19 21:22:21 +0000 |
commit | ac2a715c0bc38e8a03640c604b8ed082456b2109 (patch) | |
tree | dec73bfcbf7920e18e94c493484e7368641d3387 | |
parent | afed43baeb9b4f9ee9f449b7be4e8a89a65ce9a8 (diff) |
new migrations and working through factory issue
Diffstat (limited to '')
-rw-r--r-- | ctrack/caf/migrations/0001_initial.py | 2 | ||||
-rw-r--r-- | ctrack/caf/migrations/0002_auto_20200218_2221.py (renamed from ctrack/caf/migrations/0002_auto_20200218_1605.py) | 4 | ||||
-rw-r--r-- | ctrack/contrib/sites/migrations/0001_initial.py | 2 | ||||
-rw-r--r-- | ctrack/organisations/admin.py | 2 | ||||
-rw-r--r-- | ctrack/organisations/management/commands/generate_people.py | 97 | ||||
-rw-r--r-- | ctrack/organisations/migrations/0001_initial.py | 14 | ||||
-rw-r--r-- | ctrack/organisations/migrations/0002_auto_20200218_2221.py (renamed from ctrack/organisations/migrations/0002_auto_20200218_1605.py) | 2 | ||||
-rw-r--r-- | ctrack/organisations/models.py | 14 | ||||
-rw-r--r-- | ctrack/organisations/tests/factories.py | 114 | ||||
-rw-r--r-- | ctrack/organisations/tests/test_factories.py | 41 | ||||
-rw-r--r-- | ctrack/users/migrations/0001_initial.py | 2 |
11 files changed, 177 insertions, 117 deletions
diff --git a/ctrack/caf/migrations/0001_initial.py b/ctrack/caf/migrations/0001_initial.py index bc6cc5a..93ca671 100644 --- a/ctrack/caf/migrations/0001_initial.py +++ b/ctrack/caf/migrations/0001_initial.py @@ -1,4 +1,4 @@ -# Generated by Django 2.2.9 on 2020-02-18 16:05 +# Generated by Django 2.2.9 on 2020-02-18 22:21 from django.db import migrations, models import django.db.models.deletion diff --git a/ctrack/caf/migrations/0002_auto_20200218_1605.py b/ctrack/caf/migrations/0002_auto_20200218_2221.py index bd43d8e..629ce1f 100644 --- a/ctrack/caf/migrations/0002_auto_20200218_1605.py +++ b/ctrack/caf/migrations/0002_auto_20200218_2221.py @@ -1,4 +1,4 @@ -# Generated by Django 2.2.9 on 2020-02-18 16:05 +# Generated by Django 2.2.9 on 2020-02-18 22:21 from django.db import migrations, models import django.db.models.deletion @@ -9,8 +9,8 @@ class Migration(migrations.Migration): initial = True dependencies = [ - ('caf', '0001_initial'), ('organisations', '0001_initial'), + ('caf', '0001_initial'), ] operations = [ diff --git a/ctrack/contrib/sites/migrations/0001_initial.py b/ctrack/contrib/sites/migrations/0001_initial.py index fddaae2..92cbfd2 100644 --- a/ctrack/contrib/sites/migrations/0001_initial.py +++ b/ctrack/contrib/sites/migrations/0001_initial.py @@ -1,4 +1,4 @@ -# Generated by Django 2.2.9 on 2020-02-18 16:05 +# Generated by Django 2.2.9 on 2020-02-18 22:21 import django.contrib.sites.models from django.db import migrations, models diff --git a/ctrack/organisations/admin.py b/ctrack/organisations/admin.py index a2d2b3f..73df2a9 100644 --- a/ctrack/organisations/admin.py +++ b/ctrack/organisations/admin.py @@ -5,7 +5,7 @@ from .models import Organisation, Address, AddressType, Person, Role, Mode, Subm # So we can get the organisation name - a reverse lookup def get_organisation_name(person): - return Organisation.objects.filter(person__name=person).first().name + return Organisation.objects.filter(person__id=person.id).first().name # We need this to ensure the column header in the admin does't read the func name diff --git a/ctrack/organisations/management/commands/generate_people.py b/ctrack/organisations/management/commands/generate_people.py index 0cb8c5b..d61902e 100644 --- a/ctrack/organisations/management/commands/generate_people.py +++ b/ctrack/organisations/management/commands/generate_people.py @@ -1,99 +1,4 @@ -from datetime import datetime - -import random - -import factory -from django.core.management.base import BaseCommand, CommandError -from factory import DjangoModelFactory, Faker, SubFactory, post_generation - -from ctrack.organisations.models import Organisation, Person, Submode, Mode - - -def _random_mode(): - sms = [ - "Aviation", - "Rail", - "Maritime", - "Animal", - "Stellar", - ] - return sms[random.randint(0, len(sms) - 1)] - -def _random_submode(): - sms = [ - "Light Rail", - "Heavy Rail", - "Passenger Rail", - "Animal Transport", - "Infrastructure", - ] - return sms[random.randint(0, len(sms) - 1)] - - -class ModeFactory(DjangoModelFactory): - class Meta: - model = Mode - - descriptor = factory.LazyFunction(_random_mode) - -class SubModeFactory(DjangoModelFactory): - class Meta: - model = Submode - - descriptor = factory.LazyFunction(_random_submode) - mode = SubFactory(ModeFactory) - - -class OrganisationFactory(DjangoModelFactory): - class Meta: - model = Organisation - - name = Faker("company") - slug = Faker("lexify", text="????", letters="abcdsg") - submode = SubFactory(SubModeFactory) - designation_type = 1 - registered_company_name = Faker("company") - registered_company_number = Faker("numerify", text="######") - date_updated = Faker("date_this_year", before_today=True) - updated_by = SubFactory( - "ctrack.organisations.management.commands.generate_people.PersonFactory" - ) - comments = Faker("paragraph", nb_sentences=3) - active = True - - -class PersonFactory(DjangoModelFactory): - class Meta: - model = Person - - primary_nis_contact = True - voluntary_point_of_contact = True - has_egress = False - title = Faker("prefix") - job_title = Faker("job") - first_name = Faker("first_name") - last_name = Faker("last_name") - organisation = SubFactory(OrganisationFactory) - role = Faker("job") - email = Faker("ascii_company_email") - secondary_email = "ascii_company_email" - mobile = Faker("msisdn", locale="en_GB") - landline = Faker("phone_number", locale="en_GB") - date_updated = factory.LazyFunction(datetime.now) - updated_by = SubFactory( - "ctrack.organisations.management.commands.generate_people.PersonFactory" - ) - clearance = factory.LazyFunction(datetime.now) - clearance_sponsor = Faker("name", locale="en_GB") - clearance_start_date = factory.LazyFunction(datetime.now) - clearance_last_checked = factory.LazyFunction(datetime.now) - clearance_expiry = factory.LazyFunction(datetime.now) - active = True - date_ended = Faker("date_this_year") - predecessor = SubFactory( - "ctrack.organisations.management.commands.generate_people.PersonFactory" - ) - comments = "Yaa!" +from django.core.management import BaseCommand class Command(BaseCommand): diff --git a/ctrack/organisations/migrations/0001_initial.py b/ctrack/organisations/migrations/0001_initial.py index 0d8c2c4..1ec5895 100644 --- a/ctrack/organisations/migrations/0001_initial.py +++ b/ctrack/organisations/migrations/0001_initial.py @@ -1,4 +1,4 @@ -# Generated by Django 2.2.9 on 2020-02-18 16:05 +# Generated by Django 2.2.9 on 2020-02-18 22:21 from django.db import migrations, models import django.db.models.deletion @@ -91,14 +91,14 @@ class Migration(migrations.Migration): ('date_updated', models.DateField(auto_now=True)), ('clearance', models.IntegerField(choices=[(1, 'NA'), (2, 'BPSS'), (3, 'CTC'), (4, 'SC'), (5, 'DV'), (6, 'Other')], default=1)), ('clearance_sponsor', models.CharField(blank=True, max_length=100)), - ('clearance_start_date', models.DateField(blank=True)), - ('clearance_last_checked', models.DateField(blank=True)), - ('clearance_expiry', models.DateField(blank=True)), + ('clearance_start_date', models.DateField(blank=True, null=True)), + ('clearance_last_checked', models.DateField(blank=True, null=True)), + ('clearance_expiry', models.DateField(blank=True, null=True)), ('active', models.BooleanField(default=True)), - ('date_ended', models.DateField(blank=True)), - ('comments', models.TextField(max_length=1000)), + ('date_ended', models.DateField(blank=True, null=True)), + ('comments', models.TextField(blank=True, max_length=1000)), ('organisation', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='organisations.Organisation')), - ('predecessor', models.ForeignKey(blank=True, on_delete=django.db.models.deletion.CASCADE, related_name='previous_person', to='organisations.Person')), + ('predecessor', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, related_name='previous_person', to='organisations.Person')), ('role', models.ManyToManyField(to='organisations.Role')), ], options={ diff --git a/ctrack/organisations/migrations/0002_auto_20200218_1605.py b/ctrack/organisations/migrations/0002_auto_20200218_2221.py index 14359dd..45022d6 100644 --- a/ctrack/organisations/migrations/0002_auto_20200218_1605.py +++ b/ctrack/organisations/migrations/0002_auto_20200218_2221.py @@ -1,4 +1,4 @@ -# Generated by Django 2.2.9 on 2020-02-18 16:05 +# Generated by Django 2.2.9 on 2020-02-18 22:21 from django.conf import settings from django.db import migrations, models diff --git a/ctrack/organisations/models.py b/ctrack/organisations/models.py index 631d441..a04fa32 100644 --- a/ctrack/organisations/models.py +++ b/ctrack/organisations/models.py @@ -62,18 +62,18 @@ class Person(models.Model): updated_by = models.ForeignKey(get_user_model(), on_delete=models.CASCADE) clearance = models.IntegerField(choices=CLEARANCE_LEVEL, default=1) clearance_sponsor = models.CharField(max_length=100, blank=True) - clearance_start_date = models.DateField(blank=True) - clearance_last_checked = models.DateField(blank=True) - clearance_expiry = models.DateField(blank=True) + clearance_start_date = models.DateField(blank=True, null=True) + clearance_last_checked = models.DateField(blank=True, null=True) + clearance_expiry = models.DateField(blank=True, null=True) active = models.BooleanField(default=True) - date_ended = models.DateField(blank=True) + date_ended = models.DateField(blank=True, null=True) predecessor = models.ForeignKey( - "self", blank=True, on_delete=models.CASCADE, related_name="previous_person" + "self", blank=True, on_delete=models.CASCADE, related_name="previous_person", null=True ) - comments = models.TextField(max_length=1000) + comments = models.TextField(max_length=1000, blank=True) def __str__(self): - return self.name + return f"{self.first_name} {self.last_name}" def get_organisation_name(self): return self.organisation.name diff --git a/ctrack/organisations/tests/factories.py b/ctrack/organisations/tests/factories.py new file mode 100644 index 0000000..32692cc --- /dev/null +++ b/ctrack/organisations/tests/factories.py @@ -0,0 +1,114 @@ +import random +from datetime import datetime + +import factory +from django.contrib.auth import get_user_model +from factory import DjangoModelFactory, Faker, SubFactory, post_generation + +from ctrack.organisations.models import Mode, Organisation, Person, Role, Submode + +User = get_user_model() + + +def _random_mode(): + sms = [ + "Aviation", + "Rail", + "Maritime", + "Animal", + "Stellar", + ] + return sms[random.randint(0, len(sms) - 1)] + + +def _random_submode(): + sms = [ + "Light Rail", + "Heavy Rail", + "Passenger Rail", + "Animal Transport", + "Infrastructure", + ] + return sms[random.randint(0, len(sms) - 1)] + + +class UserFactory(DjangoModelFactory): + class Meta: + model = User + + name = Faker("name", locale="en_GB") + + +class ModeFactory(DjangoModelFactory): + class Meta: + model = Mode + + descriptor = factory.LazyFunction(_random_mode) + + +class SubModeFactory(DjangoModelFactory): + class Meta: + model = Submode + + descriptor = factory.LazyFunction(_random_submode) + mode = SubFactory(ModeFactory) + + +class OrganisationFactory(DjangoModelFactory): + class Meta: + model = Organisation + + name = Faker("company") + slug = Faker("lexify", text="????", letters="abcdsg") + submode = SubFactory(SubModeFactory) + designation_type = 1 + registered_company_name = Faker("company") + registered_company_number = Faker("numerify", text="######") + date_updated = Faker("date_this_year", before_today=True) + updated_by = SubFactory(UserFactory) + comments = Faker("paragraph", nb_sentences=3) + active = True + + +class RoleFactory(DjangoModelFactory): + class Meta: + model = Role + + name = Faker("name") + + +class PersonFactory(DjangoModelFactory): + class Meta: + model = Person + + @post_generation + def role(self, create, extracted, **kwargs): + if not create: + return + if extracted: + for role in extracted: + self.role.add(role) + + primary_nis_contact = True + voluntary_point_of_contact = True + has_egress = False + title = Faker("prefix") + job_title = Faker("job") + first_name = Faker("first_name") + last_name = Faker("last_name") + organisation = SubFactory(OrganisationFactory) + email = Faker("ascii_company_email") + secondary_email = "ascii_company_email" + mobile = Faker("msisdn", locale="en_GB") + landline = Faker("phone_number", locale="en_GB") + date_updated = factory.LazyFunction(datetime.now) + updated_by = SubFactory(UserFactory) + clearance = factory.LazyFunction(datetime.now) + clearance_sponsor = Faker("name", locale="en_GB") + clearance_start_date = factory.LazyFunction(datetime.now) + clearance_last_checked = factory.LazyFunction(datetime.now) + clearance_expiry = factory.LazyFunction(datetime.now) + active = True + date_ended = Faker("date_this_year") +# predecessor = SubFactory("ctrack.organisations.tests.factories.PersonFactory") + comments = "Yaa!" diff --git a/ctrack/organisations/tests/test_factories.py b/ctrack/organisations/tests/test_factories.py new file mode 100644 index 0000000..38082fc --- /dev/null +++ b/ctrack/organisations/tests/test_factories.py @@ -0,0 +1,41 @@ +import pytest + +from .factories import ( + PersonFactory, + ModeFactory, + SubModeFactory, + UserFactory, + OrganisationFactory, + RoleFactory) + + +def test_mode_factory(): + m = ModeFactory.build() + assert m.descriptor + + +def test_submode_factory(): + sm = SubModeFactory.build() + assert sm.descriptor + assert sm.mode + + +def test_user_factory(): + u = UserFactory.build() + assert u.name + + +def test_organisation_factory(): + o = OrganisationFactory.build() + assert o.name + + +def test_role_factory(): + r = RoleFactory.build() + assert r.name + + +def test_person_factory(): + r = RoleFactory.build() + p = PersonFactory.build(role=r) + assert p.first_name diff --git a/ctrack/users/migrations/0001_initial.py b/ctrack/users/migrations/0001_initial.py index 1532380..7c8f11a 100644 --- a/ctrack/users/migrations/0001_initial.py +++ b/ctrack/users/migrations/0001_initial.py @@ -1,4 +1,4 @@ -# Generated by Django 2.2.9 on 2020-02-18 16:05 +# Generated by Django 2.2.9 on 2020-02-18 22:21 import django.contrib.auth.models import django.contrib.auth.validators |