def setUp(self): super(TestSiteExtraction, self).setUp() self.site_1 = factories.SiteFactory(project=self.project_1, code='COT') self.ds_1_rows = [ ['What', 'When', 'Who', 'Site'], ] self.ds_1 = self._create_dataset_from_rows(self.ds_1_rows) self.assertTrue(self.ds_1.is_custodian(self.custodian_1_user)) # add a site code foreign key self.schema_1 = self.ds_1.schema_data helpers.add_model_field_foreign_key_to_schema( self.schema_1, { 'schema_field': 'Site', 'model': 'Site', 'model_field': 'code' } ) self.ds_1.data_package = helpers.create_data_package_from_schema(self.schema_1) self.ds_1.save() self.ds_1.refresh_from_db() self.assertTrue(self.ds_1.schema.has_fk_for_model('Site')) # create one record with site self.record_1 = self._create_record( self.custodian_1_client, self.ds_1, { 'What': 'Something', 'When': '2018-06-30', 'Site': self.site_1.code }) self.assertEqual(self.record_1.site.pk, self.site_1.pk)
def _create_dataset_with_schema(self, project, client, schema): resp = client.post( reverse('api:dataset-list'), data={ "name": "Test site code geometry", "type": Dataset.TYPE_OBSERVATION, "project": project.pk, 'data_package': helpers.create_data_package_from_schema(schema) }, format='json') self.assertEqual(resp.status_code, status.HTTP_201_CREATED) dataset = Dataset.objects.filter(id=resp.json().get('id')).first() self.assertIsNotNone(dataset) return dataset