def test_len_with_ctx_excluded1():
    iom = IOMemory()
    iom.add((URIRef('http://example.org/a'), URIRef('http://example.org/b'),
             URIRef('http://example.org/c')),
            context='http://example.org/ctx')
    bds = BundleDependencyStore(iom, excludes=set(['http://example.org/ctx']))
    assert 0 == bds.__len__('http://example.org/ctx')
def test_open_plugin_missing():
    with patch('owmeta_core.bundle_dependency_store._is_cacheable'), \
          patch('owmeta_core.bundle_dependency_store.plugin') as plugin:
        plugin.get.side_effect = PluginException
        cut = BundleDependencyStore()
        with raises(PluginException):
            cut.open({'conf': 'doesntmatter', 'type': 'doesntmatter'})
def test_includes_triples():
    iom = IOMemory()
    iom.add((URIRef('http://example.org/a'), URIRef('http://example.org/b'),
             URIRef('http://example.org/c')),
            context='http://example.org/ctx')
    bds = BundleDependencyStore(iom)
    assert 1 == sum(1 for _ in bds.triples((None, None, None)))
def test_excludes_contexts():
    iom = IOMemory()
    iom.add((URIRef('http://example.org/a'), URIRef('http://example.org/b'),
             URIRef('http://example.org/c')),
            context='http://example.org/ctx')
    bds = BundleDependencyStore(iom, excludes=set(['http://example.org/ctx']))
    assert set([]) == set(bds.contexts())
def test_not_cacheable():
    with patch('owmeta_core.bundle_dependency_store._is_cacheable') as is_cacheable, \
          patch('owmeta_core.bundle_dependency_store.plugin') as plugin:
        is_cacheable.return_value = False
        cut = BundleDependencyStore()
        cut.open(('StoreKey', 'StoreConf'))

        plugin.get.assert_called()
        assert cut.wrapped == plugin.get()()
def test_bds_reuse():
    with patch('owmeta_core.bundle_dependency_store._is_cacheable') as is_cacheable, \
          patch('owmeta_core.bundle_dependency_store._cache_key'), \
          patch('owmeta_core.bundle_dependency_store.plugin'):
        cut = BundleDependencyStore()
        store_cache = MagicMock()
        conf = dict(type='StoreKey', conf='StoreConf', cache=store_cache)
        cut.open(conf)
        is_cacheable.assert_called_with(RDFLIB_PLUGIN_KEY, conf)
        assert cut.wrapped == store_cache.get()
def test_excludes_all_for_excluded_context():
    iom = IOMemory()
    iom.add((URIRef('http://example.org/a'), URIRef('http://example.org/b'),
             URIRef('http://example.org/c')),
            context='http://example.org/ctx')
    iom.add((URIRef('http://example.org/a'), URIRef('http://example.org/b'),
             URIRef('http://example.org/c')),
            context='http://example.org/ctx1')
    bds = BundleDependencyStore(iom, excludes=set(['http://example.org/ctx']))
    assert 0 == sum(1 for _ in bds.triples((None, None, None),
                                           context='http://example.org/ctx'))
def test_triples_contexts():
    iom = IOMemory()
    ctx = 'http://example.org/ctx'
    ctx1 = 'http://example.org/ctx1'
    iom.add((URIRef('http://example.org/a'), URIRef('http://example.org/b'),
             URIRef('http://example.org/c')),
            context=ctx)
    iom.add((URIRef('http://example.org/a'), URIRef('http://example.org/b'),
             URIRef('http://example.org/c')),
            context=ctx1)
    bds = BundleDependencyStore(iom)
    for t, ctxs in bds.triples((None, None, None)):
        assert set([ctx, ctx1]) == set(ctxs)
def test_triples_choices_excluded():
    iom = IOMemory()
    iom.add((URIRef('http://example.org/a'), URIRef('http://example.org/b'),
             URIRef('http://example.org/c')),
            context='http://example.org/ctx')
    iom.add((URIRef('http://example.org/e'), URIRef('http://example.org/b'),
             URIRef('http://example.org/d')),
            context='http://example.org/ctx')
    bds = BundleDependencyStore(iom, excludes=set(['http://example.org/ctx']))
    assert set() == set(
        bds.triples_choices(
            (None, None,
             [URIRef('http://example.org/c'),
              URIRef('http://example.org/d')])))
def test_cached_store_created_when_cacheable():
    with patch('owmeta_core.bundle_dependency_store._is_cacheable') as is_cacheable, \
          patch('owmeta_core.bundle_dependency_store._cache_key') as cache_key, \
          patch('owmeta_core.bundle_dependency_store.plugin') as plugin:
        is_cacheable.return_value = True
        cache_key.return_value = 'cache_key'
        cut = BundleDependencyStore()
        store_cache = MagicMock()
        store_cache.get.return_value = None
        cut.open(dict(type='StoreKey', conf='StoreConf', cache=store_cache))

        # test_use_cached_store_no_plugin
        plugin.get.assert_called()

        # test_cached_sought
        store_cache.get.assert_called()

        # test_cached_not_used
        assert cut.wrapped == plugin.get()()
def test_pure_pass_throughs():
    wrapped = Mock(name='wrapped')
    cut = BundleDependencyStore(wrapped)

    cut.prefix('blah')
    wrapped.prefix.assert_called_with('blah')

    cut.namespace('blah')
    wrapped.namespace.assert_called_with('blah')

    cut.gc()
    wrapped.gc.assert_called()
def test_len_some_excludes():
    iom = IOMemory()
    iom.add((URIRef('http://example.org/a'), URIRef('http://example.org/b'),
             URIRef('http://example.org/c')),
            context='http://example.org/ctx1')
    iom.add((URIRef('http://example.org/a'), URIRef('http://example.org/b'),
             URIRef('http://example.org/c')),
            context='http://example.org/ctx2')
    iom.add((URIRef('http://example.org/a'), URIRef('http://example.org/b'),
             URIRef('http://example.org/c')),
            context='http://example.org/ctx3')
    bds = BundleDependencyStore(iom, excludes=set(['http://example.org/ctx3']))
    assert 1 == len(bds)
def test_unimplemented():
    unimplemented_methods = [
        'add',
        'addN',
        'remove',
        'add_graph',
        'remove_graph',
        'create',
        'destroy',
        'commit',
        'rollback',
    ]
    cut = BundleDependencyStore()
    for method_name in unimplemented_methods:
        with raises(NotImplementedError):
            getattr(cut, method_name)()
def test_empty_contexts_with_excludes():
    iom = IOMemory()
    bds = BundleDependencyStore(iom, excludes=set(['http://example.org/ctx']))
    assert set([]) == set(bds.contexts())
def test_repr_contains_wrapped():
    cut = BundleDependencyStore('wrapped')
    assert repr('wrapped') in repr(cut)
def test_open_open_close_query(tempdir):
    bds1 = BundleDependencyStore()
    bds2 = BundleDependencyStore()
    store_cache = StoreCache()
    store = plugin_get('FileStorageZODB', Store)()
    store.open(p(tempdir, 'db.fs'))
    trip = (URIRef('http://example.org/a'), URIRef('http://example.org/b'),
            URIRef('http://example.org/c'))
    store.add(trip, context=None)
    store.close()

    conf = dict(type='FileStorageZODB',
                conf={
                    'read_only': True,
                    'url': p(tempdir, 'db.fs')
                },
                cache=store_cache)

    print("OPEN BDS 1")
    bds1.open(conf)
    print("OPEN BDS 2")
    bds2.open(conf)

    print("CLOSE BDS 1")
    bds1.close()

    assert list(bds2.triples((None, None, None)))[0][0] == trip
def test_open_open_close_close_2(tempdir):
    '''
    A cached store, once opened, cannot be closed unilaterally by a BDS holdidng a
    reference to that store. Consequently, we must prevent closing of the store. However,
    calling 'close' on the store must remain an allowed operation (i.e., it must not raise
    an exception) so that the sharing of the store remains transparent to the BDS user.
    '''
    bds1 = BundleDependencyStore()
    bds2 = BundleDependencyStore()
    store_cache = StoreCache()
    store = plugin_get('FileStorageZODB', Store)()
    store.open(p(tempdir, 'db.fs'))
    store.close()

    conf = dict(type='FileStorageZODB',
                conf={
                    'read_only': True,
                    'url': p(tempdir, 'db.fs')
                },
                cache=store_cache)

    print("OPEN BDS 1")
    bds1.open(conf)
    print("OPEN BDS 2")
    bds2.open(conf)

    print("CLOSE BDS 2")
    bds2.close()
    print("CLOSE BDS 1")
    bds1.close()
def test_open_overlong_list():
    cut = BundleDependencyStore()
    with raises(ValueError):
        cut.open([1, 2, 3])
def test_open_overlong_tuple():
    cut = BundleDependencyStore()
    with raises(ValueError):
        cut.open((1, 2, 3))
def test_open_dict_missing_conf():
    cut = BundleDependencyStore()
    with raises(ValueError):
        cut.open({'type': 'doesntmatter'})
def test_empty_contexts_without_excludes():
    iom = IOMemory()
    bds = BundleDependencyStore(iom)
    assert set([]) == set(bds.contexts())
def test_open_invalid_config():
    cut = BundleDependencyStore()
    with raises(ValueError):
        cut.open(object())