Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions dingo/model/rule/scibase/rule_paper.py
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,8 @@ def _check_version(version: Any) -> ValidationResult:
return BaseFieldValidation._fail("version.null", "version is null")
if not isinstance(version, str):
return BaseFieldValidation._fail("version.wrong_type", "version must be a string")
if version == "":
return BaseFieldValidation._ok()
if version not in {
"publishedVersion", "acceptedVersion", "submittedVersion",
}:
Expand All @@ -231,7 +233,9 @@ def _check_version(version: Any) -> ValidationResult:
def _check_is_published(is_published: Any, version: Any) -> ValidationResult:
if is_published is None:
return BaseFieldValidation._ok()
if not isinstance(is_published, bool):
if isinstance(is_published, bool) or (isinstance(is_published, int) and is_published in (0, 1)):
is_published = bool(is_published)
else:
return BaseFieldValidation._fail(
"is_published.wrong_type", "is_published must be a boolean",
)
Expand All @@ -252,7 +256,9 @@ def _check_is_published(is_published: Any, version: Any) -> ValidationResult:
def _check_is_accepted(is_accepted: Any, version: Any) -> ValidationResult:
if is_accepted is None:
return BaseFieldValidation._ok()
if not isinstance(is_accepted, bool):
if isinstance(is_accepted, bool) or (isinstance(is_accepted, int) and is_accepted in (0, 1)):
is_accepted = bool(is_accepted)
else:
return BaseFieldValidation._fail(
"is_accepted.wrong_type", "is_accepted must be a boolean",
)
Expand Down
8 changes: 4 additions & 4 deletions docs/scibase/rule_paper_labels.md
Original file line number Diff line number Diff line change
Expand Up @@ -284,10 +284,10 @@
| `source.missing_key` | 来源 key 缺失 | 非空 `source` 缺少以下任一 key 时逐项标记:`id`、`display_name`、`issn_l`、`issn`、`is_oa`、`is_in_doaj`、`is_core`、`listed_in`、`type`、`host_organization`、`host_organization_name`、`host_organization_lineage`、`host_organization_lineage_names`。允许额外 key。 |
| `version.null` | 版本为 null | `version` 为 `null` 时标记,先于类型检查。 |
| `version.wrong_type` | 版本类型错误 | `version` 不是字符串时标记,包括布尔值、数字、列表和对象。`null` 单独标记为 `version.null`。 |
| `version.unsupported_value` | 版本不合法 | 字符串 `version` 仅允许 `publishedVersion`、`acceptedVersion`、`submittedVersion`;区分大小写,空字符串及其他字符串均不合法。 |
| `is_published.wrong_type` | 发布标记类型错误 | `is_published` 不是布尔值时标记;字符串和整数 0/1 均不符合要求。`null` 允许通过,不再检查与 `version` 的一致性。 |
| `is_published.version_mismatch` | 发布标记与版本不一致 | `is_published` 非 `null` 时,`version` 为 `publishedVersion` 必须为 true;其他情况必须为 false,包括版本缺失或非法。 |
| `is_accepted.wrong_type` | 接收标记类型错误 | `is_accepted` 不是布尔值时标记;字符串和整数 0/1 均不符合要求。`null` 允许通过,不再检查与 `version` 的一致性。 |
| `version.unsupported_value` | 版本不合法 | 字符串 `version` 仅允许 `publishedVersion`、`acceptedVersion`、`submittedVersion`;空字符串 `""` 允许通过,不再检查取值。区分大小写,其他字符串不合法。 |
| `is_published.wrong_type` | 发布标记类型错误 | `is_published` 不是布尔值,也不是整数 `0`/`1` 时标记。整数 `0` 视为 false,`1` 视为 true;字符串仍不符合要求。`null` 允许通过,不再检查与 `version` 的一致性。 |
| `is_published.version_mismatch` | 发布标记与版本不一致 | `is_published` 非 `null` 时,`version` 为 `publishedVersion` 必须为 true 或 `1`;其他情况必须为 false 或 `0`,包括版本缺失或非法。 |
| `is_accepted.wrong_type` | 接收标记类型错误 | `is_accepted` 不是布尔值,也不是整数 `0`/`1` 时标记。整数 `0` 视为 false,`1` 视为 true;字符串仍不符合要求。`null` 允许通过,不再检查与 `version` 的一致性。 |
| `is_accepted.version_mismatch` | 接收标记与版本不一致 | `is_accepted` 非 `null` 时,`version` 为 `publishedVersion` 或 `acceptedVersion` 必须为 true;其他情况必须为 false,包括版本缺失或非法。 |

逐项累积错误;缺失 key 时跳过该 key 的值检查,继续检查其他 key 和后续对象。标签与原因逐条对应,同类错误可出现多次,原因中包含对象索引和 key。
Expand Down
47 changes: 39 additions & 8 deletions test/scripts/model/rule/test_rule_paper.py
Original file line number Diff line number Diff line change
Expand Up @@ -676,15 +676,15 @@ def test_paper_locations_source_allows_extra_keys_without_value_constraints():
assert rule_paper.check_locations([location]) == (False, [], [])


@pytest.mark.parametrize("version", ["publishedVersion", "acceptedVersion", "submittedVersion"])
@pytest.mark.parametrize("version", ["publishedVersion", "acceptedVersion", "submittedVersion", ""])
def test_paper_locations_version_accepts_supported_values(version):
location = {**_paper_location(), "version": version,
"is_published": version == "publishedVersion", "is_accepted": version != "submittedVersion"}
"is_published": version == "publishedVersion", "is_accepted": version != "submittedVersion" and version != ""}
assert rule_paper.check_locations([location]) == (False, [], [])


@pytest.mark.parametrize("version", [
None, "", True, 1, [], {}, "publishedversion", "PublishedVersion", "publishedVersion ", "invalid",
None, True, 1, [], {}, "publishedversion", "PublishedVersion", "publishedVersion ", "invalid",
])
def test_paper_locations_version_errors_accumulate(version):
location = {**_paper_location(), "source": {}, "version": version, "is_published": False, "is_accepted": False}
Expand All @@ -709,7 +709,7 @@ def test_paper_locations_version_errors_accumulate(version):


@pytest.mark.parametrize("version", ["publishedVersion", "acceptedVersion", "submittedVersion"])
@pytest.mark.parametrize("is_published", [True, False])
@pytest.mark.parametrize("is_published", [True, False, 1, 0])
def test_paper_locations_is_published_version_consistency(version, is_published):
result = rule_paper.check_locations([
{**_paper_location(), "version": version, "is_published": is_published,
Expand All @@ -729,14 +729,45 @@ def test_paper_locations_is_published_version_consistency(version, is_published)
assert result == (False, [], [])


@pytest.mark.parametrize("is_published", [0, 1, "true", "false", "", [], {}])
@pytest.mark.parametrize("is_published", ["true", "false", "0", "1", "", [], {}, 2])
def test_paper_locations_is_published_requires_boolean(is_published):
result = rule_paper.check_locations([{**_paper_location(), "is_published": is_published}])
assert result == (
True, ["is_published.wrong_type"], ["item[0].is_published must be a boolean"],
)


def test_paper_locations_integer_flags_match_submitted_version():
location = {
"is_oa": "false",
"license": "",
"pdf_url": "",
"landing_page_url": "https://doaj.org/article/c199c1dcb72149af9106380d44399d70",
"source_url": "",
"origin_url": "",
"source": {
"id": "https://openalex.org/S4306401280",
"issn_l": None,
"issn": None,
"display_name": "DOAJ (DOAJ: Directory of Open Access Journals)",
"is_oa": 0,
"is_in_doaj": 0,
"type": "repository",
"listed_in": [],
"is_core": 0,
"host_organization": None,
"host_organization_name": None,
"host_organization_lineage": [],
"host_organization_lineage_names": [],
},
"version": "submittedVersion",
"is_accepted": 0,
"is_published": 0,
"meta_db_name": "openalex",
}
assert rule_paper.check_locations([location]) == (False, [], [])


def test_paper_locations_is_published_allows_null():
assert rule_paper.check_locations([{**_paper_location(), "is_published": None}]) == (False, [], [])

Expand Down Expand Up @@ -765,7 +796,7 @@ def test_paper_locations_is_published_errors_accumulate_and_missing_version_is_s


@pytest.mark.parametrize("version", ["publishedVersion", "acceptedVersion", "submittedVersion"])
@pytest.mark.parametrize("is_accepted", [True, False])
@pytest.mark.parametrize("is_accepted", [True, False, 1, 0])
def test_paper_locations_is_accepted_version_consistency(version, is_accepted):
result = rule_paper.check_locations([
{**_paper_location(), "version": version, "is_accepted": is_accepted,
Expand All @@ -785,7 +816,7 @@ def test_paper_locations_is_accepted_version_consistency(version, is_accepted):
assert result == (False, [], [])


@pytest.mark.parametrize("is_accepted", [0, 1, "true", "false", "", [], {}])
@pytest.mark.parametrize("is_accepted", ["true", "false", "0", "1", "", [], {}, 2])
def test_paper_locations_is_accepted_requires_boolean(is_accepted):
assert rule_paper.check_locations([{**_paper_location(), "is_accepted": is_accepted}]) == (
True, ["is_accepted.wrong_type"], ["item[0].is_accepted must be a boolean"],
Expand Down Expand Up @@ -821,7 +852,7 @@ def test_paper_locations_is_accepted_errors_accumulate_and_missing_version_is_sa

def test_paper_locations_reports_independent_type_labels_for_all_value_checks():
location = {**_paper_location(), "license": [], "is_oa": True, "source": [],
"version": {}, "is_published": 1, "is_accepted": "true"}
"version": {}, "is_published": 2, "is_accepted": "true"}
model = rule_paper.RulePaperFieldValidation()
model.dynamic_config = model.dynamic_config.model_copy(deep=True)
model.dynamic_config.key_list = ["locations"]
Expand Down
Loading