Example #1
0
def test_iter_json_3():
    def transform_handler(paths, el, results, phase, **kwargs):
        results[0]["spam"] = el
        return OAITransformer.PROCESSED

    record = {"path": {"to": {"field": "bla"}}, "spam": "ham"}
    rules = {"/spam": {"pre": transform_handler}}
    transformer = OAITransformer(rules=rules)
    with pytest.raises(ValueError):
        transformer.transform(record)
Example #2
0
def test_iter_json_1():
    def transform_handler(paths, el, results, phase, **kwargs):
        results[0]["spam"] = el
        return OAITransformer.PROCESSED

    record = {"path": {"to": {"field": "bla"}}, "spam": "ham"}
    rules = {"/spam": {"pre": transform_handler}}
    transformer = OAITransformer(rules=rules,
                                 unhandled_paths={
                                     "/path/to/field",
                                 })
    result = transformer.transform(record)
    assert result == {"spam": "ham"}
Example #3
0
def test_iter_json_2():
    def transform_handler(paths, el, results, phase, **kwargs):
        results[-1].setdefault("spam", [])
        results[-1]["spam"].append(el)
        return OAITransformer.PROCESSED

    record = {"path": {"to": {"field": "bla"}}, "spam": ["ham", "blabla"]}
    rules = {"/spam": {"pre": transform_handler}}
    transformer = OAITransformer(rules=rules,
                                 unhandled_paths={
                                     "/path/to/field",
                                 })
    result = transformer.transform(record)
    assert result == {"spam": ["ham", "blabla"]}
 def __init__(self,
              provider: OAIProvider,
              parser_name: str = None,
              unhandled_paths: set = None,
              validation: Callable = None,
              create_record: Callable = None,
              delete_record: Callable = None,
              update_record: Callable = None,
              pid_type: str = None,
              oai_identifiers: List[str] = None):
     super().__init__(provider)
     self.pid_type = pid_type
     self.provider = provider
     self.oai_sync = None
     self.sickle = Sickle(self.provider.oai_endpoint)
     registry.load()
     self.parsers = provider.get_parsers()
     self.rules = provider.get_rules(parser_name) or {}
     self.parser = self.parsers.get(parser_name) or {}
     self.transformer = OAITransformer(self.rules,
                                       unhandled_paths=unhandled_paths)
     self.validation_handler = validation
     self.create_record_handler = create_record
     self.update_record_handler = update_record
     self.delete_record_handler = delete_record
     self.oai_identifiers = oai_identifiers
Example #5
0
def test_iter_json_4():
    def transform_handler(paths, el, results, phase, **kwargs):
        results[0]["spam"] = el
        return OAITransformer.PROCESSED

    record = {
        "path": {
            "to": {
                "field": "bla"
            }
        },
        "spam": [{
            "ham": "blabla",
        }, {
            "unhandled_option": ["nothing"]
        }]
    }
    rules = {"/spam/ham": {"pre": transform_handler}}
    transformer = OAITransformer(rules=rules,
                                 unhandled_paths={
                                     "/path/to/field",
                                 })
    with pytest.raises(ValueError):
        result = transformer.transform(record)
Example #6
0
def test_init():
    transformer = OAITransformer(rules={},
                                 unhandled_paths=set("/path/to/field"))
    assert transformer.rules == {}
    assert transformer.unhandled_paths == set("/path/to/field")
Example #7
0
def test_init_2():
    transformer = OAITransformer()
    assert transformer.rules == {}
    assert transformer.unhandled_paths == set()