-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1151 from akvo/feature/1150-parentchild-monitorin…
…g-data [#1150] Seed Monitoring Data when it gets Approved
- Loading branch information
Showing
5 changed files
with
140 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
# Generated by Django 4.0.4 on 2024-02-07 06:41 | ||
|
||
from django.db import migrations, models | ||
import django.db.models.deletion | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
('v1_data', '0028_rename_deleted_pendingformdata_deleted_at'), | ||
] | ||
|
||
operations = [ | ||
migrations.AddField( | ||
model_name='formdata', | ||
name='parent', | ||
field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, related_name='children', to='v1_data.formdata'), | ||
), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
from django.test import TestCase | ||
from django.core.management import call_command | ||
from api.v1.v1_users.models import SystemUser | ||
from api.v1.v1_data.models import ( | ||
FormData, | ||
PendingFormData, | ||
PendingDataBatch | ||
) | ||
from api.v1.v1_forms.models import Forms | ||
from api.v1.v1_forms.constants import FormTypes | ||
from api.v1.v1_profile.models import Administration, Access | ||
from api.v1.v1_profile.constants import UserRoleTypes | ||
from api.v1.v1_data.management.commands.fake_data_seeder import ( | ||
add_fake_answers | ||
) | ||
from api.v1.v1_data.tasks import seed_approved_data | ||
|
||
|
||
class MonitoringDataTestCase(TestCase): | ||
def setUp(self): | ||
call_command('administration_seeder', '--test') | ||
call_command('form_seeder', '--test') | ||
self.user = SystemUser.objects.create_user( | ||
email='[email protected]', | ||
password='test1234', | ||
first_name='test', | ||
last_name='testing', | ||
) | ||
self.administration = Administration.objects.filter( | ||
parent__isnull=True | ||
).first() | ||
role = UserRoleTypes.user | ||
self.user_access = Access.objects.create( | ||
user=self.user, role=role, administration=self.administration | ||
) | ||
self.uuid = '1234567890' | ||
self.form = Forms.objects.filter(type=FormTypes.county).first() | ||
self.data = FormData.objects.create( | ||
parent=None, | ||
uuid=self.uuid, | ||
form=self.form, | ||
administration=self.administration, | ||
created_by=self.user, | ||
) | ||
add_fake_answers(self.data, FormTypes.county) | ||
|
||
def test_parent_data(self): | ||
self.assertTrue(self.data.name) | ||
self.assertEqual(self.data.parent, None) | ||
self.assertEqual(self.data.uuid, self.uuid) | ||
self.assertEqual(self.data.form, self.form) | ||
self.assertEqual(self.data.administration, self.administration) | ||
self.assertEqual(self.data.created_by, self.user) | ||
self.assertTrue(self.data.data_answer.count() > 0) | ||
|
||
def test_seed_monitoring_batch(self): | ||
for i in range(2): | ||
pending_data = PendingFormData.objects.create( | ||
uuid=self.uuid if i == 0 else f'{self.uuid}{i}', | ||
form=self.form, | ||
administration=self.administration, | ||
created_by=self.user, | ||
) | ||
add_fake_answers(pending_data, | ||
form_type=FormTypes.county, | ||
pending=True) | ||
self.assertTrue(PendingFormData.objects.count() == 2) | ||
batch = PendingDataBatch.objects.create( | ||
name='test batch', | ||
administration=self.administration, | ||
form=self.form, | ||
user=self.user, | ||
approved=True | ||
) | ||
batch.batch_pending_data_batch.add(*PendingFormData.objects.all()) | ||
self.assertTrue(batch.batch_pending_data_batch.count() == 2) | ||
for pending_data in batch.batch_pending_data_batch.all(): | ||
seed_approved_data(pending_data) | ||
self.assertTrue(FormData.objects.count() == 3) | ||
child_data = FormData.objects.filter( | ||
parent__isnull=False | ||
).first() | ||
self.assertEqual(child_data.parent.uuid, self.uuid) | ||
self.assertEqual(self.data.children.first().id, child_data.id) | ||
self.assertEqual(self.data.children.count(), 1) |