|
1 | 1 | /* eslint-env mocha */
|
2 | 2 | /* eslint "node/no-unpublished-require": 0 */
|
3 | 3 | const assert = require('assert');
|
| 4 | +const util = require('util'); |
| 5 | +const request = util.promisify(require('request')); |
| 6 | +const api = require('../index'); |
4 | 7 |
|
5 | 8 | describe('Observations', () => {
|
6 | 9 | describe('Model', () => {
|
@@ -605,8 +608,105 @@ describe('Observations', () => {
|
605 | 608 | });
|
606 | 609 |
|
607 | 610 | describe('API', () => {
|
| 611 | + let server; |
| 612 | + |
| 613 | + before(() => { |
| 614 | + server = api.start(); |
| 615 | + }); |
| 616 | + |
| 617 | + after(() => { |
| 618 | + server.close(); |
| 619 | + }); |
| 620 | + |
608 | 621 | describe('Output', () => {
|
| 622 | + const observation = '41f7ef3e-2dd1-4864-ac77-5daa4c13f04f'; |
| 623 | + const indicator = 'indicator1'; |
| 624 | + const timeseries = 'd3a06c6b-82a7-4efa-8f0f-6cb453deff2c'; |
| 625 | + const uri = `http://localhost:8000/observations/${observation}`; |
| 626 | + let body; |
| 627 | + before(async () => { |
| 628 | + return new Promise(async (resolve, reject) => { |
| 629 | + const options = { |
| 630 | + uri, |
| 631 | + json: true |
| 632 | + }; |
| 633 | + try { |
| 634 | + ({body} = await request(options)); |
| 635 | + resolve(); |
| 636 | + } catch (err) { |
| 637 | + reject(err); |
| 638 | + } |
| 639 | + }); |
| 640 | + }); |
| 641 | + it('should have an id', () => { |
| 642 | + assert.strictEqual(body.data.id, observation); |
| 643 | + }); |
| 644 | + |
| 645 | + it('should have the `observation` type', () => { |
| 646 | + assert.strictEqual(body.data.type, 'observation'); |
| 647 | + }); |
| 648 | + |
| 649 | + it('should have a link to itself', () => { |
| 650 | + assert.strictEqual(typeof body.data.links, 'object'); |
| 651 | + assert.strictEqual(body.data.links.self, uri); |
| 652 | + }); |
| 653 | + |
| 654 | + it('should have a valid ISO date as the `period` attribute', () => { |
| 655 | + const options = { |
| 656 | + year: 'numeric', |
| 657 | + month: '2-digit', |
| 658 | + day: '2-digit' |
| 659 | + }; |
| 660 | + assert.notStrictEqual(body.data.attributes.period, undefined); |
| 661 | + assert.strictEqual(body.data.attributes.period, new Date(body.data.attributes.period).toLocaleDateString(undefined, options)); |
| 662 | + }); |
609 | 663 |
|
| 664 | + it('should have an object for the `dimensions` attribute', () => { |
| 665 | + const dimensions = { |
| 666 | + geographicArea: '24' |
| 667 | + }; |
| 668 | + assert.strictEqual(typeof body.data.attributes.dimensions, 'object'); |
| 669 | + assert.strictEqual(JSON.stringify(body.data.attributes.dimensions), JSON.stringify(dimensions)); |
| 670 | + }); |
| 671 | + |
| 672 | + it('should have a a `value` attribute', () => { |
| 673 | + assert.notStrictEqual(body.data.attributes.value, undefined); |
| 674 | + }); |
| 675 | + |
| 676 | + it('should have a a `status` attribute', () => { |
| 677 | + assert.notStrictEqual(body.data.attributes.status, undefined); |
| 678 | + }); |
| 679 | + |
| 680 | + it('should have a valid ISO timestamp as the `dateModified` attribute', () => { |
| 681 | + assert.notStrictEqual(body.data.attributes.dateModified, undefined); |
| 682 | + assert.strictEqual(body.data.attributes.dateModified, new Date(body.data.attributes.dateModified).toISOString()); |
| 683 | + }); |
| 684 | + |
| 685 | + it('should have a relationship link to the observations\'s revisions', () => { |
| 686 | + assert.strictEqual(body.data.relationships.revisions.links.self, `${uri}/revisions`); |
| 687 | + }); |
| 688 | + |
| 689 | + it('should have a relationship link to the observations\'s notes', () => { |
| 690 | + assert.strictEqual(body.data.relationships.notes.links.self, `${uri}/notes`); |
| 691 | + }); |
| 692 | + |
| 693 | + it('should have a relationship link to the observations\'s indicator', () => { |
| 694 | + assert.strictEqual(body.data.relationships.indicator.links.self, `http://localhost:8000/indicators/${indicator}`); |
| 695 | + }); |
| 696 | + |
| 697 | + it('should have a relationship reference to the observations\'s indicator', () => { |
| 698 | + assert.strictEqual(body.data.relationships.indicator.data.id, indicator); |
| 699 | + assert.strictEqual(body.data.relationships.indicator.data.type, 'indicator'); |
| 700 | + }); |
| 701 | + |
| 702 | + it('should have a relationship link to the observations\'s timeseries', () => { |
| 703 | + assert.strictEqual(body.data.relationships.timeseries.links.self, `http://localhost:8000/timeseries/${timeseries}`); |
| 704 | + }); |
| 705 | + |
| 706 | + it('should have a relationship reference to the observations\'s timeseries', () => { |
| 707 | + assert.strictEqual(body.data.relationships.timeseries.data.id, timeseries); |
| 708 | + assert.strictEqual(body.data.relationships.timeseries.data.type, 'timeseries'); |
| 709 | + }); |
610 | 710 | });
|
611 | 711 |
|
612 | 712 | describe('Routes', () => {
|
|
0 commit comments