IEEE.org     |     IEEE Xplore Digital Library     |     IEEE Standards     |     IEEE Spectrum     |     More Sites

Commit 47c06d34 authored by James C. De Ricco's avatar James C. De Ricco
Browse files

Merge branch 'master' into feature-map-project-assets-ignore-null-bbox

parents b2cc3110 6fd8d3e4
const request = require('supertest');
const app = require('../../app');
const { setup, teardown, loadSQL } = require('../setup');
const { createTestAsset } = require('../utils');
const ENDPOINT = '/api/v1/assets';
/* global BigInt */
describe('GET assets', () => {
const ASSET1 = { latitude: 1, longitude: 2 };
const ASSET2 = { latitude: 3, longitude: 4 };
......@@ -27,11 +26,14 @@ describe('GET assets', () => {
await teardown();
});
afterEach(async () => {
await global.dbPool.query('DELETE FROM asset');
});
function contains(arr, key, val) {
for (var i = 0; i < arr.length; i++) {
if (arr[i][key] === val) return true;
}
return false;
}
it('returns empty list of assets', () => {
it('returns HTTP 200 response', () => {
return request(app)
.get(ENDPOINT)
.expect(200)
......@@ -46,11 +48,13 @@ describe('GET assets', () => {
.get(ENDPOINT)
.expect(200)
.then(res => {
expect(res.body).toEqual(expect.arrayContaining([
expect.objectContaining(ASSET1),
expect.objectContaining(ASSET2),
expect.objectContaining(ASSET3)
]));
expect(res.body).toEqual(
expect.arrayContaining([
expect.objectContaining(ASSET1),
expect.objectContaining(ASSET2),
expect.objectContaining(ASSET3)
])
);
expect(res.body).toHaveLength(3);
});
......@@ -62,12 +66,14 @@ describe('GET assets', () => {
.get(ENDPOINT + '?project_id=2')
.expect(200)
.then(res => {
expect(res.body).toEqual(expect.arrayContaining([
expect.objectContaining(ASSET2),
expect.objectContaining(ASSET3)
]));
expect(res.body).toHaveLength(2);
expect(res.body).toEqual(
expect.arrayContaining([
expect.objectContaining(ASSET2),
expect.objectContaining(ASSET3)
])
);
expect(res.body).toHaveLength(4);
});
});
......@@ -89,7 +95,7 @@ describe('GET assets', () => {
.expect(400);
});
it.skip('able to create assets', async () => {
it('able to create assets', async () => {
const asset = {
project: {
id: 1
......@@ -117,11 +123,28 @@ describe('GET assets', () => {
.post(ENDPOINT)
.send(asset)
.expect(200)
.then((response) => {
.then(response => {
const data = BigInt(response.body);
expect(data).toBeTruthy();
expect(typeof data).toBe('bigint');
});
});
it('returns assets with expected types', done => {
request(app)
.get(ENDPOINT)
.expect(200)
.then(response => {
// response.body should contain the id of the created data_type
const data = response.body;
console.log(data);
expect(data).toBeTruthy();
expect(typeof data).toBe('number');
expect(typeof data).toBe('object');
//"Randomly" checking that the data loaded from Sample-data-1.sql is present
expect(contains(data, 'sponsor_name', 'Bronx Zoo'));
expect(contains(data, 'sponsor_name', 'Seneca Park Zoo'));
expect(contains(data, 'asset_id', '21'));
expect(contains(data, 'asset_type', 'Bison'));
done();
});
});
});
INSERT INTO project (sponsor_id, name) VALUES (1, 'Madagascar Lemur Conservation');
INSERT INTO asset_type (name)
VALUES ('Ring Tailed Lemur'), ('Brown Mouse Lemur'), ('Silky Sifaka');
INSERT INTO property
(asset_type_id, name, data_type, required, is_private)
VALUES
(1, 'fur thickness', 'number', false, false);
INSERT INTO property
(asset_type_id, name, data_type, required, is_private)
VALUES
(2, 'year born', 'number', false, false);
INSERT INTO property
(asset_type_id, name, data_type, required, is_private)
VALUES
(3, 'fur color', 'text', false, false);
INSERT INTO asset (project_id, asset_type_id, location)
SELECT 1, 1, ST_MakePoint(random_between(-22.127, -22.324), random_between(46.844, 47.019))
FROM generate_series(1, 15);
INSERT INTO asset (project_id, asset_type_id, location)
SELECT 1, 2, ST_MakePoint(random_between(-22.127, -22.324), random_between(46.844, 47.019))
FROM generate_series(1, 15);
INSERT INTO asset (project_id, asset_type_id, location)
SELECT 1, 3, ST_MakePoint(random_between(-22.127, -22.324), random_between(46.844, 47.019))
FROM generate_series(1, 15);
\ No newline at end of file
INSERT INTO project (sponsor_id, name) VALUES (1, 'Madagascar Reforesting');
INSERT INTO asset_type (name) VALUES ('Tree');
INSERT INTO asset (project_id, asset_type_id, location)
SELECT 2, 4, ST_MakePoint(random_between(-16.8, -17.5), random_between(46, 47))
FROM generate_series(1, 20);
INSERT INTO project (sponsor_id, name) VALUES (1, 'African Elephant Conservation');
INSERT INTO asset_type (name)
VALUES ('Elephant');
INSERT INTO property
(asset_type_id, name, data_type, required, is_private)
VALUES
(5, 'weight', 'number', false, false);
INSERT INTO asset (project_id, asset_type_id, location)
SELECT 3, 5, ST_MakePoint(random_between(-16.80, -16.15), random_between(37.90, 38.78))
FROM generate_series(1, 30);
\ No newline at end of file
INSERT INTO sponsor (name) VALUES ('Seneca Park Zoo');
CREATE OR REPLACE FUNCTION random_between(low DOUBLE PRECISION, high DOUBLE PRECISION)
RETURNS DOUBLE PRECISION AS
$$
BEGIN
RETURN random() * (high - low) + low;
END;
$$ language 'plpgsql' STRICT;
\ No newline at end of file
......@@ -66,7 +66,7 @@ SET default_with_oids = false;
--
CREATE TABLE "public"."asset" (
"id" integer NOT NULL,
"id" bigint NOT NULL,
"project_id" integer,
"asset_type_id" integer NOT NULL,
"location" "public"."geometry"(Point)
......@@ -91,7 +91,7 @@ CREATE VIEW "public"."asset_expandedlocation" AS
--
CREATE SEQUENCE "public"."asset_id_seq"
AS integer
AS bigint
START WITH 1
INCREMENT BY 1
NO MINVALUE
......@@ -111,7 +111,7 @@ ALTER SEQUENCE "public"."asset_id_seq" OWNED BY "public"."asset"."id";
--
CREATE TABLE "public"."asset_property" (
"asset_id" integer NOT NULL,
"asset_id" bigint NOT NULL,
"property_id" integer NOT NULL,
"value" "text" NOT NULL
);
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment