def setUp(self): super().setUp() self.path = os.path.dirname(settings.BASE_DIR) self.valid_csv = '/organization/tests/files/test.csv' self.invalid_csv = '/organization/tests/files/test_invalid.csv' self.geoshape_csv = '/organization/tests/files/test_geoshape.csv' self.geotrace_csv = '/organization/tests/files/test_geotrace.csv' self.invalid_col_csv = '/organization/tests/files/invalid_col.csv' self.invalid_geom_type = ( '/organization/tests/files/invalid_geom_type.csv') self.no_tenure_type = '/organization/tests/files/no_tenure_type.csv' self.project = ProjectFactory.create(name='Test CSV Import') storage = FakeS3Storage() xlscontent = open(path + '/organization/tests/files/uttaran_test.xlsx', 'rb').read() form = storage.save('xls-forms/uttaran_test.xlsx', xlscontent) Questionnaire.objects.create_from_form(xls_form=form, project=self.project) # test for expected schema and attribute creation assert 2 == Schema.objects.all().count() assert 39 == Attribute.objects.all().count() self.attributes = [ 'deed_of_land', 'amount_othersland', 'educational_qualification', 'name_mouza', 'land_calculation', 'how_aquire_landwh', 'female_member', 'mutation_of_land', 'amount_agriland', 'nid_number', 'class_hh', 'j_l', 'how_aquire_landt', 'boundary_conflict', 'dakhal_on_land', 'village_name', 'how_aquire_landp', 'how_aquire_landd', 'ownership_conflict', 'occupation_hh', 'others_conflict', 'how_aquire_landm', 'khatain_of_land', 'male_member', 'mobile_no', 'how_aquire_landw', 'everything', 'name_father_hus' ]
def _get_form(self, form_name): storage = FakeS3Storage() file = open( path + '/questionnaires/tests/files/{}.xlsx'.format(form_name), 'rb').read() form = storage.save('xls-forms/{}.xlsx'.format(form_name), file) return form
def test_create_project_resource(self): storage = FakeS3Storage() file = open(path + '/resources/tests/files/image.jpg', 'rb').read() file_name = storage.save('image.jpg', file) project = ProjectFactory.create() user = UserFactory.create() data = { 'name': 'New resource', 'description': '', 'file': file_name, 'original_file': 'image.png' } serializer = ResourceSerializer(data=data, context={ 'content_object': project, 'contributor': user, 'project_id': project.id }) serializer.is_valid() serializer.save() assert project.resources.count() == 1 assert project.resources.first().name == data['name'] assert project.resources.first().contributor == user
def test_update_questionnaire_attribute_schema(self): storage = FakeS3Storage() file = open( path + '/questionnaires/tests/files/xls-form-attrs.xlsx', 'rb' ).read() form = storage.save('xls-forms/xls-form.xlsx', file) project = ProjectFactory.create(name='TestProject') q1 = models.Questionnaire.objects.create_from_form( xls_form=form, project=project ) q2 = models.Questionnaire.objects.create_from_form( xls_form=form, project=project ) assert 12 == Schema.objects.all().count() content_type = ContentType.objects.get( app_label='party', model='party') assert q1.id != q2.id s1 = Schema.objects.get( content_type=content_type, selectors=( project.organization.pk, project.pk, q1.id) ) assert s1 is not None s2 = Schema.objects.get( content_type=content_type, selectors=( project.organization.pk, project.pk, project.current_questionnaire) ) assert s2 is not None assert s1 != s2
def setup_models(self): self.project = ProjectFactory.create() self.resources = ResourceFactory.create_batch( 2, content_object=self.project, project=self.project) self.denied = ResourceFactory.create(content_object=self.project, project=self.project) ResourceFactory.create() self.user = UserFactory.create() assign_policies(self.user, add_clauses=[ { 'effect': 'deny', 'object': ['resource/*/*/' + self.denied.id], 'action': ['resource.*'], }, { 'effect': 'deny', 'object': ['resource/*/*/*'], 'action': ['resource.unarchive'], }, ]) self.storage = FakeS3Storage() self.file = open(path + '/resources/tests/files/image.jpg', 'rb').read() self.file_name = self.storage.save('resources/image.jpg', self.file)
def test_spatial_resource(self): storage = FakeS3Storage() file = open(path + '/resources/tests/files/tracks.gpx', 'rb').read() file_name = storage.save('resources/tracks_test.gpx', file) resource = ResourceFactory.build(file=file_name, mime_type='text/xml') spatial_resource = SpatialResourceFactory.create(resource=resource) assert spatial_resource.project.pk == resource.project.pk assert spatial_resource.archived == resource.archived
def _get_xls_form(self, form_name): path = os.path.dirname(settings.BASE_DIR) storage = FakeS3Storage() file = open( path + '/questionnaires/tests/files/{}.xlsx'.format(form_name), 'rb').read() form = storage.save('xls-forms/' + form_name + '.xlsx', file) return form
def setup_models(self): storage = FakeS3Storage() tracks = open( path + '/resources/tests/files/tracks.gpx', 'rb').read() self.tracks_file = storage.save('resources/tracks.gpx', tracks) routes = open( path + '/resources/tests/files/routes.gpx', 'rb').read() self.routes_file = storage.save('resources/routes.gpx', routes) waypoints = open( path + '/resources/tests/files/waypoints.gpx', 'rb').read() self.waypoints_file = storage.save( 'resources/waypoints.gpx', waypoints) self.project = ProjectFactory.create() # create non-spatial resources ResourceFactory.create_batch( 2, content_object=self.project, project=self.project) # create attached spatial resource self.resource = ResourceFactory.create( content_object=self.project, project=self.project, file=self.tracks_file, original_file='tracks.gpx', mime_type='text/xml' ) # unauthorized self.denied = ResourceFactory.create( content_object=self.project, project=self.project, file=self.routes_file, original_file='routes.gpx', mime_type='text/xml' ) # create archived spatial resource ResourceFactory.create( content_object=self.project, archived=True, project=self.project, file=self.routes_file, original_file='routes.gpx', mime_type='text/xml' ) # create unattached spatial resource ResourceFactory.create( content_object=None, project=self.project, file=self.waypoints_file, original_file='waypoints.gpx', mime_type='text/xml' ) self.user = UserFactory.create() additional_clauses = [ { 'effect': 'deny', 'object': ['resource/*/*/' + self.denied.id], 'action': ['resource.*'], } ] assign_policies(self.user, add_clauses=additional_clauses)
def _after_postgeneration(cls, obj, create, results=None): if not obj.file.url: storage = FakeS3Storage() file = open(path + '/resources/tests/files/image.jpg', 'rb') file_name = storage.save('resources/image.jpg', file.read()) file.close() obj.file = file_name if create: obj.save()
def test_invalid_gpx_mime_type(self): storage = FakeS3Storage() file = open(path + '/resources/tests/files/mp3.xml', 'rb').read() file_name = storage.save('resources/mp3.xml', file) resource = ResourceFactory.build(file=file_name, mime_type='text/xml') assert os.path.isfile( os.path.join(settings.MEDIA_ROOT, 's3/uploads/resources/mp3.xml')) with pytest.raises(InvalidGPXFile) as e: create_spatial_resource(Resource, resource, True) assert str(e.value) == _('Invalid GPX mime type: audio/mpeg')
def test_register_file_version(self): storage = FakeS3Storage() file = open(path + '/resources/tests/files/image.jpg', 'rb').read() file_name = storage.save('resources/thumb_new.jpg', file) resource = ResourceFactory.create() resource.file = file_name resource.save() assert resource.file.url == file_name assert len(resource.file_versions) == 1
def setup_post_data(self): storage = FakeS3Storage() file = open(path + '/resources/tests/files/image.jpg', 'rb').read() file_name = storage.save('resources/image.jpg', file) return { 'name': 'Some name', 'description': '', 'file': file_name, 'original_file': 'image.png', 'mime_type': 'image/jpeg' }
def test_create_thumbnail(self): storage = FakeS3Storage() file = open(path + '/resources/tests/files/image.jpg', 'rb').read() file_name = storage.save('resources/thumb_test.jpg', file) resource = ResourceFactory.build(file=file_name, mime_type='image/jpeg') create_thumbnails(Resource, resource, True) assert os.path.isfile( os.path.join(settings.MEDIA_ROOT, 's3/uploads/resources/thumb_test-128x128.jpg'))
def test_create_from_form(self): storage = FakeS3Storage() file = open(path + '/questionnaires/tests/files/xls-form.xlsx', 'rb').read() form = storage.save('xls-forms/xls-form.xlsx', file) model = models.Questionnaire.objects.create_from_form( xls_form=form, original_file='original.xls', project=ProjectFactory.create()) assert model.id_string == 'question_types' assert model.filename == 'xls-form' assert model.title == 'Question types' assert model.original_file == 'original.xls'
def _prepare(cls, create, **kwargs): resource = super()._prepare(create, **kwargs) if not resource.file.url: storage = FakeS3Storage() file = open(path + '/resources/tests/files/image.jpg', 'rb').read() file_name = storage.save('resources/image.jpg', file) resource.file = file_name if create: resource.save() return resource
def test_invalid_gpx_file(self): storage = FakeS3Storage() file = open(path + '/resources/tests/files/invalidgpx.xml', 'rb').read() file_name = storage.save('resources/invalidgpx.xml', file) resource = ResourceFactory.build(file=file_name, mime_type='application/xml') assert os.path.isfile( os.path.join(settings.MEDIA_ROOT, 's3/uploads/resources/invalidgpx.xml')) with pytest.raises(InvalidGPXFile) as e: create_spatial_resource(Resource, resource, True) assert str(e.value) == _('Invalid GPX file') assert Resource.objects.all().count() == 0
def setUp(self): super().setUp() storage = FakeS3Storage() file = open(path + '/resources/tests/files/image.jpg', 'rb').read() file_name = storage.save('resources/image.jpg', file) self.data = { 'name': 'Some name', 'description': '', 'file': file_name, 'original_file': 'image.jpg', 'mime_type': 'image/jpeg' } self.project = ProjectFactory.create()
def test_create_attribute_schemas(self): storage = FakeS3Storage() file = open( path + '/questionnaires/tests/files/xls-form-attrs.xlsx', 'rb' ).read() form = storage.save('xls-forms/xls-form-attrs.xlsx', file) project = ProjectFactory.create() models.Questionnaire.objects.create_from_form( xls_form=form, project=project ) # test for expected schema and attribute creation assert 6 == Schema.objects.all().count() assert 10 == Attribute.objects.all().count()
def setUp(self): super().setUp() storage = FakeS3Storage() file = open( path + '/questionnaires/tests/files/xls-form-attrs.xlsx', 'rb' ).read() form = storage.save('xls-forms/xls-form-attrs.xlsx', file) self.content_type = ContentType.objects.get( app_label='party', model='party') self.project = ProjectFactory.create(name='TestProject') models.Questionnaire.objects.create_from_form( xls_form=form, project=self.project )
def test_create_invalid_gpx(self): storage = FakeS3Storage() file = open(path + '/resources/tests/files/mp3.xml', 'rb').read() file_name = storage.save('resources/mp3.xml', file) data = { 'name': 'Some name', 'description': '', 'file': file_name, 'original_file': 'image.png', 'mime_type': 'text/xml' } response = self.request(method='POST', user=self.user, post_data=data) assert response.status_code == 200 assert self.project.resources.count() == 0
def test_create_from_invald_form(self): storage = FakeS3Storage() file = open( path + '/questionnaires/tests/files/' 'xls-form-invalid.xlsx', 'rb').read() form = storage.save('xls-forms/xls-form-invalid.xlsx', file) with pytest.raises(InvalidXLSForm) as e: models.Questionnaire.objects.create_from_form( xls_form=form, project=ProjectFactory.create()) assert "Unknown question type 'interger'." in e.value.errors assert models.Questionnaire.objects.exists() is False assert models.QuestionGroup.objects.exists() is False assert models.Question.objects.exists() is False
def test_create_spatial_resource(self): storage = FakeS3Storage() file = open(path + '/resources/tests/files/deramola.xml', 'rb').read() file_name = storage.save('resources/deramola.xml', file) resource = ResourceFactory.build(file=file_name, mime_type='text/xml') assert os.path.isfile( os.path.join(settings.MEDIA_ROOT, 's3/uploads/resources/deramola.xml')) create_spatial_resource(Resource, resource, True) spatial_resources = resource.spatial_resources.all() assert spatial_resources.count() == 1 geom = spatial_resources[0].geom assert len(geom) == 18 assert spatial_resources[0].name == 'waypoints' assert spatial_resources[0].attributes == {}
def test_update_from_form(self): storage = FakeS3Storage() file = open(path + '/questionnaires/tests/files/xls-form.xlsx', 'rb').read() form = storage.save('xls-forms/xls-form.xlsx', file) project = ProjectFactory.create() m1 = models.Questionnaire.objects.create_from_form(xls_form=form, project=project) model = models.Questionnaire.objects.create_from_form(xls_form=form, project=project) assert model.id_string == 'question_types' assert model.filename == 'xls-form' assert model.title == 'Question types' assert m1.id != model.id assert project.current_questionnaire == model.id
def test_project_resource(self): storage = FakeS3Storage() file = open(path + '/resources/tests/files/image.jpg', 'rb').read() file_name = storage.save('resources/image.jpg', file) project = ProjectFactory.create() user = UserFactory.create() resource = Resource.objects.create(name='Re', file=file_name, content_object=project, contributor=user, project=project, mime_type='image/jpeg') assert resource.name == 'Re' assert resource.content_objects.count() == 1 assert resource.mime_type == 'image/jpeg' content_object = resource.content_objects.first() assert content_object.object_id == project.id assert content_object.content_type == ContentType.objects.get( app_label='organization', model='project')
def setUp(self): super().setUp() self.storage = FakeS3Storage() self.path = os.path.dirname(settings.BASE_DIR)