Skip to content

Commit

Permalink
Update EntitySchemaSnak to handle ID prefix and add tests for Commons…
Browse files Browse the repository at this point in the history
…MediaSnak and EntitySchemaSnak
  • Loading branch information
wvanderp committed Dec 23, 2024
1 parent 7fe7878 commit 1d8b60c
Show file tree
Hide file tree
Showing 7 changed files with 135 additions and 7 deletions.
56 changes: 56 additions & 0 deletions .npm-upgrade.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
{
"ignore": {
"@typescript-eslint/eslint-plugin": {
"versions": "8.18.1",
"reason": "airbnb-eslint"
},
"@typescript-eslint/parser": {
"versions": "8.18.1",
"reason": "airbnb-eslint"
},
"eslint": {
"versions": "9.17.0",
"reason": "airbnb-eslint"
},
"eslint-import-resolver-typescript": {
"versions": "3.7.0",
"reason": "airbnb-eslint"
},
"eslint-plugin-compat": {
"versions": "6.0.2",
"reason": "airbnb-eslint"
},
"eslint-plugin-cypress": {
"versions": "4.1.0",
"reason": "airbnb-eslint"
},
"eslint-plugin-import": {
"versions": "2.31.0",
"reason": "airbnb-eslint"
},
"eslint-plugin-jest": {
"versions": "28.10.0",
"reason": "airbnb-eslint"
},
"eslint-plugin-jsdoc": {
"versions": "",
"reason": "airbnb-eslint"
},
"eslint-plugin-jsonc": {
"versions": "2.18.2",
"reason": "airbnb-eslint"
},
"eslint-plugin-no-secrets": {
"versions": "2.1.1",
"reason": "airbnb-eslint"
},
"eslint-plugin-sonarjs": {
"versions": "3.0.1",
"reason": "airbnb-eslint"
},
"eslint-plugin-unicorn": {
"versions": "56.0.1",
"reason": "airbnb-eslint"
}
}
}
3 changes: 2 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,5 +54,6 @@
"wikipedia",
"wmde",
"wvdp"
]
],
"editor.formatOnSave": true
}
8 changes: 4 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
"rimraf": "^6.0.1",
"source-map-support": "^0.5.21",
"ts-node": "10.9.2",
"typedoc": "^0.27.4",
"typedoc": "^0.27.5",
"typescript": "^5.7.2"
},
"dependencies": {
Expand Down
5 changes: 4 additions & 1 deletion src/snaks/EntitySchemaSnak.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,10 @@ export default class EntitySchemaSnak extends Snak {
constructor(snak: WikidataEntitySchemaSnak) {
super(snak);

this._numericID = Number.parseInt(snak.datavalue?.value.id ?? '', 10) || undefined;
// the ID is prefixed with E so we need to remove it
this._numericID = snak.datavalue?.value.id
? Number.parseInt(snak.datavalue.value.id.slice(1), 10)
: undefined;
}

/**
Expand Down
9 changes: 9 additions & 0 deletions tests/unit/Snaks/CommonsMediaSnak.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,13 @@ describe('Commons Media Snak', () => {
expect(snak.equals(snak2)).toBe(false);
});
});

describe('fromFileName', () => {
it('should create a snak from a file name', () => {
const snak = CommonsMediaSnak.fromFileName('P42', 'Flag of Amsterdam.svg');

expect(snak.fileName).toEqual('Flag of Amsterdam.svg');
expect(snak.property).toEqual('P42');
});
});
});
59 changes: 59 additions & 0 deletions tests/unit/Snaks/EntitySchemaSnak.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import EntitySchemaSnak from '../../../src/snaks/EntitySchemaSnak';

const EntitySnak = {
snaktype: 'value' as const,
property: 'P123',
datavalue: {
value: {
id: 'E123456',
'entity-type': 'entity-schema' as const
},
type: 'wikibase-entityid' as const
},
datatype: 'entity-schema' as const
};

describe('EntitySchemaSnak', () => {
describe('toJSON', () => {
it('should have the right JSON stringification', () => {
const snak = new EntitySchemaSnak(EntitySnak);

expect(snak.toJSON()).toStrictEqual(EntitySnak);
});
});

describe('equals', () => {
it('should be true if the items are equal', () => {
const a = new EntitySchemaSnak(EntitySnak);
const b = new EntitySchemaSnak(EntitySnak);

expect(a.equals(b)).toBe(true);
});

it('should be false if the property changes', () => {
const a = new EntitySchemaSnak(EntitySnak);
const b = new EntitySchemaSnak(EntitySnak);

b.property = 'P42';

expect(a.equals(b)).toBe(false);
});

it('should be false if the items are NOT equal', () => {
const snak = new EntitySchemaSnak(EntitySnak);
const snak2 = new EntitySchemaSnak(EntitySnak);
snak2.id = 'E654321';

expect(snak.equals(snak2)).toBe(false);
});
});

describe('fromID', () => {
it('should create a snak from an ID', () => {
const snak = EntitySchemaSnak.fromID('P2013', 'E987654');

expect(snak.id).toEqual('E987654');
expect(snak.property).toEqual('P2013');
});
});
});

0 comments on commit 1d8b60c

Please sign in to comment.