Skip to content

Commit

Permalink
Merge pull request #396 from macbre/wikidata/more-tests
Browse files Browse the repository at this point in the history
More test cases for wikidata + set a global jest timeout to 5 seconds
  • Loading branch information
macbre authored Jan 26, 2023
2 parents 2d66819 + 87cc757 commit 6f30d55
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 15 deletions.
2 changes: 1 addition & 1 deletion jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,5 @@ module.exports = {
lines: 23,
},
},
testTimeout: 1000, // one sec
testTimeout: 5000, // [ms]
};
30 changes: 22 additions & 8 deletions lib/wikidata.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,12 @@ const WIKIDATA_SERVER = "wikidata.org";
const WIKIDATA_PATH = "/w";
const WIKIDATA_USER_AGENT = "nodemw";

// get the object being the first key/value entry of a given object
/**
* Get the object being the first key/value entry of a given object
*
* @param {Object} obj
* @return {Object}
*/
function getFirstItem(obj) {
const key = Object.keys(obj).shift();
return obj[key];
Expand Down Expand Up @@ -55,15 +60,26 @@ class WikiData {
});
}

/**
* @param {Object} params
* @see https://www.wikidata.org/w/api.php?action=wbgetentities&sites=enwiki&titles=Saksun&normalize=&props=sitelinks&format=json
* @return {Promise<{entities:Object}>}
*/
async getEntities(params) {
return await this.callApi({
action: "wbgetentities",
sites: "enwiki",
...params,
});
}

/**
* @param {string} article
* @see https://www.wikidata.org/w/api.php?action=wbgetentities&sites=enwiki&titles=Saksun&normalize=&props=sitelinks&format=json
* @return {Promise<ArticleSitelinksMap|null>}
*/
async getArticleSitelinks(article) {
const resp = await this.callApi({
action: "wbgetentities",
sites: "enwiki",
const resp = await this.getEntities({
titles: article,
props: "sitelinks",
});
Expand All @@ -82,8 +98,7 @@ class WikiData {
* @return {Promise<ClaimsMap>}
*/
async getArticleClaims(article, site = "enwiki") {
const resp = await this.callApi({
action: "wbgetentities",
const resp = await this.getEntities({
sites: site,
titles: article,
props: "claims",
Expand All @@ -100,8 +115,7 @@ class WikiData {
* @return {Promise<DescriptionsMap>}
*/
async getArticleDescriptions(article, site = "enwiki") {
const resp = await this.callApi({
action: "wbgetentities",
const resp = await this.getEntities({
sites: site,
titles: article,
props: "descriptions",
Expand Down
19 changes: 13 additions & 6 deletions test/wikidata-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,12 @@ describe("WikiData API", () => {
badges: [],
},
});
}, 5000);
});

it(`gives null for not existing article`, async () => {
const res = await client.getArticleSitelinks(NOT_EXISTING_ARTICLE);
expect(res).toBeNull();
}, 5000);
});
});

describe("getArticleClaims", () => {
Expand All @@ -41,12 +41,19 @@ describe("WikiData API", () => {

expect(Object.keys(res)).toContain("P1280");
expect(Object.keys(res)).toContain("P1412");
}, 5000);

// "+1879-03-14T00:00:00Z"
const dateOfBirth = res.P569[0].mainsnak.datavalue.value;
expect(dateOfBirth.time).toMatch(/1879-03-14/);

const dateOfDeath = res.P570[0].mainsnak.datavalue.value;
expect(dateOfDeath.time).toMatch(/1955-04-18/);
});

it(`gives null for not existing article`, async () => {
const res = await client.getArticleClaims(NOT_EXISTING_ARTICLE);
expect(res).toBeNull();
}, 5000);
});
});

describe("getArticleDescriptions", () => {
Expand All @@ -63,12 +70,12 @@ describe("WikiData API", () => {
language: "pl",
value: "fizyk teoretyczny, noblista",
});
}, 5000);
});

it(`gives null for not existing article`, async () => {
const res = await client.getArticleDescriptions(NOT_EXISTING_ARTICLE);
expect(res).toBeNull();
}, 5000);
});
});

describe("getEntityClaims", () => {
Expand Down

0 comments on commit 6f30d55

Please sign in to comment.