diff --git a/modules/weko-search-ui/tests/conftest.py b/modules/weko-search-ui/tests/conftest.py index 16a93d50bf..1d142127e5 100644 --- a/modules/weko-search-ui/tests/conftest.py +++ b/modules/weko-search-ui/tests/conftest.py @@ -102,6 +102,7 @@ from invenio_records_rest.views import create_blueprint_from_app from invenio_records_ui import InvenioRecordsUI from invenio_records_ui.config import RECORDS_UI_ENDPOINTS +from invenio_records_ui.views import create_blueprint_from_app as invenio_records_ui_create_blueprint from invenio_rest import InvenioREST from invenio_search import InvenioSearch, RecordsSearch, current_search, current_search_client from invenio_stats import InvenioStats @@ -134,6 +135,7 @@ from weko_deposit.api import WekoDeposit as aWekoDeposit from weko_deposit.api import WekoIndexer, WekoRecord from weko_deposit.config import ( + PIDRELATIONS_RELATION_TYPES, WEKO_BUCKET_QUOTA_SIZE, WEKO_MAX_FILE_SIZE, WEKO_DEPOSIT_FILESIZE_LIMIT, @@ -289,7 +291,7 @@ def base_app(instance_path, search_class, request): WEKO_SCHEMA_JPCOAR_V2_SCHEMA_NAME = 'jpcoar_mapping', WEKO_SCHEMA_DDI_SCHEMA_NAME = "ddi_mapping", INDEXER_FILE_DOC_TYPE="content", - INDEXER_DEFAULT_INDEX="{}-weko-item-v1.0.0".format("test"), + INDEXER_DEFAULT_INDEX="weko-item-v1.0.0", INDEX_IMG="indextree/36466818-image.jpg", # SQLALCHEMY_DATABASE_URI=os.getenv('SQLALCHEMY_DATABASE_URI', # 'postgresql+psycopg2://invenio:dbpass123@postgresql:5432/wekotest'), @@ -318,7 +320,7 @@ def base_app(instance_path, search_class, request): }, FILES_REST_OBJECT_KEY_MAX_LEN=255, # SEARCH_UI_SEARCH_INDEX=SEARCH_UI_SEARCH_INDEX, - SEARCH_UI_SEARCH_INDEX="test-weko", + SEARCH_UI_SEARCH_INDEX="weko", CHILD_INDEX_THUMBNAIL_WIDTH = CHILD_INDEX_THUMBNAIL_WIDTH, CHILD_INDEX_THUMBNAIL_HEIGHT = CHILD_INDEX_THUMBNAIL_HEIGHT, # SEARCH_OPENSEARCH_HOSTS=os.environ.get("INVENIO_ELASTICSEARCH_HOST"), @@ -367,7 +369,7 @@ def base_app(instance_path, search_class, request): fields=["year"], ) ), - "test-weko": { + "weko": { "test-weko": {"fields": [1,2,3], "nested": 1}, 'controlnumber': {'title': 'ID', 'fields': ['control_number'], 'default_order': 'asc', 'order': 2,} }, @@ -627,9 +629,8 @@ def base_app(instance_path, search_class, request): pid_fetcher="recid", pid_value="1.0", search_class=RecordsSearch, - # search_index="test-weko", # search_index=SEARCH_UI_SEARCH_INDEX, - search_index="test-weko", + search_index="weko", search_type="item-v1.0.0", search_factory_imp="weko_search_ui.query.weko_search_factory", # record_class='', @@ -695,7 +696,8 @@ def base_app(instance_path, search_class, request): "report part": "other", "conference object": "conference output", }, - WEKO_COMMUNITIES_DEFAULT_PROPERTIES=WEKO_COMMUNITIES_DEFAULT_PROPERTIES + WEKO_COMMUNITIES_DEFAULT_PROPERTIES=WEKO_COMMUNITIES_DEFAULT_PROPERTIES, + PIDRELATIONS_RELATION_TYPES=PIDRELATIONS_RELATION_TYPES, ) app_.url_map.converters["pid"] = PIDConverter app_.config["RECORDS_REST_ENDPOINTS"]["recid"]["search_class"] = search_class @@ -759,6 +761,9 @@ def base_app(instance_path, search_class, request): app_.register_blueprint(weko_theme_blueprint) from invenio_communities.views.ui import blueprint as invenio_communities_blueprint app_.register_blueprint(invenio_communities_blueprint) + app_.register_blueprint( + invenio_records_ui_create_blueprint(app_) + ) current_assets = LocalProxy(lambda: app_.extensions["invenio-assets"]) current_assets.collect.collect() @@ -886,17 +891,6 @@ def client_request_args_FULL_TEXT(app, file_instance_mock): ) yield r -@pytest.fixture() -def location(app, db): - """Create default location.""" - tmppath = tempfile.mkdtemp() - with db.session.begin_nested(): - Location.query.delete() - loc = Location(name="local", uri=tmppath, default=True) - db.session.add(loc) - db.session.commit() - return location - @pytest.fixture() def user(app, db): @@ -1202,7 +1196,7 @@ def es_authors_index(app): @pytest.fixture() -def db_records(db, instance_path, users): +def db_records(db, instance_path, users, user_activity_log_partition_table): with db.session.begin_nested(): Location.query.delete() loc = Location(name="local", uri=instance_path, default=True) @@ -1274,7 +1268,7 @@ def db_records2(db, instance_path, users): @pytest.fixture() -def db_records3(db): +def db_records3(db, location): record_data = json_data("data/test_records2.json") item_data = json_data("data/test_items2.json") record_num = len(record_data) @@ -1825,23 +1819,6 @@ def create_file_instance(db): return file_path -@pytest.yield_fixture() -def open_search(app): - """Provide OpenSearch access, create and clean indices. - - Don't create template so that the test or another fixture can modify the - enabled events. - """ - current_search_client.indices.delete(index="*") - current_search_client.indices.delete_template("*") - list(current_search.create()) - try: - yield current_search_client - finally: - current_search_client.indices.delete(index="*") - current_search_client.indices.delete_template("*") - - def generate_events( app, index_id="33", @@ -2413,20 +2390,6 @@ def item_render(): return data -@pytest.yield_fixture() -def open_search(app): - """OpenSearch fixture.""" - try: - list(current_search.create()) - # except RequestError: - except: - list(current_search.delete(ignore=[404])) - list(current_search.create(ignore=[400])) - current_search_client.indices.refresh() - yield current_search_client - list(current_search.delete(ignore=[404])) - - @pytest.fixture() def deposit(app, open_search, users, location, db): """New deposit with files.""" @@ -2442,7 +2405,7 @@ def deposit(app, open_search, users, location, db): @pytest.fixture() -def db_index(client, users): +def db_index(client, users, user_activity_log_partition_table): index_metadata = { "id": 1, "parent": 0, @@ -2959,7 +2922,7 @@ def search_records2(app, db, db_index, location, db_itemtype, db_oaischema): return {"indexer": indexer, "results": results} @pytest.fixture() -def indextree(app, client, users): +def indextree(app, client, users, user_activity_log_partition_table): from weko_index_tree.api import Indexes index_metadata = { @@ -3165,20 +3128,22 @@ def record_indexer_receiver(app, json=None, record=None, index=None, return json - - -@pytest.yield_fixture() +@pytest.fixture def open_search(app): """OpenSearch fixture.""" try: + # delete alias to avoid "Invalid alias name [test-*] is already used as a concrete index" error + current_search_client.indices.delete_alias( + name="test-*", index="weko-*", ignore=[404, 400] + ) current_search_client.indices.delete(index="test-*") - list(current_search.create()) + current_search.create() except RequestError: - list(current_search.delete(ignore=[404])) - list(current_search.create(ignore=[400])) + current_search.delete(ignore=[404]) + current_search.create(ignore=[400]) current_search_client.indices.refresh() yield current_search_client - list(current_search.delete(ignore=[404])) + current_search.delete(ignore=[404]) @pytest.yield_fixture() @@ -4340,6 +4305,8 @@ def factory(id,datas): version_id=1, is_deleted=False, ) + with db.session.begin_nested(): + db.session.add(item_type) if "mapping" in datas: item_type_mapping = dict() @@ -4348,8 +4315,6 @@ def factory(id,datas): item_type_mapping = ItemTypeMapping(id=id, item_type_id=id, mapping=item_type_mapping) db.session.add(item_type_mapping) result["item_type_mapping"] = item_type_mapping - with db.session.begin_nested(): - db.session.add(item_type) db.session.commit() result["item_type_name"] = item_type_name @@ -4444,24 +4409,6 @@ def sample_config(app, db): return source_name -@pytest.fixture() -def location(app, db): - """Create default location.""" - tmppath = tempfile.mkdtemp() - - location = Location.query.filter_by(name="testloc").count() - if location != 1: - loc = Location(name="testloc", uri=tmppath, default=True) - db.session.add(loc) - db.session.commit() - else: - loc = Location.query.filter_by(name="testloc").first() - - yield loc - - shutil.rmtree(tmppath) - - @pytest.fixture() def harvest_setting(app, db, test_indices): setting_list = [] @@ -4607,11 +4554,15 @@ def location(app, db): """Create default location.""" tmppath = tempfile.mkdtemp() with db.session.begin_nested(): + # Delete existing locations to ensure a clean state Location.query.delete() - loc = Location(name='local', uri=tmppath, default=True) + loc = Location(name="local", uri=tmppath, default=True) db.session.add(loc) db.session.commit() - return loc + + yield loc + # Ensure the temporary directory is removed after the test + shutil.rmtree(tmppath) @pytest.fixture() @@ -4648,11 +4599,15 @@ def db_itemtype_jpcoar(app, db): is_deleted=False, ) - item_type_multiple_mapping = ItemTypeMapping(id=10, item_type_id=10, mapping=item_type_multiple_mapping) - with db.session.begin_nested(): db.session.add(item_type_multiple_name) db.session.add(item_type_multiple) + + item_type_multiple_mapping = ItemTypeMapping( + id=10, item_type_id=item_type_multiple.id, mapping=item_type_multiple_mapping + ) + + with db.session.begin_nested(): db.session.add(item_type_multiple_mapping) db.session.commit() @@ -4786,3 +4741,29 @@ def ro_crate(): shutil.make_archive(zip_path.replace(".zip", ""), 'zip', "tests/data/zip_crate/") yield zip_path shutil.rmtree(temp_dir) + + +@pytest.fixture +def user_activity_log_partition_table(app, db): + """Create user activity log partition.""" + # Create partition for current month + now = datetime.now() + start = now.date().replace(day=1) + end = (start + timedelta(days=31)).replace(day=1) + partition_name = f"user_activity_logs_{now.year}_{now.month:02d}" + create_partition_sql = f""" + CREATE TABLE IF NOT EXISTS {partition_name} + PARTITION OF user_activity_logs + FOR VALUES FROM ('{start}') TO ('{end}'); + """ + + with db.session.begin_nested(): + db.session.execute(create_partition_sql) + db.session.commit() + + +@pytest.fixture +def without_remove_session(app): + """Fixture to temporarily disable the removal of the database session.""" + with patch("weko_workflow.views.db.session.remove"): + yield diff --git a/modules/weko-search-ui/tests/data/item-v1.0.0.json b/modules/weko-search-ui/tests/data/item-v1.0.0.json index 5f1817ad97..540fa2e216 100644 --- a/modules/weko-search-ui/tests/data/item-v1.0.0.json +++ b/modules/weko-search-ui/tests/data/item-v1.0.0.json @@ -3,6 +3,8 @@ "number_of_shards": 1, "number_of_replicas": 1, "index.mapping.total_fields.limit": 50000, + "refresh_interval": "1s", + "index.max_ngram_diff": 2, "analysis": { "tokenizer": { "ja_tokenizer": { @@ -59,7 +61,6 @@ ], "tokenizer": "standard", "filter": [ - "standard", "lowercase", "stop", "cjk_width" @@ -72,1150 +73,1147 @@ } }, "mappings": { - "item-v1.0.0": { - "properties": { - "path": { - "type": "keyword", - "index": true, - "fields": { - "tree": { - "type": "text", - "fielddata": true, - "analyzer": "paths" - } + "properties": { + "path": { + "type": "keyword", + "index": true, + "fields": { + "tree": { + "type": "text", + "fielddata": true, + "analyzer": "paths" + } + } + }, + "item_type_id": { + "type": "keyword", + "index": true + }, + "itemtype": { + "type": "text", + "fields": { + "keyword": { + "type": "keyword", + "ignore_above": 256 } }, - "item_type_id": { - "type": "keyword", - "index": true - }, - "itemtype": { - "type": "text", - "fields": { - "keyword": { - "type": "keyword", - "ignore_above": 256 - } + "fielddata": true, + "copy_to": [ + "search_other" + ] + }, + "publish_status": { + "type": "keyword", + "index": true + }, + "publish_date": { + "type": "date", + "format": "yyyy-MM-dd||yyyy-MM||yyyy" + }, + "_created": { + "type": "date" + }, + "_updated": { + "type": "date" + }, + "_oai": { + "type": "object", + "properties": { + "id": { + "type": "keyword", + "index": true }, - "fielddata": true, - "copy_to": [ - "search_other" - ] - }, - "publish_status": { - "type": "keyword", - "index": true - }, - "publish_date": { - "type": "date", - "format": "yyyy-MM-dd||yyyy-MM||yyyy" - }, - "_created": { - "type": "date" - }, - "_updated": { + "sets": { + "type": "keyword", + "index": true + }, + "updated": { "type": "date" - }, - "_oai": { - "type": "object", - "properties": { - "id": { - "type": "keyword", - "index": true - }, - "sets": { - "type": "keyword", - "index": true - }, - "updated": { - "type": "date" - } } - }, - "control_number": { - "type": "keyword", - "index": true - }, - "title": { - "type": "keyword", - "index": true, - "copy_to": [ - "search_title" - ] - }, - "feedback_mail_list": { - "type": "nested", - "properties": { - "author_id": { - "type": "keyword", - "index": true - }, - "email": { - "type": "keyword", - "index": true - } + } + }, + "control_number": { + "type": "keyword", + "index": true + }, + "title": { + "type": "keyword", + "index": true, + "copy_to": [ + "search_title" + ] + }, + "feedback_mail_list": { + "type": "nested", + "properties": { + "author_id": { + "type": "keyword", + "index": true + }, + "email": { + "type": "keyword", + "index": true } - }, - "alternative": { - "type": "keyword", - "index": true, - "copy_to": [ - "search_title" - ] - }, - "creator": { - "type": "object", - "properties": { - "nameIdentifier": { - "type": "keyword", - "index": true, - "copy_to": [ - "search_creator" - ] - }, - "creatorName": { - "type": "keyword", - "index": true, - "copy_to": [ - "search_creator" - ] - }, - "familyName": { - "type": "keyword", - "index": true, - "copy_to": [ - "search_creator" - ] - }, - "givenName": { - "type": "keyword", - "index": true, - "copy_to": [ - "search_creator" - ] - }, - "creatorAlternative": { - "type": "keyword", - "index": true, - "copy_to": [ - "search_creator" - ] - }, - "affiliation": { - "type": "object", - "properties": { - "nameIdentifier": { - "type": "keyword", - "index": true, - "copy_to": [ - "search_identifier" - ] - }, - "affiliationName": { - "type": "keyword", - "index": true, - "copy_to": [ - "search_other" - ] - } + } + }, + "alternative": { + "type": "keyword", + "index": true, + "copy_to": [ + "search_title" + ] + }, + "creator": { + "type": "object", + "properties": { + "nameIdentifier": { + "type": "keyword", + "index": true, + "copy_to": [ + "search_creator" + ] + }, + "creatorName": { + "type": "keyword", + "index": true, + "copy_to": [ + "search_creator" + ] + }, + "familyName": { + "type": "keyword", + "index": true, + "copy_to": [ + "search_creator" + ] + }, + "givenName": { + "type": "keyword", + "index": true, + "copy_to": [ + "search_creator" + ] + }, + "creatorAlternative": { + "type": "keyword", + "index": true, + "copy_to": [ + "search_creator" + ] + }, + "affiliation": { + "type": "object", + "properties": { + "nameIdentifier": { + "type": "keyword", + "index": true, + "copy_to": [ + "search_identifier" + ] + }, + "affiliationName": { + "type": "keyword", + "index": true, + "copy_to": [ + "search_other" + ] } } } - }, - "contributor": { - "type": "object", - "properties": { - "@attributes": { - "type": "object", - "properties": { - "contributorType": { - "type": "keyword" - } + } + }, + "contributor": { + "type": "object", + "properties": { + "@attributes": { + "type": "object", + "properties": { + "contributorType": { + "type": "keyword" } - }, - "nameIdentifier": { - "type": "keyword", - "index": true, - "copy_to": [ - "search_contributor" - ] - }, - "contributorName": { - "type": "keyword", - "index": true, - "copy_to": [ - "search_contributor" - ] - }, - "familyName": { - "type": "keyword", - "index": true, - "copy_to": [ - "search_contributor" - ] - }, - "givenName": { - "type": "keyword", - "index": true, - "copy_to": [ - "search_contributor" - ] - }, - "contributorAlternative": { - "type": "keyword", - "index": true, - "copy_to": [ - "search_contributor" - ] - }, - "affiliation": { - "type": "object", - "properties": { - "nameIdentifier": { - "type": "keyword", - "index": true, - "copy_to": [ - "search_identifier" - ] - }, - "affiliationName": { - "type": "keyword", - "index": true, - "copy_to": [ - "search_other" - ] - } + } + }, + "nameIdentifier": { + "type": "keyword", + "index": true, + "copy_to": [ + "search_contributor" + ] + }, + "contributorName": { + "type": "keyword", + "index": true, + "copy_to": [ + "search_contributor" + ] + }, + "familyName": { + "type": "keyword", + "index": true, + "copy_to": [ + "search_contributor" + ] + }, + "givenName": { + "type": "keyword", + "index": true, + "copy_to": [ + "search_contributor" + ] + }, + "contributorAlternative": { + "type": "keyword", + "index": true, + "copy_to": [ + "search_contributor" + ] + }, + "affiliation": { + "type": "object", + "properties": { + "nameIdentifier": { + "type": "keyword", + "index": true, + "copy_to": [ + "search_identifier" + ] + }, + "affiliationName": { + "type": "keyword", + "index": true, + "copy_to": [ + "search_other" + ] } } } - }, - "accessRights": { - "type": "keyword", - "index": true, - "copy_to": [ - "search_other" - ] - }, - "apc": { - "type": "text", - "index": true, - "copy_to": [ - "search_other" - ] - }, - "rights": { - "type": "text", - "copy_to": [ - "search_other" - ] - }, - "rightsHolder": { - "type": "object", - "properties": { - "nameIdentifier": { - "type": "keyword", - "index": true, - "copy_to": [ - "search_identifier" - ] - }, - "rightsHolderName": { - "type": "keyword", - "index": true, - "copy_to": [ - "search_other" - ] - } + } + }, + "accessRights": { + "type": "keyword", + "index": true, + "copy_to": [ + "search_other" + ] + }, + "apc": { + "type": "text", + "index": true, + "copy_to": [ + "search_other" + ] + }, + "rights": { + "type": "text", + "copy_to": [ + "search_other" + ] + }, + "rightsHolder": { + "type": "object", + "properties": { + "nameIdentifier": { + "type": "keyword", + "index": true, + "copy_to": [ + "search_identifier" + ] + }, + "rightsHolderName": { + "type": "keyword", + "index": true, + "copy_to": [ + "search_other" + ] } - }, - "subject": { - "type": "object", - "properties": { - "value": { - "type": "keyword", - "copy_to": [ - "search_other" - ] - }, - "subjectScheme": { - "type": "keyword", - "index": true, - "copy_to": [ - "search_attr" - ] - } + } + }, + "subject": { + "type": "object", + "properties": { + "value": { + "type": "keyword", + "copy_to": [ + "search_other" + ] + }, + "subjectScheme": { + "type": "keyword", + "index": true, + "copy_to": [ + "search_attr" + ] } - }, - "description": { - "type": "object", - "properties": { - "value": { - "type": "keyword", - "ignore_above": 256, - "copy_to": [ - "search_des" - ] - }, - "descriptionType": { - "type": "keyword" - } + } + }, + "description": { + "type": "object", + "properties": { + "value": { + "type": "keyword", + "ignore_above": 256, + "copy_to": [ + "search_des" + ] + }, + "descriptionType": { + "type": "keyword" } - }, - "publisher": { - "type": "text", - "copy_to": [ - "search_publisher" - ] - }, - "date": { - "type": "nested", - "properties": { - "dateType": { - "type": "keyword", - "index": true, - "copy_to": [ - "search_attr" - ] - }, - "value": { - "type": "keyword" - } + } + }, + "publisher": { + "type": "text", + "copy_to": [ + "search_publisher" + ] + }, + "date": { + "type": "nested", + "properties": { + "dateType": { + "type": "keyword", + "index": true, + "copy_to": [ + "search_attr" + ] + }, + "value": { + "type": "keyword" } - }, - "language": { - "type": "keyword", - "copy_to": [ - "search_other" - ] - }, - "version": { - "type": "text", - "index": true, - "copy_to": [ - "search_other" - ] - }, - "versionType": { - "type": "text", - "copy_to": [ - "search_other" - ] - }, - "identifier": { - "type": "nested", - "properties": { - "value": { - "type": "text", - "copy_to": [ - "search_other" - ] - }, - "identifierType": { - "type": "keyword", - "index": true, - "copy_to": [ - "search_attr" - ] - } + } + }, + "language": { + "type": "keyword", + "copy_to": [ + "search_other" + ] + }, + "version": { + "type": "text", + "index": true, + "copy_to": [ + "search_other" + ] + }, + "versionType": { + "type": "text", + "copy_to": [ + "search_other" + ] + }, + "identifier": { + "type": "nested", + "properties": { + "value": { + "type": "text", + "copy_to": [ + "search_other" + ] + }, + "identifierType": { + "type": "keyword", + "index": true, + "copy_to": [ + "search_attr" + ] } - }, - "identifierRegistration": { - "type": "nested", - "properties": { - "value": { - "type": "text", - "copy_to": [ - "search_other" - ] - }, - "identifierType": { - "type": "keyword", - "index": true, - "copy_to": [ - "search_attr" - ] - } + } + }, + "identifierRegistration": { + "type": "nested", + "properties": { + "value": { + "type": "text", + "copy_to": [ + "search_other" + ] + }, + "identifierType": { + "type": "keyword", + "index": true, + "copy_to": [ + "search_attr" + ] } - }, - "relation": { - "type": "object", - "properties": { - "relatedIdentifier": { - "type": "nested", - "properties": { - "value": { - "type": "text", - "copy_to": [ - "search_other" - ] - }, - "identifierType": { - "type": "keyword", - "index": true, - "copy_to": [ - "search_attr" - ] - } + } + }, + "relation": { + "type": "object", + "properties": { + "relatedIdentifier": { + "type": "nested", + "properties": { + "value": { + "type": "text", + "copy_to": [ + "search_other" + ] + }, + "identifierType": { + "type": "keyword", + "index": true, + "copy_to": [ + "search_attr" + ] } - }, - "relatedTitle": { - "type": "keyword", - "index": true, - "copy_to": [ - "search_other" - ] - }, - "relationType": { - "type": "nested", - "properties": { - "value": { - "type": "text", - "index": true - }, - "item_links": { - "type": "keyword", - "index": true - }, - "item_title": { - "type": "keyword", - "index": true - } + } + }, + "relatedTitle": { + "type": "keyword", + "index": true, + "copy_to": [ + "search_other" + ] + }, + "relationType": { + "type": "nested", + "properties": { + "value": { + "type": "text", + "index": true + }, + "item_links": { + "type": "keyword", + "index": true + }, + "item_title": { + "type": "keyword", + "index": true } } } - }, - "temporal": { - "type": "keyword", - "copy_to": [ - "search_other" - ] - }, - "text1": { - "type": "text", - "fields": { - "raw": { - "type": "keyword" - } + } + }, + "temporal": { + "type": "keyword", + "copy_to": [ + "search_other" + ] + }, + "text1": { + "type": "text", + "fields": { + "raw": { + "type": "keyword" } - }, - "text2": { - "type": "text", - "fields": { - "raw": { - "type": "keyword" - } + } + }, + "text2": { + "type": "text", + "fields": { + "raw": { + "type": "keyword" } - }, - "text3": { - "type": "text", - "fields": { - "raw": { - "type": "keyword" - } + } + }, + "text3": { + "type": "text", + "fields": { + "raw": { + "type": "keyword" } - }, - "text4": { - "type": "text", - "fields": { - "raw": { - "type": "keyword" - } + } + }, + "text4": { + "type": "text", + "fields": { + "raw": { + "type": "keyword" } - }, - "text5": { - "type": "text", - "fields": { - "raw": { - "type": "keyword" - } + } + }, + "text5": { + "type": "text", + "fields": { + "raw": { + "type": "keyword" } - }, - "text6": { - "type": "text", - "fields": { - "raw": { - "type": "keyword" - } + } + }, + "text6": { + "type": "text", + "fields": { + "raw": { + "type": "keyword" } - }, - "text7": { - "type": "text", - "fields": { - "raw": { - "type": "keyword" - } + } + }, + "text7": { + "type": "text", + "fields": { + "raw": { + "type": "keyword" } - }, - "text8": { - "type": "text", - "fields": { - "raw": { - "type": "keyword" - } + } + }, + "text8": { + "type": "text", + "fields": { + "raw": { + "type": "keyword" } - }, - "text9": { - "type": "text", - "fields": { - "raw": { - "type": "keyword" - } + } + }, + "text9": { + "type": "text", + "fields": { + "raw": { + "type": "keyword" } - }, - "text10": { - "type": "text", - "fields": { - "raw": { - "type": "keyword" - } + } + }, + "text10": { + "type": "text", + "fields": { + "raw": { + "type": "keyword" } - }, - "text11": { - "type": "text", - "fields": { - "raw": { - "type": "keyword" - } + } + }, + "text11": { + "type": "text", + "fields": { + "raw": { + "type": "keyword" } - }, - "text12": { - "type": "text", - "fields": { - "raw": { - "type": "keyword" - } + } + }, + "text12": { + "type": "text", + "fields": { + "raw": { + "type": "keyword" } - }, - "text13": { - "type": "text", - "fields": { - "raw": { - "type": "keyword" - } + } + }, + "text13": { + "type": "text", + "fields": { + "raw": { + "type": "keyword" } - }, - "text14": { - "type": "text", - "fields": { - "raw": { - "type": "keyword" - } + } + }, + "text14": { + "type": "text", + "fields": { + "raw": { + "type": "keyword" } - }, - "text15": { - "type": "text", - "fields": { - "raw": { - "type": "keyword" - } + } + }, + "text15": { + "type": "text", + "fields": { + "raw": { + "type": "keyword" } - }, - "text16": { - "type": "text", - "fields": { - "raw": { - "type": "keyword" - } + } + }, + "text16": { + "type": "text", + "fields": { + "raw": { + "type": "keyword" } - }, - "text17": { - "type": "text", - "fields": { - "raw": { - "type": "keyword" - } + } + }, + "text17": { + "type": "text", + "fields": { + "raw": { + "type": "keyword" } - }, - "text18": { - "type": "text", - "fields": { - "raw": { - "type": "keyword" - } + } + }, + "text18": { + "type": "text", + "fields": { + "raw": { + "type": "keyword" } - }, - "text19": { - "type": "text", - "fields": { - "raw": { - "type": "keyword" - } + } + }, + "text19": { + "type": "text", + "fields": { + "raw": { + "type": "keyword" } - }, - "text20": { - "type": "text", - "fields": { - "raw": { - "type": "keyword" - } + } + }, + "text20": { + "type": "text", + "fields": { + "raw": { + "type": "keyword" } - }, - "text21": { - "type": "text", - "fields": { - "raw": { - "type": "keyword" - } + } + }, + "text21": { + "type": "text", + "fields": { + "raw": { + "type": "keyword" } - }, - "text22": { - "type": "text", - "fields": { - "raw": { - "type": "keyword" - } + } + }, + "text22": { + "type": "text", + "fields": { + "raw": { + "type": "keyword" } - }, - "text23": { - "type": "text", - "fields": { - "raw": { - "type": "keyword" - } + } + }, + "text23": { + "type": "text", + "fields": { + "raw": { + "type": "keyword" } - }, - "text24": { - "type": "text", - "fields": { - "raw": { - "type": "keyword" - } + } + }, + "text24": { + "type": "text", + "fields": { + "raw": { + "type": "keyword" } - }, - "text25": { - "type": "text", - "fields": { - "raw": { - "type": "keyword" - } + } + }, + "text25": { + "type": "text", + "fields": { + "raw": { + "type": "keyword" } - }, - "text26": { - "type": "text", - "fields": { - "raw": { - "type": "keyword" - } + } + }, + "text26": { + "type": "text", + "fields": { + "raw": { + "type": "keyword" } - }, - "text27": { - "type": "text", - "fields": { - "raw": { - "type": "keyword" - } + } + }, + "text27": { + "type": "text", + "fields": { + "raw": { + "type": "keyword" } - }, - "text28": { - "type": "text", - "fields": { - "raw": { - "type": "keyword" - } + } + }, + "text28": { + "type": "text", + "fields": { + "raw": { + "type": "keyword" } - }, - "text29": { - "type": "text", - "fields": { - "raw": { - "type": "keyword" - } + } + }, + "text29": { + "type": "text", + "fields": { + "raw": { + "type": "keyword" } - }, - "text30": { - "type": "text", - "fields": { - "raw": { - "type": "keyword" - } + } + }, + "text30": { + "type": "text", + "fields": { + "raw": { + "type": "keyword" } - }, - "integer_range1":{ - "type": "integer_range" - }, - "integer_range2":{ - "type": "integer_range" - }, - "integer_range3":{ - "type": "integer_range" - }, - "integer_range4":{ - "type": "integer_range" - }, - "integer_range5":{ - "type": "integer_range" - }, - "float_range1":{ - "type": "float_range" - }, - "float_range2":{ - "type": "float_range" - }, - "float_range3":{ - "type": "float_range" - }, - "float_range4":{ - "type": "float_range" - }, - "float_range5":{ - "type": "float_range" - }, - "geo_point1":{ - "type": "geo_point" - }, - "date_range1":{ - "type" : "date_range", - "format" : "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||yyyy-MM||yyyy||epoch_millis" - }, - "date_range2":{ - "type" : "date_range", - "format" : "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||yyyy-MM||yyyy||epoch_millis" - }, - "date_range3":{ - "type" : "date_range", - "format" : "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||yyyy-MM||yyyy||epoch_millis" - }, - "date_range4":{ - "type" : "date_range", - "format" : "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||yyyy-MM||yyyy||epoch_millis" - }, - "date_range5":{ - "type" : "date_range", - "format" : "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||yyyy-MM||yyyy||epoch_millis" - }, - - "geo_shape1":{ - "type" : "geo_shape" - }, - "geoLocation": { - "type": "object", - "properties": { - "geoLocationPoint": { - "type": "object", - "properties": { - "pointLongitude": { - "type": "geo_point" - }, - "pointLatitude": { - "type": "geo_point" - } - } - }, - "geoLocationBox": { - "type": "object", - "properties": { - "westBoundLongitude": { - "type": "geo_point" - }, - "eastBoundLongitude": { - "type": "geo_point" - }, - "southBoundLatitude": { - "type": "geo_point" - }, - "northBoundLatitude": { - "type": "geo_point" - } + } + }, + "integer_range1": { + "type": "integer_range" + }, + "integer_range2": { + "type": "integer_range" + }, + "integer_range3": { + "type": "integer_range" + }, + "integer_range4": { + "type": "integer_range" + }, + "integer_range5": { + "type": "integer_range" + }, + "float_range1": { + "type": "float_range" + }, + "float_range2": { + "type": "float_range" + }, + "float_range3": { + "type": "float_range" + }, + "float_range4": { + "type": "float_range" + }, + "float_range5": { + "type": "float_range" + }, + "geo_point1": { + "type": "geo_point" + }, + "date_range1": { + "type": "date_range", + "format": "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||yyyy-MM||yyyy||epoch_millis" + }, + "date_range2": { + "type": "date_range", + "format": "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||yyyy-MM||yyyy||epoch_millis" + }, + "date_range3": { + "type": "date_range", + "format": "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||yyyy-MM||yyyy||epoch_millis" + }, + "date_range4": { + "type": "date_range", + "format": "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||yyyy-MM||yyyy||epoch_millis" + }, + "date_range5": { + "type": "date_range", + "format": "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||yyyy-MM||yyyy||epoch_millis" + }, + "geo_shape1": { + "type": "geo_shape" + }, + "geoLocation": { + "type": "object", + "properties": { + "geoLocationPoint": { + "type": "object", + "properties": { + "pointLongitude": { + "type": "geo_point" + }, + "pointLatitude": { + "type": "geo_point" } - }, - "geoLocationPlace": { - "type": "keyword", - "copy_to": [ - "search_other" - ] } - } - }, - "fundingReference": { - "type": "object", - "properties": { - "funderIdentifier": { - "type": "keyword", - "index": true, - "copy_to": [ - "search_identifier" - ] - }, - "funderName": { - "type": "keyword", - "index": true, - "copy_to": [ - "search_other" - ] - }, - "awardNumber": { - "type": "keyword", - "index": true, - "copy_to": [ - "search_other" - ] - }, - "awardTitle": { - "type": "keyword", - "index": true, - "copy_to": [ - "search_other" - ] + }, + "geoLocationBox": { + "type": "object", + "properties": { + "westBoundLongitude": { + "type": "geo_point" + }, + "eastBoundLongitude": { + "type": "geo_point" + }, + "southBoundLatitude": { + "type": "geo_point" + }, + "northBoundLatitude": { + "type": "geo_point" + } } + }, + "geoLocationPlace": { + "type": "keyword", + "copy_to": [ + "search_other" + ] } - }, - "sourceIdentifier": { - "type": "nested", - "properties": { - "value": { - "type": "text", - "copy_to": [ - "search_other" - ] - }, - "identifierType": { - "type": "keyword", - "index": true, - "copy_to": [ - "search_attr" - ] - } + } + }, + "fundingReference": { + "type": "object", + "properties": { + "funderIdentifier": { + "type": "keyword", + "index": true, + "copy_to": [ + "search_identifier" + ] + }, + "funderName": { + "type": "keyword", + "index": true, + "copy_to": [ + "search_other" + ] + }, + "awardNumber": { + "type": "keyword", + "index": true, + "copy_to": [ + "search_other" + ] + }, + "awardTitle": { + "type": "keyword", + "index": true, + "copy_to": [ + "search_other" + ] } - }, - "sourceTitle": { - "type": "text", - "fields": { - "ja": { - "type": "text" - } + } + }, + "sourceIdentifier": { + "type": "nested", + "properties": { + "value": { + "type": "text", + "copy_to": [ + "search_other" + ] }, - "copy_to": [ - "search_other" - ] - }, - "author_link": { - "type": "text", - "fields" : { - "raw" : { - "type" : "keyword", - "ignore_above" : 256 - } + "identifierType": { + "type": "keyword", + "index": true, + "copy_to": [ + "search_attr" + ] + } + } + }, + "sourceTitle": { + "type": "text", + "fields": { + "ja": { + "type": "text" } }, - "volume": { - "type": "text", - "index": true, - "copy_to": [ - "search_other" - ] - }, - "issue": { - "type": "text", - "index": true, - "copy_to": [ - "search_other" - ] - }, - "numPages": { - "type": "text" - }, - "pageStart": { - "type": "text" - }, - "pageEnd": { - "type": "text" - }, - "dissertationNumber": { - "type": "text", - "copy_to": [ - "search_other" - ] + "copy_to": [ + "search_other" + ] + }, + "author_link": { + "type": "text", + "fields": { + "raw": { + "type": "keyword", + "ignore_above": 256 + } + } + }, + "volume": { + "type": "text", + "index": true, + "copy_to": [ + "search_other" + ] + }, + "issue": { + "type": "text", + "index": true, + "copy_to": [ + "search_other" + ] + }, + "numPages": { + "type": "text" + }, + "pageStart": { + "type": "text" + }, + "pageEnd": { + "type": "text" + }, + "dissertationNumber": { + "type": "text", + "copy_to": [ + "search_other" + ] + }, + "degreeName": { + "type": "text", + "fields": { + "ja": { + "type": "text" + } }, - "degreeName": { - "type": "text", - "fields": { - "ja": { - "type": "text" - } + "copy_to": [ + "search_other" + ] + }, + "dateGranted": { + "type": "keyword" + }, + "degreeGrantor": { + "type": "object", + "properties": { + "nameIdentifier": { + "type": "keyword", + "index": true, + "copy_to": [ + "search_identifier" + ] }, - "copy_to": [ - "search_other" - ] - }, - "dateGranted": { - "type": "keyword" - }, - "degreeGrantor": { - "type": "object", - "properties": { - "nameIdentifier": { - "type": "keyword", - "index": true, - "copy_to": [ - "search_identifier" - ] - }, - "degreeGrantorName": { - "type": "keyword", - "index": true, - "copy_to": [ - "search_other", - "dgName" - ] - } + "degreeGrantorName": { + "type": "keyword", + "index": true, + "copy_to": [ + "search_other", + "dgName" + ] } - }, - "conference": { - "type": "object", - "properties": { - "conferenceName": { - "type": "keyword", - "index": true, - "copy_to": [ - "search_other" - ] - }, - "conferenceSequence": { - "type": "keyword", - "index": true, - "copy_to": [ - "search_other" - ] - }, - "conferencePlace": { - "type": "keyword", - "index": true, - "copy_to": [ - "search_other" - ] - }, - "conferenceCountry": { - "type": "keyword", - "index": true, - "copy_to": [ - "search_other" - ] - } + } + }, + "conference": { + "type": "object", + "properties": { + "conferenceName": { + "type": "keyword", + "index": true, + "copy_to": [ + "search_other" + ] + }, + "conferenceSequence": { + "type": "keyword", + "index": true, + "copy_to": [ + "search_other" + ] + }, + "conferencePlace": { + "type": "keyword", + "index": true, + "copy_to": [ + "search_other" + ] + }, + "conferenceCountry": { + "type": "keyword", + "index": true, + "copy_to": [ + "search_other" + ] } - }, - "file": { - "type": "object", - "properties": { - "URI": { - "type": "nested", - "properties": { - "value": { - "type": "text" - }, - "objectType": { - "type": "keyword", - "index": true - } + } + }, + "file": { + "type": "object", + "properties": { + "URI": { + "type": "nested", + "properties": { + "value": { + "type": "text" + }, + "objectType": { + "type": "keyword", + "index": true } - }, - "mimeType": { - "type": "keyword", - "index": true - }, - "extent": { - "type": "keyword", - "index": true - }, - "date": { - "type": "nested", - "properties": { - "dateType": { - "type": "keyword", - "index": true - }, - "value": { - "type": "keyword" - } + } + }, + "mimeType": { + "type": "keyword", + "index": true + }, + "extent": { + "type": "keyword", + "index": true + }, + "date": { + "type": "nested", + "properties": { + "dateType": { + "type": "keyword", + "index": true + }, + "value": { + "type": "keyword" } - }, - "version": { - "type": "text" } + }, + "version": { + "type": "text" } - }, - "content": { - "type": "nested", - "properties": { - "file_id": { - "type": "keyword", - "index": true - }, - "groups": { - "type": "keyword", - "index": true - }, - "file_name": { - "type": "text", - "fields": { - "ja": { - "type": "text" - } + } + }, + "content": { + "type": "nested", + "properties": { + "file_id": { + "type": "keyword", + "index": true + }, + "groups": { + "type": "keyword", + "index": true + }, + "file_name": { + "type": "text", + "fields": { + "ja": { + "type": "text" } - }, - "display_name": { - "type": "text", - "fields": { - "ja": { - "type": "text" - } + } + }, + "display_name": { + "type": "text", + "fields": { + "ja": { + "type": "text" } - }, - "license_notation": { - "type": "text" - }, - "file": { - "type": "text", - "term_vector": "with_positions_offsets", - "store": true, - "fields": { - "ja": { - "type": "text", - "term_vector": "with_positions_offsets", - "store": true - } + } + }, + "license_notation": { + "type": "text" + }, + "file": { + "type": "text", + "term_vector": "with_positions_offsets", + "store": true, + "fields": { + "ja": { + "type": "text", + "term_vector": "with_positions_offsets", + "store": true } - }, - "attachment": { - "properties": { - "content": { - "type": "text", - "term_vector": "with_positions_offsets", - "store": true, - "fields": { - "ja": { - "type": "text", - "term_vector": "with_positions_offsets", - "store": true - } + } + }, + "attachment": { + "properties": { + "content": { + "type": "text", + "term_vector": "with_positions_offsets", + "store": true, + "fields": { + "ja": { + "type": "text", + "term_vector": "with_positions_offsets", + "store": true } } } } } - }, - "weko_creator_id": { - "type": "text", - "fielddata": true, - "index": true - }, - "weko_id": { - "type": "text", - "fielddata": true, - "index": true - }, - "search_title": { - "type": "text", - "fields": { - "ja": { - "type": "text" - } - } - }, - "search_creator": { - "type": "text", - "fields": { - "ja": { - "type": "text" - } + } + }, + "weko_creator_id": { + "type": "text", + "fielddata": true, + "index": true + }, + "weko_id": { + "type": "text", + "fielddata": true, + "index": true + }, + "search_title": { + "type": "text", + "fields": { + "ja": { + "type": "text" } - }, - "search_contributor": { - "type": "text", - "fields": { - "ja": { - "type": "text" - } + } + }, + "search_creator": { + "type": "text", + "fields": { + "ja": { + "type": "text" } - }, - "search_other": { - "type": "text", - "fields": { - "ja": { - "type": "text" - } + } + }, + "search_contributor": { + "type": "text", + "fields": { + "ja": { + "type": "text" } - }, - "search_identifier": { - "type": "text" - }, - "search_attr": { - "type": "text" - }, - "search_string": { - "type": "text" - }, - "search_publisher": { - "type": "text", - "fields": { - "ja": { - "type": "text" - } + } + }, + "search_other": { + "type": "text", + "fields": { + "ja": { + "type": "text" } - }, - "search_des": { - "type": "text", - "fields": { - "ja": { - "type": "text" - } + } + }, + "search_identifier": { + "type": "text" + }, + "search_attr": { + "type": "text" + }, + "search_string": { + "type": "text" + }, + "search_publisher": { + "type": "text", + "fields": { + "ja": { + "type": "text" } - }, - "dgName": { - "type": "text", - "fields": { - "ja": { - "type": "text" - } + } + }, + "search_des": { + "type": "text", + "fields": { + "ja": { + "type": "text" } } }, - "dynamic_templates": [ - { - "weko_id": { - "match_mapping_type": "string", - "match_pattern": "regex", - "match": "^weko_id$", - "mapping": { - "type": "text", - "fielddata": true, - "index": false, - "copy_to": "weko_id" - } + "dgName": { + "type": "text", + "fields": { + "ja": { + "type": "text" } - }, - { - "string": { - "match_mapping_type": "string", - "mapping": { - "type": "text", - "index": false, - "copy_to": "search_string", - "fields": { - "raw": { - "type": "keyword", - "ignore_above": 256 - } + } + } + }, + "dynamic_templates": [ + { + "weko_id": { + "match_mapping_type": "string", + "match_pattern": "regex", + "match": "^weko_id$", + "mapping": { + "type": "text", + "fielddata": true, + "index": false, + "copy_to": "weko_id" + } + } + }, + { + "string": { + "match_mapping_type": "string", + "mapping": { + "type": "text", + "index": false, + "copy_to": "search_string", + "fields": { + "raw": { + "type": "keyword", + "ignore_above": 256 } } } - }, - { - "date_string": { - "match_mapping_type": "date", - "mapping": { - "type": "text", - "index": false, - "copy_to": "search_string", - "fields": { - "raw": { - "type": "keyword", - "ignore_above": 256 - } + } + }, + { + "date_string": { + "match_mapping_type": "date", + "mapping": { + "type": "text", + "index": false, + "copy_to": "search_string", + "fields": { + "raw": { + "type": "keyword", + "ignore_above": 256 } } } } - ] - } + } + ] } -} +} \ No newline at end of file diff --git a/modules/weko-search-ui/tests/helpers.py b/modules/weko-search-ui/tests/helpers.py index e3fbd58284..e296f240eb 100644 --- a/modules/weko-search-ui/tests/helpers.py +++ b/modules/weko-search-ui/tests/helpers.py @@ -14,6 +14,7 @@ from weko_deposit.api import WekoDeposit, WekoRecord from invenio_pidrelations.models import PIDRelation +from invenio_pidrelations.utils import resolve_relation_type_config def json_data(filename): with open(join(dirname(__file__),filename), "r") as f: @@ -23,24 +24,28 @@ def json_data(filename): def create_record(record_data, item_data): """Create a test record.""" with db.session.begin_nested(): + # resolve relation type + relation_type_version = resolve_relation_type_config("version") + relation_type_draft = resolve_relation_type_config("record_draft") + record_data = copy.deepcopy(record_data) item_data = copy.deepcopy(item_data) rec_uuid = uuid.uuid4() recid = PersistentIdentifier.create('recid', record_data["recid"],object_type='rec', object_uuid=rec_uuid,status=PIDStatus.REGISTERED) depid = PersistentIdentifier.create('depid', record_data["recid"],object_type='rec', object_uuid=rec_uuid,status=PIDStatus.REGISTERED) - rel = PIDRelation.create(recid,depid,3) + rel = PIDRelation.create(recid, depid, relation_type_draft.id) db.session.add(rel) parent = None doi = None if not ('.' in record_data["recid"]): parent = PersistentIdentifier.create('parent', "parent:{}".format(record_data["recid"]),object_type='rec', object_uuid=rec_uuid,status=PIDStatus.REGISTERED) - rel = PIDRelation.create(parent,recid,2,0) + rel = PIDRelation.create(parent, recid, relation_type_version.id, 0) db.session.add(rel) doi = PersistentIdentifier.create('doi', " https://doi.org/10.xyz/{}".format((str(record_data["recid"])).zfill(10)),object_type='rec', object_uuid=rec_uuid,status=PIDStatus.REGISTERED) else: parent = PersistentIdentifier.get('parent','parent:{}'.format((str(record_data["recid"])).split('.')[0])) - rel = PIDRelation.create(parent,recid,2,(str(record_data["recid"])).split('.')[1]) + rel = PIDRelation.create(parent, recid, relation_type_version.id, (str(record_data["recid"])).split('.')[1]) db.session.add(rel) record = WekoRecord.create(record_data, id_=rec_uuid) @@ -55,12 +60,16 @@ def create_record(record_data, item_data): def create_record2(record_data, item_data): with db.session.begin_nested(): + # resolve relation type + relation_type_version = resolve_relation_type_config("version") + relation_type_draft = resolve_relation_type_config("record_draft") + record_data = copy.deepcopy(record_data) item_data = copy.deepcopy(item_data) rec_uuid = uuid.uuid4() recid = PersistentIdentifier.create('recid', record_data["recid"],object_type='rec', object_uuid=rec_uuid,status=PIDStatus.REGISTERED) depid = PersistentIdentifier.create('depid', record_data["recid"],object_type='rec', object_uuid=rec_uuid,status=PIDStatus.REGISTERED) - rel = PIDRelation.create(recid,depid,3) + rel = PIDRelation.create(recid, depid, relation_type_draft.id) db.session.add(rel) parent=None doi = None @@ -68,10 +77,10 @@ def create_record2(record_data, item_data): if '.' in record_data["recid"]: parent = PersistentIdentifier.get("recid",int(float(record_data["recid"]))) recid_p = PIDRelation.get_child_relations(parent).one_or_none() - PIDRelation.create(recid_p.parent, recid,2) + PIDRelation.create(recid_p.parent, recid, relation_type_version.id) else: parent = PersistentIdentifier.create('parent', "parent:{}".format(record_data["recid"]),object_type='rec', object_uuid=rec_uuid,status=PIDStatus.REGISTERED) - rel = PIDRelation.create(parent, recid,2,0) + rel = PIDRelation.create(parent, recid, relation_type_version.id, 0) db.session.add(rel) RecordIdentifier.next() if record_data.get("_oai").get("id"): diff --git a/modules/weko-search-ui/tests/test_admin.py b/modules/weko-search-ui/tests/test_admin.py index 4f095fc2a1..6064814a54 100644 --- a/modules/weko-search-ui/tests/test_admin.py +++ b/modules/weko-search-ui/tests/test_admin.py @@ -30,7 +30,10 @@ # class ItemManagementBulkDelete(BaseView): # def index(self): # .tox/c1/bin/pytest --cov=weko_search_ui tests/test_admin.py::test_ItemManagementBulkDelete_index -vv -s --cov-branch --cov-report=term --basetemp=/code/modules/weko-search-ui/.tox/c1/tmp -def test_ItemManagementBulkDelete_index(i18n_app, open_search, users, indices, mocker): +def test_ItemManagementBulkDelete_index( + i18n_app, open_search, users, indices, + user_activity_log_partition_table, mocker +): i18n_app.config['WEKO_SEARCH_TYPE_INDEX'] = 'index' with i18n_app.test_client() as client: with patch("flask_login.utils._get_user", return_value=users[3]['obj']): @@ -149,12 +152,21 @@ def test_index_acl(self,client, users, db_records2): res = client.get(url) assert res.status == '500 INTERNAL SERVER ERROR' + # Test when query parameter item_management is "sort" url = url_for("items/search.index", item_management="sort", _external=True) with patch("flask_login.utils._get_user", return_value=user): with patch("flask.templating._render", return_value=""): - res = client.get(url, query_string={"item_management": "update"}) + res = client.get(url) assert res.status == '200 OK' + # Test when query parameter item_management is "update" + url = url_for("items/search.index", item_management="update", _external=True) + with patch("flask_login.utils._get_user", return_value=user): + with patch("flask.templating._render", return_value=""): + res = client.get(url) + assert res.status == '200 OK' + + # .tox/c1/bin/pytest --cov=weko_search_ui tests/test_admin.py::TestItemManagementBulkSearch::test_index -vv -s --cov-branch --cov-report=term --basetemp=/code/modules/weko-search-ui/.tox/c1/tmp def test_index(self, i18n_app, users, indices2, mocker): i18n_app.config['WEKO_SEARCH_TYPE_INDEX'] = 'index' @@ -235,7 +247,7 @@ def test_ItemImportView_check(self, i18n_app, users, client,client_request_args, test = ItemImportView() task = MagicMock() task.task_id = 1 - with patch("weko_search_ui.tasks.check_import_items_task.apply_async",return_Value=task): + with patch("weko_search_ui.tasks.check_import_items_task.apply_async",return_value=task): assert test.check() # def get_check_status(self) -> jsonify: ~ GOOD @@ -307,14 +319,22 @@ def test_ItemImportView_get_check_status(self, i18n_app, users, client_request_a assert res.status_code == 200 # def download_check(self): ~ GOOD + # .tox/c1/bin/pytest --cov=weko_search_ui tests/test_admin.py::TestItemImportView::test_ItemImportView_download_check -vv -s --cov-branch --cov-report=term --basetemp=/code/modules/weko-search-ui/.tox/c1/tmp def test_ItemImportView_download_check(self, i18n_app, users, client_request_args, db_records2): with patch("flask_login.utils._get_user", return_value=users[3]['obj']): - test = ItemImportView() - assert test.download_check() + download_check_url = url_for("items/import.download_check", _external=True) + response = client_request_args.post( + download_check_url, json={} + ) + assert response.status_code == 200 + # def import_items(self) -> jsonify: ~ GOOD # .tox/c1/bin/pytest --cov=weko_search_ui tests/test_admin.py::TestItemImportView::test_ItemImportView_import_items -vv -s --cov-branch --cov-report=term --basetemp=/code/modules/weko-search-ui/.tox/c1/tmp - def test_ItemImportView_import_items(self, i18n_app, users, client, client_request_args, db_records2, mocker): + def test_ItemImportView_import_items( + self, i18n_app, users, client, client_request_args, db_records2, + user_activity_log_partition_table, mocker + ): mocker.patch("flask_login.utils._get_user", return_value=users[3]['obj']) mocker.patch("weko_search_ui.admin.create_flow_define") mocker.patch("weko_search_ui.admin.handle_workflow") @@ -355,16 +375,28 @@ def test_ItemImportView_import_items(self, i18n_app, users, client, client_reque # def get_status(self): ~ GOOD + # .tox/c1/bin/pytest --cov=weko_search_ui tests/test_admin.py::TestItemImportView::test_ItemImportView_get_status -vv -s --cov-branch --cov-report=term --basetemp=/code/modules/weko-search-ui/.tox/c1/tmp def test_ItemImportView_get_status(self, i18n_app, users, client_request_args, db_records2): with patch("flask_login.utils._get_user", return_value=users[3]['obj']): - test = ItemImportView() - assert test.get_status() + get_status_url = url_for("items/import.get_status", _external=True) + response = client_request_args.post( + get_status_url, json={}, + ) + assert response.status_code == 200 + # def download_import(self): ~ GOOD + # .tox/c1/bin/pytest --cov=weko_search_ui tests/test_admin.py::TestItemImportView::test_ItemImportView_download_import -vv -s --cov-branch --cov-report=term --basetemp=/code/modules/weko-search-ui/.tox/c1/tmp def test_ItemImportView_download_import(self, i18n_app, users, client_request_args, db_records2): with patch("flask_login.utils._get_user", return_value=users[3]['obj']): - test = ItemImportView() - assert test.download_import() + download_import_url = url_for( + "items/import.download_import", _external=True + ) + response = client_request_args.post( + download_import_url, json={}, + ) + assert response.status_code == 200 + # def get_disclaimer_text(self): ~ GOOD def test_ItemImportView_get_disclaimer_text(self, i18n_app, users, client_request_args, db_records2): @@ -562,7 +594,9 @@ def test_ItemRocrateImportView_download_check(self, i18n_app, users, mocker): # def import_items(self) -> jsonify: ~ GOOD # .tox/c1/bin/pytest --cov=weko_search_ui tests/test_admin.py::TestItemRocrateImportView::test_ItemRocrateImportView_import_items -vv -s --cov-branch --cov-report=term --basetemp=/code/modules/weko-search-ui/.tox/c1/tmp - def test_ItemRocrateImportView_import_items(self, i18n_app, users, client, mocker): + def test_ItemRocrateImportView_import_items( + self, i18n_app, users, client, user_activity_log_partition_table, mocker + ): mocker.patch("flask_login.utils._get_user", return_value=users[3]['obj']) mocker.patch("weko_search_ui.admin.create_flow_define") mocker.patch("weko_search_ui.admin.handle_workflow") @@ -606,7 +640,7 @@ def test_ItemRocrateImportView_import_items(self, i18n_app, users, client, mocke # log group id is False with patch("weko_logging.activity_logger.UserActivityLogger.issue_log_group_id", return_value=False): - res = client.post(url, data=json.dumps({}), content_type="application/json") + res = client.post(url, json=data) assert res.status_code == 200 json_data = res.get_json() assert json_data["status"] == "success" @@ -820,7 +854,7 @@ def test_ItemBulkExport_export_all(users, client, redis_connect, mocker): with patch("weko_search_ui.admin.get_export_status", return_value=(True, '', '', '', 'STARTED', start_time_str, '')): - res = client.post(url) + res = client.post(url, json={}) assert json.loads(res.data) == {'data': { 'celery_is_run': True, 'is_lifetime': True, @@ -840,7 +874,7 @@ def test_ItemBulkExport_export_all(users, client, redis_connect, mocker): with patch("weko_search_ui.admin.get_export_status", return_value=(True, 'test_uri', '', '', 'STARTED', start_time_str, '')): mocker.patch("weko_search_ui.utils.AsyncResult",side_effect=MockAsyncResult) - res = client.post(url) + res = client.post(url, json={}) assert json.loads(res.data) == {'data': { 'celery_is_run': True, 'is_lifetime': True, @@ -855,7 +889,7 @@ def test_ItemBulkExport_export_all(users, client, redis_connect, mocker): with patch("weko_search_ui.admin.get_export_status", return_value=(False, '', '', '', 'STARTED', start_time_str, '')): - res = client.post(url) + res = client.post(url, json={}) assert json.loads(res.data) == {'data': { 'celery_is_run': True, 'is_lifetime': True, diff --git a/modules/weko-search-ui/tests/test_api.py b/modules/weko-search-ui/tests/test_api.py index 097d58df61..ce5b6612bd 100644 --- a/modules/weko-search-ui/tests/test_api.py +++ b/modules/weko-search-ui/tests/test_api.py @@ -16,9 +16,8 @@ ## class SearchSetting(object): # get_results_setting(cls): +# .tox/c1/bin/pytest --cov=weko_search_ui tests/test_api.py::test_get_results_setting -vv -s --cov-branch --cov-report=term --basetemp=/code/modules/weko-search-ui/.tox/c1/tmp def test_get_results_setting(i18n_app, users, db, app): - from sqlalchemy.sql import func - test_1 = SearchManagement( id=1, default_dis_sort_index="id", @@ -30,15 +29,21 @@ def test_get_results_setting(i18n_app, users, db, app): ] } ) - - assert SearchSetting.get_results_setting()[0] == app.config['RECORDS_REST_SORT_OPTIONS'] - assert SearchSetting.get_results_setting()[1] == 20 + + actual = SearchSetting.get_results_setting() + assert isinstance(actual, tuple) + assert len(actual) == 2 + assert actual[0] == app.config["RECORDS_REST_SORT_OPTIONS"] + assert actual[1] == 20 db.session.add(test_1) db.session.commit() - assert SearchSetting.get_results_setting()[0] != app.config['RECORDS_REST_SORT_OPTIONS'] - assert SearchSetting.get_results_setting()[1] == 20 + actual2 = SearchSetting.get_results_setting() + assert isinstance(actual2, tuple) + assert len(actual2) == 2 + assert actual2[0] != app.config["RECORDS_REST_SORT_OPTIONS"] + assert actual2[1] == 20 # get_default_sort(cls, search_type, root_flag=False): def test_get_default_sort(i18n_app, users, db, app): @@ -81,28 +86,62 @@ def test_get_default_sort(i18n_app, users, db, app): sort_key_str = ad_config.WEKO_ADMIN_MANAGEMENT_OPTIONS["dlt_index_sort_selected"] assert SearchSetting.get_default_sort(root_flag=True, search_type="keyword")[0] in sort_key_str + # get_sort_key(cls, key_str): -def test_get_sort_key(i18n_app, users, app): - sort_key = app.config['RECORDS_REST_SORT_OPTIONS']['test-weko']['test-weko']['fields'][0] +# .tox/c1/bin/pytest --cov=weko_search_ui tests/test_api.py::test_get_sort_key -vv -s --cov-branch --cov-report=term --basetemp=/code/modules/weko-search-ui/.tox/c1/tmp +def test_get_sort_key(i18n_app, users): + search_index = i18n_app.config["SEARCH_UI_SEARCH_INDEX"] + sort_key = "test-sort-field" + sort_options = { + "fields": [1, 2, 3], + "nested": 2 + } + # Check that the sort key is not present in the configuration + assert SearchSetting.get_sort_key(key_str=sort_key) is None + assert SearchSetting.get_sort_key(key_str=f"not-{sort_key}") is None + + # Update the config to include a test sort option + i18n_app.config["RECORDS_REST_SORT_OPTIONS"] |= { + search_index: { + sort_key: sort_options, + }, + } + + # Check that the sort key is now present and returns the expected value + expected = sort_options["fields"][0] + assert SearchSetting.get_sort_key(key_str=sort_key) == expected + assert SearchSetting.get_sort_key(key_str=f"not-{sort_key}") is None - assert SearchSetting.get_sort_key(key_str="test-weko") == sort_key - assert SearchSetting.get_sort_key(key_str="not-test-weko") != sort_key +# .tox/c1/bin/pytest --cov=weko_search_ui tests/test_api.py::test_get_custom_sort -vv -s --cov-branch --cov-report=term --basetemp=/code/modules/weko-search-ui/.tox/c1/tmp # get_custom_sort(cls, index_id, sort_type): -def test_get_custom_sort(i18n_app, users, indices): +@pytest.mark.parametrize("sort_type", ["asc", "desc"]) +def test_get_custom_sort(i18n_app, users, indices, sort_type): index_id = 33 - assert SearchSetting.get_custom_sort(index_id, sort_type="asc")[0]['_script']['order'] == 'asc' - assert SearchSetting.get_custom_sort(index_id, sort_type="asc")[1]['_created']['order'] == 'desc' - assert SearchSetting.get_custom_sort(index_id, sort_type="desc")[0]['_script']['order'] == 'desc' - assert SearchSetting.get_custom_sort(index_id, sort_type="desc")[1]['_created']['order'] == 'asc' + assert SearchSetting.get_custom_sort(index_id, sort_type=sort_type)[0]['_script']['order'] == sort_type + assert SearchSetting.get_custom_sort(index_id, sort_type=sort_type)[1]['_created']['order'] == sort_type + # get_nested_sorting(cls, key_str): -def test_get_nested_sorting(i18n_app, users, app): - key_str = "test-weko" - check_key = app.config['RECORDS_REST_SORT_OPTIONS'][key_str][key_str]['nested'] +# .tox/c1/bin/pytest --cov=weko_search_ui tests/test_api.py::test_get_nested_sorting -vv -s --cov-branch --cov-report=term --cov-report=html --basetemp=/code/modules/weko-search-ui/.tox/c1/tmp +def test_get_nested_sorting(i18n_app): + nested_field = "test-nested" + search_index = i18n_app.config["SEARCH_UI_SEARCH_INDEX"] + expected = 2 + + i18n_app.config["RECORDS_REST_SORT_OPTIONS"] |= { + search_index: { + nested_field: { + "fields": [1, 2, 3], + "nested": expected, + }, + }, + } - assert SearchSetting.get_nested_sorting(key_str) == check_key + # Check that the nested sorting is returned correctly for the existing key + assert SearchSetting.get_nested_sorting(nested_field) == expected + assert SearchSetting.get_nested_sorting(f"not-{nested_field}") is None # .tox/c1/bin/pytest --cov=weko_search_ui tests/test_api.py::test_get_search_detail_keyword -vv -s --cov-branch --cov-report=term --cov-report=html --basetemp=/code/modules/weko-search-ui/.tox/c1/tmp # def get_search_detail_keyword(str): diff --git a/modules/weko-search-ui/tests/test_ext.py b/modules/weko-search-ui/tests/test_ext.py index 6298dcdc09..130d0fb3f2 100644 --- a/modules/weko-search-ui/tests/test_ext.py +++ b/modules/weko-search-ui/tests/test_ext.py @@ -4,7 +4,7 @@ import pytest import unittest from mock import patch, MagicMock, Mock -from flask import current_app, make_response, request +from flask import Flask from flask_login import current_user from flask_babel import Babel @@ -12,15 +12,16 @@ # class WekoSearchUI -def test_WekoSearchUI(i18n_app, app): - test = WekoSearchUI(i18n_app) - assert test - -def test_WekoSearchUI_2(i18n_app, app): - app.config.pop("INDEX_IMG") +def test_WekoSearchUI(instance_path): + app = Flask('testapp', instance_path=instance_path) test = WekoSearchUI(app) assert test +# def test_WekoSearchUI_2(i18n_app, app): +# app.config.pop("INDEX_IMG") +# test = WekoSearchUI(app) +# assert test + # class WekoSearchREST def test_WekoSearchREST(i18n_app): diff --git a/modules/weko-search-ui/tests/test_mapper.py b/modules/weko-search-ui/tests/test_mapper.py index 4b39f2ef0a..454b8e7af2 100644 --- a/modules/weko-search-ui/tests/test_mapper.py +++ b/modules/weko-search-ui/tests/test_mapper.py @@ -5326,7 +5326,7 @@ def test_extract_text_from_files(self, app, db, item_type2, mocker, tmp_path): mapper = JsonLdMapper(item_type2.model.id, None) mapper.data_path = str(tmp_path) - file_content = "これは\r\nテキストファイルです\r\n" + file_content = "これは\r\n日本語テキストファイルです\r\n" file_name = "サンプル.txt" tmpfile = tmp_path / file_name diff --git a/modules/weko-search-ui/tests/test_query.py b/modules/weko-search-ui/tests/test_query.py index aa4081296b..b39ec3961b 100644 --- a/modules/weko-search-ui/tests/test_query.py +++ b/modules/weko-search-ui/tests/test_query.py @@ -3,10 +3,11 @@ import pytest from opensearch_dsl.query import Match, Range, Terms, Bool +from flask import current_app +from flask_principal import identity_changed, Identity from mock import patch, MagicMock from werkzeug.datastructures import MultiDict from invenio_accounts.testutils import login_user_via_session - from invenio_i18n.ext import current_i18n from invenio_search import RecordsSearch from weko_admin.config import WEKO_ADMIN_MANAGEMENT_OPTIONS @@ -26,8 +27,13 @@ # def get_item_type_aggs(search_index): # .tox/c1/bin/pytest --cov=weko_search_ui tests/test_query.py::test_get_item_type_aggs -vv -s --cov-branch --cov-report=xml --basetemp=/code/modules/weko-search-ui/.tox/c1/tmp def test_get_item_type_aggs(i18n_app, users, client_request_args, db_records2, records): - with patch("flask_login.utils._get_user", return_value=users[3]['obj']): - assert not get_item_type_aggs("test-weko") + with i18n_app.test_request_context(): + identity_changed.send( + current_app._get_current_object(), + identity=Identity(users[3]['obj'].id) + ) + with patch("flask_login.utils._get_user", return_value=users[3]['obj']): + assert not get_item_type_aggs("test-weko") # def get_permission_filter(index_id: str = None): @@ -77,40 +83,40 @@ def test_get_permission_filter(i18n_app, users, client_request_args, indices): # index_id in is_perm_indexes with patch("weko_index_tree.api.Indexes.get_browsing_tree_paths", return_value=["33", "33/44", "66"]): res = get_permission_filter(33) - assert res == ([Bool(must=[Bool(should=[Terms(path=['33'])])], should=[Bool(must=[Terms(publish_status=['0', '1']), Match(weko_creator_id=5)]), Bool(must=[Terms(publish_status=['0', '1']), Terms(weko_shared_ids=[5])]), Bool(must=[Terms(publish_status=['0', '1'])])]), Bool(must=[Match(relation_version_is_last='true')])], + assert res == ([Bool(minimum_should_match="1", must=[Bool(should=[Terms(path=['33'])])], should=[Bool(must=[Terms(publish_status=['0', '1']), Match(weko_creator_id=5)]), Bool(must=[Terms(publish_status=['0', '1']), Terms(weko_shared_ids=[5])]), Bool(must=[Terms(publish_status=['0', '1'])])]), Bool(must=[Match(relation_version_is_last='true')])], ["33", "33/44", '66']) # index_id not in is_perm_indexes with patch("weko_index_tree.api.Indexes.get_browsing_tree_paths", return_value=["33", "33/44", "66"]): res = get_permission_filter(33333) - assert res == ([Bool(must=[Bool()], should=[Bool(must=[Terms(publish_status=['0', '1']), Match(weko_creator_id=5)]), Bool(must=[Terms(publish_status=['0', '1']), Terms(weko_shared_ids=[5])]), Bool(must=[Terms(publish_status=['0', '1'])])]), Bool(must=[Match(relation_version_is_last='true')])], + assert res == ([Bool(minimum_should_match="1", must=[Bool()], should=[Bool(must=[Terms(publish_status=['0', '1']), Match(weko_creator_id=5)]), Bool(must=[Terms(publish_status=['0', '1']), Terms(weko_shared_ids=[5])]), Bool(must=[Terms(publish_status=['0', '1'])])]), Bool(must=[Match(relation_version_is_last='true')])], ['33', '33/44', '66']) # exist index_id, search_type = INDEX with i18n_app.test_request_context("/test?search_type=2"): # index_id in is_perm_indexes with patch("weko_index_tree.api.Indexes.get_browsing_tree_paths", return_value=["33", "33/44", "66"]): res = get_permission_filter(33) - assert res == ([Bool(must=[Terms(path=['33'])], should=[Bool(must=[Terms(publish_status=['0', '1']), Match(weko_creator_id=5)]), Bool(must=[Terms(publish_status=['0', '1']), Terms(weko_shared_ids=[5])]), Bool(must=[Terms(publish_status=['0', '1'])])]), Bool(must=[Match(relation_version_is_last='true')])], + assert res == ([Bool(minimum_should_match="1", must=[Terms(path=['33'])], should=[Bool(must=[Terms(publish_status=['0', '1']), Match(weko_creator_id=5)]), Bool(must=[Terms(publish_status=['0', '1']), Terms(weko_shared_ids=[5])]), Bool(must=[Terms(publish_status=['0', '1'])])]), Bool(must=[Match(relation_version_is_last='true')])], ['33', '33/44', '66']) # index_id not in is_perm_indexes with patch("weko_index_tree.api.Indexes.get_browsing_tree_paths", return_value=["33", "33/44", "66"]): res = get_permission_filter(33333) - assert res == ([Bool(must=[Terms(path=[])], should=[Bool(must=[Terms(publish_status=['0', '1']), Match(weko_creator_id=5)]), Bool(must=[Terms(publish_status=['0', '1']), Terms(weko_shared_ids=[5])]), Bool(must=[Terms(publish_status=['0', '1'])])]), Bool(must=[Match(relation_version_is_last='true')])], + assert res == ([Bool(minimum_should_match="1", must=[Terms(path=[])], should=[Bool(must=[Terms(publish_status=['0', '1']), Match(weko_creator_id=5)]), Bool(must=[Terms(publish_status=['0', '1']), Terms(weko_shared_ids=[5])]), Bool(must=[Terms(publish_status=['0', '1'])])]), Bool(must=[Match(relation_version_is_last='true')])], ['33', '33/44', '66']) # not exist index_id with patch("weko_index_tree.api.Indexes.get_browsing_tree_paths", return_value=["33", "33/44", '66']): res = get_permission_filter() - assert res == ([Bool(must=[Terms(path=['33', '44', '66'])], should=[Bool(must=[Terms(publish_status=['0', '1']), Match(weko_creator_id=5)]), Bool(must=[Terms(publish_status=['0', '1']), Terms(weko_shared_ids=[5])]), Bool(must=[Terms(publish_status=['0', '1'])])]), Bool(must=[Match(relation_version_is_last='true')])], ['33', '33/44', '66']) + assert res == ([Bool(minimum_should_match="1", must=[Terms(path=['33', '44', '66'])], should=[Bool(must=[Terms(publish_status=['0', '1']), Match(weko_creator_id=5)]), Bool(must=[Terms(publish_status=['0', '1']), Terms(weko_shared_ids=[5])]), Bool(must=[Terms(publish_status=['0', '1'])])]), Bool(must=[Match(relation_version_is_last='true')])], ['33', '33/44', '66']) # not admin user with patch("flask_login.utils._get_user", return_value=users[1]['obj']): with patch("weko_search_ui.query.check_permission_user",return_value=(users[1]["id"],True)): with i18n_app.test_request_context("/test?search_type=0"): res = get_permission_filter(33) - assert res == ([Bool(must=[Bool()], should=[Bool(must=[Terms(publish_status=['0', '1']), Match(weko_creator_id=2)]), Bool(must=[Terms(publish_status=['0', '1']), Terms(weko_shared_ids=[2])]), Bool(must=[Terms(publish_status=['0']), Range(publish_date={'lte': 'now/d', 'time_zone': 'UTC'})])]), Bool(must=[Match(relation_version_is_last='true')])], []) + assert res == ([Bool(minimum_should_match="1", must=[Bool()], should=[Bool(must=[Terms(publish_status=['0', '1']), Match(weko_creator_id=2)]), Bool(must=[Terms(publish_status=['0', '1']), Terms(weko_shared_ids=[2])]), Bool(must=[Terms(publish_status=['0']), Range(publish_date={'lte': 'now/d', 'time_zone': 'UTC'})])]), Bool(must=[Match(relation_version_is_last='true')])], []) with i18n_app.test_request_context("/test?search_type=2"): res = get_permission_filter(33) - assert res == ([Bool(must=[Terms(path=[])], should=[Bool(must=[Terms(publish_status=['0', '1']), Match(weko_creator_id=2)]), Bool(must=[Terms(publish_status=['0', '1']), Terms(weko_shared_ids=[2])]), Bool(must=[Terms(publish_status=['0']), Range(publish_date={'lte': 'now/d', 'time_zone': 'UTC'})])]), Bool(must=[Match(relation_version_is_last='true')])], []) + assert res == ([Bool(minimum_should_match="1", must=[Terms(path=[])], should=[Bool(must=[Terms(publish_status=['0', '1']), Match(weko_creator_id=2)]), Bool(must=[Terms(publish_status=['0', '1']), Terms(weko_shared_ids=[2])]), Bool(must=[Terms(publish_status=['0']), Range(publish_date={'lte': 'now/d', 'time_zone': 'UTC'})])]), Bool(must=[Match(relation_version_is_last='true')])], []) res = get_permission_filter() - assert res == ([Bool(must=[Terms(path=[])], should=[Bool(must=[Terms(publish_status=['0', '1']), Match(weko_creator_id=2)]), Bool(must=[Terms(publish_status=['0', '1']), Terms(weko_shared_ids=[2])]), Bool(must=[Terms(publish_status=['0']), Range(publish_date={'lte': 'now/d', 'time_zone': 'UTC'})])]), Bool(must=[Match(relation_version_is_last='true')])], []) + assert res == ([Bool(minimum_should_match="1", must=[Terms(path=[])], should=[Bool(must=[Terms(publish_status=['0', '1']), Match(weko_creator_id=2)]), Bool(must=[Terms(publish_status=['0', '1']), Terms(weko_shared_ids=[2])]), Bool(must=[Terms(publish_status=['0']), Range(publish_date={'lte': 'now/d', 'time_zone': 'UTC'})])]), Bool(must=[Match(relation_version_is_last='true')])], []) # is_perm is False with patch('weko_search_ui.query.search_permission.can', return_value=False): with patch("flask_login.utils._get_user", return_value=users[3]['obj']): @@ -123,14 +129,14 @@ def test_get_permission_filter(i18n_app, users, client_request_args, indices): with patch("flask_login.utils._get_user", return_value=users[3]['obj']): with patch("weko_index_tree.api.Indexes.get_browsing_tree_paths", return_value=["33", "33/44"]): res = get_permission_filter(33) - expected = ([Terms(publish_status=['0', '1']), Terms(path=['33']), Bool(must=[Terms(publish_status=['0', '1']), Match(relation_version_is_last='true')])], + expected = ([Bool(minimum_should_match="1", must=[Terms(publish_status=['0', '1']), Terms(path=['33'])], should=[Bool(must=[Terms(publish_status=['0', '1']), Match(relation_version_is_last='true')])])], ['33','33/44']) assert res==expected mock_searchperm = MagicMock(side_effect=MockSearchPerm) with patch('weko_search_ui.query.search_permission', mock_searchperm): with patch("weko_index_tree.api.Indexes.get_browsing_tree_paths", return_value=["33", "33/44"]): res = get_permission_filter() - expected = ([Bool(must=[Terms(path=['33', '44'])], should=[Bool(must=[Terms(publish_status=['0', '1']), Match(weko_creator_id='5')]), Bool(must=[Terms(publish_status=['0', '1']), Terms(weko_shared_ids=['5'])]), Bool(must=[Terms(publish_status=['0', '1'])])]), Bool(must=[Match(relation_version_is_last='true')])], + expected = ([Bool(minimum_should_match="1", must=[Terms(path=['33', '44'])], should=[Bool(must=[Terms(publish_status=['0', '1']), Match(weko_creator_id='5')]), Bool(must=[Terms(publish_status=['0', '1']), Terms(weko_shared_ids=['5'])]), Bool(must=[Terms(publish_status=['0', '1'])])]), Bool(must=[Match(relation_version_is_last='true')])], ['33','33/44']) assert res==expected @@ -239,7 +245,7 @@ def test_default_search_factory(app, users, communities): app.extensions['invenio-queues'] = 1 res = default_search_factory(self=None, search=search) query = (res[0].query()).to_dict() - assert query == {"query": {"bool": {"filter": [{"bool": {"must": [{"bool": {"should": [{"bool": {"must": [{"terms": {"publish_status": ["0", "1"]}}, {"match": {"weko_creator_id": "5"}}]}}, {"bool": {"must": [{"terms": {"publish_status": ["0", "1"]}}, {"terms": {"weko_shared_ids": ["5"]}}]}}, {"bool": {"must": [{"terms": {"publish_status": ["0", "1"]}}]}}], "must": [{"terms": {"path": ["33", "44"]}}]}}, {"bool": {"must": [{"match": {"relation_version_is_last": "true"}}]}}, {"bool": {"should": [{"match": {"language": {"operator": "and", "query": "jpn"}}}, {"bool": {"filter": [{"script": {"script": {"source": "boolean flg=false; for(lang in doc['language']){if (!params.param1.contains(lang)){flg=true;}} return flg;", "params": {"param1": ["jpn", "eng", "fra", "ita", "deu", "spa", "zho", "rus", "lat", "msa", "epo", "ara", "ell", "kor", "other"]}}}}]}}]}}, {"bool": {"should": [{"nested": {"path": "relation.relatedIdentifier", "query": {"bool": {"must": [{"match": {"relation.relatedIdentifier.value": {"operator": "and", "query": "1"}}}]}}}}]}}, {"bool": {"should": [{"nested": {"path": "content", "query": {"bool": {"must": [{"terms": {"content.licensetype.raw": ["test_license"]}}]}}}}]}}, {"nested": {"path": "file.date", "query": {"bool": {"should": [{"term": {"file.date.dateType": "Accepted"}}], "must": [{"range": {"file.date.value": {"gte": "2022-10-01", "lte": "2022-10-30"}}}]}}}}, {"range": {"date_range1": {"gte": "2022-10-01", "lte": "2022-10-30"}}}, {"match": {"text1": {"operator": "and", "query": "test_text"}}}]}}], "must": [{"match_all": {}}]}}, "_source": {"excludes": ["content"]}} + assert query == {"query": {"bool": {"filter": [{"bool": {"must": [{"bool": {"should": [{"bool": {"must": [{"terms": {"publish_status": ["0", "1"]}}, {"match": {"weko_creator_id": "5"}}]}}, {"bool": {"must": [{"terms": {"publish_status": ["0", "1"]}}, {"terms": {"weko_shared_ids": ["5"]}}]}}, {"bool": {"must": [{"terms": {"publish_status": ["0", "1"]}}]}}], "must": [{"terms": {"path": ["33", "44"]}}], "minimum_should_match": "1"}}, {"bool": {"must": [{"match": {"relation_version_is_last": "true"}}]}}, {"bool": {"should": [{"match": {"language": {"operator": "and", "query": "jpn"}}}, {"bool": {"filter": [{"script": {"script": {"source": "boolean flg=false; for(lang in doc['language']){if (!params.param1.contains(lang)){flg=true;}} return flg;", "params": {"param1": ["jpn", "eng", "fra", "ita", "deu", "spa", "zho", "rus", "lat", "msa", "epo", "ara", "ell", "kor", "other"]}}}}]}}]}}, {"bool": {"should": [{"nested": {"path": "relation.relatedIdentifier", "query": {"bool": {"must": [{"match": {"relation.relatedIdentifier.value": {"operator": "and", "query": "1"}}}]}}}}]}}, {"bool": {"should": [{"nested": {"path": "content", "query": {"bool": {"must": [{"terms": {"content.licensetype.raw": ["test_license"]}}]}}}}]}}, {"nested": {"path": "file.date", "query": {"bool": {"should": [{"term": {"file.date.dateType": "Accepted"}}], "must": [{"range": {"file.date.value": {"gte": "2022-10-01", "lte": "2022-10-30"}}}]}}}}, {"range": {"date_range1": {"gte": "2022-10-01", "lte": "2022-10-30"}}}, {"match": {"text1": {"operator": "and", "query": "test_text"}}}]}}], "must": [{"match_all": {}}]}}, "_source": {"excludes": ["content"]}} mock_searchperm = MagicMock(side_effect=MockSearchPerm) @@ -602,7 +608,7 @@ def test_default_search_factory_no_queries(app, users, communities): {'bool': {'must': [{'terms': {'publish_status': ['0', '1']}}, {'terms': {'weko_shared_ids': [None]}}]}}, {'bool': {'must': [{'terms': {'publish_status': ['0']}}, {'range': {'publish_date': {'lte': 'now/d', 'time_zone': 'UTC'}}}]}}], - 'must': [{'terms': {'path': []}}]}} + 'must': [{'terms': {'path': []}}], 'minimum_should_match': '1'}} EXPECT1 = {'bool': {'must': [{'match': {'relation_version_is_last': 'true'}}]}} # _get_keywords_query test (type) diff --git a/modules/weko-search-ui/tests/test_rest.py b/modules/weko-search-ui/tests/test_rest.py index 4c855ef277..0029854433 100644 --- a/modules/weko-search-ui/tests/test_rest.py +++ b/modules/weko-search-ui/tests/test_rest.py @@ -154,13 +154,15 @@ def test_create_blueprint(i18n_app, app, users): endpoints = app.config['WEKO_SEARCH_REST_ENDPOINTS'] assert create_blueprint(app, endpoints) + +# .tox/c1/bin/pytest --cov=weko_search_ui tests/test_rest.py::test_IndexSearchResource_get_facet -vv -s --cov-branch --cov-report=term --basetemp=/code/modules/weko-search-ui/.tox/c1/tmp def test_IndexSearchResource_get_facet(i18n_app,client_rest, db, users, item_type, facet_search_setting): i18n_app.config['WEKO_SEARCH_TYPE_INDEX'] = 'index' sname = current_app.config["SERVER_NAME"] def dummy_response(data): if isinstance(data, str): data = json_data(data) - dummy=response.Response(Search(), data) + dummy=dsl.response.Response(dsl.Search(), data) return dummy param = {"page":"1", "size":"20", @@ -441,11 +443,12 @@ def test_IndexSearchResourceAPI(client_rest, db_register2, db_rocrate_mapping): with open('tests/data/rocrate/search_result.json', 'r') as f: search_result = json.load(f) search = MagicMock() - search.execute = lambda: DummySearchResult(search_result) + search.params.return_value = search + search.execute.return_value = DummySearchResult(search_result) mock_search_factory.return_value = (search, '') with patch('weko_search_ui.rest.get_facet_search_query') as mock_get_facet_search_query: search.aggs = {} - mock_get_facet_search_query.return_value = {"test-weko": {"aggs": {"Data Type": {"filter": {}} }}} + mock_get_facet_search_query.return_value = {"weko": {"aggs": {"Data Type": {"filter": {}} }}} res = client_rest.get(target_url) assert res.status_code == 200 assert search.aggs == {'Data Type': {'filter': {}}} diff --git a/modules/weko-search-ui/tests/test_tasks.py b/modules/weko-search-ui/tests/test_tasks.py index 305179d832..00a11872b6 100644 --- a/modules/weko-search-ui/tests/test_tasks.py +++ b/modules/weko-search-ui/tests/test_tasks.py @@ -33,7 +33,11 @@ def test_check_import_items_task(i18n_app, users, mocker): list_record_has_error = {"list_record": [{"id": 1, "errors": ["test_error"]}], "data_path": "/tmp/data"} mocker.patch("shutil.rmtree", return_value="") - mocker.patch("weko_search_ui.tasks.remove_temp_dir_task.apply_async", return_value="") + mock_apply_async = mocker.patch( + "weko_search_ui.tasks.remove_temp_dir_task.apply_async", + autospec=True + ) + mock_apply_async.return_value = "" with patch("weko_search_ui.tasks.check_tsv_import_items", return_value=check_result_has_error): res = check_import_items_task(file_path=file_path, is_change_identifier=True, host_url="https://localhost") @@ -56,7 +60,7 @@ def test_check_import_items_task(i18n_app, users, mocker): mock_datetime = mocker.patch('weko_search_ui.tasks.datetime', autospec=True) mock_datetime.now.return_value = datetime(2025, 4, 1, 12, 0, 0) - mock_apply_async = mocker.patch('weko_search_ui.tasks.remove_temp_dir_task.apply_async', autospec=True) + mock_apply_async.reset_mock() with patch("weko_search_ui.tasks.check_tsv_import_items", return_value=data): with patch("shutil.rmtree", return_value=""): result = check_import_items_task(file_path=file_path,is_change_identifier=True,host_url="https://localhost") @@ -180,12 +184,23 @@ def test_import_item(i18n_app, users, mocker): mock_datetime = mocker.patch("weko_search_ui.tasks.datetime") mock_datetime.now.return_value = datetime(2025, 1, 1, 12, 00, 00) with patch("flask_login.utils._get_user", return_value=users[3]['obj']): - with patch("weko_search_ui.tasks.import_items_to_system", return_value={}): + mock_success_data = {"success": True, "recid": 123} + with patch("weko_search_ui.tasks.import_items_to_system", return_value=mock_success_data): res = import_item({"item"}, "request_info") - assert res == {"start_date": "2025-01-01 12:00:00"} + # Check that the result contains the expected success data and the correct start date + assert res == { + **mock_success_data, + "start_date": "2025-01-01 12:00:00" + } with patch("weko_search_ui.tasks.import_items_to_system", side_effect=Exception("test error")): res = import_item({"item"}, "request_info") - assert res == None + # Check that the result contains the expected error message and the correct start date + # Note: The error message is "Internal server error" because the exception is caught and a generic error message is returned. + assert res == { + "success": False, + "error_id": "Internal server error", + "start_date": "2025-01-01 12:00:00" + } # def remove_temp_dir_task(path): def test_remove_temp_dir_task(i18n_app, users, indices): @@ -392,33 +407,74 @@ def mock_open(path, encoding=None): # def is_import_running(): # .tox/c1/bin/pytest --cov=weko_search_ui tests/test_tasks.py::test_is_import_running -vv -s --cov-branch --cov-report=term --basetemp=/code/modules/weko-search-ui/.tox/c1/tmp def test_is_import_running(i18n_app): + # Mock the Celery inspect.active and inspect.reserved methods to return different scenarios mock_task_valid = {"celery@worker1": [{"name": "weko_search_ui.tasks.import_item"}]} mock_task_invalid = {"celery@worker1": [{"name": "invalid_task_name"}]} - with patch("weko_search_ui.tasks.check_celery_is_run", return_value=True): - with patch("celery.task.control.inspect.active", return_value=MagicMock()): - with patch("celery.task.control.inspect.reserved", return_value=MagicMock()): - assert is_import_running() == None - with patch("celery.task.control.inspect.reserved", return_value=mock_task_valid): - assert is_import_running() == "is_import_running" - with patch("celery.task.control.inspect.reserved", return_value=mock_task_invalid): - assert is_import_running() == None - with patch("celery.task.control.inspect.active", return_value=mock_task_valid): - assert is_import_running() == "is_import_running" - with patch("celery.task.control.inspect.active", return_value=mock_task_invalid): - with patch("celery.task.control.inspect.reserved", return_value=MagicMock()): - assert is_import_running() == None - with patch("weko_search_ui.tasks.check_celery_is_run", return_value=False): + + # Mock the Celery inspect.active method to return False + mock_inspect = MagicMock() + mock_inspect.active.return_value = MagicMock() + mock_inspect.reserved.return_value = MagicMock() + mock_inspect.ping.return_value = True + + mock_ext = MagicMock() + mock_ext.celery.control.inspect.return_value = mock_inspect + + with patch.dict( + i18n_app.extensions, {"invenio-celery": mock_ext}, clear=False + ): + # Test when the Celery inspect.active method and inspect.reserved returns MagicMock (no tasks running) + assert is_import_running() == False + + # Test when the Celery inspect.active method returns a valid task + mock_inspect.reserved.return_value = mock_task_valid + assert is_import_running() == "is_import_running" + + # Test when the Celery inspect.active method returns an invalid task + mock_inspect.reserved.return_value = mock_task_invalid + assert is_import_running() == False + + # Test when the Celery inspect.active method returns a valid task and inspect.reserved returns an invalid task + mock_inspect.active.return_value = mock_task_valid + assert is_import_running() == "is_import_running" + + # Test when the Celery inspect.active method returns an invalid task and inspect.reserved returns a valid task + mock_inspect.active.return_value = mock_task_invalid + mock_inspect.reserved.return_value = MagicMock() + assert is_import_running() == False + + # Test when the "invenio-celery" extension is not present in i18n_app.extensions + with patch.dict(i18n_app.extensions, {"invenio-celery": None}, clear=False): + assert is_import_running() == "celery_not_run" + + # Test when the Celery inspect.ping method returns False + mock_inspect.ping.return_value = False + with patch.dict(i18n_app.extensions, {"invenio-celery": mock_ext}, clear=False): assert is_import_running() == "celery_not_run" # def check_celery_is_run(): # .tox/c1/bin/pytest --cov=weko_search_ui tests/test_tasks.py::test_check_celery_is_run -vv -s --cov-branch --cov-report=term --basetemp=/code/modules/weko-search-ui/.tox/c1/tmp def test_check_celery_is_run(i18n_app): - with patch("celery.task.control.inspect.ping",return_value={'hostname': True}): - assert check_celery_is_run()==True - - with patch("celery.task.control.inspect.ping",return_value={}): - assert check_celery_is_run()==False + # Mock the Celery inspect.active method to return False + mock_inspect = MagicMock() + + mock_ext = MagicMock() + mock_ext.celery.control.inspect.return_value = mock_inspect + + # Test when the Celery inspect.ping method returns True + mock_inspect.ping.return_value = True + with patch.dict( + i18n_app.extensions, {"invenio-celery": mock_ext}, clear=False + ): + assert check_celery_is_run() == True + + # Test when the Celery inspect.ping method returns False + mock_inspect.ping.return_value = False + with patch.dict( + i18n_app.extensions, {"invenio-celery": mock_ext}, clear=False + ): + assert check_celery_is_run() == False class TestCheckSessionLifetime(unittest.TestCase): diff --git a/modules/weko-search-ui/tests/test_utils.py b/modules/weko-search-ui/tests/test_utils.py index cf97e39596..0d58481c9e 100644 --- a/modules/weko-search-ui/tests/test_utils.py +++ b/modules/weko-search-ui/tests/test_utils.py @@ -24,6 +24,7 @@ from invenio_pidstore.models import PersistentIdentifier, PIDStatus, Redirect from invenio_pidrelations.models import PIDRelation from invenio_pidstore.errors import PIDDoesNotExistError +from invenio_search.engine import search from io import StringIO from opensearchpy import helpers, OpenSearchException, NotFoundError from opensearch_dsl import Search @@ -896,27 +897,29 @@ def test_make_file_info(tmpdir): # def getEncode(filepath): -def test_getEncode(): - csv_files = [ - {"file": "eucjp_lf_items.csv", "enc": "euc-jp"}, - {"file": "iso2022jp_lf_items.csv", "enc": "iso-2022-jp"}, - {"file": "sjis_lf_items.csv", "enc": "shift_jis"}, - {"file": "utf8_cr_items.csv", "enc": "utf-8"}, - {"file": "utf8_crlf_items.csv", "enc": "utf-8"}, - {"file": "utf8_lf_items.csv", "enc": "utf-8"}, - {"file": "utf8bom_lf_items.csv", "enc": "utf-8-sig"}, - {"file": "utf16be_bom_lf_items.csv", "enc": "utf-16"}, - {"file": "utf16le_bom_lf_items.csv", "enc": "utf-16"}, +# .tox/c1/bin/pytest --cov=weko_search_ui tests/test_utils.py::test_getEncode -vv -s --cov-branch --cov-report=term --basetemp=/code/modules/weko-search-ui/.tox/c1/tmp +@pytest.mark.parametrize( + "file, expected_encoding", + [ + pytest.param("eucjp_lf_items.csv", "euc-jp", id="eucjp"), + pytest.param("iso2022jp_lf_items.csv", "iso-2022-jp", id="iso2022jp"), + pytest.param("sjis_lf_items.csv", "shift_jis", id="sjis"), + pytest.param("utf8_cr_items.csv", "utf-8", id="utf8_cr"), + pytest.param("utf8_crlf_items.csv", "utf-8", id="utf8_crlf"), + pytest.param("utf8_lf_items.csv", "utf-8", id="utf8_lf"), + pytest.param("utf8bom_lf_items.csv", "utf-8-sig", id="utf8bom_lf"), + pytest.param("utf16be_bom_lf_items.csv", "utf-16", id="utf16be_bom_lf"), + pytest.param("utf16le_bom_lf_items.csv", "utf-16", id="utf16le_bom_lf"), # {"file":"utf32be_bom_lf_items.csv","enc":"utf-32"}, # {"file":"utf32le_bom_lf_items.csv","enc":"utf-32"}, - {"file": "big5.txt", "enc": "tis-620"}, + pytest.param("big5.txt", "tis-620", id="tis-620"), ] - - for f in csv_files: - filepath = os.path.join( - os.path.dirname(os.path.realpath(__file__)), "data", "csv", f["file"] - ) - assert getEncode(filepath).lower() == f["enc"] +) +def test_getEncode(file, expected_encoding): + filepath = os.path.join( + os.path.dirname(os.path.realpath(__file__)), "data", "csv", file + ) + assert getEncode(filepath).lower() == expected_encoding # def read_stats_file(file_path: str, file_name: str, file_format: str) -> dict: @@ -1195,7 +1198,7 @@ def test_handle_check_duplicate_record(app): # def handle_check_exist_record(list_record) -> list: # .tox/c1/bin/pytest --cov=weko_search_ui tests/test_utils.py::test_handle_check_exist_record -vv -s --cov-branch --cov-report=term --basetemp=/code/modules/weko-search-ui/.tox/c1/tmp -def test_handle_check_exist_record(app): +def test_handle_check_exist_record(app, db): case = unittest.TestCase() # case 1 import new items filepath = os.path.join( @@ -1489,7 +1492,9 @@ def find_and_update_location_size(): loc.size = row[1] """ # .tox/c1/bin/pytest --cov=weko_search_ui tests/test_utils.py::test_register_item_metadata -vv -s --cov-branch --cov-report=term --basetemp=/code/modules/weko-search-ui/.tox/c1/tmp -def test_register_item_metadata(i18n_app, es_item_file_pipeline, deposit, search_records, mocker): +def test_register_item_metadata( + i18n_app, search_item_file_pipeline, deposit, search_records, mocker +): item = search_records["results"][0]["item"] root_path = os.path.dirname(os.path.abspath(__file__)) item["$schema"] = "/items/jsonschema/1000" @@ -1500,7 +1505,10 @@ def test_register_item_metadata(i18n_app, es_item_file_pipeline, deposit, search # .tox/c1/bin/pytest --cov=weko_search_ui tests/test_utils.py::test_register_item_metadata2 -vv -s --cov-branch --cov-report=term --basetemp=/code/modules/weko-search-ui/.tox/c1/tmp -def test_register_item_metadata2(i18n_app, es_item_file_pipeline, deposit, search_records, db_index, open_search, db, mocker): +def test_register_item_metadata2( + i18n_app, search_item_file_pipeline, deposit, search_records, db_index, + open_search, db, mocker +): item = search_records["results"][0]["item"] item["item_type_id"] = 1000 item["$schema"] = "/items/jsonschema/1000" @@ -1525,7 +1533,10 @@ def test_register_item_metadata2(i18n_app, es_item_file_pipeline, deposit, searc # .tox/c1/bin/pytest --cov=weko_search_ui tests/test_utils.py::test_register_item_metadata3 -vv -s --cov-branch --cov-report=term --basetemp=/code/modules/weko-search-ui/.tox/c1/tmp # @pytest.mark.parametrize('order_if', [1,2]) @pytest.mark.parametrize('order_if', [1,2,3,4]) -def test_register_item_metadata3(i18n_app, es_item_file_pipeline, deposit, search_records2, db_index, open_search, db, mocker, order_if): +def test_register_item_metadata3( + i18n_app, search_item_file_pipeline, deposit, search_records2, db_index, + open_search, db, mocker, order_if +): item = search_records2["results"][0]["item"] item["item_type_id"] = 1000 item["$schema"] = "/items/jsonschema/1000" @@ -1583,7 +1594,9 @@ def test_register_item_metadata3(i18n_app, es_item_file_pipeline, deposit, searc # def update_publish_status(item_id, status): # .tox/c1/bin/pytest --cov=weko_search_ui tests/test_utils.py::test_update_publish_status -vv -s --cov-branch --cov-report=term --basetemp=/code/modules/weko-search-ui/.tox/c1/tmp -def test_update_publish_status(i18n_app, es_item_file_pipeline, search_records): +def test_update_publish_status( + i18n_app, search_item_file_pipeline, search_records +): item_id = 1 status = None @@ -1593,7 +1606,9 @@ def test_update_publish_status(i18n_app, es_item_file_pipeline, search_records): # def handle_workflow(item: dict): # .tox/c1/bin/pytest --cov=weko_search_ui tests/test_utils.py::test_handle_workflow -vv -s --cov-branch --cov-report=term --basetemp=/code/modules/weko-search-ui/.tox/c1/tmp -def test_handle_workflow(i18n_app, es_item_file_pipeline, search_records, db): +def test_handle_workflow( + i18n_app, search_item_file_pipeline, search_records, db +): item = search_records["results"][0]["item"] with patch( @@ -1640,14 +1655,14 @@ def test_handle_metadata_amend_by_doi(): assert item["metadata"] == metadata # def create_work_flow(item_type_id): -# .tox/c1/bin/pytest --cov=weko_search_ui tests/test_utils.py::test_update_publish_status -vv -s --cov-branch --cov-report=term --basetemp=/code/modules/weko-search-ui/.tox/c1/tmp +# .tox/c1/bin/pytest --cov=weko_search_ui tests/test_utils.py::test_create_work_flow -vv -s --cov-branch --cov-report=term --basetemp=/code/modules/weko-search-ui/.tox/c1/tmp def test_create_work_flow(i18n_app, db_itemtype, db_workflow): # Doesn't return any value assert not create_work_flow(db_itemtype["item_type"].id) # def create_flow_define(): -# .tox/c1/bin/pytest --cov=weko_search_ui tests/test_utils.py::test_update_publish_status -vv -s --cov-branch --cov-report=term --basetemp=/code/modules/weko-search-ui/.tox/c1/tmp +# .tox/c1/bin/pytest --cov=weko_search_ui tests/test_utils.py::test_create_flow_define -vv -s --cov-branch --cov-report=term --basetemp=/code/modules/weko-search-ui/.tox/c1/tmp def test_create_flow_define(i18n_app, db_workflow): # Doesn't return anything assert not create_flow_define() @@ -1656,7 +1671,8 @@ def test_create_flow_define(i18n_app, db_workflow): # def send_item_created_event_to_search(item, request_info): *** ERR # .tox/c1/bin/pytest --cov=weko_search_ui tests/test_utils.py::test_send_item_created_event_to_search -vv -s --cov-branch --cov-report=term --basetemp=/code/modules/weko-search-ui/.tox/c1/tmp def test_send_item_created_event_to_search( - i18n_app, es_item_file_pipeline, search_records, client_request_args, users, open_search + i18n_app, search_item_file_pipeline, search_records, client_request_args, + users, open_search ): # with patch("weko_search_ui.utils.send_item_created_event_to_search._push_item_to_search", return_value=""): # with patch("weko_search_ui.utils._push_item_to_search", return_value=""): @@ -1673,7 +1689,9 @@ def test_send_item_created_event_to_search( # def import_items_to_system(item: dict, request_info=None, is_gakuninrdm=False): ERROR = TypeError: handle_remove_search_metadata() missing 2 required positional arguments: 'bef_metadata' and 'bef_las... # .tox/c1/bin/pytest --cov=weko_search_ui tests/test_utils.py::test_import_items_to_system -vv -s --cov-branch --cov-report=term --basetemp=/code/modules/weko-search-ui/.tox/c1/tmp -def test_import_items_to_system(i18n_app, db, es_item_file_pipeline, search_records, app, mocker): +def test_import_items_to_system( + i18n_app, db, search_item_file_pipeline, search_records, app, mocker +): item = search_records["results"][0]["item"] db.session.commit() with patch("weko_search_ui.utils.register_item_metadata", return_value={}), \ @@ -1780,7 +1798,9 @@ def test_import_items_to_system(i18n_app, db, es_item_file_pipeline, search_reco # def import_items_to_activity(item, request_info): # .tox/c1/bin/pytest --cov=weko_search_ui tests/test_utils.py::test_import_items_to_activity -vv -s --cov-branch --cov-report=term --basetemp=/code/modules/weko-search-ui/.tox/c1/tmp -def test_import_items_to_activity(i18n_app, es_item_file_pipeline, search_records, db_workflow, mocker): +def test_import_items_to_activity( + i18n_app, search_item_file_pipeline, search_records, db_workflow, mocker +): mock_auto = mocker.patch("weko_workflow.headless.HeadlessActivity.auto", return_value=("test/A-TEST-1", "end_action", "2000001")) item = search_records["results"][0]["item"] @@ -1835,7 +1855,9 @@ def test_import_items_to_activity(i18n_app, es_item_file_pipeline, search_record # def delete_items_with_activity(item_id, request_info): # .tox/c1/bin/pytest --cov=weko_search_ui tests/test_utils.py::test_delete_items_with_activity -vv -s --cov-branch --cov-report=term --basetemp=/code/modules/weko-search-ui/.tox/c1/tmp -def test_delete_items_with_activity(i18n_app, es_item_file_pipeline, search_records, db_workflow, mocker): +def test_delete_items_with_activity( + i18n_app, search_item_file_pipeline, search_records, db_workflow, mocker +): request_info = { "user_id": 1, "shared_ids": None, @@ -1873,7 +1895,9 @@ def test_delete_items_with_activity(i18n_app, es_item_file_pipeline, search_reco # def handle_item_title(list_record): -def test_handle_item_title(i18n_app, db_itemtype, es_item_file_pipeline, search_records): +def test_handle_item_title( + i18n_app, db_itemtype, search_item_file_pipeline, search_records +): list_record = [search_records["results"][0]["item"]] list_record[0]["item_type_id"] = db_itemtype["item_type"].id @@ -2115,7 +2139,9 @@ def test_handle_check_cnri_2(i18n_app): # def handle_check_doi_indexes(list_record): -def test_handle_check_doi_indexes(i18n_app, es_item_file_pipeline, search_records): +def test_handle_check_doi_indexes( + i18n_app, search_item_file_pipeline, search_records +): list_record = [search_records["results"][0]["item"]] # Doesn't return any value @@ -2124,7 +2150,9 @@ def test_handle_check_doi_indexes(i18n_app, es_item_file_pipeline, search_record # def handle_check_doi_ra(list_record): # .tox/c1/bin/pytest --cov=weko_search_ui tests/test_utils.py::test_handle_check_doi_ra -vv -s --cov-branch --cov-report=term --basetemp=/code/modules/weko-search-ui/.tox/c1/tmp -def test_handle_check_doi_ra(app, db, es_item_file_pipeline, search_records,identifier, mocker): +def test_handle_check_doi_ra( + app, db, search_item_file_pipeline, search_records,identifier, mocker +): # list_record = [search_records['results'][0]['item']] item = MagicMock() @@ -2832,7 +2860,9 @@ def test_handle_check_operation_flags(tmpdir): assert len(os.listdir(tmp_dir)) == 6 # def register_item_handle(item): -def test_register_item_handle(i18n_app, es_item_file_pipeline, search_records): +def test_register_item_handle( + i18n_app, search_item_file_pipeline, search_records +): item = search_records["results"][0]["item"] assert not register_item_handle(item) @@ -3104,7 +3134,7 @@ def test_register_item_doi(i18n_app, db_activity, identifier, mocker): # def register_item_update_publish_status(item, status): def test_register_item_update_publish_status( - i18n_app, es_item_file_pipeline, search_records + i18n_app, search_item_file_pipeline, search_records ): item = search_records["results"][0]["item"] # item = db_activity['item'] @@ -3118,7 +3148,7 @@ def test_register_item_update_publish_status( # def handle_doi_required_check(record): def test_handle_doi_required_check( i18n_app, - es_item_file_pipeline, + search_item_file_pipeline, search_records, record_with_metadata, db_itemtype, @@ -4449,12 +4479,15 @@ def test_cancel_export_all(i18n_app, users, redis_connect, mocker): } datastore = redis_connect datastore.put(cache_key, "test_task_key".encode("utf-8"), ttl_secs=30) + # Create a mock control object and set it to current_app.control + mock_revoke = mocker.patch( + "celery.current_app.control.revoke", return_value=MagicMock() + ) # export_status is True with patch("weko_search_ui.utils.get_export_status", return_value=(True,None,None,None,None,None,None)), \ patch("weko_search_ui.utils.shutil.rmtree") as mock_rmtree: datastore.put(file_cache_key, json.dumps(file_json).encode('utf-8'), ttl_secs=30) - mock_revoke = mocker.patch("weko_search_ui.utils.revoke") mock_delete_id = mocker.patch("weko_search_ui.utils.delete_task_id_cache_on_revoke.apply_async") result = cancel_export_all() assert result == True @@ -4463,10 +4496,11 @@ def test_cancel_export_all(i18n_app, users, redis_connect, mocker): mock_revoke.assert_called_with("test_task_key",terminate=True) mock_delete_id.assert_called_with(args=("test_task_key","admin_cache_KEY_EXPORT_ALL_5"),countdown=60) mock_rmtree.assert_called_with('export/path/test.zip') + # export_status is False + mock_revoke.reset_mock() with patch("weko_search_ui.utils.get_export_status", return_value=(False,None,None,None,None,None,None)): datastore.put(file_cache_key, json.dumps(file_json).encode('utf-8'), ttl_secs=30) - mock_revoke = mocker.patch("weko_search_ui.utils.revoke") mock_delete_id = mocker.patch("weko_search_ui.utils.delete_task_id_cache_on_revoke.apply_async") result = cancel_export_all() assert result == True @@ -4735,7 +4769,9 @@ def test_handle_check_item_is_locked(i18n_app, db_activity): # def handle_remove_search_metadata(item, bef_metadata, bef_last_ver_metadata): -def test_handle_remove_search_metadata(i18n_app, es_item_file_pipeline, search_records): +def test_handle_remove_search_metadata( + i18n_app, search_item_file_pipeline, search_records +): item = search_records["results"][0]["item"] bef_metadata = {} bef_metadata["_id"] = 9 @@ -4782,7 +4818,8 @@ def test_function(): with pytest.raises(Forbidden): test_function() -# .tox/c1/bin/pytest --cov=weko_search_ui tests/test_utils.py::test_check_index_access_permissions_issue_50659 -vv -s --cov-branch --cov-report=term --basetemp=/code/modules/weko-search_ui/.tox/c1/tmp + +# .tox/c1/bin/pytest --cov=weko_search_ui tests/test_utils.py::test_check_index_access_permissions_issue_50659 -vv -s --cov-branch --cov-report=term --basetemp=/code/modules/weko-search-ui/.tox/c1/tmp def test_check_index_access_permissions_issue_50659(i18n_app, client_request_args, users): @check_index_access_permissions def test_function(): diff --git a/modules/weko-search-ui/tests/test_views.py b/modules/weko-search-ui/tests/test_views.py index 48582ce214..65598d8ff8 100644 --- a/modules/weko-search-ui/tests/test_views.py +++ b/modules/weko-search-ui/tests/test_views.py @@ -53,7 +53,7 @@ def test_search_acl_guest(app,client,db_register2,index_style,users,db_register) assert ret.status_code == 404 - +# .tox/c1/bin/pytest --cov=weko_search_ui tests/test_views.py::test_search_acl -vv -s --cov-branch --cov-report=term --basetemp=/code/modules/weko-search-ui/.tox/c1/tmp @pytest.mark.parametrize( "id, status_code", [ @@ -67,7 +67,10 @@ def test_search_acl_guest(app,client,db_register2,index_style,users,db_register) # (7, 302), ], ) -def test_search_acl(app,client,db_register2,index_style,users,db_register,id,status_code): +def test_search_acl( + app, client, db_register2, index_style, users, db_register, + without_remove_session, id, status_code, +): url = url_for("weko_search_ui.search", _external=True) with patch("flask_login.utils._get_user", return_value=users[id]['obj']): with patch("flask.templating._render", return_value=""): diff --git a/modules/weko-search-ui/weko_search_ui/tasks.py b/modules/weko-search-ui/weko_search_ui/tasks.py index dbb7dd3d52..c42069027d 100644 --- a/modules/weko-search-ui/weko_search_ui/tasks.py +++ b/modules/weko-search-ui/weko_search_ui/tasks.py @@ -299,12 +299,19 @@ def is_import_running(): celery_app = current_app.extensions.get('invenio-celery') _timeout = current_app.config.get("CELERY_GET_STATUS_TIMEOUT", 3.0) - inspect = celery_app.celery.control.inspect(timeout=_timeout) - if celery_app is None or not inspect.ping(): + if celery_app is None: current_app.logger.error("Celery app is not initialized.") return "celery_not_run" + # Check if Celery is running by pinging the workers + inspect = celery_app.celery.control.inspect(timeout=_timeout) + if not inspect.ping(): + current_app.logger.error( + "Ping to Celery workers failed. Celery may not be running." + ) + return "celery_not_run" + active = inspect.active() for tasks in active.values(): for task in tasks: