def test_project_relationship_invalid(self): with pytest.raises(exceptions.ProjectRelationshipError): project = ProjectFactory() TenureRelationshipFactory.create( party__project=project, spatial_unit__project=project )
def test_full_list_with_unauthorized_user(self): TenureRelationshipFactory.create(project=self.prj, party=self.party1, spatial_unit=self.su2) response = self.request() assert response.status_code == 403 assert response.content['detail'] == PermissionDenied.default_detail
def test_get_with_questionnaire(self): questionnaire = q_factories.QuestionnaireFactory.create() self.project.current_questionnaire = questionnaire.id self.project.save() location_type_question = q_factories.QuestionFactory.create( questionnaire=questionnaire, name='location_type', label={'en': 'Location type', 'de': 'Parzelle Typ'}, type='S1') q_factories.QuestionOptionFactory.create( question=location_type_question, name=self.location.type, label={'en': 'House', 'de': 'Haus'}) tenure_type_question = q_factories.QuestionFactory.create( questionnaire=questionnaire, name='tenure_type', label={'en': 'Location type', 'de': 'Parzelle Typ'}, type='S1') q_factories.QuestionOptionFactory.create( question=tenure_type_question, name='LH', label={'en': 'Leasehold', 'de': 'Miete'}) q_factories.QuestionOptionFactory.create( question=tenure_type_question, name='WR', label={'en': 'Water rights', 'de': 'Wasserecht'}) lh_ten = TenureRelationshipFactory.create( tenure_type=TenureRelationshipType.objects.get(id='LH'), spatial_unit=self.location, project=self.project) wr_ten = TenureRelationshipFactory.create( tenure_type=TenureRelationshipType.objects.get(id='WR'), spatial_unit=self.location, project=self.project) relationships = self.location.tenurerelationship_set.all() for rel in relationships: if lh_ten == rel: rel.type_labels = ('data-label-de="Miete" ' 'data-label-en="Leasehold"') elif wr_ten == rel: rel.type_labels = ('data-label-de="Wasserecht" ' 'data-label-en="Water rights"') user = UserFactory.create() assign_policies(user) response = self.request(user=user) assert response.status_code == 200 assert response.content == self.render_content( type_labels=('data-label-de="Parzelle Typ" ' 'data-label-en="Location type"'), type_choice_labels=('data-label-de="Haus" data-label-en="House"'), relationships=relationships, form_lang_default='en', form_langs=[('en', 'English'), ('de', 'German')] )
def test_get_with_questionnaire(self): questionnaire = q_factories.QuestionnaireFactory.create() self.project.current_questionnaire = questionnaire.id self.project.save() location_type_question = q_factories.QuestionFactory.create( questionnaire=questionnaire, name='location_type', label={'en': 'Location type', 'de': 'Parzelle Typ'}, type='S1') q_factories.QuestionOptionFactory.create( question=location_type_question, name=self.location.type, label={'en': 'House', 'de': 'Haus'}) tenure_type_question = q_factories.QuestionFactory.create( questionnaire=questionnaire, name='tenure_type', label={'en': 'Location type', 'de': 'Parzelle Typ'}, type='S1') q_factories.QuestionOptionFactory.create( question=tenure_type_question, name='LH', label={'en': 'Leasehold', 'de': 'Miete'}) q_factories.QuestionOptionFactory.create( question=tenure_type_question, name='WR', label={'en': 'Water rights', 'de': 'Wasserecht'}) lh_ten = TenureRelationshipFactory.create( tenure_type=TenureRelationshipType.objects.get(id='LH'), spatial_unit=self.location, project=self.project) lh_ten.type_labels = ('data-label-de="Miete" ' 'data-label-en="Leasehold"') wr_ten = TenureRelationshipFactory.create( tenure_type=TenureRelationshipType.objects.get(id='WR'), spatial_unit=self.location, project=self.project) wr_ten.type_labels = ('data-label-de="Wasserecht" ' 'data-label-en="Water rights"') user = UserFactory.create() assign_policies(user) response = self.request(user=user) assert response.status_code == 200 assert response.content == self.render_content( type_labels=('data-label-de="Parzelle Typ" ' 'data-label-en="Location type"'), type_choice_labels=('data-label-de="Haus" data-label-en="House"'), relationships=[wr_ten, lh_ten], form_lang_default='en', form_langs=[('en', 'English'), ('de', 'German')] )
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_tenure_relationship_invalid_attribute(self): project = ProjectFactory.create(name='TestProject') QuestionnaireFactory.create(project=project) content_type = ContentType.objects.get(app_label='party', model='tenurerelationship') create_attrs_schema(project=project, dict=tenure_relationship_xform_group, content_type=content_type, errors=[]) assert 1 == Schema.objects.all().count() with pytest.raises(KeyError): TenureRelationshipFactory.create(project=project, attributes={ 'invalid_attribute': 'yes', })
def test_set_attributes(self): tenure_relationship = TenureRelationshipFactory.create() attributes = {'description': 'Additional attribute data'} tenure_relationship.attributes = attributes tenure_relationship.save() assert attributes['description'] == tenure_relationship.attributes[ 'description']
def test_get_absolute_url(self): tenurerel = TenureRelationshipFactory.create() assert tenurerel.get_absolute_url() == ( '/organizations/{org}/projects/{prj}/relationships/{id}/'.format( org=tenurerel.project.organization.slug, prj=tenurerel.project.slug, id=tenurerel.id))
def setup_models(self): self.prj = ProjectFactory.create() self.tenure = TenureRelationshipFactory.create(project=self.prj) self.resource = ResourceFactory.create(content_object=self.tenure, project=self.prj) self.user = UserFactory.create() assign_policies(self.user)
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 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_ui_detail_url(self): tenurerel = TenureRelationshipFactory.create() assert tenurerel.ui_detail_url == ( '/organizations/{org}/projects/{prj}/relationships/{id}/'.format( org=tenurerel.project.organization.slug, prj=tenurerel.project.slug, id=tenurerel.id))
def test_get_values(self): project = ProjectFactory.create(current_questionnaire='123abc') exporter = Exporter(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, '123abc', )) text_type = AttributeType.objects.get(name='text') attr = Attribute.objects.create(schema=schema, name='key', long_name='Test field', attr_type=text_type, index=0, required=False, omit=False) ttype = TenureRelationshipType.objects.get(id='LH') item = TenureRelationshipFactory.create(project=project, tenure_type=ttype, attributes={'key': 'text'}) model_attrs = ('id', 'party_id', 'spatial_unit_id', 'tenure_type.label') schema_attrs = [attr] values = exporter.get_values(item, model_attrs, schema_attrs) assert values == [ item.id, item.party_id, item.spatial_unit_id, 'Leasehold', 'text' ]
def test_tenure_relationship_invalid_attribute(self): project = ProjectFactory.create(name='TestProject') QuestionnaireFactory.create(project=project) content_type = ContentType.objects.get( app_label='party', model='tenurerelationship') create_attrs_schema( project=project, dict=tenure_relationship_xform_group, content_type=content_type, errors=[]) assert 1 == Schema.objects.all().count() with pytest.raises(KeyError): TenureRelationshipFactory.create( project=project, attributes={ 'invalid_attribute': 'yes', } )
def test_get_values(self): project = ProjectFactory.create(current_questionnaire="123abc") exporter = Exporter(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, "123abc") ) text_type = AttributeType.objects.get(name="text") select_m_type = AttributeType.objects.get(name="select_multiple") attr = Attribute.objects.create( schema=schema, name="key", long_name="Test field", attr_type=text_type, index=0, required=False, omit=False ) attr2 = Attribute.objects.create( schema=schema, name="key_2", long_name="Test select multiple field", attr_type=select_m_type, index=1, choices=["choice_1", "choice_2", "choice_3"], choice_labels=["Choice 1", "Choice 2", "Choice 3"], required=False, omit=False, ) ttype = TenureRelationshipType.objects.get(id="LH") item = TenureRelationshipFactory.create( project=project, tenure_type=ttype, attributes={"key": "text", "key_2": ["choice_1", "choice_2"]} ) model_attrs = ("id", "party_id", "spatial_unit_id", "tenure_type.label") schema_attrs = [attr, attr2] values = exporter.get_values(item, model_attrs, schema_attrs) assert values == [item.id, item.party_id, item.spatial_unit_id, "Leasehold", "text", "choice_1, choice_2"]
def test_search_filter(self): file = self.get_file('/resources/tests/files/image.jpg', 'rb') not_found = self.storage.save('resources/bild.jpg', file.read()) file.close() tenure = TenureRelationshipFactory.create(project=self.prj) ResourceFactory.create_from_kwargs([{ 'content_object': tenure, 'project': self.prj, 'file': self.file_name }, { 'content_object': tenure, 'project': self.prj, 'file': self.file_name }, { 'content_object': tenure, 'project': self.prj, 'file': not_found }]) response = self.request(user=self.user, url_kwargs={ 'organization': self.prj.organization.slug, 'project': self.prj.slug, 'tenure_rel_id': tenure.id }, get_data={'search': 'image'}) assert response.status_code == 200 assert len(response.content['results']) == 2
def test_reverse_ordering(self): tenure = TenureRelationshipFactory.create(project=self.prj) ResourceFactory.create_from_kwargs([ { 'content_object': tenure, 'project': self.prj, 'name': 'A' }, { 'content_object': tenure, 'project': self.prj, 'name': 'B' }, { 'content_object': tenure, 'project': self.prj, 'name': 'C' }, ]) response = self.request(user=self.user, url_kwargs={'tenure_rel_id': tenure.id}, get_data={'ordering': '-name'}) assert response.status_code == 200 assert len(response.content['results']) == 3 names = [resource['name'] for resource in response.content['results']] assert (names == sorted(names, reverse=True))
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 setup_models(self): self.prj = ProjectFactory.create() self.tenure = TenureRelationshipFactory.create( project=self.prj) self.resource = ResourceFactory.create(content_object=self.tenure, project=self.prj) self.user = UserFactory.create() assign_policies(self.user)
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_get_values(self): project = ProjectFactory.create(current_questionnaire='123abc') questionnaire = q_factories.QuestionnaireFactory.create(id='123abc') question = q_factories.QuestionFactory.create( questionnaire=questionnaire, name='tenure_type', type='S1') q_factories.QuestionOptionFactory.create(question=question, name='LH', label='Miete') exporter = Exporter(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, '123abc', )) text_type = AttributeType.objects.get(name='text') select_m_type = AttributeType.objects.get(name='select_multiple') Attribute.objects.create(schema=schema, name='key', long_name='Test field', attr_type=text_type, index=0, required=False, omit=False) Attribute.objects.create( schema=schema, name='key_2', long_name='Test select multiple field', attr_type=select_m_type, index=1, choices=['choice_1', 'choice_2', 'choice_3'], choice_labels=['Choice 1', 'Choice 2', 'Choice 3'], required=False, omit=False) item = TenureRelationshipFactory.create(project=project, tenure_type='LH', attributes={ 'key': 'text', 'key_2': ['choice_1', 'choice_2'] }) model_attrs = ('id', 'party_id', 'spatial_unit_id', 'tenure_type', 'tenure_type_label') schema_attrs = exporter.get_schema_attrs(content_type) values = exporter.get_values(item, model_attrs, schema_attrs) assert values == { 'id': item.id, 'party_id': item.party_id, 'spatial_unit_id': item.spatial_unit_id, 'tenure_type': 'LH', 'tenure_type_label': 'Miete', 'key': 'text', 'key_2': 'choice_1, choice_2', }
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_tenure_type_label_questionnaire_single_lang(self): questionnaire = q_factories.QuestionnaireFactory.create() question = q_factories.QuestionFactory.create( questionnaire=questionnaire, name='tenure_type', type='S1') q_factories.QuestionOptionFactory.create(question=question, name='FREE', label='Eigentum') tenure = TenureRelationshipFactory.create( project=questionnaire.project, tenure_type='FREE') assert tenure.tenure_type_label == 'Eigentum'
def test_set_attributes(self): tenure_relationship = TenureRelationshipFactory.create() attributes = { 'description': 'Additional attribute data' } tenure_relationship.attributes = attributes tenure_relationship.save() assert attributes[ 'description'] == tenure_relationship.attributes['description']
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 setup_models(self): self.user = UserFactory.create() assign_policies(self.user) self.prj = ProjectFactory.create(slug='test-project', access='public') self.tenure = TenureRelationshipFactory.create(project=self.prj) self.resources = ResourceFactory.create_batch( 2, project=self.prj, content_object=self.tenure) ResourceFactory.create(project=self.prj) self.file = self.get_file('/resources/tests/files/image.jpg', 'rb') self.file_name = self.storage.save('resources/image.jpg', self.file)
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.tenure = TenureRelationshipFactory.create( project=self.prj) self.resources = ResourceFactory.create_batch( 2, project=self.prj, content_object=self.tenure) ResourceFactory.create(project=self.prj) self.file = self.get_file( '/resources/tests/files/image.jpg', 'rb') self.file_name = self.storage.save('resources/image.jpg', self.file)
def setup_models(self): self.project = ProjectFactory.create(slug='test-project') self.location = SpatialUnitFactory.create( project=self.project, geometry='SRID=4326;POINT(0 0)', ) self.party1 = PartyFactory.create(project=self.project) self.party2 = PartyFactory.create(project=self.project) self.tenure_rel = TenureRelationshipFactory.create( project=self.project, spatial_unit=self.location, party=self.party1, ) self.resource = ResourceFactory.create(project=self.project)
def test_detach_tenure_relationship_resources(self): project = ProjectFactory.create() tenure = TenureRelationshipFactory.create(project=project) resource = ResourceFactory.create(project=project) resource.content_objects.create(content_object=tenure) assert ContentObject.objects.filter( object_id=tenure.id, resource=resource, ).exists() assert resource in tenure.resources tenure.delete() assert not ContentObject.objects.filter(object_id=tenure.id, resource=resource).exists()
def test_tenure_type_label_questionnaire_default_lang(self): questionnaire = q_factories.QuestionnaireFactory.create( default_language='en') question = q_factories.QuestionFactory.create( questionnaire=questionnaire, name='tenure_type', type='S1') q_factories.QuestionOptionFactory.create(question=question, name='FREE', label={ 'en': 'Freehold', 'de': 'Eigentum' }) tenure = TenureRelationshipFactory.create( project=questionnaire.project, tenure_type='FREE') assert tenure.tenure_type_label == 'Freehold'
def test_detach_tenure_relationship_resources(self): project = ProjectFactory.create() tenure = TenureRelationshipFactory.create(project=project) resource = ResourceFactory.create(project=project) resource.content_objects.create( content_object=tenure) assert ContentObject.objects.filter( object_id=tenure.id, resource=resource,).exists() assert resource in tenure.resources tenure.delete() assert not ContentObject.objects.filter( object_id=tenure.id, resource=resource).exists()
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_reverse_ordering(self): tenure = TenureRelationshipFactory.create( project=self.prj) ResourceFactory.create_from_kwargs([ {'content_object': tenure, 'project': self.prj, 'name': 'A'}, {'content_object': tenure, 'project': self.prj, 'name': 'B'}, {'content_object': tenure, 'project': self.prj, 'name': 'C'}, ]) response = self.request( user=self.user, url_kwargs={'tenure_rel_id': tenure.id}, get_data={'ordering': '-name'}) assert response.status_code == 200 assert len(response.content) == 3 names = [resource['name'] for resource in response.content] assert(names == sorted(names, reverse=True))
def test_tenure_relationship_schema(self): project = ProjectFactory.create(name='TestProject') QuestionnaireFactory.create(project=project) content_type = ContentType.objects.get( app_label='party', model='tenurerelationship') create_attrs_schema( project=project, dict=tenure_relationship_xform_group, content_type=content_type, errors=[]) tr = TenureRelationshipFactory.create( project=project, attributes={ 'notes': 'Some additional textual info'} ) 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 'notes' in tr.attributes.attributes
def test_set_attributes(self): # add attribute schema content_type = ContentType.objects.get(app_label='party', model='tenurerelationship') 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) tenure_relationship = TenureRelationshipFactory.create() attributes = {'description': 'Additional attribute data'} tenure_relationship.attributes = attributes tenure_relationship.save() assert attributes['description'] == tenure_relationship.attributes[ 'description']
def setup_models(self): self.project = ProjectFactory.create(slug='test-project') self.location = SpatialUnitFactory.create( project=self.project, geometry='SRID=4326;POINT(0 0)', ) self.party1 = PartyFactory.create(project=self.project) self.party2 = PartyFactory.create(project=self.project) self.tenure_rel = TenureRelationshipFactory.create( project=self.project, spatial_unit=self.location, party=self.party1, ) self.resource = ResourceFactory.create(project=self.project) self.query_format = ('{{"query": {{"bool": {{"should": [' '{{"multi_match": {{"query": "{q}"}}}}]}}}},' ' "from": {f}, "size": {s}}}')
def test_detach_deferred_tenure_relationship_resources(self): project = ProjectFactory.create() tenure = TenureRelationshipFactory.create(project=project) resource = ResourceFactory.create(project=project) resource.content_objects.create( content_object=tenure) assert ContentObject.objects.filter( object_id=tenure.id, resource=resource,).exists() tenure_deferred = TenureRelationship.objects.all( ).defer('attributes')[0] assert resource in tenure_deferred.resources tenure_deferred.delete() assert not ContentObject.objects.filter( object_id=tenure.id, resource=resource).exists() assert TenureRelationship.objects.all().count() == 0
def test_get_values(self): project = ProjectFactory.create(current_questionnaire='123abc') exporter = Exporter(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, '123abc', )) text_type = AttributeType.objects.get(name='text') select_m_type = AttributeType.objects.get(name='select_multiple') Attribute.objects.create( schema=schema, name='key', long_name='Test field', attr_type=text_type, index=0, required=False, omit=False ) Attribute.objects.create( schema=schema, name='key_2', long_name='Test select multiple field', attr_type=select_m_type, index=1, choices=['choice_1', 'choice_2', 'choice_3'], choice_labels=['Choice 1', 'Choice 2', 'Choice 3'], required=False, omit=False ) ttype = TenureRelationshipType.objects.get(id='LH') item = TenureRelationshipFactory.create(project=project, tenure_type=ttype, attributes={ 'key': 'text', 'key_2': ['choice_1', 'choice_2']}) model_attrs = ('id', 'party_id', 'spatial_unit_id', 'tenure_type.label') schema_attrs = exporter.get_schema_attrs(content_type) values = exporter.get_values(item, model_attrs, schema_attrs) assert values == { 'id': item.id, 'party_id': item.party_id, 'spatial_unit_id': item.spatial_unit_id, 'tenure_type.label': 'Leasehold', 'key': 'text', 'key_2': 'choice_1, choice_2', }
def setUp(self): super().setUp() PolicyFactory.load_policies() test_objs = load_test_data(get_test_data()) self.org = test_objs['organizations'][0] self.prj = test_objs['projects'][1] OrganizationRole.objects.create(organization=self.org, user=UserFactory.create( username='******', password='******'), admin=True) ResourceFactory.create_batch(2, content_object=self.prj, project=self.prj) su = SpatialUnitFactory(geometry=GEOSGeometry( '{"type": "Polygon",' '"coordinates": [[' '[-5.1031494140625000,' ' 8.1299292850467957], ' '[-5.0482177734375000, ' '7.6837733211111425], ' '[-4.6746826171875000, ' '7.8252894725496338], ' '[-4.8641967773437491, ' '8.2278005261522775], ' '[-5.1031494140625000, ' '8.1299292850467957]]]}'), project=self.prj, type='MI', attributes={}) ResourceFactory.create(content_object=su, project=self.prj) party = PartyFactory.create(project=test_objs['projects'][1]) tenure = TenureRelationshipFactory.create( project=self.prj, party=party, spatial_unit=su, tenure_type=TenureRelationshipType.objects.create( id='CR', label='Customary Rights')) ResourceFactory.create(content_object=su, project=self.prj) ResourceFactory.create(content_object=party, project=self.prj) ResourceFactory.create(content_object=tenure, project=self.prj)
def test_search_filter(self): not_found = self.storage.save('resources/bild.jpg', self.file) tenure = TenureRelationshipFactory.create( project=self.prj) ResourceFactory.create_from_kwargs([ {'content_object': tenure, 'project': self.prj, 'file': self.file_name}, {'content_object': tenure, 'project': self.prj, 'file': self.file_name}, {'content_object': tenure, 'project': self.prj, 'file': not_found} ]) response = self.request( user=self.user, url_kwargs={'organization': self.prj.organization.slug, 'project': self.prj.slug, 'tenure_rel_id': tenure.id}, get_data={'search': 'image'}) assert response.status_code == 200 assert len(response.content) == 2
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_check_for_duplicate_submission(self): geoshape = ('45.56342779158167 -122.67650283873081 0.0 0.0;' '45.56176327330353 -122.67669159919024 0.0 0.0;' '45.56151562182025 -122.67490658909082 0.0 0.0;' '45.563479432877415 -122.67494414001703 0.0 0.0;' '45.56176327330353 -122.67669159919024 0.0 0.0') data = { 'id': 'a1', 'meta': { 'instanceID': 'uuid:b3f225d3-0fac-4a0b-80c7-60e6db4cc0ad' }, 'version': str(self.questionnaire.version), 'party_repeat': [{ 'party_name': 'Party One', 'party_type': 'IN', 'party_attributes_individual': { 'fname': False, 'fname_two': 'socks', }, 'party_photo': 'sad_birthday.png', 'party_resource_invite': 'invitation.pdf', }, { 'party_name': 'Party Two', 'party_type': 'GR', 'party_attributes_group': { 'fname': True, 'fname_two': 'video games', }, 'party_photo': 'awesome_birthday.png', 'party_resource_invite': 'invitation_two.pdf', }], 'location_type': 'BU', 'location_geometry': geoshape, 'location_attributes': { 'fname': False, 'fname_two': 'Location One', }, 'location_photo': 'resource_one.png', 'location_resource_invite': 'resource_two.pdf', 'tenure_type': 'CO', 'tenure_relationship_attributes': { 'fname': False, 'fname_two': 'Tenure One' }, 'tenure_resource_photo': 'resource_three.png' } assert not mh.check_for_duplicate_submission( mh(), data, self.questionnaire) party1 = PartyFactory.create( project=self.project, name='Party One', type='IN', attributes={ 'fname': False, 'fname_two': 'socks', }) party2 = PartyFactory.create( project=self.project, name='Party Two', type='GR', attributes={ 'fname': True, 'fname_two': 'video games', }) su = SpatialUnitFactory.create( project=self.project, type='BU', geometry=mh()._format_geometry(data), attributes={ 'fname': False, 'fname_two': 'Location One' }) tenure1 = TenureRelationshipFactory.create( project=self.project, spatial_unit=su, party=party1, tenure_type=TenureRelationshipType.objects.get(id='CO'), attributes={ 'fname': False, 'fname_two': 'Tenure One' }) tenure2 = TenureRelationshipFactory.create( project=self.project, spatial_unit=su, party=party2, tenure_type=TenureRelationshipType.objects.get(id='CO'), attributes={ 'fname': False, 'fname_two': 'Tenure One' }) xform = XFormSubmission.objects.create( json_submission={}, user=self.user, questionnaire=self.questionnaire, instanceID='uuid:b3f225d3-0fac-4a0b-80c7-60e6db4cc0ad') xform.parties.add(*[party1, party2]) xform.spatial_units.add(su) xform.tenure_relationships.add(*[tenure1, tenure2]) additional_resources = mh.check_for_duplicate_submission( mh(), data, self.questionnaire) (questionnaire, parties, party_resources, locations, location_resources, tenure_relationships, tenure_resources) = additional_resources assert Party.objects.all().count() == 2 assert SpatialUnit.objects.all().count() == 1 assert TenureRelationship.objects.all().count() == 2 assert questionnaire == self.questionnaire assert party_resources[0]['id'] == party1.id assert party_resources[0]['resources'] == ['sad_birthday.png', 'invitation.pdf'] assert party_resources[1]['id'] == party2.id assert party_resources[1]['resources'] == ['awesome_birthday.png', 'invitation_two.pdf'] assert location_resources[0]['id'] == su.id assert location_resources[0]['resources'] == ['resource_one.png', 'resource_two.pdf'] assert tenure_resources[0]['id'] == tenure1.id assert tenure_resources[0]['resources'] == ['resource_three.png'] assert tenure_resources[1]['id'] == tenure2.id assert tenure_resources[1]['resources'] == ['resource_three.png']
def test_spatial_unit_tenure_relationships(self): relationship = TenureRelationshipFactory.create() queryset = relationship.spatial_unit.tenure_relationships.all() assert len(queryset) == 1 assert queryset[0] is not None
def setUp(self): super().setUp() self.project = ProjectFactory.create(current_questionnaire='123abc') self.spatial_content_type = ContentType.objects.get( app_label='spatial', model='spatialunit') sp_schema = Schema.objects.create( content_type=self.spatial_content_type, selectors=( self.project.organization.id, self.project.id, '123abc', )) attr_type_text = AttributeType.objects.get(name='text') attr_type_int = AttributeType.objects.get(name='integer') attr_type_dec = AttributeType.objects.get(name='decimal') Attribute.objects.create(schema=sp_schema, name='key', long_name='Test field', attr_type=attr_type_text, index=0, required=False, omit=False) Attribute.objects.create(schema=sp_schema, name='integer', long_name='Test integer field', attr_type=attr_type_int, index=1, required=False, omit=False) Attribute.objects.create(schema=sp_schema, name='decimal', long_name='Test decimal field', attr_type=attr_type_dec, index=2, required=False, omit=False) self.spatialunit_1 = SpatialUnitFactory.create(project=self.project, geometry='POINT (1 1)', attributes={ 'key': 'value 1', 'integer': '1', 'decimal': '1.0' }) self.spatialunit_2 = SpatialUnitFactory.create(project=self.project, geometry='POINT (2 2)', attributes={ 'key': 'value 2', 'integer': '2', 'decimal': '2.0' }) self.spatialunits = [self.spatialunit_1, self.spatialunit_2] self.spatial_attrs = ['id', 'geometry.ewkt'] self.party_content_type = ContentType.objects.get(app_label='party', model='party') pt_schema = Schema.objects.create(content_type=self.party_content_type, selectors=( self.project.organization.id, self.project.id, '123abc', )) pt_schema_in = Schema.objects.create( content_type=self.party_content_type, selectors=(self.project.organization.id, self.project.id, '123abc', 'IN')) pt_schema_gr = Schema.objects.create( content_type=self.party_content_type, selectors=(self.project.organization.id, self.project.id, '123abc', 'GR')) attr_type = AttributeType.objects.get(name='text') Attribute.objects.create(schema=pt_schema, name='default_attr', long_name='Test field', attr_type=attr_type, index=0, required=False, omit=False) Attribute.objects.create(schema=pt_schema_in, name='party_in', long_name='Test IN field', attr_type=attr_type, index=0, required=False, omit=False) Attribute.objects.create(schema=pt_schema_gr, name='party_gr', long_name='Test GR field', attr_type=attr_type, index=0, required=False, omit=False) party_in = PartyFactory.create(project=self.project, type='IN', attributes={ 'default_attr': 'Test Schema', 'party_in': 'Test IN attribute' }) party_gr = PartyFactory.create(project=self.project, type='GR', attributes={ 'default_attr': 'Another Test Schema', 'party_gr': 'Test GR attribute' }) self.parties = [party_in, party_gr] TenureRelationshipFactory.create(project=self.project, party=self.parties[0], spatial_unit=self.spatialunit_1)
def test_tenure_relationship_creation(self): tenure_relationship = TenureRelationshipFactory.create() assert tenure_relationship.tenure_type is not None
def test_party_tenure_relationships(self): relationship = TenureRelationshipFactory.create() queryset = relationship.party.tenure_relationships.all() assert len(queryset) == 1 assert queryset[0] is not None
def test_tenure_relationship_type_not_set(self): with pytest.raises(ValueError): TenureRelationshipFactory.create(tenure_type=None)
def test_project_reverse_tenure_relationships(self): relationship = TenureRelationshipFactory.create() assert len(relationship.project.tenure_relationships.all()) == 1
def test_left_and_right_project_ids(self): with pytest.raises(exceptions.ProjectRelationshipError): project = ProjectFactory() TenureRelationshipFactory.create( party__project=project )
def test_name(self): tenurerel = TenureRelationshipFactory.create() assert tenurerel.name == "<{party}> {type} <{su}>".format( party=tenurerel.party.name, type=tenurerel.tenure_type.label, su=tenurerel.spatial_unit.get_type_display())
def test_ui_class_name(self): tenurerel = TenureRelationshipFactory.create() assert tenurerel.ui_class_name == "Relationship"