def restricted_record(app): restricted_collection = Collection( name='Restricted Collection', dbquery='collections.primary:"Restricted Collection"', ) another_restricted_collection = Collection( name='Another Restricted Collection', dbquery='collections.primary:"Another Restricted Collection"', ) db.session.add_all([restricted_collection, another_restricted_collection]) db.session.commit() record = { "$schema": "http://localhost:5000/schemas/records/hep.json", "control_number": 222, "titles": [ { "title": "Supersymmetric gauge field theory and string theory" } ], "collections": [ { "primary": "HEP" }, { "primary": "THESIS" }, { "primary": "Restricted Collection" }, { "primary": "Another Restricted Collection" } ] } record = _create_and_index_record(record) yield record collection = Collection.query.filter_by( name='Restricted Collection' ).one() another_collection = Collection.query.filter_by( name='Another Restricted Collection' ).one() pid = PersistentIdentifier.get('lit', '222') db.session.delete(collection) db.session.delete(another_collection) db.session.delete(pid) record._delete(force=True) current_app.extensions[ 'invenio-db'].versioning_manager.transaction_cls.query.delete() db.session.commit() current_cache.delete('restricted_collections')
def test_query(app): """Test query.""" runner = CliRunner() script_info = ScriptInfo(create_app=lambda info: app) # a # (None) # +------------------+--------------------+ # | | # b e # (None) (title:Test2 OR title:Test3) # +------+-----+ +------------+------------+ # | | | | | # c d f g h # (title:Test0) (title:Test1) (title:Test2) (None) (None) # | | # i j # (title:Test3) (title:Test4) with runner.isolated_filesystem(): with app.app_context(): a = Collection(name="a") b = Collection(name="b", parent=a) e = Collection( name="e", dbquery="title:Test2 OR title:Test3", parent=a) c = Collection(name="c", dbquery="title:Test0", parent=b) d = Collection(name="d", dbquery="title:Test1", parent=b) f = Collection(name="f", dbquery="title:Test2", parent=e) g = Collection(name="g", parent=e) h = Collection(name="h", parent=e) i = Collection(name="i", dbquery="title:Test3", parent=g) j = Collection(name="j", dbquery="title:Test4", parent=h) for c in [a, b, c, d, e, f, g, h, i, j]: db.session.add(c) db.session.commit() result = runner.invoke(cmd, ['query'], obj=script_info) assert 0 != result.exit_code result = runner.invoke(cmd, ['query', 'not-exist'], obj=script_info) assert 0 != result.exit_code colls = {'a': '\n', 'b': '\n', 'c': 'title:Test0\n', 'd': 'title:Test1\n', 'e': 'title:Test2 OR title:Test3\n', 'f': 'title:Test2\n', 'g': '\n', 'h': '\n', 'i': 'title:Test3\n', 'j': 'title:Test4\n'} for (name, query) in iter(colls.items()): result = runner.invoke(cmd, ['query', name], obj=script_info) assert 0 == result.exit_code assert result.output == query
def test_build_cache_with_no_collections(app): """Test database backend.""" with app.test_request_context(): cache = SimpleCache() current_collections.cache = cache schema = { 'type': 'object', 'properties': { 'title': {'type': 'string'}, 'field': {'type': 'boolean'}, 'hello': {'type': 'array'}, }, 'required': ['title'], } # A is creating `record0` with db.session.begin_nested(): record0 = Record.create({'title': 'Test0', '$schema': schema}) assert record0['_collections'] == [] # somewhere, B add new collection `mycoll` with db.session.begin_nested(): mycoll = Collection(name="mycoll", dbquery="title:Test0") db.session.add(mycoll) # reload list of collections record0.commit() # now the collection 'mycoll' should appear in `record0` of A! assert record0['_collections'] == ['mycoll']
def test_view(app): """Test view.""" with app.app_context(): current_collections.unregister_signals() view_url = url_for('invenio_collections.collection') view_test_url = url_for('invenio_collections.collection', name='Test') with app.test_client() as client: res = client.get(view_url) assert res.status_code == 404 with app.app_context(): with db.session.begin_nested(): collection = Collection(name='Test') db.session.add(collection) db.session.commit() assert 1 == collection.id assert 'Collection <id: 1, name: Test, dbquery: None>' == repr( collection) with app.test_client() as client: res = client.get(view_url) assert res.status_code == 200 res = client.get(view_test_url) assert res.status_code == 200 with app.app_context(): current_collections.unregister_signals()
def restricted_record(app): restricted_collection = Collection( name='Restricted Collection', dbquery='_collections:"Restricted Collection"', ) another_restricted_collection = Collection( name='Another Restricted Collection', dbquery='_collections:"Another Restricted Collection"', ) db.session.add_all([restricted_collection, another_restricted_collection]) db.session.commit() record = { '$schema': 'http://localhost:5000/schemas/records/hep.json', '_collections': [ 'Literature', 'Restricted Collection', 'Another Restricted Collection', ], 'control_number': 222, 'document_type': [ 'article', ], 'titles': [{ 'title': 'restricted' }], } record = _create_and_index_record(record) yield record collection = Collection.query.filter_by(name='Restricted Collection').one() another_collection = Collection.query.filter_by( name='Another Restricted Collection').one() pid = PersistentIdentifier.get('lit', '222') db.session.delete(collection) db.session.delete(another_collection) db.session.delete(pid) record._delete(force=True) current_app.extensions[ 'invenio-db'].versioning_manager.transaction_cls.query.delete() db.session.commit() current_cache.delete('restricted_collections')
def load(collections, parent=None): """Create new collection.""" for data in collections or []: collection = Collection(name=data['name'], dbquery=data.get('dbquery'), parent=parent) db.session.add(collection) db.session.flush() load(data.get('children'), parent=collection)
def collection_metadata(db): """Create a bucket.""" col = Collection(name='test') db.session.add(col) db.session.commit() col_meta = CollectionMetadata(collection=col, infos={'description': SAMPLE_DESCRIPTION}) db.session.add(col_meta) db.session.commit() return col_meta
def init_collections(): """Init default collections.""" for collection in current_app.config['INSPIRE_COLLECTIONS_DEFINITION']: c = Collection(name=collection['name'], dbquery=collection['query']) db.session.add(c) db.session.commit()
def _try_populate_collections(): """Try to update collections.""" schema = { 'type': 'object', 'properties': { 'title': { 'type': 'string' }, 'field': { 'type': 'boolean' }, }, 'required': ['title'], } # a # (None) # +------------------+--------------------+ # | | # b e # (None) (title:Test2 OR title:Test3) # +------+-----+ +------------+------------+ # | | | | | # c d f g h # (title:Test0) (title:Test1) (title:Test2) (None) (None) # | | # i j # (title:Test3) (title:Test4)) a = Collection(name="a") b = Collection(name="b", parent=a) e = Collection(name="e", dbquery="title:Test2 OR title:Test3", parent=a) c = Collection(name="c", dbquery="title:Test0", parent=b) d = Collection(name="d", dbquery="title:Test1", parent=b) f = Collection(name="f", dbquery="title:Test2", parent=e) g = Collection(name="g", parent=e) h = Collection(name="h", parent=e) i = Collection(name="i", dbquery="title:Test3", parent=g) j = Collection(name="j", dbquery="title:Test4", parent=h) with db.session.begin_nested(): for coll in [a, b, c, d, e, f, g, h, i, j]: db.session.add(coll) db.session.commit() # start tests record0 = Record.create({'title': 'Test0', '$schema': schema}) assert 'a' in record0['_collections'] assert 'c' in record0['_collections'] assert 'b' in record0['_collections'] assert len(record0['_collections']) == 3 record = Record.create({'title': 'TestNotFound', '$schema': schema}) assert record['_collections'] == [] record = Record.create({'title': 'Test1', '$schema': schema}) assert 'a' in record['_collections'] assert 'b' in record['_collections'] assert 'd' in record['_collections'] assert len(record['_collections']) == 3 record = Record.create({'title': 'Test2', '$schema': schema}) assert 'a' in record['_collections'] assert 'e' in record['_collections'] assert 'f' in record['_collections'] assert len(record['_collections']) == 3 record3 = Record.create({'title': 'Test3', '$schema': schema}) assert 'a' in record3['_collections'] assert 'e' in record3['_collections'] assert 'g' in record3['_collections'] assert 'i' in record3['_collections'] assert len(record3['_collections']) == 4 record4 = Record.create({'title': 'Test4', '$schema': schema}) assert 'h' in record4['_collections'] assert 'j' in record4['_collections'] assert len(record4['_collections']) == 2 # test delete db.session.delete(j) db.session.commit() record4.commit() assert 'h' not in record4['_collections'] assert 'j' not in record4['_collections'] assert len(record4['_collections']) == 0 # test update dbquery i.dbquery = None db.session.add(i) db.session.commit() record3.commit() assert 'a' in record3['_collections'] assert 'e' in record3['_collections'] assert len(record3['_collections']) == 2 # test update dbquery i.dbquery = 'title:Test3' db.session.add(i) db.session.commit() record3.commit() assert 'a' in record3['_collections'] assert 'e' in record3['_collections'] assert 'g' in record3['_collections'] assert 'i' in record3['_collections'] assert len(record3['_collections']) == 4 # test update name a.name = "new-a" db.session.add(a) db.session.commit() record3.commit() assert 'g' in record3['_collections'] assert 'i' in record3['_collections'] assert 'new-a' in record3['_collections'] assert 'e' in record3['_collections'] assert len(record3['_collections']) == 4 # test update name c.name = "new-c" db.session.add(c) db.session.commit() record0.commit() assert 'new-a' in record0['_collections'] assert 'new-c' in record0['_collections'] assert 'b' in record0['_collections'] assert len(record0['_collections']) == 3
def test_delete(app): """Test CLI delete.""" runner = CliRunner() script_info = ScriptInfo(create_app=lambda info: app) # a # (None) # +------------------+--------------------+ # | | # b e # (None) (title:Test2 OR title:Test3) # +------+-----+ +------------+------------+ # | | | | | # c d f g h # (title:Test0) (title:Test1) (title:Test2) (None) (None) # | | # i j # (title:Test3) (title:Test4) with runner.isolated_filesystem(): with app.app_context(): a = Collection(name="a") b = Collection(name="b", parent=a) e = Collection( name="e", dbquery="title:Test2 OR title:Test3", parent=a) c = Collection(name="c", dbquery="title:Test0", parent=b) d = Collection(name="d", dbquery="title:Test1", parent=b) f = Collection(name="f", dbquery="title:Test2", parent=e) g = Collection(name="g", parent=e) h = Collection(name="h", parent=e) i = Collection(name="i", dbquery="title:Test3", parent=g) j = Collection(name="j", dbquery="title:Test4", parent=h) for c in [a, b, c, d, e, f, g, h, i, j]: db.session.add(c) db.session.commit() result = runner.invoke(cmd, ['delete'], obj=script_info) assert 0 != result.exit_code result = runner.invoke(cmd, ['delete', 'not-exist'], obj=script_info) assert 0 != result.exit_code with app.app_context(): result = runner.invoke(cmd, ['delete', 'a', '-n'], obj=script_info) assert 0 == result.exit_code for coll in ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j']: assert Collection.query.filter_by( name=coll).first() is not None db.session.expunge_all() result = runner.invoke(cmd, ['delete', 'j'], obj=script_info) assert 0 == result.exit_code db.session.expunge_all() for coll in ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i']: assert Collection.query.filter_by( name=coll).first() is not None assert Collection.query.filter_by(name='j').first() is None result = runner.invoke(cmd, ['delete', 'e'], obj=script_info) assert 0 == result.exit_code for coll in ['a', 'b', 'c', 'd']: assert Collection.query.filter_by( name=coll).first() is not None for coll in ['e', 'f', 'g', 'h', 'i', 'j']: assert Collection.query.filter_by(name=coll).first() is None result = runner.invoke(cmd, ['delete', 'c', '-v'], obj=script_info) assert 0 == result.exit_code assert Collection.query.filter_by(name='c').first() is None
def test_path(app): """Test CLI path.""" runner = CliRunner() script_info = ScriptInfo(create_app=lambda info: app) # a # (None) # +------------------+--------------------+ # | | # b e # (None) (title:Test2 OR title:Test3) # +------+-----+ +------------+------------+ # | | | | | # c d f g h # (title:Test0) (title:Test1) (title:Test2) (None) (None) # | | # i j # (title:Test3) (title:Test4) with runner.isolated_filesystem(): with app.app_context(): a = Collection(name="a") b = Collection(name="b", parent=a) e = Collection( name="e", dbquery="title:Test2 OR title:Test3", parent=a) c = Collection(name="c", dbquery="title:Test0", parent=b) d = Collection(name="d", dbquery="title:Test1", parent=b) f = Collection(name="f", dbquery="title:Test2", parent=e) g = Collection(name="g", parent=e) h = Collection(name="h", parent=e) i = Collection(name="i", dbquery="title:Test3", parent=g) j = Collection(name="j", dbquery="title:Test4", parent=h) for c in [a, b, c, d, e, f, g, h, i, j]: db.session.add(c) db.session.commit() result = runner.invoke(cmd, ['path'], obj=script_info) assert 0 != result.exit_code result = runner.invoke(cmd, ['path', 'not-exist'], obj=script_info) assert 0 != result.exit_code result = runner.invoke(cmd, ['path', 'a'], obj=script_info) assert 0 == result.exit_code aspected = ( "Collection <id: 1, name: a, dbquery: None>\n" ) assert result.output == aspected result = runner.invoke(cmd, ['path', 'e'], obj=script_info) assert 0 == result.exit_code aspected = ( "Collection <id: 1, name: a, dbquery: None>\n " "+-- Collection <id: 3, name: e, dbquery: " "title:Test2 OR title:Test3>\n" ) assert result.output == aspected result = runner.invoke(cmd, ['path', 'g'], obj=script_info) assert 0 == result.exit_code aspected = ( "Collection <id: 1, name: a, dbquery: None>\n " "+-- Collection <id: 3, name: e, dbquery: " "title:Test2 OR title:Test3>\n " " +-- Collection <id: 7, name: g, dbquery: None>\n" ) assert result.output == aspected result = runner.invoke(cmd, ['path', 'i'], obj=script_info) assert 0 == result.exit_code aspected = ( "Collection <id: 1, name: a, dbquery: None>\n " "+-- Collection <id: 3, name: e, dbquery: " "title:Test2 OR title:Test3>\n " " +-- Collection <id: 7, name: g, dbquery: None>\n " " +-- Collection <id: 9, name: i, dbquery: title:Test3>\n" ) assert result.output == aspected