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_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 test_format_geometry(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')

        geotrace = ('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;'
                    '45.56342779158167 -122.67650283873081 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;'
                '45.56181562182025 -122.67500658909082 0.0 0.0;')

        data = {
            'location_geometry': geoshape,
        }

        geom = mh()._format_geometry(data)
        assert 'POLYGON' in geom

        data = {
            'location_geoshape': geoshape,
        }

        geom = mh()._format_geometry(data)
        assert 'POLYGON' in geom

        data = {
            'location_geotrace': line,
        }

        geom = mh()._format_geometry(data)
        assert 'LINE' in geom

        data = {
            'location_geometry': geotrace,
        }

        geom = mh()._format_geometry(data)
        assert 'POLYGON' in geom
    def test_format_geometry(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')

        geotrace = ('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;'
                    '45.56342779158167 -122.67650283873081 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;'
                '45.56181562182025 -122.67500658909082 0.0 0.0;')

        data = {
            'location_geometry': geoshape,
        }

        geom = mh()._format_geometry(data)
        assert 'POLYGON' in geom

        data = {
            'location_geoshape': geoshape,
        }

        geom = mh()._format_geometry(data)
        assert 'POLYGON' in geom

        data = {
            'location_geotrace': line,
        }

        geom = mh()._format_geometry(data)
        assert 'LINE' in geom

        data = {
            'location_geometry': geotrace,
        }

        geom = mh()._format_geometry(data)
        assert 'POLYGON' in geom
    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_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_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_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_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_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_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_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_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_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