def test_validate_config_json_provided(self): invalid_config_mapper = FieldMapper( config_json=json_string({"add_literals": "invalid value"})) self.assertIsNone( invalid_config_mapper.validate_config_json( json_string({"add_literals": { "foo": "bar" }})))
def create_field_mapper(request): form = None if request.method == "POST": form = FieldMapperForm(request.POST) if form.is_valid(): new_field_mapper = FieldMapper(**form.cleaned_data) new_field_mapper.save() return redirect(reverse('configuration')) if form is None: form = FieldMapperForm return render(request, 'core/new_configuration_object.html', { 'form': form, 'object_name': 'Field Mapper', })
def setUpTestData(cls): cls.attributes = { 'name': 'Test Field Mapper', 'config_json': json_string({"add_literals": { "foo": "bar" }}), 'field_mapper_type': 'xml2kvp' } cls.field_mapper = FieldMapper(**cls.attributes)
def setUpTestData(cls): cls.transformation = Transformation(name='Class Test Transformation') cls.transformation.save() cls.oai_endpoint = OAIEndpoint(name='Class Test OAI endpoint') cls.oai_endpoint.save() cls.validation_scenario = ValidationScenario(name='Class Test Validation Scenario', validation_type='python') cls.validation_scenario.save() cls.rits = RecordIdentifierTransformation(name='Class Test RITS') cls.rits.save() cls.dpla_download = DPLABulkDataDownload() cls.dpla_download.save() cls.field_mapper = FieldMapper() cls.field_mapper.save()
def test_validate_config_json_invalid(self): invalid_config_mapper = FieldMapper( config_json=json_string({"add_literals": "invalid value"})) self.assertRaises(jsonschema.exceptions.ValidationError, invalid_config_mapper.validate_config_json)
def test_config_none(self): no_config_mapper = FieldMapper(name='new field mapper') self.assertIsNone(no_config_mapper.config)
def field_mapper_update(request): """ Create and save JSON to FieldMapper instance, or update pre-existing """ LOGGER.debug(request.POST) # get update type update_type = request.POST.get('update_type') # handle new FieldMapper creation if update_type == 'new': LOGGER.debug('creating new FieldMapper instance') field_mapper = FieldMapper( name=request.POST.get('fm_name'), config_json=request.POST.get('fm_config_json'), field_mapper_type='xml2kvp' ) # validate fm_config before creating try: field_mapper.validate_config_json() field_mapper.save() return JsonResponse({'results': True, 'msg': 'New Field Mapper configurations were <strong>saved</strong> as: <strong>%s</strong>' % request.POST.get( 'fm_name')}, status=201) except jsonschema.ValidationError as err: return JsonResponse({'results': False, 'msg': 'Could not <strong>create</strong> <strong>%s</strong>, the following error was had: %s' % ( field_mapper.name, str(err))}, status=409) # handle update if update_type == 'update': LOGGER.debug('updating pre-existing FieldMapper instance') # get fm instance field_mapper = FieldMapper.objects.get(pk=int(request.POST.get('fm_id'))) # update and save field_mapper.config_json = request.POST.get('fm_config_json') # validate fm_config before updating try: field_mapper.validate_config_json() field_mapper.save() return JsonResponse({'results': True, 'msg': 'Field Mapper configurations for <strong>%s</strong> were <strong>updated</strong>' % field_mapper.name}, status=200) except jsonschema.ValidationError as err: return JsonResponse({'results': False, 'msg': 'Could not <strong>update</strong> <strong>%s</strong>, the following error was had: %s' % ( field_mapper.name, str(err))}, status=409) # handle delete if update_type == 'delete': LOGGER.debug('deleting pre-existing FieldMapper instance') # get fm instance field_mapper = FieldMapper.objects.get(pk=int(request.POST.get('fm_id'))) # delete field_mapper.delete() return JsonResponse({'results': True, 'msg': 'Field Mapper configurations for <strong>%s</strong> were <strong>deleted</strong>' % field_mapper.name}, status=200)