def setup_models(self): self.user = UserFactory.create() assign_policies(self.user) self.org = OrganizationFactory.create(slug="namati") self.prj = ProjectFactory.create(slug="test-project", organization=self.org, access="public") self.party1 = PartyFactory.create(project=self.prj, name="Landowner") self.party2 = PartyFactory.create(project=self.prj, name="Leaser")
def setup_models(self): self.user = UserFactory.create() assign_policies(self.user) self.org = OrganizationFactory.create(slug='namati') self.prj = ProjectFactory.create( slug='test-project', organization=self.org, access='public') self.party1 = PartyFactory.create(project=self.prj, name='Landowner') self.party2 = PartyFactory.create(project=self.prj, name='Leaser')
def setup_models(self): self.user = UserFactory.create() assign_policies(self.user) self.org = OrganizationFactory.create(slug='namati') self.prj = ProjectFactory.create(slug='test-project', organization=self.org, access='public') self.party1 = PartyFactory.create(project=self.prj, name='Landowner') self.party2 = PartyFactory.create(project=self.prj, name='Leaser')
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_repr(self): project = ProjectFactory.build(slug='prj') party1 = PartyFactory.build(id='abc123', project=project) party2 = PartyFactory.build(id='def456', project=project) relationship = PartyRelationshipFactory.build(id='abc123', project=project, party1=party1, party2=party2, type='C') assert repr(relationship) == ('<PartyRelationship id=abc123' ' party1=abc123 party2=def456' ' project=prj type=C>')
def test_repr(self): project = ProjectFactory.build(slug='prj') party1 = PartyFactory.build(id='abc123', project=project) party2 = PartyFactory.build(id='def456', project=project) relationship = PartyRelationshipFactory.build( id='abc123', project=project, party1=party1, party2=party2, type='C') assert repr(relationship) == ('<PartyRelationship id=abc123' ' party1=abc123 party2=def456' ' project=prj type=C>')
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_render(self): project = ProjectFactory.create() party_1 = PartyFactory.create(project=project) party_2 = PartyFactory.create(project=project) widget = SelectPartyWidget(project=project) rendered = widget.render(name='name', value='value') assert ('<select id="party-select" name="name">' in rendered) assert ('<option value="' + party_1.id + '" data-type="' '' + party_1.get_type_display() + '">' + party_1.name + '' '</option>' in rendered) assert ('<option value="' + party_2.id + '" data-type="' '' + party_2.get_type_display() + '">' + party_2.name + '' '</option>' in rendered)
def test_render(self): project = ProjectFactory.create() party_1 = PartyFactory.create(project=project) party_2 = PartyFactory.create(project=project) widget = SelectPartyWidget(project=project) rendered = widget.render(name='name', value='value') assert ('<select id="party-select" name="name" class="form-control" ' ' data-parsley-required="true">' in rendered) assert ('<option value="' + party_1.id + '" data-type="' '' + party_1.get_type_display() + '">' + party_1.name + '' '</option>' in rendered) assert ('<option value="' + party_2.id + '" data-type="' '' + party_2.get_type_display() + '">' + party_2.name + '' '</option>' in rendered)
def test_add_attribute_fields(self): party = PartyFactory.create() form = MockAttributeModelForm(self.project, instance=party) assert len(form.fields) == 6 attr = form.fields.get('party::gr::homeowner') assert attr is not None assert attr.label == 'Homeowner'
def test_post_with_authorized_invalid_existing_party_data(self): user = UserFactory.create() assign_policies(user) party = PartyFactory.create(project=self.project) response = self.request(method='POST', user=user, post_data={'new_entity': ''}) data = self.post_data.copy() data['new_entity'] = '' form = forms.TenureRelationshipForm( project=self.project, spatial_unit=self.spatial_unit, data=data, schema_selectors=( {'name': 'organization', 'value': self.project.organization, 'selector': self.project.organization.id}, {'name': 'project', 'value': self.project, 'selector': self.project.id}, {'name': 'questionnaire', 'value': self.project.current_questionnaire, 'selector': self.project.current_questionnaire} ) ) assert response.status_code == 200 expected = self.render_content(form=form) assert response.content == expected assert TenureRelationship.objects.count() == 0 assert Party.objects.count() == 1 assert Party.objects.first().name == party.name
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_get_absolute_url(self): party = PartyFactory.create() assert party.get_absolute_url() == ( '/organizations/{org}/projects/{prj}/records/parties/{id}/'.format( org=party.project.organization.slug, prj=party.project.slug, id=party.id))
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_ui_detail_url(self): party = PartyFactory.create() assert party.ui_detail_url == ( '/organizations/{org}/projects/{prj}/records/parties/{id}/'.format( org=party.project.organization.slug, prj=party.project.slug, id=party.id))
def test_party_attribute_schema(self): project = ProjectFactory.create(name='TestProject') QuestionnaireFactory.create(project=project) content_type = ContentType.objects.get( app_label='party', model='party') create_attrs_schema( project=project, dict=individual_party_xform_group, content_type=content_type, errors=[]) party = PartyFactory.create( name='TestParty', project=project, type='IN', attributes={ 'homeowner': 'yes', 'dob': '1980-01-01' } ) 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, party.type] assert 'homeowner' in party.attributes.attributes assert 'dob' in party.attributes.attributes assert 'gender' in party.attributes.attributes assert 3 == Attribute.objects.filter(schema=schema).count() assert party.attributes['homeowner'] == 'yes' assert party.attributes['dob'] == '1980-01-01'
def test_post_with_authorized_invalid_existing_party_data(self): user = UserFactory.create() assign_policies(user) party = PartyFactory.create(project=self.project) response = self.request(method='POST', user=user, post_data={'new_entity': ''}) data = self.post_data.copy() data['new_entity'] = '' form = forms.TenureRelationshipForm( project=self.project, spatial_unit=self.spatial_unit, data=data, schema_selectors=({ 'name': 'organization', 'value': self.project.organization, 'selector': self.project.organization.id }, { 'name': 'project', 'value': self.project, 'selector': self.project.id }, { 'name': 'questionnaire', 'value': self.project.current_questionnaire, 'selector': self.project.current_questionnaire })) assert response.status_code == 200 expected = self.render_content(form=form) assert response.content == expected assert TenureRelationship.objects.count() == 0 assert Party.objects.count() == 1 assert Party.objects.first().name == party.name
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_format_create_resource(self): party = PartyFactory.create(project=self.project) file_name = 'test_image_two.png' with open(path + '/xforms/tests/files/test_image_one.png', 'rb') as src: file = src.read() file_data = InMemoryUploadedFile( file=io.BytesIO(file), field_name='test_image_two', name=file_name, content_type='image/png', size=len(file), charset='utf-8', ) files = {file_name: file_data} data = { 'parties': [{ 'id': party.id, 'resources': [file_name] }], 'locations': [{ 'id': '1234', 'resources': ['not_created.png'] }] } mh()._format_create_resource(data, self.user, self.project, files, file_name, 'parties', Party) assert Resource.objects.all().count() == 1 resource = Resource.objects.get(name='test_image_two.png') assert resource in party.resources.all()
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_format_create_resource(self): party = PartyFactory.create(project=self.project) file_name = 'test_image_two.png' file = open( path + '/xforms/tests/files/test_image_two.png', 'rb' ).read() file_data = InMemoryUploadedFile( file=io.BytesIO(file), field_name='test_image_two', name=file_name, content_type='image/png', size=len(file), charset='utf-8', ) files = {file_name: file_data} data = { 'parties': [{'id': party.id, 'resources': [file_name]}], 'locations': [{'id': '1234', 'resources': ['not_created.png']}] } mh()._format_create_resource(data, self.user, self.project, files, file_name, 'parties', Party) assert Resource.objects.all().count() == 1 resource = Resource.objects.get(name='test_image_two.png') assert resource in party.resources.all()
def test_party_attribute_schema(self): project = ProjectFactory.create(name='TestProject') QuestionnaireFactory.create(project=project) content_type = ContentType.objects.get(app_label='party', model='party') create_attrs_schema(project=project, question_group_dict=individual_party_xform_group, content_type=content_type, errors=[], attr_type_ids=get_attr_type_ids()) party = PartyFactory.create(name='TestParty', project=project, type='IN', attributes={ 'homeowner': 'yes', 'dob': '1980-01-01' }) 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, party.type ] assert 'homeowner' in party.attributes.attributes assert 'dob' in party.attributes.attributes assert 'gender' in party.attributes.attributes assert 3 == Attribute.objects.filter(schema=schema).count() assert party.attributes['homeowner'] == 'yes' assert party.attributes['dob'] == '1980-01-01'
def test_update_invalid_record_with_different_project(self): other_party = PartyFactory.create(name="Other") response = self.request(user=self.user, method="PATCH", post_data={"party2": other_party.id}) assert response.status_code == 400 err_msg = "'party1' project ({}) should be equal to 'party2' project ({})" assert response.content["non_field_errors"][0] == ( err_msg.format(self.party1.project.slug, other_party.project.slug) )
def test_party_invalid_attribute(self): project = ProjectFactory.create(name='TestProject') QuestionnaireFactory.create(project=project) content_type = ContentType.objects.get( app_label='party', model='party') create_attrs_schema( project=project, dict=individual_party_xform_group, content_type=content_type, errors=[]) assert 1 == Schema.objects.all().count() with pytest.raises(KeyError): PartyFactory.create( name='TestParty', project=project, attributes={ 'invalid_attribute': 'yes', 'dob': '1980-01-01' } )
def test_repr(self): project = ProjectFactory.build(slug='prj') party = PartyFactory.build(id='abc123', name='TeaParty', project=project, type='IN') assert repr(party) == ('<Party id=abc123 name=TeaParty project=prj' ' type=IN>')
def test_party_invalid_attribute(self): project = ProjectFactory.create(name='TestProject') QuestionnaireFactory.create(project=project) content_type = ContentType.objects.get(app_label='party', model='party') create_attrs_schema(project=project, dict=individual_party_xform_group, content_type=content_type, errors=[]) assert 1 == Schema.objects.all().count() with pytest.raises(KeyError): PartyFactory.create(name='TestParty', project=project, attributes={ 'invalid_attribute': 'yes', 'dob': '1980-01-01' })
def test_clean_with_existing_party(self): project = ProjectFactory.create() party = PartyFactory.create(project=project) questionnaire = QuestionnaireFactory.create(project=project) spatial_unit = SpatialUnitFactory.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, questionnaire.id, )) attr_type = AttributeType.objects.get(name='text') Attribute.objects.create(schema=schema, name='r_name', long_name='Relationship field', attr_type=attr_type, index=0, required=False, omit=False) content_type = ContentType.objects.get(app_label='party', model='party') schema_in = Schema.objects.create(content_type=content_type, selectors=(project.organization.id, project.id, questionnaire.id, 'IN')) Attribute.objects.create(schema=schema_in, name='p_in_name', long_name='Party IN field', attr_type=attr_type, index=0, required=True, omit=False) schema_gr = Schema.objects.create(content_type=content_type, selectors=(project.organization.id, project.id, questionnaire.id, 'GR')) Attribute.objects.create(schema=schema_gr, name='p_gr_name', long_name='Party GR field', attr_type=attr_type, index=0, required=True, omit=False) form = forms.TenureRelationshipForm(project=project, spatial_unit=spatial_unit, data={ 'new_entity': '', 'id': party.id, 'tenure_type': 'CU' }) assert form.is_valid() form.save()
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 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_choice_attribute(self): project = ProjectFactory.create(name='TestProject') QuestionnaireFactory.create(project=project) content_type = ContentType.objects.get(app_label='party', model='party') create_attrs_schema(project=project, question_group_dict=individual_party_xform_group, content_type=content_type, errors=[], attr_type_ids=get_attr_type_ids()) assert 1 == Schema.objects.all().count() with pytest.raises(ValidationError): PartyFactory.create(name='TestParty', project=project, attributes={ 'homeowner': 'blah!', 'dob': '1980-01-01' })
def test_create_invalid_record_with_different_project(self): other_party = PartyFactory.create(name="Other") invalid_data = {"party1": self.party1.id, "party2": other_party.id, "type": "C"} response = self.request(user=self.user, method="POST", post_data=invalid_data) assert response.status_code == 400 assert PartyRelationship.objects.count() == 0 err_msg = "'party1' project ({}) should be equal to 'party2' " "project ({})" assert response.content["non_field_errors"][0] == (err_msg.format(self.prj.slug, other_party.project.slug))
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 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_update_invalid_record_with_different_project(self): other_party = PartyFactory.create(name="Other") response = self.request(user=self.user, method='PATCH', post_data={'party2': other_party.id}) assert response.status_code == 400 err_msg = ( "'party1' project ({}) should be equal to 'party2' project ({})") assert response.content['non_field_errors'][0] == (err_msg.format( self.party1.project.slug, other_party.project.slug))
def test_write_items(self): project = ProjectFactory.create(current_questionnaire='123abc') content_type = ContentType.objects.get(app_label='party', model='party') schema = Schema.objects.create(content_type=content_type, selectors=( project.organization.id, project.id, '123abc', )) for idx, type in enumerate(['text', 'boolean', 'dateTime', 'integer']): attr_type = AttributeType.objects.get(name=type) Attribute.objects.create(schema=schema, name=type, long_name=type, attr_type=attr_type, index=idx, required=False, omit=False) party = PartyFactory.create(project=project, name='Donald Duck', type='IN', attributes={ 'text': 'text', 'boolean': True, 'dateTime': '2011-08-12 11:13', 'integer': 1, }) exporter = ShapeExporter(project) dst_dir = os.path.join(settings.MEDIA_ROOT, 'temp/party') if not os.path.exists(dst_dir): os.makedirs(dst_dir) filename = os.path.join(dst_dir, 'parties.csv') exporter.write_items(filename, [party], content_type, ('id', 'name', 'type')) with open(filename) as csvfile: csvreader = csv.reader(csvfile) for i, row in enumerate(csvreader): assert len(row) == 7 if i == 0: assert row == [ 'id', 'name', 'type', 'text', 'boolean', 'dateTime', 'integer' ] else: assert row == [ party.id, party.name, party.type, 'text', 'True', '2011-08-12 11:13', '1' ]
def test_required_attribute(self): project = ProjectFactory.create(name='TestProject') QuestionnaireFactory.create(project=project) content_type = ContentType.objects.get(app_label='party', model='party') create_attrs_schema(project=project, dict=individual_party_xform_group, content_type=content_type, errors=[]) # with pytest.raises(ValidationError): # PartyFactory.create( # project=project, # attributes={} # ) # should raise validation error as dob value is required? PartyFactory.create(project=project, attributes={}) schema = Schema.objects.get(content_type=content_type) attr = Attribute.objects.get(schema=schema, name='dob') assert attr.required
def test_repr(self): project = ProjectFactory.build(slug='prj') party = PartyFactory.build(id='abc123', project=project) su = SpatialUnitFactory.build(id='def456', project=project) relationship = TenureRelationshipFactory.build(id='abc123', project=project, party=party, spatial_unit=su, tenure_type='CR') assert repr(relationship) == ('<TenureRelationship id=abc123' ' party=abc123 spatial_unit=def456' ' project=prj tenure_type=CR>')
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_edit_party_with_attributes(self): data = { 'name': 'Cadasta', 'type': 'IN', 'party::in::fname': 'updated value', 'party::in::age': 37 } project = ProjectFactory.create() content_type = ContentType.objects.get(app_label='party', model='party') schema = Schema.objects.create(content_type=content_type, selectors=( project.organization.id, project.id, )) Attribute.objects.create( schema=schema, name='fname', long_name='Test field', attr_type=AttributeType.objects.get(name='text'), index=0) Attribute.objects.create( schema=schema, name='homeowner', long_name='Homeowner', attr_type=AttributeType.objects.get(name='boolean'), index=1) Attribute.objects.create( schema=schema, name='age', long_name='Homeowner Age', attr_type=AttributeType.objects.get(name='integer'), index=2, required=True, default=0) party = PartyFactory.create(project=project, attributes={ 'homeowner': True, 'fname': 'test', 'age': 35 }) form = forms.PartyForm(project=project, instance=party, data=data) form.is_valid() form.save() assert Party.objects.filter(project=project).count() == 1 party = Party.objects.filter(project=project).first() assert party.attributes.get('fname') == 'updated value' assert party.attributes.get('age') == 37
def test_repr(self): project = ProjectFactory.build(slug='prj') party = PartyFactory.build(id='abc123', project=project) su = SpatialUnitFactory.build(id='def456', project=project) relationship = TenureRelationshipFactory.build( id='abc123', project=project, party=party, spatial_unit=su, tenure_type=TenureRelationshipType(id='CR')) assert repr(relationship) == ('<TenureRelationship id=abc123' ' party=abc123 spatial_unit=def456' ' project=prj tenure_type=CR>')
def test_update_invalid_record_with_different_project(self): other_party = PartyFactory.create() response = self.request(user=self.user, method='PATCH', post_data={'party': other_party.id}) assert response.status_code == 400 err_msg = ("'party' project ({}) should be equal to " "'spatial_unit' project ({})") assert response.content['non_field_errors'][0] == (err_msg.format( other_party.project.slug, self.spatial_unit.project.slug)) self.rel.refresh_from_db() assert self.rel.party == self.party assert self.rel.spatial_unit == self.spatial_unit
def test_required_attribute(self): project = ProjectFactory.create(name='TestProject') QuestionnaireFactory.create(project=project) content_type = ContentType.objects.get( app_label='party', model='party') create_attrs_schema( project=project, dict=individual_party_xform_group, content_type=content_type, errors=[]) # with pytest.raises(ValidationError): # PartyFactory.create( # project=project, # attributes={} # ) # should raise validation error as dob value is required? PartyFactory.create( project=project, attributes={} ) schema = Schema.objects.get(content_type=content_type) attr = Attribute.objects.get(schema=schema, name='dob') assert attr.required
def test_detach_party_resources(self): project = ProjectFactory.create() party = PartyFactory.create(project=project) resource = ResourceFactory.create(project=project) resource.content_objects.create(content_object=party) assert ContentObject.objects.filter(object_id=party.id, resource=resource).exists() assert resource in party.resources party.delete() assert not ContentObject.objects.filter(object_id=party.id, resource=resource).exists()
def test_clean_with_existing_party(self): project = ProjectFactory.create() party = PartyFactory.create(project=project) questionnaire = QuestionnaireFactory.create(project=project) spatial_unit = SpatialUnitFactory.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, questionnaire.id, )) attr_type = AttributeType.objects.get(name='text') Attribute.objects.create( schema=schema, name='r_name', long_name='Relationship field', attr_type=attr_type, index=0, required=False, omit=False ) content_type = ContentType.objects.get( app_label='party', model='party') schema_in = Schema.objects.create( content_type=content_type, selectors=( project.organization.id, project.id, questionnaire.id, 'IN')) Attribute.objects.create( schema=schema_in, name='p_in_name', long_name='Party IN field', attr_type=attr_type, index=0, required=True, omit=False ) schema_gr = Schema.objects.create( content_type=content_type, selectors=( project.organization.id, project.id, questionnaire.id, 'GR')) Attribute.objects.create( schema=schema_gr, name='p_gr_name', long_name='Party GR field', attr_type=attr_type, index=0, required=True, omit=False ) form = forms.TenureRelationshipForm(project=project, spatial_unit=spatial_unit, data={'new_entity': '', 'id': party.id, 'tenure_type': 'CU'}) assert form.is_valid() form.save()
def test_detach_party_resources(self): project = ProjectFactory.create() party = PartyFactory.create(project=project) resource = ResourceFactory.create(project=project) resource.content_objects.create( content_object=party) assert ContentObject.objects.filter( object_id=party.id, resource=resource).exists() assert resource in party.resources party.delete() assert not ContentObject.objects.filter( object_id=party.id, resource=resource).exists()
def test_update_invalid_record_with_different_project(self): other_party = PartyFactory.create() response = self.request(user=self.user, method='PATCH', post_data={'party': other_party.id}) assert response.status_code == 400 err_msg = ( "'party' project ({}) should be equal to " "'spatial_unit' project ({})") assert response.content['non_field_errors'][0] == ( err_msg.format(other_party.project.slug, self.spatial_unit.project.slug)) self.rel.refresh_from_db() assert self.rel.party == self.party assert self.rel.spatial_unit == self.spatial_unit
def test_adding_attributes(self): # add attribute schema content_type = ContentType.objects.get(app_label='party', model='party') 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) party = PartyFactory.create( attributes={'description': 'Mad Hatters Tea Party'}) assert party.attributes['description'] == 'Mad Hatters Tea Party'
def test_post_existing_party_with_authorized(self): user = UserFactory.create() assign_policies(user) party = PartyFactory.create(project=self.project) response = self.request(method='POST', user=user, post_data={'new_entity': '', 'id': party.id}) assert response.status_code == 302 assert (response.location == self.expected_success_url + '#relationships') assert TenureRelationship.objects.count() == 1 rel = TenureRelationship.objects.first() assert rel.attributes.get('r_name') == 'Rel Name' assert Party.objects.count() == 1 assert Party.objects.first().name == party.name
def test_get_resource_names(self): data = { 'party_type': 'Party Type', 'party_photo': 'Party Photo', 'party_resource_thing': 'Party Resource Thing', 'tenure_resource_thing': 'Tenure Resource Thing', } model = PartyFactory.create() resources = mh._get_resource_names(self, data, model, 'party') assert resources['id'] == model.id assert 'Party Photo' in resources['resources'] assert 'Party Resource Thing' in resources['resources'] assert 'Tenure Resource Thing' not in resources['resources'] assert 'Party Type' not in resources['resources']
def test_adding_attributes(self): # add attribute schema content_type = ContentType.objects.get( app_label='party', model='party') 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 ) party = PartyFactory.create( attributes={ 'description': 'Mad Hatters Tea Party' }) assert party.attributes['description'] == 'Mad Hatters Tea Party'
def test_default_party_attribute_schema(self): schema = Schema.objects.get( content_type=self.content_type, selectors=(self.project.organization.pk, self.project.pk, self.project.current_questionnaire) ) assert schema is not None assert 1 == schema.attributes.count() party = PartyFactory.create( name='TestParty', project=self.project, attributes={ 'notes': 'Some textual stuff' } ) assert 'notes' in party.attributes.attributes assert 'Some textual stuff' == party.attributes['notes']
def test_create_invalid_record_with_different_project(self): other_party = PartyFactory.create(name="Other") invalid_data = { 'party': other_party.id, 'spatial_unit': self.su2.id, 'type': 'C' } response = self.request(user=self.user, method='POST', post_data=invalid_data) assert response.status_code == 400 assert TenureRelationship.objects.count() == 0 err_msg = ("'party' project ({}) should be equal to " "'spatial_unit' project ({})") assert response.content['non_field_errors'][0] == ( err_msg.format(other_party.project.slug, self.prj.slug))
def test_save_exisiting_party(self): project = ProjectFactory.create() party = PartyFactory.create(project=project) spatial_unit = SpatialUnitFactory.create(project=project) form = forms.TenureRelationshipForm(project=project, spatial_unit=spatial_unit, data={'new_entity': '', 'id': party.id, 'tenure_type': 'CU'}) form.is_valid() form.save() assert TenureRelationship.objects.count() == 1 rel = TenureRelationship.objects.first() assert rel.party == party assert rel.spatial_unit == spatial_unit assert rel.tenure_type_id == 'CU'
def test_detach_deferred_party_resources(self): project = ProjectFactory.create() party = PartyFactory.create(project=project) resource = ResourceFactory.create(project=project) resource.content_objects.create( content_object=party) assert ContentObject.objects.filter( object_id=party.id, resource=resource).exists() party_deferred = Party.objects.all().defer('attributes')[0] assert resource in party_deferred.resources party_deferred.delete() assert not ContentObject.objects.filter( object_id=party.id, resource=resource).exists() assert Party.objects.all().count() == 0