def test_do_not_send_questionnaire_when_project_has_data(self): project = ProjectFactory.create() questionnaire = QuestionnaireFactory.create( project=project, xls_form=self._get_form('xls-form')) SpatialUnitFactory.create(project=project) data = { 'name': 'New name', 'access': project.access, 'contacts-TOTAL_FORMS': 1, 'contacts-INITIAL_FORMS': 0, 'contacts-0-name': '', 'contacts-0-email': '', 'contacts-0-tel': '' } form = forms.ProjectEditDetails( instance=project, data=data, initial={'questionnaire': questionnaire.xls_form.url}) assert form.is_valid() is True form.save() project.refresh_from_db() assert project.name == data['name'] assert project.current_questionnaire == questionnaire.id
def test_has_records(self): project = ProjectFactory.create() assert project.has_records is False project = ProjectFactory.create() SpatialUnitFactory.create(project=project) assert project.has_records is True
def test_replace_questionnaire_when_project_has_data(self): project = ProjectFactory.create() questionnaire = QuestionnaireFactory.create( project=project, xls_form=self._get_form('xls-form')) SpatialUnitFactory.create(project=project) data = { 'name': 'New name', 'questionnaire': self._get_form('xls-form-copy'), 'access': project.access, 'contacts-TOTAL_FORMS': 1, 'contacts-INITIAL_FORMS': 0, 'contacts-0-name': '', 'contacts-0-email': '', 'contacts-0-tel': '' } form = forms.ProjectEditDetails( instance=project, data=data, initial={'questionnaire': questionnaire.xls_form.url}) assert form.is_valid() is False assert ("Data has already been contributed to this project. To " "ensure data integrity, uploading a new questionnaire is " "disabled for this project." in form.errors.get('questionnaire'))
def test_has_records(self): project = ProjectFactory.create() assert project.has_records is False project = ProjectFactory.create() SpatialUnitFactory.create(project=project) assert project.has_records is True
def test_write_features(self): ensure_dirs() project = ProjectFactory.create(current_questionnaire='123abc') exporter = ShapeExporter(project) content_type = ContentType.objects.get(app_label='spatial', model='spatialunit') schema = Schema.objects.create(content_type=content_type, selectors=( project.organization.id, project.id, '123abc', )) attr_type = AttributeType.objects.get(name='text') Attribute.objects.create(schema=schema, name='key', long_name='Test field', attr_type=attr_type, index=0, required=False, omit=False) su1 = SpatialUnitFactory.create(project=project, geometry='POINT (1 1)', attributes={'key': 'value 1'}) su2 = SpatialUnitFactory.create(project=project, geometry='POINT (2 2)', attributes={'key': 'value 2'}) dst_dir = os.path.join(settings.MEDIA_ROOT, 'temp/file4') ds = exporter.create_datasource(dst_dir, 'file4') layers = exporter.create_shp_layers(ds, 'file4') csvfile = os.path.join(dst_dir, 'locations.csv') exporter.write_features(layers, csvfile) assert len(layers[0]) == 2 f = layers[0].GetNextFeature() while f: geom = f.geometry() assert geom.ExportToWkt() in ['POINT (1 1)', 'POINT (2 2)'] assert f.GetFieldAsString('id') in [su1.id, su2.id] f = layers[0].GetNextFeature() ds.Destroy() with open(csvfile) as csvfile: csvreader = csv.reader(csvfile) for i, row in enumerate(csvreader): if i == 0: assert row == ['id', 'type', 'key'] elif row[0] == su1.id: assert row == [su1.id, su1.type, 'value 1'] elif row[1] == su2.id: assert row == [su2.id, su2.type, 'value 2']
def test_create_with_blocked_questionnaire_upload(self): data = {'xls_form': self.get_form('xls-form')} SpatialUnitFactory.create(project=self.prj) response = self.request(method='PUT', user=self.user, post_data=data) assert response.status_code == 400 assert ("Data has already been contributed to this " "project. To ensure data integrity, uploading a " "new questionnaire is disabled for this project." in response.content.get('xls_form'))
def test_create_with_blocked_questionnaire_upload(self): data = {'xls_form': self.get_form('xls-form')} SpatialUnitFactory.create(project=self.prj) response = self.request(method='PUT', user=self.user, post_data=data) assert response.status_code == 400 assert ("Data has already been contributed to this " "project. To ensure data integrity, uploading a " "new questionnaire is disabled for this project." in response.content.get('xls_form'))
def setup_models(self): self.user = UserFactory.create() assign_policies(self.user) self.prj = ProjectFactory.create(slug='test-project', access='public') self.party = PartyFactory.create(project=self.prj) self.spatial_unit = SpatialUnitFactory.create(project=self.prj) self.rel = TenureRelationshipFactory.create( project=self.prj, party=self.party, spatial_unit=self.spatial_unit) self.party2 = PartyFactory.create(project=self.prj) self.spatial_unit2 = SpatialUnitFactory.create(project=self.prj)
def setup_models(self): self.user = UserFactory.create() assign_policies(self.user) self.prj = ProjectFactory.create(slug='test-project', access='public') self.party = PartyFactory.create(project=self.prj) self.spatial_unit = SpatialUnitFactory.create(project=self.prj) self.rel = TenureRelationshipFactory.create( project=self.prj, party=self.party, spatial_unit=self.spatial_unit) self.party2 = PartyFactory.create(project=self.prj) self.spatial_unit2 = SpatialUnitFactory.create(project=self.prj)
def test_get_xls_download(self): ensure_dirs() data = {"type": "xls"} user = UserFactory.create() project = ProjectFactory.create() geometry = "SRID=4326;POINT (30 10)" SpatialUnitFactory.create(project=project, geometry=geometry) form = forms.DownloadForm(project, user, data=data) assert form.is_valid() is True path, mime = form.get_file() assert "{}-{}".format(project.id, user.id) in path assert mime == "application/vnd.openxmlformats-officedocument." "spreadsheetml.sheet"
def test_get_shape_download(self): ensure_dirs() data = {'type': 'shp'} user = UserFactory.create() project = ProjectFactory.create() content_type = ContentType.objects.get(app_label='spatial', model='spatialunit') schema = Schema.objects.create( content_type=content_type, selectors=(project.organization.id, project.id)) attr_type = AttributeType.objects.get(name='text') Attribute.objects.create( schema=schema, name='key', long_name='Test field', attr_type=attr_type, index=0, required=False, omit=False ) su1 = SpatialUnitFactory.create( project=project, geometry='POINT (1 1)', attributes={'key': 'value 1'}) SpatialUnitFactory.create( project=project, geometry='SRID=4326;' 'MULTIPOLYGON (((30 20, 45 40, 10 40, 30 20)),' '((15 5, 40 10, 10 20, 5 10, 15 5)))', attributes={'key': 'value 2'}) party = PartyFactory.create(project=project) TenureRelationshipFactory.create( spatial_unit=su1, party=party, project=project) form = forms.DownloadForm(project, user, data=data) assert form.is_valid() is True path, mime = form.get_file() assert '{}-{}'.format(project.id, user.id) in path assert (mime == 'application/zip') with ZipFile(path, 'r') as testzip: assert len(testzip.namelist()) == 12 assert 'point.dbf' in testzip.namelist() assert 'point.prj' in testzip.namelist() assert 'point.shp' in testzip.namelist() assert 'point.shx' in testzip.namelist() assert 'multipolygon.dbf' in testzip.namelist() assert 'multipolygon.prj' in testzip.namelist() assert 'multipolygon.shp' in testzip.namelist() assert 'multipolygon.shx' in testzip.namelist() assert 'relationships.csv' in testzip.namelist() assert 'parties.csv' in testzip.namelist() assert 'locations.csv' in testzip.namelist() assert 'README.txt' in testzip.namelist()
def test_get_xls_download(self): ensure_dirs() data = {'type': 'xls'} user = UserFactory.create() project = ProjectFactory.create() geometry = 'SRID=4326;POINT (30 10)' SpatialUnitFactory.create(project=project, geometry=geometry) form = forms.DownloadForm(project, user, data=data) assert form.is_valid() is True path, mime = form.get_file() assert '{}-{}'.format(project.id, user.id) in path assert (mime == 'application/vnd.openxmlformats-officedocument.' 'spreadsheetml.sheet')
def test_omit_attribute(self): project = ProjectFactory.create(name='TestProject') QuestionnaireFactory.create(project=project) content_type = ContentType.objects.get(app_label='spatial', model='spatialunit') create_attrs_schema(project=project, dict=location_xform_group, content_type=content_type, errors=[]) with pytest.raises(KeyError): SpatialUnitFactory.create(project=project, attributes={ 'notes': 'Some textual content', })
def test_make_download(self): ensure_dirs() project = ProjectFactory.create(current_questionnaire='123abc') exporter = ShapeExporter(project) content_type = ContentType.objects.get(app_label='spatial', model='spatialunit') schema = Schema.objects.create(content_type=content_type, selectors=( project.organization.id, project.id, '123abc', )) attr_type = AttributeType.objects.get(name='text') Attribute.objects.create(schema=schema, name='key', long_name='Test field', attr_type=attr_type, index=0, required=False, omit=False) SpatialUnitFactory.create(project=project, geometry='POINT (1 1)', attributes={'key': 'value 1'}) SpatialUnitFactory.create( project=project, geometry='SRID=4326;' 'MULTIPOLYGON (((30 20, 45 40, 10 40, 30 20)),' '((15 5, 40 10, 10 20, 5 10, 15 5)))', attributes={'key': 'value 2'}) path, mime = exporter.make_download('file5') assert path == os.path.join(settings.MEDIA_ROOT, 'temp/file5.zip') assert mime == 'application/zip' with ZipFile(path, 'r') as testzip: assert len(testzip.namelist()) == 10 assert 'point.dbf' in testzip.namelist() assert 'point.prj' in testzip.namelist() assert 'point.shp' in testzip.namelist() assert 'point.shx' in testzip.namelist() assert 'multipolygon.dbf' in testzip.namelist() assert 'multipolygon.prj' in testzip.namelist() assert 'multipolygon.shp' in testzip.namelist() assert 'multipolygon.shx' in testzip.namelist() assert 'locations.csv' in testzip.namelist() assert 'README.txt' in testzip.namelist()
def test_write_items(self): project = ProjectFactory.create(current_questionnaire='123abc') content_type = ContentType.objects.get(app_label='spatial', model='spatialunit') schema = Schema.objects.create(content_type=content_type, selectors=( project.organization.id, project.id, '123abc', )) attr_type = AttributeType.objects.get(name='text') Attribute.objects.create(schema=schema, name='key', long_name='Test field', attr_type=attr_type, index=0, required=False, omit=False) spatialunit_1 = SpatialUnitFactory.create( project=project, geometry='POINT (1 1)', attributes={'key': 'value 1'}) spatialunit_2 = SpatialUnitFactory.create( project=project, geometry='POINT (2 2)', attributes={'key': 'value 2'}) spatialunits = [spatialunit_1, spatialunit_2] attrs = ['id', 'geometry.ewkt'] workbook = Workbook() worksheet = workbook.create_sheet() worksheet.title = 'locations' exporter = XLSExporter(project) exporter.write_items(worksheet, spatialunits, content_type, attrs) assert worksheet['A1'].value == 'id' assert worksheet['B1'].value == 'geometry.ewkt' assert worksheet['C1'].value == 'key' assert worksheet['A2'].value == spatialunit_1.id assert worksheet['B2'].value == spatialunit_1.geometry.ewkt assert worksheet['C2'].value == 'value 1' assert worksheet['A3'].value == spatialunit_2.id assert worksheet['B3'].value == spatialunit_2.geometry.ewkt assert worksheet['C3'].value == 'value 2'
def test_spatial_unit_invalid_attribute(self): project = ProjectFactory.create(name='TestProject') QuestionnaireFactory.create(project=project) content_type = ContentType.objects.get(app_label='spatial', model='spatialunit') create_attrs_schema(project=project, dict=location_xform_group, content_type=content_type, errors=[]) assert 1 == Schema.objects.all().count() with pytest.raises(KeyError): SpatialUnitFactory.create(project=project, attributes={ 'invalid_attribute': 'yes', })
def test_omit_attribute(self): project = ProjectFactory.create(name='TestProject') QuestionnaireFactory.create(project=project) content_type = ContentType.objects.get( app_label='spatial', model='spatialunit') create_attrs_schema( project=project, dict=location_xform_group, content_type=content_type, errors=[]) with pytest.raises(KeyError): SpatialUnitFactory.create( project=project, attributes={ 'notes': 'Some textual content', } )
def test_get_absolute_url(self): su = SpatialUnitFactory.create() assert su.get_absolute_url() == ('/organizations/{org}/projects/{prj}/' 'records/locations/{id}/'.format( org=su.project.organization.slug, prj=su.project.slug, id=su.id))
def setup_models(self): self.user = UserFactory.create() assign_policies(self.user) self.prj = ProjectFactory.create(slug='test-project', access='public') self.party1 = PartyFactory.create(project=self.prj, name='Landowner') self.su2 = SpatialUnitFactory.create(project=self.prj, type='PA')
def test_spatial_unit_invalid_attribute(self): project = ProjectFactory.create(name='TestProject') QuestionnaireFactory.create(project=project) content_type = ContentType.objects.get( app_label='spatial', model='spatialunit') create_attrs_schema( project=project, dict=location_xform_group, content_type=content_type, errors=[]) assert 1 == Schema.objects.all().count() with pytest.raises(KeyError): SpatialUnitFactory.create( project=project, attributes={ 'invalid_attribute': 'yes', } )
def setup_models(self): self.user = UserFactory.create() assign_policies(self.user) self.project = ProjectFactory.create(slug='test-project') QuestionnaireFactory.create(project=self.project) content_type = ContentType.objects.get(app_label='party', model='party') create_attrs_schema(project=self.project, dict=individual_party_xform_group, content_type=content_type, errors=[]) create_attrs_schema(project=self.project, dict=default_party_xform_group, content_type=content_type, errors=[]) content_type = ContentType.objects.get(app_label='party', model='tenurerelationship') create_attrs_schema(project=self.project, dict=tenure_relationship_xform_group, content_type=content_type, errors=[]) self.su = SpatialUnitFactory.create(project=self.project, type='CB') self.party = PartyFactory.create(project=self.project, type='IN', attributes={ 'gender': 'm', 'homeowner': 'yes', 'dob': '1951-05-05' }) self.tenure_rel = TenureRelationshipFactory.create( spatial_unit=self.su, party=self.party, project=self.project, attributes={'notes': 'PBS is the best.'}) self.resource = ResourceFactory.create(project=self.project) self.results = get_fake_es_api_results(self.project, self.su, self.party, self.tenure_rel, self.resource) self.proj_result = self.results['hits']['hits'][0] self.su_result = self.results['hits']['hits'][1] self.party_result = self.results['hits']['hits'][2] self.tenure_rel_result = self.results['hits']['hits'][3] self.resource_result = self.results['hits']['hits'][4] self.query = 'searching' self.query_body = { 'query': { 'simple_query_string': { 'default_operator': 'and', 'query': self.query, } } } self.es_endpoint = '{}/project-{}/_search'.format( api_url, self.project.id) self.es_body = json.dumps(self.query_body, sort_keys=True)
def test_spatial_unit_attribute_schema(self): project = ProjectFactory.create(name='TestProject') QuestionnaireFactory.create(project=project) content_type = ContentType.objects.get(app_label='spatial', model='spatialunit') create_attrs_schema(project=project, question_group_dict=location_xform_group, content_type=content_type, errors=[], attr_type_ids=get_attr_type_ids()) spatial_unit = SpatialUnitFactory.create(project=project, attributes={ 'quality': 'polygon_high', 'infrastructure': ['water', 'food'] }) assert 1 == Schema.objects.all().count() schema = Schema.objects.get(content_type=content_type) assert schema is not None assert schema.selectors == [ project.organization.pk, project.pk, project.current_questionnaire ] assert 'quality' in spatial_unit.attributes.attributes assert 'polygon_high' == spatial_unit.attributes['quality'] assert 'infrastructure' in spatial_unit.attributes.attributes assert 'water' in spatial_unit.attributes['infrastructure'] # notes field is omitted in xform assert 'notes' not in spatial_unit.attributes.attributes
def setup_models(self): self.project = ProjectFactory.create(slug='test-project') self.su = SpatialUnitFactory.create(project=self.project) self.party = PartyFactory.create(project=self.project) self.tenure_rel = TenureRelationshipFactory.create( spatial_unit=self.su, party=self.party, project=self.project) self.resource = ResourceFactory.create(project=self.project)
def test_spatial_unit_attribute_schema(self): project = ProjectFactory.create(name='TestProject') QuestionnaireFactory.create(project=project) content_type = ContentType.objects.get( app_label='spatial', model='spatialunit') create_attrs_schema( project=project, dict=location_xform_group, content_type=content_type, errors=[]) spatial_unit = SpatialUnitFactory.create( project=project, attributes={ 'quality': 'polygon_high', 'infrastructure': ['water', 'food'] } ) assert 1 == Schema.objects.all().count() schema = Schema.objects.get(content_type=content_type) assert schema is not None assert schema.selectors == [ project.organization.pk, project.pk, project.current_questionnaire] assert 'quality' in spatial_unit.attributes.attributes assert 'polygon_high' == spatial_unit.attributes['quality'] assert 'infrastructure' in spatial_unit.attributes.attributes assert 'water' in spatial_unit.attributes['infrastructure'] # notes field is omitted in xform assert 'notes' not in spatial_unit.attributes.attributes
def test_unknown_attributes(self): project = ProjectFactory.create(name='Test Project') su = SpatialUnitFactory.create(project=project) party = PartyFactory.create(project=project) content_type = ContentType.objects.get( app_label='party', model='tenurerelationship') schema = Schema.objects.create( content_type=content_type, selectors=(project.organization.id, project.id, )) Attribute.objects.create( schema=schema, name='notes', long_name='Notes', attr_type=AttributeType.objects.get(name='text'), index=0 ) data = { 'spatial_unit': su.id, 'party': party.id, 'tenure_type': 'WR', 'attributes': { 'notes': 'Blah', 'age': 'Blah' } } serializer = serializers.TenureRelationshipSerializer( data=data, context={'project': project} ) assert serializer.is_valid() is False assert 'Unknown attribute "age"' in serializer.errors['attributes']
def setup_models(self): self.user = UserFactory.create() assign_policies(self.user) self.prj = ProjectFactory.create(slug='test-project', access='public') self.party1 = PartyFactory.create(project=self.prj, name='Landowner') self.su2 = SpatialUnitFactory.create(project=self.prj, type='PA')
def test_invalid_custom_tenure_type(self): project = ProjectFactory.create() questionnaire = q_factories.QuestionnaireFactory.create( project=project) question = q_factories.QuestionFactory.create( type='S1', name='tenure_type', questionnaire=questionnaire) q_factories.QuestionOptionFactory.create( question=question, name='FU', label='FU Label') su = SpatialUnitFactory.create(project=project) party = PartyFactory.create(project=project) data = { 'tenure_type': 'FH', 'spatial_unit': su.id, 'party': party.id } serializer = serializers.TenureRelationshipSerializer( data=data, context={'project': project}) assert serializer.is_valid() is False assert ("'FH' is not a valid choice for field 'tenure_type'." in serializer.errors['tenure_type'])
def setup_models(self): self.project = ProjectFactory.create() self.org_slug = self.project.organization.slug self.resource = ResourceFactory.create(project=self.project) self.location = SpatialUnitFactory.create(project=self.project) self.party = PartyFactory.create(project=self.project) self.tenurerel = TenureRelationshipFactory.create(project=self.project) self.project_attachment = ContentObject.objects.create( resource_id=self.resource.id, content_object=self.project, ) self.location_attachment = ContentObject.objects.create( resource_id=self.resource.id, content_object=self.location, ) self.party_attachment = ContentObject.objects.create( resource_id=self.resource.id, content_object=self.party, ) self.tenurerel_attachment = ContentObject.objects.create( resource_id=self.resource.id, content_object=self.tenurerel, ) self.user = UserFactory.create() assign_permissions(self.user)
def test_get_absolute_url(self): su = SpatialUnitFactory.create() assert su.get_absolute_url() == ( '/organizations/{org}/projects/{prj}/' 'records/locations/{id}/'.format( org=su.project.organization.slug, prj=su.project.slug, id=su.id))
def test_geometry(self): spatial_unit = SpatialUnitFactory.create(geometry='SRID=4326;POLYGON((' '11.36667 47.25000, ' '11.41667 47.25000, ' '11.41667 47.28333, ' '11.36667 47.28333, ' '11.36667 47.25000))') assert spatial_unit.geometry is not None
def test_make_download(self): ensure_dirs() project = ProjectFactory.create(current_questionnaire='123abc') exporter = ShapeExporter(project) content_type = ContentType.objects.get(app_label='spatial', model='spatialunit') schema = Schema.objects.create( content_type=content_type, selectors=(project.organization.id, project.id, '123abc', )) attr_type = AttributeType.objects.get(name='text') Attribute.objects.create( schema=schema, name='key', long_name='Test field', attr_type=attr_type, index=0, required=False, omit=False ) SpatialUnitFactory.create( project=project, geometry='POINT (1 1)', attributes={'key': 'value 1'}) SpatialUnitFactory.create( project=project, geometry='SRID=4326;' 'MULTIPOLYGON (((30 20, 45 40, 10 40, 30 20)),' '((15 5, 40 10, 10 20, 5 10, 15 5)))', attributes={'key': 'value 2'}) path, mime = exporter.make_download('file5') assert path == os.path.join(settings.MEDIA_ROOT, 'temp/file5.zip') assert mime == 'application/zip' with ZipFile(path, 'r') as testzip: assert len(testzip.namelist()) == 10 assert 'point.dbf' in testzip.namelist() assert 'point.prj' in testzip.namelist() assert 'point.shp' in testzip.namelist() assert 'point.shx' in testzip.namelist() assert 'multipolygon.dbf' in testzip.namelist() assert 'multipolygon.prj' in testzip.namelist() assert 'multipolygon.shp' in testzip.namelist() assert 'multipolygon.shx' in testzip.namelist() assert 'locations.csv' in testzip.namelist() assert 'README.txt' in testzip.namelist()
def setup_models(self): self.user = UserFactory.create() assign_policies(self.user) self.project = ProjectFactory.create(slug='test-project') QuestionnaireFactory.create(project=self.project) content_type = ContentType.objects.get( app_label='party', model='party') create_attrs_schema( project=self.project, dict=individual_party_xform_group, content_type=content_type, errors=[]) create_attrs_schema( project=self.project, dict=default_party_xform_group, content_type=content_type, errors=[]) content_type = ContentType.objects.get( app_label='party', model='tenurerelationship') create_attrs_schema( project=self.project, dict=tenure_relationship_xform_group, content_type=content_type, errors=[]) self.su = SpatialUnitFactory.create(project=self.project, type='CB') self.party = PartyFactory.create( project=self.project, type='IN', attributes={ 'gender': 'm', 'homeowner': 'yes', 'dob': '1951-05-05' }) self.tenure_rel = TenureRelationshipFactory.create( spatial_unit=self.su, party=self.party, project=self.project, attributes={'notes': 'PBS is the best.'}) self.resource = ResourceFactory.create(project=self.project) self.results = get_fake_es_api_results( self.project, self.su, self.party, self.tenure_rel, self.resource) self.proj_result = self.results['hits']['hits'][0] self.su_result = self.results['hits']['hits'][1] self.party_result = self.results['hits']['hits'][2] self.tenure_rel_result = self.results['hits']['hits'][3] self.resource_result = self.results['hits']['hits'][4] self.query = 'searching' self.query_body = { 'query': { 'simple_query_string': { 'default_operator': 'and', 'query': self.query, } }, 'from': 10, 'size': 20, 'sort': {'_score': {'order': 'desc'}}, } self.es_endpoint = '{}/project-{}/_search/'.format(api_url, self.project.id) self.es_body = json.dumps(self.query_body, sort_keys=True)
def test_geometry(self): spatial_unit = SpatialUnitFactory.create( geometry='SRID=4326;POLYGON((' '11.36667 47.25000, ' '11.41667 47.25000, ' '11.41667 47.28333, ' '11.36667 47.28333, ' '11.36667 47.25000))') assert spatial_unit.geometry is not None
def test_update_spatial_unit_fails(self): project = ProjectFactory.create() su = SpatialUnitFactory.create(type='BU', project=project) spatial_data = {'properties': {'type': ''}} serializer = serializers.SpatialUnitSerializer( su, data=spatial_data, partial=True, context={'project': project}) with pytest.raises(ValidationError): serializer.is_valid(raise_exception=True)
def test_valid_tenure_type(self): project = ProjectFactory.create() su = SpatialUnitFactory.create(project=project) party = PartyFactory.create(project=project) data = {'tenure_type': 'FH', 'spatial_unit': su.id, 'party': party.id} serializer = serializers.TenureRelationshipSerializer( data=data, context={'project': project}) assert serializer.is_valid() is True
def setup_models(self): self.user = UserFactory.create() assign_policies(self.user) self.prj = ProjectFactory.create(slug='test-project', access='public') party = PartyFactory.create(project=self.prj, name='Landowner') spatial_unit = SpatialUnitFactory.create(project=self.prj, type='PA') self.rel = TenureRelationshipFactory.create( project=self.prj, party=party, spatial_unit=spatial_unit)
def test_write_features(self): ensure_dirs() project = ProjectFactory.create(current_questionnaire="123abc") exporter = ShapeExporter(project) content_type = ContentType.objects.get(app_label="spatial", model="spatialunit") schema = Schema.objects.create( content_type=content_type, selectors=(project.organization.id, project.id, "123abc") ) attr_type = AttributeType.objects.get(name="text") Attribute.objects.create( schema=schema, name="key", long_name="Test field", attr_type=attr_type, index=0, required=False, omit=False ) su1 = SpatialUnitFactory.create(project=project, geometry="POINT (1 1)", attributes={"key": "value 1"}) su2 = SpatialUnitFactory.create(project=project, geometry="POINT (2 2)", attributes={"key": "value 2"}) dst_dir = os.path.join(settings.MEDIA_ROOT, "temp/file4") ds = exporter.create_datasource(dst_dir, "file4") layers = exporter.create_shp_layers(ds, "file4") csvfile = os.path.join(dst_dir, "locations.csv") exporter.write_features(layers, csvfile) assert len(layers[0]) == 2 f = layers[0].GetNextFeature() while f: geom = f.geometry() assert geom.ExportToWkt() in ["POINT (1 1)", "POINT (2 2)"] assert f.GetFieldAsString("id") in [su1.id, su2.id] f = layers[0].GetNextFeature() ds.Destroy() with open(csvfile) as csvfile: csvreader = csv.reader(csvfile) for i, row in enumerate(csvreader): if i == 0: assert row == ["id", "type", "key"] elif row[0] == su1.id: assert row == [su1.id, su1.type, "value 1"] elif row[1] == su2.id: assert row == [su2.id, su2.type, "value 2"]
def test_location_type_label_questionnaire_single_lang(self): questionnaire = q_factories.QuestionnaireFactory.create() question = q_factories.QuestionFactory.create( questionnaire=questionnaire, name='location_type', type='S1') q_factories.QuestionOptionFactory.create(question=question, name='HOUSE', label='Haus') su = SpatialUnitFactory.create(project=questionnaire.project, type='HOUSE') assert su.location_type_label == 'Haus'
def test_get_values_null_geom(self): project = ProjectFactory.create(current_questionnaire='123abc') exporter = Exporter(project) item = SpatialUnitFactory.create(project=project, geometry=None) content_type = ContentType.objects.get(app_label='spatial', model='spatialunit') model_attrs = ('id', 'geometry.wkt') schema_attrs = exporter.get_schema_attrs(content_type) values = exporter.get_values(item, model_attrs, schema_attrs) assert values == {'id': item.id, 'geometry.wkt': None}
def setup_models(self): self.user = UserFactory.create() assign_policies(self.user) self.prj = ProjectFactory.create(slug='test-project', access='public') party = PartyFactory.create(project=self.prj, name='Landowner') spatial_unit = SpatialUnitFactory.create(project=self.prj, type='PA') self.rel = TenureRelationshipFactory.create(project=self.prj, party=party, spatial_unit=spatial_unit)
def test_invalid_tenure_type(self): project = ProjectFactory.create() su = SpatialUnitFactory.create(project=project) party = PartyFactory.create(project=project) data = {'tenure_type': 'BOO', 'spatial_unit': su.id, 'party': party.id} serializer = serializers.TenureRelationshipSerializer( data=data, context={'project': project}) assert serializer.is_valid() is False assert ("'BOO' is not a valid choice for field 'tenure_type'." in serializer.errors['tenure_type'])
def test_reassign_extent(self): spatial_unit = SpatialUnitFactory.create( geometry='SRID=4326;POLYGON((' '211.36667 47.25000, ' '211.41667 47.25000, ' '211.41667 47.28333, ' '211.36667 47.28333, ' '211.36667 47.25000))' ) assert spatial_unit.geometry.boundary.coords == ( (-148.63333, 47.25), (-148.58333, 47.25), (-148.58333, 47.28333), (-148.63333, 47.28333), (-148.63333, 47.25))
def test_get_all_download(self): ensure_dirs() data = {'type': 'all'} user = UserFactory.create() project = ProjectFactory.create() geometry = 'SRID=4326;POINT (30 10)' SpatialUnitFactory.create(project=project, geometry=geometry) res = ResourceFactory.create(project=project) form = forms.DownloadForm(project, user, data=data) assert form.is_valid() is True path, mime = form.get_file() assert '{}-{}'.format(project.id, user.id) in path assert mime == 'application/zip' with ZipFile(path, 'r') as testzip: assert len(testzip.namelist()) == 4 assert res.original_file in testzip.namelist() assert 'resources.xlsx' in testzip.namelist() assert 'data.xlsx' in testzip.namelist() assert 'data-shp.zip' in testzip.namelist()
def test_get_values_null_geom(self): project = ProjectFactory.create(current_questionnaire='123abc') exporter = Exporter(project) item = SpatialUnitFactory.create( project=project, geometry=None) content_type = ContentType.objects.get(app_label='spatial', model='spatialunit') model_attrs = ('id', 'geometry.wkt') schema_attrs = exporter.get_schema_attrs(content_type) values = exporter.get_values(item, model_attrs, schema_attrs) assert values == {'id': item.id, 'geometry.wkt': None}
def test_reassign_extent(self): spatial_unit = SpatialUnitFactory.create( geometry='SRID=4326;POLYGON((' '211.36667 47.25000, ' '211.41667 47.25000, ' '211.41667 47.28333, ' '211.36667 47.28333, ' '211.36667 47.25000))' ) assert spatial_unit.geometry.boundary.coords == ( (-148.63333, 47.25), (-148.58333, 47.25), (-148.58333, 47.28333), (-148.63333, 47.28333), (-148.63333, 47.25))
def test_update_spatial_unit(self): project = ProjectFactory.create() su = SpatialUnitFactory.create(type='BU', project=project) spatial_data = {'properties': {'type': 'AP', 'project': project}} serializer = serializers.SpatialUnitSerializer( su, data=spatial_data, partial=True, context={'project': project}) serializer.is_valid(raise_exception=True) serializer.save() su_update = SpatialUnit.objects.get(id=su.id) assert su_update.type == 'AP'
def test_get_all_download(self): ensure_dirs() data = {"type": "all"} user = UserFactory.create() project = ProjectFactory.create() geometry = "SRID=4326;POINT (30 10)" SpatialUnitFactory.create(project=project, geometry=geometry) res = ResourceFactory.create(project=project) form = forms.DownloadForm(project, user, data=data) assert form.is_valid() is True path, mime = form.get_file() assert "{}-{}".format(project.id, user.id) in path assert mime == "application/zip" with ZipFile(path, "r") as testzip: assert len(testzip.namelist()) == 4 assert res.original_file in testzip.namelist() assert "resources.xlsx" in testzip.namelist() assert "data.xlsx" in testzip.namelist() assert "data-shp.zip" in testzip.namelist()
def test_make_download(self): ensure_dirs() project = ProjectFactory.create(current_questionnaire="123abc") exporter = ShapeExporter(project) content_type = ContentType.objects.get(app_label="spatial", model="spatialunit") schema = Schema.objects.create( content_type=content_type, selectors=(project.organization.id, project.id, "123abc") ) attr_type = AttributeType.objects.get(name="text") Attribute.objects.create( schema=schema, name="key", long_name="Test field", attr_type=attr_type, index=0, required=False, omit=False ) SpatialUnitFactory.create(project=project, geometry="POINT (1 1)", attributes={"key": "value 1"}) path, mime = exporter.make_download("file5") assert path == os.path.join(settings.MEDIA_ROOT, "temp/file5.zip") assert mime == "application/zip" with ZipFile(path, "r") as testzip: assert len(testzip.namelist()) == 16 assert project.slug + "-point.dbf" in testzip.namelist() assert project.slug + "-point.prj" in testzip.namelist() assert project.slug + "-point.shp" in testzip.namelist() assert project.slug + "-point.shx" in testzip.namelist() assert project.slug + "-line.dbf" in testzip.namelist() assert project.slug + "-line.prj" in testzip.namelist() assert project.slug + "-line.shp" in testzip.namelist() assert project.slug + "-line.shx" in testzip.namelist() assert project.slug + "-polygon.dbf" in testzip.namelist() assert project.slug + "-polygon.prj" in testzip.namelist() assert project.slug + "-polygon.shp" in testzip.namelist() assert project.slug + "-polygon.shx" in testzip.namelist() assert "relationships.csv" in testzip.namelist() assert "parties.csv" in testzip.namelist() assert "locations.csv" in testzip.namelist() assert "README.txt" in testzip.namelist()
def test_empty_geometries(self): geoms = ( "POINT EMPTY", # "POLYGON EMPTY", # Uncomment after Django 1.11 or libgeos 3.6.1 "LINESTRING EMPTY", "MULTIPOINT EMPTY", "MULTILINESTRING EMPTY", "MULTIPOLYGON EMPTY", "GEOMETRYCOLLECTION EMPTY", ) for geom in geoms: spatial_unit = SpatialUnitFactory.create( geometry=GEOSGeometry(geom)) assert spatial_unit.geometry.wkt == geom
def test_detach_spatial_unit_resources(self): project = ProjectFactory.create() su = SpatialUnitFactory.create(project=project) resource = ResourceFactory.create(project=project) resource.content_objects.create( content_object=su) assert ContentObject.objects.filter( object_id=su.id, resource=resource,).exists() assert resource in su.resources su.delete() assert not ContentObject.objects.filter( object_id=su.id, resource=resource).exists()
def setup_models(self): self.project = ProjectFactory.create() self.location = SpatialUnitFactory.create(project=self.project) self.resource = ResourceFactory.create(project=self.project) self.project_attachment = ContentObject.objects.create( resource_id=self.resource.id, content_object=self.project, ) self.location_attachment = ContentObject.objects.create( resource_id=self.resource.id, content_object=self.location, ) self.user = UserFactory.create() assign_permissions(self.user)
def test_adding_attributes(self): # add attribute schema content_type = ContentType.objects.get( app_label='spatial', model='spatialunit') sch = Schema.objects.create(content_type=content_type, selectors=()) attr_type = AttributeType.objects.get(name="text") Attribute.objects.create( schema=sch, name='description', long_name='Description', required=False, index=1, attr_type=attr_type ) space = SpatialUnitFactory.create( attributes={ 'description': 'The happiest place on earth' }) assert space.attributes['description'] == 'The happiest place on earth'
def setUp(self): super().setUp() self.project = ProjectFactory.create(current_questionnaire="123abc") self.content_type = ContentType.objects.get(app_label="spatial", model="spatialunit") schema = Schema.objects.create( content_type=self.content_type, selectors=(self.project.organization.id, self.project.id, "123abc") ) attr_type = AttributeType.objects.get(name="text") Attribute.objects.create( schema=schema, name="key", long_name="Test field", attr_type=attr_type, index=0, required=False, omit=False ) self.spatialunit_1 = SpatialUnitFactory.create( project=self.project, geometry="POINT (1 1)", attributes={"key": "value 1"} ) self.spatialunit_2 = SpatialUnitFactory.create( project=self.project, geometry="POINT (2 2)", attributes={"key": "value 2"} ) self.spatialunits = [self.spatialunit_1, self.spatialunit_2] self.attrs = ["id", "geometry.ewkt"] self.parties = PartyFactory.create_batch(project=self.project, size=2) TenureRelationshipFactory.create(project=self.project, party=self.parties[0], spatial_unit=self.spatialunit_1)
def test_pack_resource_data(self): project = ProjectFactory.create() exporter = ResourceExporter(project) res = ResourceFactory.create(project=project) loc = SpatialUnitFactory.create(project=project) loc2 = SpatialUnitFactory.create(project=project) par = PartyFactory.create(project=project) rel = TenureRelationshipFactory.create(project=project) ContentObject.objects.create(resource=res, content_object=loc) ContentObject.objects.create(resource=res, content_object=loc2) ContentObject.objects.create(resource=res, content_object=par) ContentObject.objects.create(resource=res, content_object=rel) packed = exporter.pack_resource_data(res) assert packed[0] == res.id assert packed[1] == res.name assert packed[2] == res.description assert packed[3] == res.original_file assert loc.id in packed[4] assert loc2.id in packed[4] assert par.id in packed[5] assert rel.id in packed[6]
def test_detach_deferred_spatial_unit_resources(self): project = ProjectFactory.create() su = SpatialUnitFactory.create(project=project) resource = ResourceFactory.create(project=project) resource.content_objects.create( content_object=su) assert ContentObject.objects.filter( object_id=su.id, resource=resource,).exists() su_deferred = SpatialUnit.objects.all().defer('attributes')[0] assert resource in su_deferred.resources su_deferred.delete() assert not ContentObject.objects.filter( object_id=su.id, resource=resource).exists() assert SpatialUnit.objects.all().count() == 0