Skip to content

Commit e5b94b5

Browse files
committed
polishing upt the api for prod. added validator to ensure json responses match prod responses
1 parent f6876af commit e5b94b5

File tree

10 files changed

+130
-103
lines changed

10 files changed

+130
-103
lines changed

api-tests/app.ts

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@ import * as fs from 'fs';
88
import * as path from 'path';
99

1010

11-
const testFiles = [
11+
const endpointsToFix = [
1212
//can't find the gmna.lookup_units table. 'geologic_units_gmna.ts',
1313
//'geologic_units_gmus.ts'
1414
//'mancos_test_cases.ts',
1515
//uses gmus.lookup_units table. queries need to be customized and changed to match all of the lookup tables within postgresql 'mobile_point_details.ts;
1616
]
17-
/*
17+
const testFiles = [
1818
'carto_small.ts',
1919
'columns.ts',
2020
'defs.ts',
@@ -37,21 +37,19 @@ const testFiles = [
3737
'defs_timescales.ts',
3838
'fossils.ts',
3939
'geologic_units_burwell.ts',
40-
'geologic_units_gmna.ts',
41-
'geologic_units_gmus.ts',
4240
'index.ts',
43-
'mancos_test_cases.ts',
4441
'mobile_fossil_collections.ts',
4542
'mobile_macro_summary.ts',
4643
'mobile_map_query.ts',
4744
'mobile_point.ts',
4845
'mobile_point_details.ts',
49-
'paleogeography.ts',
46+
/*'paleogeography.ts',
5047
'root.ts',
51-
'sections.ts',
52-
'stats.ts',
53-
'tiles.ts',
54-
*/
48+
'sections.ts',*/
49+
'stats.ts' ]
50+
//'tiles.ts'
51+
52+
5553
const testDir = path.join(__dirname, 'v2Tests');
5654

5755
testFiles.forEach((file) => {

api-tests/package-lock.json

Lines changed: 38 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

api-tests/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
"test": "mocha -r ts-node/register app.ts"
99
},
1010
"dependencies": {
11+
"axios": "^1.7.9",
1112
"express": "^5.0.0",
1213
"mocha": "^9.0.0",
1314
"supertest": "^6.1.3"

api-tests/v2Tests/columns.ts

Lines changed: 55 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -2,100 +2,83 @@ var request = require("supertest"),
22
validators = require("../validators"),
33
settings = require("../settings");
44

5-
it("should return metadata", function (done) {
6-
request(settings.host)
7-
.get("/columns")
8-
.expect(validators.aSuccessfulRequest)
9-
.expect(validators.json)
10-
.expect(validators.metadata)
11-
.end(function (error: any, res: any) {
12-
if (error) return done(error);
13-
done();
14-
});
5+
it("should return metadata", async function () {
6+
const localResponse = await request(settings.host)
7+
.get("/columns")
8+
.expect(validators.aSuccessfulRequest)
9+
.expect(validators.json)
10+
.expect(validators.metadata)
11+
await validators.compareWithProduction("/columns", localResponse);
1512
});
1613

14+
15+
16+
1717
//Fixed this test to work on dev and local
18-
it("should return a sample", function (done) {
19-
request(settings.host)
20-
.get("/columns?sample")
21-
.expect(validators.aSuccessfulRequest)
22-
.expect(validators.json)
23-
.expect(validators.aSample)
24-
.end(function (error: any, res: any) {
25-
if (error) return done(error);
26-
done();
27-
});
18+
it("should return a sample", async function () {
19+
const localResponse = await request(settings.host)
20+
.get("/columns?sample")
21+
.expect(validators.aSuccessfulRequest)
22+
.expect(validators.json)
23+
.expect(validators.aSample)
24+
await validators.compareWithProduction("/columns?sample", localResponse);
25+
2826
});
2927

3028
//Fixed this test to work on dev and local
31-
it("should accept an interval name", function (done) {
32-
request(settings.host)
29+
it("should accept an interval name", async function () {
30+
const localResponse = await request(settings.host)
3331
.get("/columns?interval_name=Permian")
3432
.expect(validators.aSuccessfulRequest)
3533
.expect(validators.json)
3634
.expect(validators.atLeastOneResult)
37-
.end(function (error: any, res: any) {
38-
if (error) return done(error);
39-
done();
40-
});
35+
await validators.compareWithProduction("/columns?interval_name=Permian", localResponse)
4136
});
4237

4338

44-
it("should accept an age", function (done) {
45-
request(settings.host)
39+
it("should accept an age", async function () {
40+
const localResponse = await request(settings.host)
4641
.get("/columns?age=271")
4742
.expect(validators.aSuccessfulRequest)
4843
.expect(validators.json)
4944
.expect(validators.atLeastOneResult)
50-
.end(function (error: any, res: any) {
51-
if (error) return done(error);
52-
done();
53-
});
45+
await validators.compareWithProduction("/columns?age=271", localResponse)
5446
});
5547

5648

57-
it("should accept an age_top and age_bottom", function (done) {
58-
request(settings.host)
49+
it("should accept an age_top and age_bottom", async function () {
50+
const localResponse = await request(settings.host)
5951
.get("/columns?age_top=200&age_bottom=250")
6052
.expect(validators.aSuccessfulRequest)
6153
.expect(validators.json)
6254
.expect(validators.atLeastOneResult)
63-
.end(function (error: any, res: any) {
64-
if (error) return done(error);
65-
done();
66-
});
55+
await validators.compareWithProduction("/columns?age_top=200&age_bottom=250", localResponse)
6756
});
6857

6958
//fixed test in dev and it is now working.
70-
it("should accept a strat_name parameter", function (done) {
71-
request(settings.host)
59+
it("should accept a strat_name parameter", async function () {
60+
const localResponse = await request(settings.host)
7261
.get("/columns?strat_name=mancos")
7362
.expect(validators.aSuccessfulRequest)
7463
.expect(validators.json)
7564
.expect(validators.atLeastOneResult)
76-
.end(function (error: any, res: any) {
77-
if (error) return done(error);
78-
done();
79-
});
65+
await validators.compareWithProduction("/columns?strat_name=mancos", localResponse)
8066
});
8167

82-
it("should accept a strat_name_id parameter", function (done) {
83-
request(settings.host)
68+
it("should accept a strat_name_id parameter", async function () {
69+
const localResponse = await request(settings.host)
8470
.get("/columns?strat_name_id=1205")
8571
.expect(validators.aSuccessfulRequest)
8672
.expect(validators.json)
8773
.expect(validators.atLeastOneResult)
88-
.end(function (error: any, res: any) {
89-
if (error) return done(error);
90-
done();
91-
});
74+
await validators.compareWithProduction("/columns?strat_name_id=1205", localResponse)
9275
});
9376

9477

9578
//Checking to see if the rest of the tests below work in prod.
9679
//count for max number of columns ~50?
97-
it("should accept a latitude and longitude", function (done) {
98-
request(settings.host)
80+
it("should accept a latitude and longitude", async function () {
81+
const localResponse = await request(settings.host)
9982
.get("/columns?lat=43.3&lng=-89.3")
10083
.expect(validators.aSuccessfulRequest)
10184
.expect(validators.json)
@@ -105,62 +88,45 @@ request(settings.host)
10588
throw new Error("Columns returning the wrong column for the lat/lng");
10689
}
10790
})
108-
.end(function (error: any, res: any) {
109-
if (error) return done(error);
110-
done();
111-
});
91+
await validators.compareWithProduction("/columns?lat=43.3&lng=-89.3", localResponse)
11292
});
11393

114-
it("should return topojson", function (done) {
115-
request(settings.host)
94+
it("should return topojson", async function () {
95+
const localResponse = await request(settings.host)
11696
.get("/columns?age=2&format=topojson")
11797
.expect(validators.aSuccessfulRequest)
11898
.expect(validators.topoJSON)
119-
.end(function (error: any, res: any) {
120-
if (error) return done(error);
121-
done();
122-
});
12399
});
124100

125-
it("should return csv", function (done) {
126-
request(settings.host)
101+
it("should return csv", async function () {
102+
const localResponse = await request(settings.host)
127103
.get("/columns?age=2&format=csv")
128104
.expect(validators.aSuccessfulRequest)
129105
.expect(validators.csv)
130-
.end(function (error: any, res: any) {
131-
if (error) return done(error);
132-
done();
133-
});
134106
});
135107

136108

137-
it("should accept a project_id", function (done) {
109+
it("should accept a project_id", async function () {
138110
this.timeout(7000);
139-
request(settings.host)
111+
const localResponse = await request(settings.host)
140112
.get("/columns?project_id=4")
141113
.expect(validators.aSuccessfulRequest)
142114
.expect(validators.json)
143115
.expect(validators.atLeastOneResult)
144-
.end(function (error: any, res: any) {
145-
if (error) return done(error);
146-
done();
147-
});
116+
await validators.compareWithProduction("/columns?project_id=4", localResponse)
148117
});
149118

150-
it("should accept a lat/lng and return all adjacent columns", function (done) {
151-
request(settings.host)
152-
.get("/columns?lat=43.3&lng=-89.3&adjacents=true")
153-
.expect(validators.aSuccessfulRequest)
154-
.expect(validators.json)
155-
.expect(validators.atLeastOneResult)
156-
.expect(function (res: { body: { success: { data: string | any[]; }; }; }) {
157-
if (res.body.success.data.length != 6) {
158-
throw new Error("Wrong number of adjacent columns being returned");
159-
}
160-
})
161-
.end(function (error: any, res: any) {
162-
if (error) return done(error);
163-
done();
164-
});
119+
it("should accept a lat/lng and return all adjacent columns", async function () {
120+
const localResponse = await request(settings.host)
121+
.get("/columns?lat=43.3&lng=-89.3&adjacents=true")
122+
.expect(validators.aSuccessfulRequest)
123+
.expect(validators.json)
124+
.expect(validators.atLeastOneResult)
125+
.expect(function (res: { body: { success: { data: string | any[]; }; }; }) {
126+
if (res.body.success.data.length != 6) {
127+
throw new Error("Wrong number of adjacent columns being returned");
128+
}
129+
})
130+
await validators.compareWithProduction("/columns?lat=43.3&lng=-89.3&adjacents=true", localResponse)
165131
});
166132

api-tests/v2Tests/defs_measurements.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
});
4646

4747
it("should accept a measurement class", function (done) {
48-
this.timeout(5000);
48+
this.timeout(9000);
4949
request(settings.host)
5050
.get("/defs/measurements?measurement_class=geochemical")
5151
.expect(validators.aSuccessfulRequest)
@@ -56,7 +56,7 @@
5656
if (d.class != "geochemical") {
5757
throw new Error("Wrong measurement class returned");
5858
}
59-
});
59+
})
6060
})
6161
.end(function (error: any, res: any) {
6262
if (error) return done(error);

api-tests/v2Tests/defs_strat_name_concepts.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
});
4040

4141
it("should output CSV", function (done) {
42+
this.timeout(9000)
4243
request(settings.host)
4344
.get("/defs/strat_name_concepts?all&format=csv")
4445
.expect(validators.aSuccessfulRequest)

api-tests/v2Tests/mobile_macro_summary.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
});
1616

1717
it("should accept a latitude and longitude", function (done) {
18+
this.timeout(10000);
1819
request(settings.host)
1920
.get("/mobile/macro_summary?lat=43.0706192&lng=-89.406167")
2021
.expect(validators.aSuccessfulRequest)

api-tests/validators.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
//var sizeOf = require("image-size");
2+
const axios = require("axios");
23

34
module.exports = {
45
aSuccessfulRequest: function (res: { statusCode: number; headers: { [x: string]: string; }; }) {
@@ -154,5 +155,20 @@ module.exports = {
154155
throw new Error(`t_int_age is not a numeric type: ${item.t_int_age} (type: ${typeof item.t_int_age})`);
155156
}
156157
});
158+
},
159+
160+
async compareWithProduction(queryParams = "", localResponse: any) {
161+
const prodUrl = `https://www.macrostrat.org/api/v2${queryParams}`;
162+
const externalResponse = await axios.get(prodUrl);
163+
if (JSON.stringify(localResponse.body) !== JSON.stringify(externalResponse.data)) {
164+
throw new Error(
165+
`Mismatch for endpoint: ${queryParams}\nLocal: ${JSON.stringify(
166+
localResponse.body,
167+
null,
168+
2
169+
)}\nProduction: ${JSON.stringify(externalResponse.data, null, 2)}`
170+
);
171+
}
157172
}
158173
};
174+

0 commit comments

Comments
 (0)