示例#1
0
    def make_merger(self, stratname, typename):
        """
        return a Merger instance using a set of strategies having a name
        and a schema for a given type.  

        :param stratname str:  a name for the set of strategies to use.  This 
                               corresponds to a set of schemas that have merge 
                               strategies encoded into them.  
        :param typename  str:  a name for the particular type that the data
                               to be merged conform to.  
        """
        stratdir = os.path.join(self.root, stratname)
        if stratname.startswith('.') or not os.path.exists(stratdir):
            raise MergeError("Strategy convention not recognized: " +
                             stratname)

        cache = ejsl.DirectorySchemaCache(stratdir)

        schemafile = os.path.join(stratdir, "{0}-schema.json".format(typename))
        if not os.path.exists(schemafile):
            raise MergeError("Schema Type name not supported: " + typename)

        with open(schemafile) as fd:
            schema = json.load(fd)

        out = Merger(schema, self.strategies, 'OrderedDict')
        for schema in list(cache.schemas().values()):
            out.cache_schema(schema)

        return out
示例#2
0
 def test_schemas(self, schemafiles):
     sdir = os.path.join(schemafiles.parent, "schemas")
     cache = loader.DirectorySchemaCache(sdir)
     loc = cache.schemas()
     assert loc['http://json-schema.org/draft-04/schema#']['id'] == \
         "http://json-schema.org/draft-04/schema#"
     assert loc['http://mgi.nist.gov/json/registry-resource/v0.1#']['id'] == \
         "http://mgi.nist.gov/json/registry-resource/v0.1#"
示例#3
0
 def test_locations(self, schemafiles):
     sdir = os.path.join(schemafiles.parent, "schemas")
     cache = loader.DirectorySchemaCache(sdir)
     loc = cache.locations()
     assert loc['http://json-schema.org/draft-04/schema'] == \
         os.path.join(sdir, "extern", "json-schema.json")
     assert loc['http://mgi.nist.gov/json/registry-resource/v0.1'] == \
         os.path.join(sdir, "registry-resource_schema.json")
示例#4
0
    def test_recursive(self, schemafiles):
        sdir = schemafiles("schemas")
        cache = loader.DirectorySchemaCache(sdir)

        locs = cache.locations()
        assert len(locs) == 2

        locs = cache.locations(recursive=False)
        assert len(locs) == 1
示例#5
0
    def test_openfile2(self, schemafiles):
        sfile = os.path.join(schemafiles.parent, "schemas", "extern",
                             "json-schema.json")
        assert os.path.exists(sfile)

        cache = loader.DirectorySchemaCache(schemafiles.parent)
        id, schema = cache.open_file(sfile)

        assert id == 'http://json-schema.org/draft-04/schema#'
        assert schema['id'] == id
示例#6
0
    def test_openfile(self, schemafiles):
        sfile = os.path.join(schemafiles.parent, "schemas",
                             "registry-resource_schema.json")
        assert os.path.exists(sfile)

        cache = loader.DirectorySchemaCache(schemafiles.parent)
        id, schema = cache.open_file(sfile)

        assert id == 'http://mgi.nist.gov/json/registry-resource/v0.1#'
        assert schema['id'] == id
示例#7
0
    def test_save(self, schemafiles):
        sdir = os.path.join(schemafiles.parent, "schemas")
        slfile = os.path.join(sdir,"schemaLocation.json")
        if os.path.exists(slfile):
            os.remove(slfile)
        assert not os.path.exists(slfile)

        cache = loader.DirectorySchemaCache(sdir)
        cache.save_locations()

        assert os.path.exists(slfile)
        with open(slfile) as fd:
            loc = json.load(fd)
        assert loc['http://json-schema.org/draft-04/schema'] == \
            os.path.join("extern", "json-schema.json")
        assert loc['http://mgi.nist.gov/json/registry-resource/v0.1'] == \
            "registry-resource_schema.json"
示例#8
0
    def test_save_abs(self, schemafiles):
        slfile = os.path.join(schemafiles.parent, "locations.json")
        if os.path.exists(slfile):
            os.remove(slfile)
        assert not os.path.exists(slfile)

        try:
            cache = loader.DirectorySchemaCache(datadir)
            cache.save_locations(slfile, True)

            assert os.path.exists(slfile)
            with open(slfile) as fd:
                loc = json.load(fd)
            assert loc['file://'+os.path.join(datadir,"noid_schema.json")] == \
                os.path.join(datadir, "noid_schema.json")
            assert len(loc) == 2
        finally:
            if os.path.exists(slfile):
                os.remove(slfile)
示例#9
0
 def test_locs_nota(self):
     cache = loader.DirectorySchemaCache(datadir)
     loc = cache.locations()
     assert "file://" + os.path.join(datadir, "noid_schema.json") in loc
     assert len(loc) == 2
示例#10
0
 def test_notaschema(self):
     cache = loader.DirectorySchemaCache(datadir)
     with pytest.raises(loader.DirectorySchemaCache.NotASchemaError):
         cache.open_file("loc.json")
示例#11
0
 def test_openfile_fileid(self):
     cache = loader.DirectorySchemaCache(datadir)
     (id, schema) = cache.open_file("noid_schema.json")
     assert id == "file://" + os.path.join(datadir, "noid_schema.json")