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_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_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_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_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_open_invalid_config():
    cut = BundleDependencyStore()
    with raises(ValueError):
        cut.open(object())
def test_open_dict_missing_conf():
    cut = BundleDependencyStore()
    with raises(ValueError):
        cut.open({'type': 'doesntmatter'})
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))