def test_get_questionnaire(self): questionnaire = mh._get_questionnaire( self, 'a1', '0') assert questionnaire == self.questionnaire with pytest.raises(ValidationError): mh._get_questionnaire( self, 'bad_info', '0')
def test_get_questionnaire(self): questionnaire = mh._get_questionnaire( self, 'a1', '0') assert questionnaire == self.questionnaire with pytest.raises(ValidationError): mh._get_questionnaire( self, 'bad_info', '0')
def test_check_perm(self): with pytest.raises(PermissionDenied): mh._check_perm(mh, self.user, self.project) org_role = OrganizationRole.objects.get( user=self.user, organization=self.project.organization) org_role.admin = True org_role.save() try: mh._check_perm(mh, self.user, self.project) except PermissionDenied: self.fail("PermissionDenied raised unexpectedly")
def test_check_perm(self): with pytest.raises(PermissionDenied): mh._check_perm(mh, self.user, self.project) org_role = OrganizationRole.objects.get( user=self.user, organization=self.project.organization) org_role.admin = True org_role.save() try: mh._check_perm(mh, self.user, self.project) except PermissionDenied: self.fail("PermissionDenied raised unexpectedly")
def create(self, request, *args, **kwargs): if request.method.upper() == 'HEAD': return Response( headers=self.get_openrosa_headers(request), status=status.HTTP_204_NO_CONTENT, ) try: instance = ModelHelper().upload_submission_data(request) except InvalidXMLSubmission as e: logger.debug(str(e)) return self._sendErrorResponse(request, e) serializer = XFormSubmissionSerializer(instance) json = JSONRenderer().render(serializer.data) stream = BytesIO(json) data = JSONParser().parse(stream) serializer = XFormSubmissionSerializer(data=data) # Every possible error that would make the serializer not valid # has already been checked for, so no failsafe is necessary. if serializer.is_valid(): data = serializer.save() return Response(headers=self.get_openrosa_headers(request), status=status.HTTP_201_CREATED, content_type='application/xml')
def test_create_models(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_name': 'Party One', 'party_type': 'IN', 'party_attributes_individual': { 'fname': False, 'fname_two': 'socks', }, 'party_photo': 'sad_birthday.png', 'party_resource_invite': 'invitation.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' } (questionnaire, parties, party_resources, locations, location_resources, tenure_relationships, tenure_resources) = mh.create_models(mh(), data) assert questionnaire == self.questionnaire party = Party.objects.get(name='Party One') assert parties == [party] assert party_resources[0]['id'] == party.id assert 'sad_birthday.png' in party_resources[0]['resources'] assert 'invitation.pdf' in party_resources[0]['resources'] location = SpatialUnit.objects.get(type='BU') assert locations == [location] assert location_resources[0]['id'] == location.id assert 'resource_two.pdf' in location_resources[0]['resources'] tenure = TenureRelationship.objects.get(spatial_unit=location) assert tenure_relationships == [tenure] assert tenure.party == party assert tenure_resources[0]['id'] == tenure.id assert 'resource_three.png' in tenure_resources[0]['resources']
def test_format_repeat(self): data = { 'party_type': 'Not repeating', 'party_name': 'Still not repeating' } group = mh._format_repeat(self, data, ['party']) assert type(group) == list assert group[0] == data data = { 'party_type': 'Repeating', 'party_name': 'Totally repeating', 'party_repeat': [{ 'repeat_type': 'Just One' }] } group = mh._format_repeat(self, data, ['party']) assert type(group) == list assert len(group) == 1 assert group[0]['repeat_type'] == 'Just One' assert 'party_repeat' not in group assert 'party_type' not in group assert 'party_name' not in group data = { 'party_type': 'Repeating', 'party_name': 'Totally repeating', 'party_repeat': [{ 'repeat_type': 'First!' }, { 'repeat_type': 'Second!' }] } group = mh._format_repeat(self, data, ['party']) assert type(group) == list assert len(group) == 2 assert group[0]['repeat_type'] == 'First!' assert group[1]['repeat_type'] == 'Second!' assert 'party_repeat' not in group assert 'party_type' not in group assert 'party_name' not in group
def test_format_repeat(self): data = { 'party_type': 'Not repeating', 'party_name': 'Still not repeating' } group = mh._format_repeat(self, data, ['party']) assert type(group) == list assert group[0] == data data = { 'party_type': 'Repeating', 'party_name': 'Totally repeating', 'party_repeat': [{ 'repeat_type': 'Just One' }] } group = mh._format_repeat(self, data, ['party']) assert type(group) == list assert len(group) == 1 assert group[0]['repeat_type'] == 'Just One' assert 'party_repeat' not in group assert 'party_type' not in group assert 'party_name' not in group data = { 'party_type': 'Repeating', 'party_name': 'Totally repeating', 'party_repeat': [{ 'repeat_type': 'First!' }, { 'repeat_type': 'Second!' }] } group = mh._format_repeat(self, data, ['party']) assert type(group) == list assert len(group) == 2 assert group[0]['repeat_type'] == 'First!' assert group[1]['repeat_type'] == 'Second!' assert 'party_repeat' not in group assert 'party_type' not in group assert 'party_name' not in group
def test_sanitize_submission_with_negative_longitude(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_name': 'Party One', 'party_type': 'IN', 'party_attributes_individual': { 'fname': False, 'fname_two': 'socks', }, 'party_photo': 'sad_birthday.png', 'party_resource_invite': 'invitation.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' } try: mh.sanitize_submission(mh(), data, self.sanitizeable_questions) except InvalidXMLSubmission: assert False, "InvalidXMLSubmission raised unexpectedly" else: assert True
def test_get_resource_files(self): data = { 'ardvark': 'Ardvark!', 'party_resource_thing': 'Party Resource Thing!', 'location_resource_thing': 'Location Resource!', 'party_photo': 'Party Photo!' } resources = mh._get_resource_files(self, data, 'party') assert type(resources) == list assert 'Party Resource Thing!' in resources assert 'Party Photo!' in resources assert 'Ardvark!' not in resources assert 'Location Resource!' not in resources resources = mh._get_resource_files(self, data, 'location') assert 'Location Resource!' in resources assert 'Ardvark!' not in resources assert 'Party Resource Thing!' not in resources assert 'Party Photo!' not in resources
def test_get_resource_files(self): data = { 'ardvark': 'Ardvark!', 'party_resource_thing': 'Party Resource Thing!', 'location_resource_thing': 'Location Resource!', 'party_photo': 'Party Photo!' } resources = mh._get_resource_files(self, data, 'party') assert type(resources) == list assert 'Party Resource Thing!' in resources assert 'Party Photo!' in resources assert 'Ardvark!' not in resources assert 'Location Resource!' not in resources resources = mh._get_resource_files(self, data, 'location') assert 'Location Resource!' in resources assert 'Ardvark!' not in resources assert 'Party Resource Thing!' not in resources assert 'Party Photo!' not in resources
def test_sanitize_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_name': 'Party ⚽ One', 'party_type': 'IN', 'party_attributes_individual': { 'fname': False, 'fname_two': 'socks', }, 'party_photo': 'sad_birthday.png', 'party_resource_invite': 'invitation.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' } with pytest.raises(InvalidXMLSubmission) as e: mh.sanitize_submission(mh(), data) assert str(e.value) == SANITIZE_ERROR
def test_create_models(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', 'version': str(self.questionnaire.version), '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', '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' } (questionnaire, party_resources, location_resources, tenure_resources) = mh.create_models(mh(), data) assert questionnaire == self.questionnaire party = Party.objects.get(name='Party One') assert party_resources[0]['id'] == party.id assert 'sad_birthday.png' in party_resources[0]['resources'] assert 'invitation.pdf' in party_resources[0]['resources'] location = SpatialUnit.objects.get(type='BU') assert location_resources[0]['id'] == location.id assert 'resource_two.pdf' in location_resources[0]['resources'] tenure = TenureRelationship.objects.get(spatial_unit=location) assert tenure.party == party assert tenure_resources[0]['id'] == tenure.id assert 'resource_three.png' in tenure_resources[0]['resources']
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_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 create(self, request, *args, **kwargs): if request.method.upper() == 'HEAD': return Response(headers=self.get_openrosa_headers(request), status=status.HTTP_204_NO_CONTENT,) try: instance = ModelHelper().upload_submission_data(request) except InvalidXMLSubmission as e: logger.debug(str(e)) return self._sendErrorResponse(request, e, status.HTTP_400_BAD_REQUEST) except PermissionDenied as e: return self._sendErrorResponse(request, e, status.HTTP_403_FORBIDDEN) # If an already existing XFormSummission is sent back # don't create another. if type(instance) == XFormSubmission: return Response( headers=self.get_openrosa_headers(request), status=status.HTTP_201_CREATED, content_type=self.DEFAULT_CONTENT_TYPE ) instance, parties, locations, tenure_relationships = instance serializer = XFormSubmissionSerializer(instance) json = JSONRenderer().render(serializer.data) stream = BytesIO(json) data = JSONParser().parse(stream) serializer = XFormSubmissionSerializer(data=data) # Every possible error that would make the serializer not valid # has already been checked for, so no failsafe is necessary. if serializer.is_valid(): data = serializer.save() data.parties.add(*parties) data.spatial_units.add(*locations) data.tenure_relationships.add(*tenure_relationships) success_msg = _("Form was Successfully Received") return self._formatMessageResponse( request, success_msg, status.HTTP_201_CREATED )
def test_get_attributes(self): data = { 'party_type': 'Party Type', 'party_attributes_individual': { 'name_indv': 'Party Indv Attrs', 'type_indv': 'Party for one', }, 'party_attributes_people': { 'name_ppl': 'Party People Attrs', 'type_ppl': 'Where my party people at?', }, 'party_name': 'House Party' } attributes = mh._get_attributes(self, data, 'party') assert attributes['name_indv'] == 'Party Indv Attrs' assert attributes['type_indv'] == 'Party for one' assert attributes['name_ppl'] == 'Party People Attrs' assert attributes['type_ppl'] == 'Where my party people at?' assert 'party_name' not in attributes assert 'party_type' not in attributes
def test_get_attributes(self): data = { 'party_type': 'Party Type', 'party_attributes_individual': { 'name_indv': 'Party Indv Attrs', 'type_indv': 'Party for one', }, 'party_attributes_people': { 'name_ppl': 'Party People Attrs', 'type_ppl': 'Where my party people at?', }, 'party_name': 'House Party' } attributes = mh._get_attributes(self, data, 'party') assert attributes['name_indv'] == 'Party Indv Attrs' assert attributes['type_indv'] == 'Party for one' assert attributes['name_ppl'] == 'Party People Attrs' assert attributes['type_ppl'] == 'Where my party people at?' assert 'party_name' not in attributes assert 'party_type' not in attributes
def test_create_spatial_unit(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') line = ('45.56342779158167 -122.67650283873081 0.0 0.0;' '45.56176327330353 -122.67669159919024 0.0 0.0;' '45.56151562182025 -122.67490658909082 0.0 0.0;') # ~~~~~~~~~~~~~~~~~~~~~~~~~~ # test without repeats # ~~~~~~~~~~~~~~~~~~~~~~~~~~ data = { 'location_type': 'BU', 'location_geometry': geoshape, 'location_attributes': { 'fname': False, 'fname_two': 'Location One', }, 'location_photo': 'resource.png', 'location_resource_invite': 'resource_two.pdf', } location_objects, location_resources = mh.create_spatial_unit( mh(), data, self.project) assert len(location_objects) == 1 location = SpatialUnit.objects.get(type='BU') assert location.attributes == { 'fname': False, 'fname_two': 'Location One'} assert location.geometry.geom_type == 'Polygon' assert len(location_resources) == 1 assert location_resources[0]['id'] == location.id assert len(location_resources[0]['resources']) == 2 assert 'resource.png' in location_resources[0]['resources'] assert 'resource_two.pdf' in location_resources[0]['resources'] assert location.project == self.project # ~~~~~~~~~~~~~~~~~~~~~~~~~~ # test with repeats # ~~~~~~~~~~~~~~~~~~~~~~~~~~ data = { 'location_repeat': [{ 'location_type': 'PA', 'location_geotrace': line, 'location_attributes': { 'fname': False, 'fname_two': 'Location One', }, 'location_photo': 'resource.png', 'location_resource_invite': 'resource_two.pdf', }, { 'location_type': 'CB', 'location_geoshape': geoshape, 'location_attributes': { 'fname': True, 'fname_two': 'Location Two', }, 'location_photo': 'resource_three.png', 'location_resource_invite': 'resource_four.pdf', }] } location_objects, location_resources = mh.create_spatial_unit( mh(), data, self.project) assert len(location_objects) == 2 location = SpatialUnit.objects.get(type='PA') assert location.geometry.geom_type == 'LineString' assert location.attributes == { 'fname': False, 'fname_two': 'Location One'} location2 = SpatialUnit.objects.get(type='CB') assert location2.geometry.geom_type == 'Polygon' assert location2.attributes == { 'fname': True, 'fname_two': 'Location Two'} assert len(location_resources) == 2 assert location_resources[0]['id'] == location.id assert len(location_resources[0]['resources']) == 2 assert 'resource.png' in location_resources[0]['resources'] assert 'resource_two.pdf' in location_resources[0]['resources'] assert location.project == self.project assert location_resources[1]['id'] == location2.id assert len(location_resources[1]['resources']) == 2 assert 'resource_three.png' in location_resources[1]['resources'] assert 'resource_four.pdf' in location_resources[1]['resources'] assert location2.project == self.project # ~~~~~~~~~~~~~~~~~~~~~~~~~~ # test fails # ~~~~~~~~~~~~~~~~~~~~~~~~~~ data = { 'location_nonsense': 'BLAH BLAH', 'location_geometry': line, 'location_attributes': { 'fname': False, 'fname_two': 'Location One', }, 'location_photo': 'resource.png', 'location_resource_invite': 'resource_two.pdf', } with pytest.raises(InvalidXMLSubmission): mh.create_spatial_unit( mh(), data, self.project) assert SpatialUnit.objects.count() == 3
def test_create_resource(self): # ~~~~~~~~~~~~~~~~~~~~~~~~~~ # test attaching resources # ~~~~~~~~~~~~~~~~~~~~~~~~~~ with open(path + '/xforms/tests/files/test_image_one.png', 'rb') as src: file = src.read() data = InMemoryUploadedFile( file=io.BytesIO(file), field_name='test_image_one', name='{}.png'.format('test_image_one'), content_type='image/png', size=len(file), charset='utf-8', ) party = PartyFactory.create(project=self.project) mh.create_resource(self, data, self.user, self.project, content_object=party) assert len(party.resources) == 1 resource = Resource.objects.get(name='test_image_one.png') assert resource in party.resources # ~~~~~~~~~~~~~~~~~~~~~~~~~~ # test attaching existing resources # ~~~~~~~~~~~~~~~~~~~~~~~~~~ party2 = PartyFactory.create(project=self.project) mh.create_resource(self, data, self.user, self.project, content_object=party2) assert Resource.objects.count() == 1 assert len(party2.resources) == 1 assert resource in party2.resources # ~~~~~~~~~~~~~~~~~~~~~~~~~~ # test without content object # ~~~~~~~~~~~~~~~~~~~~~~~~~~ with open(path + '/xforms/tests/files/test_image_two.png', 'rb') as src: file = src.read() data = InMemoryUploadedFile( file=io.BytesIO(file), field_name='test_image_two', name='{}.png'.format('test_image_two'), content_type='image/png', size=len(file), charset='utf-8', ) mh.create_resource(self, data, self.user, self.project, content_object=None) assert Resource.objects.count() == 2 resource = Resource.objects.get(name='test_image_two.png') assert resource.content_objects.count() == 0 # ~~~~~~~~~~~~~~~~~~~~~~~~~~ # test failing # ~~~~~~~~~~~~~~~~~~~~~~~~~~ with pytest.raises(InvalidXMLSubmission): mh.create_resource(self, data, self.user, self.project, content_object='ardvark') assert Resource.objects.count() == 2
def test_create_tenure_relationship(self): # ~~~~~~~~~~~~~~~~~~~~~~~~~~ # test without repeats # ~~~~~~~~~~~~~~~~~~~~~~~~~~ party = PartyFactory.create(project=self.project) location = SpatialUnitFactory.create(project=self.project) data = { 'tenure_type': 'CO', 'tenure_relationship_attributes': { 'fname': False, 'fname_two': 'Tenure One' }, 'tenure_resource_photo': 'resource.png' } tenure_relationships, tenure_resources = mh.create_tenure_relationship( mh(), data, [party], [location], self.project) tenure = TenureRelationship.objects.get(tenure_type='CO') assert tenure_relationships == [tenure] assert tenure.party == party assert tenure.spatial_unit == location assert tenure.attributes == {'fname': False, 'fname_two': 'Tenure One'} assert len(tenure_resources) == 1 assert tenure_resources[0]['id'] == tenure.id assert 'resource.png' in tenure_resources[0]['resources'] # ~~~~~~~~~~~~~~~~~~~~~~~~~~ # inside party_repeat # ~~~~~~~~~~~~~~~~~~~~~~~~~~ party2 = PartyFactory.create(project=self.project) party3 = PartyFactory.create(project=self.project) data = { 'party_repeat': [{ 'tenure_type': 'WR', 'tenure_relationship_attributes': { 'fname': False, 'fname_two': 'Tenure Two' }, 'tenure_resource_photo': 'resource_two.png' }, { 'tenure_type': 'CO', 'tenure_relationship_attributes': { 'fname': True, 'fname_two': 'Tenure Three' }, 'tenure_resource_photo': 'resource_three.png' }] } tenure_relationships, tenure_resources = mh.create_tenure_relationship( mh(), data, [party2, party3], [location], self.project) tenure2 = TenureRelationship.objects.get(party=party2) tenure3 = TenureRelationship.objects.get(party=party3) assert tenure_relationships == [tenure2, tenure3] assert tenure2.spatial_unit == location assert tenure2.tenure_type == 'WR' assert tenure2.attributes == { 'fname': False, 'fname_two': 'Tenure Two' } assert tenure3.spatial_unit == location assert tenure3.tenure_type == 'CO' assert tenure3.attributes == { 'fname': True, 'fname_two': 'Tenure Three' } assert len(tenure_resources) == 2 assert tenure_resources[0]['id'] == tenure2.id assert 'resource_two.png' in tenure_resources[0]['resources'] assert tenure_resources[1]['id'] == tenure3.id assert 'resource_three.png' in tenure_resources[1]['resources'] # ~~~~~~~~~~~~~~~~~~~~~~~~~~ # inside location_repeat # ~~~~~~~~~~~~~~~~~~~~~~~~~~ location2 = SpatialUnitFactory.create(project=self.project) location3 = SpatialUnitFactory.create(project=self.project) data = { 'location_repeat': [{ 'tenure_type': 'WR', 'tenure_relationship_attributes': { 'fname': False, 'fname_two': 'Tenure Four' }, 'tenure_resource_photo': 'resource_four.png' }, { 'tenure_type': 'CO', 'tenure_relationship_attributes': { 'fname': True, 'fname_two': 'Tenure Five' }, 'tenure_resource_photo': 'resource_five.png' }] } tenure_relationships, tenure_resources = mh.create_tenure_relationship( mh(), data, [party], [location2, location3], self.project) tenure4 = TenureRelationship.objects.get(spatial_unit=location2) tenure5 = TenureRelationship.objects.get(spatial_unit=location3) assert tenure_relationships == [tenure4, tenure5] assert tenure4.party == party assert tenure4.tenure_type == 'WR' assert tenure4.attributes == { 'fname': False, 'fname_two': 'Tenure Four' } assert tenure5.party == party assert tenure5.tenure_type == 'CO' assert tenure5.attributes == { 'fname': True, 'fname_two': 'Tenure Five' } assert len(tenure_resources) == 2 assert tenure_resources[0]['id'] == tenure4.id assert 'resource_four.png' in tenure_resources[0]['resources'] assert tenure_resources[1]['id'] == tenure5.id assert 'resource_five.png' in tenure_resources[1]['resources'] # ~~~~~~~~~~~~~~~~~~~~~~~~~~ # outside party_repeat # ~~~~~~~~~~~~~~~~~~~~~~~~~~ party4 = PartyFactory.create(project=self.project) party5 = PartyFactory.create(project=self.project) data = { 'party_repeat': [], 'tenure_type': 'CO', 'tenure_relationship_attributes': { 'fname': True, 'fname_two': 'Tenure 6, 7' }, 'tenure_resource_photo': 'resource_six.png' } tenure_relationships, tenure_resources = mh.create_tenure_relationship( mh(), data, [party4, party5], [location], self.project) tenure6 = TenureRelationship.objects.get(party=party4) tenure7 = TenureRelationship.objects.get(party=party5) assert tenure_relationships == [tenure6, tenure7] assert tenure6.spatial_unit == location assert tenure6.tenure_type == 'CO' assert tenure6.attributes == { 'fname': True, 'fname_two': 'Tenure 6, 7' } assert tenure7.spatial_unit == location assert tenure7.tenure_type == 'CO' assert tenure7.attributes == { 'fname': True, 'fname_two': 'Tenure 6, 7' } assert len(tenure_resources) == 2 assert tenure_resources[0]['id'] == tenure6.id assert 'resource_six.png' in tenure_resources[0]['resources'] assert tenure_resources[1]['id'] == tenure7.id assert 'resource_six.png' in tenure_resources[1]['resources'] # ~~~~~~~~~~~~~~~~~~~~~~~~~~ # outside location_repeat # ~~~~~~~~~~~~~~~~~~~~~~~~~~ location4 = SpatialUnitFactory.create(project=self.project) location5 = SpatialUnitFactory.create(project=self.project) data = { 'location_repeat': [], 'tenure_type': 'WR', 'tenure_relationship_attributes': { 'fname': False, 'fname_two': 'Tenure 8, 9' }, 'tenure_resource_photo': 'resource_seven.png' } tenure_relationships, tenure_resources = mh.create_tenure_relationship( mh(), data, [party], [location4, location5], self.project) tenure8 = TenureRelationship.objects.get(spatial_unit=location4) tenure9 = TenureRelationship.objects.get(spatial_unit=location5) assert tenure_relationships == [tenure8, tenure9] assert tenure8.party == party assert tenure8.tenure_type == 'WR' assert tenure8.attributes == { 'fname': False, 'fname_two': 'Tenure 8, 9' } assert tenure9.party == party assert tenure9.tenure_type == 'WR' assert tenure9.attributes == { 'fname': False, 'fname_two': 'Tenure 8, 9' } assert len(tenure_resources) == 2 assert tenure_resources[0]['id'] == tenure8.id assert 'resource_seven.png' in tenure_resources[0]['resources'] assert tenure_resources[1]['id'] == tenure9.id assert 'resource_seven.png' in tenure_resources[1]['resources'] data = { 'location_repeat': [], 'tenure_nonsense': 'Blah blah blah', 'tenure_relationship_attributes': { 'fname': False, 'fname_two': 'Tenure 8, 9' }, 'tenure_resource_photo': 'resource_seven.png' } # ~~~~~~~~~~~~~~~~~~~~~~~~~~ # test failing # ~~~~~~~~~~~~~~~~~~~~~~~~~~ with pytest.raises(InvalidXMLSubmission): mh.create_tenure_relationship(mh(), data, [party], [location4, location5], self.project) assert TenureRelationship.objects.count() == 9
def test_create_spatial_unit(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') line = ('45.56342779158167 -122.67650283873081 0.0 0.0;' '45.56176327330353 -122.67669159919024 0.0 0.0;' '45.56151562182025 -122.67490658909082 0.0 0.0;') # ~~~~~~~~~~~~~~~~~~~~~~~~~~ # test without repeats # ~~~~~~~~~~~~~~~~~~~~~~~~~~ data = { 'location_type': 'BU', 'location_geometry': geoshape, 'location_attributes': { 'fname': False, 'fname_two': 'Location One', }, 'location_photo': 'resource.png', 'location_resource_invite': 'resource_two.pdf', } location_objects, location_resources = mh.create_spatial_unit( mh(), data, self.project) assert len(location_objects) == 1 location = SpatialUnit.objects.get(type='BU') assert location.attributes == { 'fname': False, 'fname_two': 'Location One' } assert location.geometry.geom_type == 'Polygon' assert len(location_resources) == 1 assert location_resources[0]['id'] == location.id assert len(location_resources[0]['resources']) == 2 assert 'resource.png' in location_resources[0]['resources'] assert 'resource_two.pdf' in location_resources[0]['resources'] assert location.project == self.project # ~~~~~~~~~~~~~~~~~~~~~~~~~~ # test with repeats # ~~~~~~~~~~~~~~~~~~~~~~~~~~ data = { 'location_repeat': [{ 'location_type': 'PA', 'location_geotrace': line, 'location_attributes': { 'fname': False, 'fname_two': 'Location One', }, 'location_photo': 'resource.png', 'location_resource_invite': 'resource_two.pdf', }, { 'location_type': 'CB', 'location_geoshape': geoshape, 'location_attributes': { 'fname': True, 'fname_two': 'Location Two', }, 'location_photo': 'resource_three.png', 'location_resource_invite': 'resource_four.pdf', }] } location_objects, location_resources = mh.create_spatial_unit( mh(), data, self.project) assert len(location_objects) == 2 location = SpatialUnit.objects.get(type='PA') assert location.geometry.geom_type == 'LineString' assert location.attributes == { 'fname': False, 'fname_two': 'Location One' } location2 = SpatialUnit.objects.get(type='CB') assert location2.geometry.geom_type == 'Polygon' assert location2.attributes == { 'fname': True, 'fname_two': 'Location Two' } assert len(location_resources) == 2 assert location_resources[0]['id'] == location.id assert len(location_resources[0]['resources']) == 2 assert 'resource.png' in location_resources[0]['resources'] assert 'resource_two.pdf' in location_resources[0]['resources'] assert location.project == self.project assert location_resources[1]['id'] == location2.id assert len(location_resources[1]['resources']) == 2 assert 'resource_three.png' in location_resources[1]['resources'] assert 'resource_four.pdf' in location_resources[1]['resources'] assert location2.project == self.project # ~~~~~~~~~~~~~~~~~~~~~~~~~~ # test fails # ~~~~~~~~~~~~~~~~~~~~~~~~~~ data = { 'location_nonsense': 'BLAH BLAH', 'location_geometry': line, 'location_attributes': { 'fname': False, 'fname_two': 'Location One', }, 'location_photo': 'resource.png', 'location_resource_invite': 'resource_two.pdf', } with pytest.raises(InvalidXMLSubmission): mh.create_spatial_unit(mh(), data, self.project) assert SpatialUnit.objects.count() == 3
def test_create_party(self): # ~~~~~~~~~~~~~~~~~~~~~~~~~~ # test without repeats # ~~~~~~~~~~~~~~~~~~~~~~~~~~ data = { '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_objects, party_resources = mh.create_party( mh(), data, self.project) assert len(party_objects) == 1 party = Party.objects.get(name='Party One') assert party.type == 'IN' assert party.attributes == {'fname': False, 'fname_two': 'socks'} assert len(party_resources) == 1 assert party_resources[0]['id'] == party.id assert len(party_resources[0]['resources']) == 2 assert 'sad_birthday.png' in party_resources[0]['resources'] assert 'invitation.pdf' in party_resources[0]['resources'] assert party.project == self.project # ~~~~~~~~~~~~~~~~~~~~~~~~~~ # test with repeats # ~~~~~~~~~~~~~~~~~~~~~~~~~~ data = { 'party_repeat': [{ 'party_name': 'Party Two', 'party_type': 'IN', 'party_attributes_individual': { 'fname': False, 'fname_two': 'socks', }, 'party_photo': 'sad_birthday.png', 'party_resource_invite': 'invitation.pdf', }, { 'party_name': 'Party Three', 'party_type': 'GR', 'party_attributes_group': { 'fname': True, 'fname_two': 'video games', }, 'party_photo': 'awesome_birthday.png', 'party_resource_invite': 'invitation_two.pdf', }] } party_objects, party_resources = mh.create_party( mh(), data, self.project) assert len(party_objects) == 2 party = Party.objects.get(name='Party Two') assert party.type == 'IN' assert party.attributes == {'fname': False, 'fname_two': 'socks'} party2 = Party.objects.get(name='Party Three') assert party2.type == 'GR' assert party2.attributes == {'fname': True, 'fname_two': 'video games'} assert len(party_resources) == 2 assert party_resources[0]['id'] == party.id assert len(party_resources[0]['resources']) == 2 assert 'sad_birthday.png' in party_resources[0]['resources'] assert 'invitation.pdf' in party_resources[0]['resources'] assert party.project == self.project assert party_resources[1]['id'] == party2.id assert len(party_resources[1]['resources']) == 2 assert 'awesome_birthday.png' in party_resources[1]['resources'] assert 'invitation_two.pdf' in party_resources[1]['resources'] assert party2.project == self.project # ~~~~~~~~~~~~~~~~~~~~~~~~~~ # test without fails # ~~~~~~~~~~~~~~~~~~~~~~~~~~ data = { 'party_nonsense': 'Blah blah blah', 'party_type': 'IN', 'party_attributes_individual': { 'fname': False, 'fname_two': 'socks', }, 'party_photo': 'sad_birthday.png', 'party_resource_invite': 'invitation.pdf', } with pytest.raises(InvalidXMLSubmission): mh.create_party(mh(), data, self.project) assert Party.objects.count() == 3
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='CO', attributes={ 'fname': False, 'fname_two': 'Tenure One' }) tenure2 = TenureRelationshipFactory.create(project=self.project, spatial_unit=su, party=party2, tenure_type='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_create_models(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_name': 'Party One', 'party_type': 'IN', 'party_attributes_individual': { 'fname': False, 'fname_two': 'socks', }, 'party_photo': 'sad_birthday.png', 'party_resource_invite': 'invitation.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' } user = UserFactory.create() OrganizationRole.objects.create(user=user, organization=self.project.organization, admin=True) (questionnaire, parties, party_resources, locations, location_resources, tenure_relationships, tenure_resources) = mh.create_models(mh(), data, user) assert questionnaire == self.questionnaire party = Party.objects.get(name='Party One') assert parties == [party] assert party_resources[0]['id'] == party.id assert 'sad_birthday.png' in party_resources[0]['resources'] assert 'invitation.pdf' in party_resources[0]['resources'] location = SpatialUnit.objects.get(type='BU') assert locations == [location] assert location_resources[0]['id'] == location.id assert 'resource_two.pdf' in location_resources[0]['resources'] tenure = TenureRelationship.objects.get(spatial_unit=location) assert tenure_relationships == [tenure] assert tenure.party == party assert tenure_resources[0]['id'] == tenure.id assert 'resource_three.png' in tenure_resources[0]['resources']
def test_create_resource(self): # ~~~~~~~~~~~~~~~~~~~~~~~~~~ # test attaching resources # ~~~~~~~~~~~~~~~~~~~~~~~~~~ file = open( path + '/xforms/tests/files/test_image_one.png', 'rb' ).read() data = InMemoryUploadedFile( file=io.BytesIO(file), field_name='test_image_one', name='{}.png'.format('test_image_one'), content_type='image/png', size=len(file), charset='utf-8', ) party = PartyFactory.create(project=self.project) mh.create_resource( self, data, self.user, self.project, content_object=party) assert len(party.resources) == 1 resource = Resource.objects.get(name='test_image_one.png') assert resource in party.resources # ~~~~~~~~~~~~~~~~~~~~~~~~~~ # test attaching existing resources # ~~~~~~~~~~~~~~~~~~~~~~~~~~ party2 = PartyFactory.create(project=self.project) mh.create_resource( self, data, self.user, self.project, content_object=party2) assert Resource.objects.count() == 1 assert len(party2.resources) == 1 assert resource in party2.resources # ~~~~~~~~~~~~~~~~~~~~~~~~~~ # test without content object # ~~~~~~~~~~~~~~~~~~~~~~~~~~ file = open( path + '/xforms/tests/files/test_image_two.png', 'rb' ).read() data = InMemoryUploadedFile( file=io.BytesIO(file), field_name='test_image_two', name='{}.png'.format('test_image_two'), content_type='image/png', size=len(file), charset='utf-8', ) mh.create_resource( self, data, self.user, self.project, content_object=None) assert Resource.objects.count() == 2 resource = Resource.objects.get(name='test_image_two.png') assert resource.content_objects.count() == 0 # ~~~~~~~~~~~~~~~~~~~~~~~~~~ # test failing # ~~~~~~~~~~~~~~~~~~~~~~~~~~ with pytest.raises(InvalidXMLSubmission): mh.create_resource( self, data, self.user, self.project, content_object='ardvark') assert Resource.objects.count() == 2
def test_get_sanitizable_questions(self): QuestionFactory.create(name='text', type='TX', questionnaire=self.questionnaire) QuestionFactory.create(name='note', type='NO', questionnaire=self.questionnaire) QuestionFactory.create(name='integer', type='IN', questionnaire=self.questionnaire) QuestionFactory.create(name='select_one', type='S1', questionnaire=self.questionnaire) QuestionFactory.create(name='select_multiple', type='SM', questionnaire=self.questionnaire) QuestionFactory.create(name='geopoint', type='GP', questionnaire=self.questionnaire) QuestionFactory.create(name='geotrace', type='GT', questionnaire=self.questionnaire) QuestionFactory.create(name='geoshape', type='GS', questionnaire=self.questionnaire) QuestionFactory.create(name='date', type='DA', questionnaire=self.questionnaire) QuestionFactory.create(name='time', type='TI', questionnaire=self.questionnaire) QuestionFactory.create(name='datetime', type='DT', questionnaire=self.questionnaire) QuestionFactory.create(name='calculate', type='CA', questionnaire=self.questionnaire) QuestionFactory.create(name='acknowledge', type='AC', questionnaire=self.questionnaire) QuestionFactory.create(name='photo', type='PH', questionnaire=self.questionnaire) QuestionFactory.create(name='audio', type='AU', questionnaire=self.questionnaire) QuestionFactory.create(name='video', type='VI', questionnaire=self.questionnaire) QuestionFactory.create(name='barcode', type='BC', questionnaire=self.questionnaire) QuestionFactory.create(name='start', type='ST', questionnaire=self.questionnaire) QuestionFactory.create(name='end', type='EN', questionnaire=self.questionnaire) QuestionFactory.create(name='today', type='TD', questionnaire=self.questionnaire) QuestionFactory.create(name='deviceid', type='DI', questionnaire=self.questionnaire) QuestionFactory.create(name='subscriber_id', type='SI', questionnaire=self.questionnaire) QuestionFactory.create(name='simserial', type='SS', questionnaire=self.questionnaire) QuestionFactory.create(name='phonenumber', type='PN', questionnaire=self.questionnaire) sanitizeable_questions = mh.get_sanitizable_questions( mh(), self.questionnaire.id_string, self.questionnaire.version) assert len(sanitizeable_questions) == 2 assert 'text' in sanitizeable_questions assert 'note' in sanitizeable_questions assert 'integer' not in sanitizeable_questions assert 'select_one' not in sanitizeable_questions assert 'select_multiple' not in sanitizeable_questions assert 'geopoint' not in sanitizeable_questions assert 'geotrace' not in sanitizeable_questions assert 'geoshape' not in sanitizeable_questions assert 'date' not in sanitizeable_questions assert 'time' not in sanitizeable_questions assert 'datetime' not in sanitizeable_questions assert 'calculate' not in sanitizeable_questions assert 'acknowledge' not in sanitizeable_questions assert 'photo' not in sanitizeable_questions assert 'audio' not in sanitizeable_questions assert 'video' not in sanitizeable_questions assert 'barcode' not in sanitizeable_questions assert 'start' not in sanitizeable_questions assert 'end' not in sanitizeable_questions assert 'today' not in sanitizeable_questions assert 'deviceid' not in sanitizeable_questions assert 'subscriber_id' not in sanitizeable_questions assert 'simserial' not in sanitizeable_questions assert 'phonenumber' not in sanitizeable_questions
def test_create_tenure_relationship(self): # ~~~~~~~~~~~~~~~~~~~~~~~~~~ # test without repeats # ~~~~~~~~~~~~~~~~~~~~~~~~~~ party = PartyFactory.create(project=self.project) location = SpatialUnitFactory.create(project=self.project) data = { 'tenure_type': 'CO', 'tenure_relationship_attributes': { 'fname': False, 'fname_two': 'Tenure One' }, 'tenure_resource_photo': 'resource.png' } tenure_relationships, tenure_resources = mh.create_tenure_relationship( mh(), data, [party], [location], self.project) tenure = TenureRelationship.objects.get(tenure_type='CO') assert tenure_relationships == [tenure] assert tenure.party == party assert tenure.spatial_unit == location assert tenure.attributes == {'fname': False, 'fname_two': 'Tenure One'} assert len(tenure_resources) == 1 assert tenure_resources[0]['id'] == tenure.id assert 'resource.png' in tenure_resources[0]['resources'] # ~~~~~~~~~~~~~~~~~~~~~~~~~~ # inside party_repeat # ~~~~~~~~~~~~~~~~~~~~~~~~~~ party2 = PartyFactory.create(project=self.project) party3 = PartyFactory.create(project=self.project) data = { 'party_repeat': [{ 'tenure_type': 'WR', 'tenure_relationship_attributes': { 'fname': False, 'fname_two': 'Tenure Two' }, 'tenure_resource_photo': 'resource_two.png' }, { 'tenure_type': 'CO', 'tenure_relationship_attributes': { 'fname': True, 'fname_two': 'Tenure Three' }, 'tenure_resource_photo': 'resource_three.png' }] } tenure_relationships, tenure_resources = mh.create_tenure_relationship( mh(), data, [party2, party3], [location], self.project) tenure2 = TenureRelationship.objects.get(party=party2) tenure3 = TenureRelationship.objects.get(party=party3) assert tenure_relationships == [tenure2, tenure3] assert tenure2.spatial_unit == location assert tenure2.tenure_type.id == 'WR' assert tenure2.attributes == { 'fname': False, 'fname_two': 'Tenure Two'} assert tenure3.spatial_unit == location assert tenure3.tenure_type.id == 'CO' assert tenure3.attributes == { 'fname': True, 'fname_two': 'Tenure Three'} assert len(tenure_resources) == 2 assert tenure_resources[0]['id'] == tenure2.id assert 'resource_two.png' in tenure_resources[0]['resources'] assert tenure_resources[1]['id'] == tenure3.id assert 'resource_three.png' in tenure_resources[1]['resources'] # ~~~~~~~~~~~~~~~~~~~~~~~~~~ # inside location_repeat # ~~~~~~~~~~~~~~~~~~~~~~~~~~ location2 = SpatialUnitFactory.create(project=self.project) location3 = SpatialUnitFactory.create(project=self.project) data = { 'location_repeat': [{ 'tenure_type': 'WR', 'tenure_relationship_attributes': { 'fname': False, 'fname_two': 'Tenure Four' }, 'tenure_resource_photo': 'resource_four.png' }, { 'tenure_type': 'CO', 'tenure_relationship_attributes': { 'fname': True, 'fname_two': 'Tenure Five' }, 'tenure_resource_photo': 'resource_five.png' }] } tenure_relationships, tenure_resources = mh.create_tenure_relationship( mh(), data, [party], [location2, location3], self.project) tenure4 = TenureRelationship.objects.get(spatial_unit=location2) tenure5 = TenureRelationship.objects.get(spatial_unit=location3) assert tenure_relationships == [tenure4, tenure5] assert tenure4.party == party assert tenure4.tenure_type.id == 'WR' assert tenure4.attributes == { 'fname': False, 'fname_two': 'Tenure Four'} assert tenure5.party == party assert tenure5.tenure_type.id == 'CO' assert tenure5.attributes == { 'fname': True, 'fname_two': 'Tenure Five'} assert len(tenure_resources) == 2 assert tenure_resources[0]['id'] == tenure4.id assert 'resource_four.png' in tenure_resources[0]['resources'] assert tenure_resources[1]['id'] == tenure5.id assert 'resource_five.png' in tenure_resources[1]['resources'] # ~~~~~~~~~~~~~~~~~~~~~~~~~~ # outside party_repeat # ~~~~~~~~~~~~~~~~~~~~~~~~~~ party4 = PartyFactory.create(project=self.project) party5 = PartyFactory.create(project=self.project) data = { 'party_repeat': [], 'tenure_type': 'CO', 'tenure_relationship_attributes': { 'fname': True, 'fname_two': 'Tenure 6, 7' }, 'tenure_resource_photo': 'resource_six.png' } tenure_relationships, tenure_resources = mh.create_tenure_relationship( mh(), data, [party4, party5], [location], self.project) tenure6 = TenureRelationship.objects.get(party=party4) tenure7 = TenureRelationship.objects.get(party=party5) assert tenure_relationships == [tenure6, tenure7] assert tenure6.spatial_unit == location assert tenure6.tenure_type.id == 'CO' assert tenure6.attributes == { 'fname': True, 'fname_two': 'Tenure 6, 7'} assert tenure7.spatial_unit == location assert tenure7.tenure_type.id == 'CO' assert tenure7.attributes == { 'fname': True, 'fname_two': 'Tenure 6, 7'} assert len(tenure_resources) == 2 assert tenure_resources[0]['id'] == tenure6.id assert 'resource_six.png' in tenure_resources[0]['resources'] assert tenure_resources[1]['id'] == tenure7.id assert 'resource_six.png' in tenure_resources[1]['resources'] # ~~~~~~~~~~~~~~~~~~~~~~~~~~ # outside location_repeat # ~~~~~~~~~~~~~~~~~~~~~~~~~~ location4 = SpatialUnitFactory.create(project=self.project) location5 = SpatialUnitFactory.create(project=self.project) data = { 'location_repeat': [], 'tenure_type': 'WR', 'tenure_relationship_attributes': { 'fname': False, 'fname_two': 'Tenure 8, 9' }, 'tenure_resource_photo': 'resource_seven.png' } tenure_relationships, tenure_resources = mh.create_tenure_relationship( mh(), data, [party], [location4, location5], self.project) tenure8 = TenureRelationship.objects.get(spatial_unit=location4) tenure9 = TenureRelationship.objects.get(spatial_unit=location5) assert tenure_relationships == [tenure8, tenure9] assert tenure8.party == party assert tenure8.tenure_type.id == 'WR' assert tenure8.attributes == { 'fname': False, 'fname_two': 'Tenure 8, 9'} assert tenure9.party == party assert tenure9.tenure_type.id == 'WR' assert tenure9.attributes == { 'fname': False, 'fname_two': 'Tenure 8, 9'} assert len(tenure_resources) == 2 assert tenure_resources[0]['id'] == tenure8.id assert 'resource_seven.png' in tenure_resources[0]['resources'] assert tenure_resources[1]['id'] == tenure9.id assert 'resource_seven.png' in tenure_resources[1]['resources'] data = { 'location_repeat': [], 'tenure_nonsense': 'Blah blah blah', 'tenure_relationship_attributes': { 'fname': False, 'fname_two': 'Tenure 8, 9' }, 'tenure_resource_photo': 'resource_seven.png' } # ~~~~~~~~~~~~~~~~~~~~~~~~~~ # test failing # ~~~~~~~~~~~~~~~~~~~~~~~~~~ with pytest.raises(InvalidXMLSubmission): mh.create_tenure_relationship( mh(), data, [party], [location4, location5], self.project) assert TenureRelationship.objects.count() == 9
def test_create_party(self): # ~~~~~~~~~~~~~~~~~~~~~~~~~~ # test without repeats # ~~~~~~~~~~~~~~~~~~~~~~~~~~ data = { '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_objects, party_resources = mh.create_party( mh(), data, self.project ) assert len(party_objects) == 1 party = Party.objects.get(name='Party One') assert party.type == 'IN' assert party.attributes == {'fname': False, 'fname_two': 'socks'} assert len(party_resources) == 1 assert party_resources[0]['id'] == party.id assert len(party_resources[0]['resources']) == 2 assert 'sad_birthday.png' in party_resources[0]['resources'] assert 'invitation.pdf' in party_resources[0]['resources'] assert party.project == self.project # ~~~~~~~~~~~~~~~~~~~~~~~~~~ # test with repeats # ~~~~~~~~~~~~~~~~~~~~~~~~~~ data = { 'party_repeat': [{ 'party_name': 'Party Two', 'party_type': 'IN', 'party_attributes_individual': { 'fname': False, 'fname_two': 'socks', }, 'party_photo': 'sad_birthday.png', 'party_resource_invite': 'invitation.pdf', }, { 'party_name': 'Party Three', 'party_type': 'GR', 'party_attributes_group': { 'fname': True, 'fname_two': 'video games', }, 'party_photo': 'awesome_birthday.png', 'party_resource_invite': 'invitation_two.pdf', }] } party_objects, party_resources = mh.create_party( mh(), data, self.project ) assert len(party_objects) == 2 party = Party.objects.get(name='Party Two') assert party.type == 'IN' assert party.attributes == {'fname': False, 'fname_two': 'socks'} party2 = Party.objects.get(name='Party Three') assert party2.type == 'GR' assert party2.attributes == { 'fname': True, 'fname_two': 'video games'} assert len(party_resources) == 2 assert party_resources[0]['id'] == party.id assert len(party_resources[0]['resources']) == 2 assert 'sad_birthday.png' in party_resources[0]['resources'] assert 'invitation.pdf' in party_resources[0]['resources'] assert party.project == self.project assert party_resources[1]['id'] == party2.id assert len(party_resources[1]['resources']) == 2 assert 'awesome_birthday.png' in party_resources[1]['resources'] assert 'invitation_two.pdf' in party_resources[1]['resources'] assert party2.project == self.project # ~~~~~~~~~~~~~~~~~~~~~~~~~~ # test without fails # ~~~~~~~~~~~~~~~~~~~~~~~~~~ data = { 'party_nonsense': 'Blah blah blah', 'party_type': 'IN', 'party_attributes_individual': { 'fname': False, 'fname_two': 'socks', }, 'party_photo': 'sad_birthday.png', 'party_resource_invite': 'invitation.pdf', } with pytest.raises(InvalidXMLSubmission): mh.create_party( mh(), data, self.project ) assert Party.objects.count() == 3
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']