Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add test to avoid removal of unit external ids #20

Closed
wants to merge 3 commits into from
Closed
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
31 changes: 31 additions & 0 deletions .github/workflows/python-ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
name: python-ci-main

on:
pull_request:
paths:
- '/versions/v1/units.json'
branches:
- main

jobs:
compare-json:
runs-on: ubuntu-latest
steps:
- name: Checkout main
uses: actions/checkout@v3
with:
ref: main
path: main

- name: Checkout PR
uses: actions/checkout@v3
with:
path: pr

- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: '3.10'

- name: Compare JSON
run: python ./src/compare_external_ids.py
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ To ensure the integrity of the catalog, the following tests are conducted:
5. **Consistent References**: All quantity references in `unitSystems.json` must exist in `units.json`.
6. **Unique aliases**: All pairs of (`alias` and `quantity`) must be unique, for all **aliases** in `aliasNames`.
7. **ExternalId Format**: All unit `externalIds` must follow the pattern `{quantity}:{unit}`, where both `quantity` and `unit` are in **snake_case**.
8. **Avoid breaking changes**: There can be no removals of unit `externalIds` in `units.json`. Only additions are supported.

## Attribution
Some of the units are sourced from QUDT.org, which is licensed under the [Creative Commons Attribution 4.0 International License](https://creativecommons.org/licenses/by/4.0/).
Expand Down
20 changes: 20 additions & 0 deletions src/compare_external_ids.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import json


def get_external_ids(file_path: str) -> set:
with open(file_path, "r") as f:
data = json.load(f)
return set(item["externalId"] for item in data)


main_external_ids: set = get_external_ids("main/versions/v1/units.json")
pr_external_ids: set = get_external_ids("pr/versions/v1/units.json")

# 8. Avoid breaking changes: There can be no removals of unit `externalIds` in `units.json`.
# Only additions are supported.
removed_external_ids: set = main_external_ids - pr_external_ids

if removed_external_ids:
raise Exception(
f'externalId(s) [{", ".join(removed_external_ids)}] have been removed from the unit catalogue.'
)
49 changes: 0 additions & 49 deletions versions/v1/units.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,55 +47,6 @@
"source": "qudt.org",
"sourceReference": "http://qudt.org/vocab/unit/FT-PER-SEC2"
},
{
"externalId": "acceleration:in-per-sec2",
"name": "IN-PER-SEC2",
"quantity": "Acceleration",
"longName": "Inch per Square second",
"aliasNames": [
"in per square second",
"Inch / SEC2",
"IN / square second",
"IN PER SEC2",
"inch / SEC2",
"Inch per s²",
"in / Square Second",
"inch / s²",
"in / square second",
"Inch per Square Second",
"Inch / Square Second",
"Inch / square second",
"Inch / s²",
"inch / Square Second",
"in / SEC2",
"in / s²",
"inch / square second",
"inch per square second",
"Inch per Square second",
"Inch per SEC2",
"in per Square Second",
"Inch per square second",
"inch per Square Second",
"IN per Square Second",
"inch per s²",
"in per s²",
"IN / s²",
"IN per s²",
"inch per SEC2",
"IN / Square Second",
"in per SEC2",
"IN per square second",
"IN per SEC2",
"IN / SEC2"
],
"symbol": "in/s²",
"conversion": {
"multiplier": 0.0254,
"offset": 0.0
},
"source": "qudt.org",
"sourceReference": "http://qudt.org/vocab/unit/IN-PER-SEC2"
},
{
"externalId": "acceleration:m-per-sec2",
"name": "M-PER-SEC2",
Expand Down