api-core
Version:
Model-based dynamic multi-level APIs for any provider, plus multiple consumption channels
345 lines • 15.8 kB
JavaScript
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
const tap = require('tap');
const Api_1 = require("../src/Api");
const builder = require("../src/query/ApiQueryBuilder");
const OneToOneRelation_1 = require("../src/relations/OneToOneRelation");
const OneToManyRelation_1 = require("../src/relations/OneToManyRelation");
const StudentEdge_1 = require("./env/edges/StudentEdge");
const ClassEdge_1 = require("./env/edges/ClassEdge");
const CourseEdge_1 = require("./env/edges/CourseEdge");
const CourseTypeEdge_1 = require("./env/edges/CourseTypeEdge");
const SchoolEdge_1 = require("./env/edges/SchoolEdge");
const ApiRequest_1 = require("../src/request/ApiRequest");
const studentEdge = new StudentEdge_1.StudentEdge, classEdge = new ClassEdge_1.ClassEdge, courseEdge = new CourseEdge_1.CourseEdge, courseTypeEdge = new CourseTypeEdge_1.CourseTypeEdge, schoolEdge = new SchoolEdge_1.SchoolEdge;
let api;
tap.test('creating the API should work', (t) => {
api = new Api_1.Api({ name: 'test-service', version: '1.0' })
.edge(studentEdge)
.edge(classEdge)
.edge(courseEdge)
.edge(courseTypeEdge)
.edge(schoolEdge)
.relation(new OneToOneRelation_1.OneToOneRelation(courseEdge, courseTypeEdge))
.relation(new OneToManyRelation_1.OneToManyRelation(courseTypeEdge, courseEdge))
.relation(new OneToManyRelation_1.OneToManyRelation(studentEdge, courseEdge))
.relation(new OneToOneRelation_1.OneToOneRelation(studentEdge, classEdge))
.relation(new OneToOneRelation_1.OneToOneRelation(classEdge, schoolEdge))
.relation(new OneToOneRelation_1.OneToOneRelation(courseEdge, classEdge))
.relation(new OneToManyRelation_1.OneToManyRelation(classEdge, studentEdge))
.relation(new OneToManyRelation_1.OneToManyRelation(classEdge, courseEdge))
.relation(new OneToManyRelation_1.OneToManyRelation(schoolEdge, studentEdge))
.relation(new OneToManyRelation_1.OneToManyRelation(schoolEdge, classEdge));
t.end();
});
tap.test('POST /schools/s2/students', (t) => __awaiter(void 0, void 0, void 0, function* () {
const request = yield api.parseRequest(['schools', 's2', 'students']);
request.type = ApiRequest_1.ApiRequestType.Create;
request.body = {
id: "s7",
fullName: "Marry Test",
email: "marry.test@gmail.com",
classId: "c1"
};
const query = api.buildQuery(request);
t.equal(query.steps.length, 5, 'should build a 5 step query');
t.ok(query.steps[0] instanceof builder.SetBodyQueryStep, 'SET BODY');
t.ok(query.steps[1] instanceof builder.ExtendContextLiveQueryStep, 'EXTEND');
t.ok(query.steps[2] instanceof builder.QueryEdgeQueryStep, 'QUERY /schools');
t.ok(query.steps[3] instanceof builder.RelateChangeQueryStep, 'RELATE CHANGE schoolId');
t.ok(query.steps[4] instanceof builder.QueryEdgeQueryStep, 'QUERY /students');
query.execute()
.then(resp => {
t.same(resp.data, {
id: "s7",
fullName: "Marry Test",
email: "marry.test@gmail.com",
schoolId: "s2",
classId: "c1"
});
t.equal(resp.metadata, null);
t.end();
})
.catch(() => {
t.ok(false, "a valid query should not fail");
t.end();
});
}));
tap.test('POST /schools/s2/classes/c1/students', (t) => __awaiter(void 0, void 0, void 0, function* () {
const request = yield api.parseRequest(['schools', 's2', 'classes', 'c1', 'students']);
request.type = ApiRequest_1.ApiRequestType.Create;
request.body = {
id: "s8",
fullName: "Adam Test",
email: "adam.test@gmail.com"
};
const query = api.buildQuery(request);
t.equal(query.steps.length, 8, 'should build a 8 step query');
t.ok(query.steps[0] instanceof builder.SetBodyQueryStep, 'SET BODY');
t.ok(query.steps[1] instanceof builder.ExtendContextLiveQueryStep, 'EXTEND');
t.ok(query.steps[2] instanceof builder.QueryEdgeQueryStep, 'QUERY /schools');
t.ok(query.steps[3] instanceof builder.RelateChangeQueryStep, 'RELATE CHANGE schoolId');
t.ok(query.steps[4] instanceof builder.ExtendContextLiveQueryStep, 'EXTEND');
t.ok(query.steps[5] instanceof builder.QueryEdgeQueryStep, 'QUERY /classes');
t.ok(query.steps[6] instanceof builder.RelateChangeQueryStep, 'RELATE CHANGE classId');
t.ok(query.steps[7] instanceof builder.QueryEdgeQueryStep, 'QUERY /students');
query.execute()
.then(resp => {
t.same(resp.data, {
id: "s8",
fullName: "Adam Test",
email: "adam.test@gmail.com",
schoolId: "s2",
classId: "c1"
});
t.equal(resp.metadata, null);
t.end();
})
.catch(() => {
t.ok(false, "a valid query should not fail");
t.end();
});
}));
tap.test('POST /schools/s2/students (invalid body)', (t) => __awaiter(void 0, void 0, void 0, function* () {
const request = yield api.parseRequest(['schools', 's2', 'students']);
request.type = ApiRequest_1.ApiRequestType.Create;
request.body = {
id: "s7",
fullName: "Marry Test",
email: "marry.test@gmail.com",
classId: "c1",
schoolId: "s3"
};
const query = api.buildQuery(request);
t.equal(query.steps.length, 5, 'should build a 5 step query');
t.ok(query.steps[0] instanceof builder.SetBodyQueryStep, 'SET BODY');
t.ok(query.steps[1] instanceof builder.ExtendContextLiveQueryStep, 'EXTEND');
t.ok(query.steps[2] instanceof builder.QueryEdgeQueryStep, 'QUERY /schools');
t.ok(query.steps[3] instanceof builder.RelateChangeQueryStep, 'RELATE CHANGE schoolId');
t.ok(query.steps[4] instanceof builder.QueryEdgeQueryStep, 'QUERY /students');
query.execute()
.then(resp => {
t.same(resp.data, {
id: "s7",
fullName: "Marry Test",
email: "marry.test@gmail.com",
schoolId: "s2",
classId: "c1"
});
t.equal(resp.metadata, null);
t.end();
})
.catch(() => {
t.ok(false, "a valid query should not fail");
t.end();
});
}));
tap.test('POST /schools/s2/classes/c1/students (invalid body)', (t) => __awaiter(void 0, void 0, void 0, function* () {
const request = yield api.parseRequest(['schools', 's2', 'classes', 'c1', 'students']);
request.type = ApiRequest_1.ApiRequestType.Create;
request.body = {
id: "s8",
fullName: "Adam Test",
email: "adam.test@gmail.com",
classId: "c2",
schoolId: "s3"
};
const query = api.buildQuery(request);
t.equal(query.steps.length, 8, 'should build a 8 step query');
t.ok(query.steps[0] instanceof builder.SetBodyQueryStep, 'SET BODY');
t.ok(query.steps[1] instanceof builder.ExtendContextLiveQueryStep, 'EXTEND');
t.ok(query.steps[2] instanceof builder.QueryEdgeQueryStep, 'QUERY /schools');
t.ok(query.steps[3] instanceof builder.RelateChangeQueryStep, 'RELATE CHANGE schoolId');
t.ok(query.steps[4] instanceof builder.ExtendContextLiveQueryStep, 'EXTEND');
t.ok(query.steps[5] instanceof builder.QueryEdgeQueryStep, 'QUERY /classes');
t.ok(query.steps[6] instanceof builder.RelateChangeQueryStep, 'RELATE CHANGE classId');
t.ok(query.steps[7] instanceof builder.QueryEdgeQueryStep, 'QUERY /students');
query.execute()
.then(resp => {
t.same(resp.data, {
id: "s8",
fullName: "Adam Test",
email: "adam.test@gmail.com",
schoolId: "s2",
classId: "c1"
});
t.equal(resp.metadata, null);
t.end();
})
.catch(() => {
t.ok(false, "a valid query should not fail");
t.end();
});
}));
tap.test('PATCH /schools/s2/students/s7', (t) => __awaiter(void 0, void 0, void 0, function* () {
const request = yield api.parseRequest(['schools', 's2', 'students', 's7']);
request.type = ApiRequest_1.ApiRequestType.Patch;
request.body = {
fullName: "Merry Test",
};
const query = api.buildQuery(request);
t.equal(query.steps.length, 8, 'should build a 8 step query');
t.ok(query.steps[0] instanceof builder.SetBodyQueryStep, 'SET BODY');
t.ok(query.steps[1] instanceof builder.ExtendContextLiveQueryStep, 'EXTEND');
t.ok(query.steps[2] instanceof builder.QueryEdgeQueryStep, 'QUERY /schools');
t.ok(query.steps[3] instanceof builder.RelateChangeQueryStep, 'RELATE CHANGE schoolId');
t.ok(query.steps[4] instanceof builder.RelateQueryStep, 'RELATE schoolId');
t.ok(query.steps[5] instanceof builder.ExtendContextLiveQueryStep, 'EXTEND CONTEXT');
t.ok(query.steps[6] instanceof builder.ExtendContextQueryStep, 'APPLY PARAMS');
t.ok(query.steps[7] instanceof builder.QueryEdgeQueryStep, 'QUERY /students');
query.execute()
.then(resp => {
t.same(resp.data, {
id: "s7",
fullName: "Merry Test",
email: "marry.test@gmail.com",
schoolId: "s2",
classId: "c1"
});
t.equal(resp.metadata, null);
t.end();
})
.catch(() => {
t.ok(false, "a valid query should not fail");
t.end();
});
}));
tap.test('PATCH /schools/s2/students/s7 (invalid body)', (t) => __awaiter(void 0, void 0, void 0, function* () {
const request = yield api.parseRequest(['schools', 's2', 'students', 's7']);
request.type = ApiRequest_1.ApiRequestType.Patch;
request.body = {
fullName: "Merry Test",
schoolId: "s3"
};
const query = api.buildQuery(request);
t.equal(query.steps.length, 8, 'should build a 8 step query');
t.ok(query.steps[0] instanceof builder.SetBodyQueryStep, 'SET BODY');
t.ok(query.steps[1] instanceof builder.ExtendContextLiveQueryStep, 'EXTEND');
t.ok(query.steps[2] instanceof builder.QueryEdgeQueryStep, 'QUERY /schools');
t.ok(query.steps[3] instanceof builder.RelateChangeQueryStep, 'RELATE CHANGE schoolId');
t.ok(query.steps[4] instanceof builder.RelateQueryStep, 'RELATE schoolId');
t.ok(query.steps[5] instanceof builder.ExtendContextLiveQueryStep, 'EXTEND CONTEXT');
t.ok(query.steps[6] instanceof builder.ExtendContextQueryStep, 'APPLY PARAMS');
t.ok(query.steps[7] instanceof builder.QueryEdgeQueryStep, 'QUERY /students');
query.execute()
.then(resp => {
t.same(resp.data, {
id: "s7",
fullName: "Merry Test",
email: "marry.test@gmail.com",
schoolId: "s2",
classId: "c1"
});
t.equal(resp.metadata, null);
t.end();
})
.catch(() => {
t.ok(false, "a valid query should not fail");
t.end();
});
}));
tap.test('PUT /schools/s2/students/s7', (t) => __awaiter(void 0, void 0, void 0, function* () {
const request = yield api.parseRequest(['schools', 's2', 'students', 's7']);
request.type = ApiRequest_1.ApiRequestType.Update;
request.body = {
id: "s7",
fullName: "Merry Test",
email: "merry.test@gmail.com",
schoolId: "s2",
classId: "c1"
};
const query = api.buildQuery(request);
t.equal(query.steps.length, 8, 'should build a 8 step query');
t.ok(query.steps[0] instanceof builder.SetBodyQueryStep, 'SET BODY');
t.ok(query.steps[1] instanceof builder.ExtendContextLiveQueryStep, 'EXTEND');
t.ok(query.steps[2] instanceof builder.QueryEdgeQueryStep, 'QUERY /schools');
t.ok(query.steps[3] instanceof builder.RelateChangeQueryStep, 'RELATE CHANGE schoolId');
t.ok(query.steps[4] instanceof builder.RelateQueryStep, 'RELATE schoolId');
t.ok(query.steps[5] instanceof builder.ExtendContextLiveQueryStep, 'EXTEND CONTEXT');
t.ok(query.steps[6] instanceof builder.ExtendContextQueryStep, 'APPLY PARAMS');
t.ok(query.steps[7] instanceof builder.QueryEdgeQueryStep, 'QUERY /students');
query.execute()
.then(resp => {
t.same(resp.data, {
id: "s7",
fullName: "Merry Test",
email: "merry.test@gmail.com",
schoolId: "s2",
classId: "c1"
});
t.equal(resp.metadata, null);
t.end();
})
.catch(() => {
t.ok(false, "a valid query should not fail");
t.end();
});
}));
tap.test('PUT /schools/s2/students/s7 (invalid body)', (t) => __awaiter(void 0, void 0, void 0, function* () {
const request = yield api.parseRequest(['schools', 's2', 'students', 's7']);
request.type = ApiRequest_1.ApiRequestType.Update;
request.body = {
id: "s7",
fullName: "Merry Test",
email: "merry.test@gmail.com",
schoolId: "s3",
classId: "c1"
};
const query = api.buildQuery(request);
t.equal(query.steps.length, 8, 'should build a 8 step query');
t.ok(query.steps[0] instanceof builder.SetBodyQueryStep, 'SET BODY');
t.ok(query.steps[1] instanceof builder.ExtendContextLiveQueryStep, 'EXTEND');
t.ok(query.steps[2] instanceof builder.QueryEdgeQueryStep, 'QUERY /schools');
t.ok(query.steps[3] instanceof builder.RelateChangeQueryStep, 'RELATE CHANGE schoolId');
t.ok(query.steps[4] instanceof builder.RelateQueryStep, 'RELATE schoolId');
t.ok(query.steps[5] instanceof builder.ExtendContextLiveQueryStep, 'EXTEND CONTEXT');
t.ok(query.steps[6] instanceof builder.ExtendContextQueryStep, 'APPLY PARAMS');
t.ok(query.steps[7] instanceof builder.QueryEdgeQueryStep, 'QUERY /students');
query.execute()
.then(resp => {
t.same(resp.data, {
id: "s7",
fullName: "Merry Test",
email: "merry.test@gmail.com",
schoolId: "s2",
classId: "c1"
});
t.equal(resp.metadata, null);
t.end();
})
.catch(() => {
t.ok(false, "a valid query should not fail");
t.end();
});
}));
tap.test('DELETE /schools/s2/students/s7', (t) => __awaiter(void 0, void 0, void 0, function* () {
const request = yield api.parseRequest(['schools', 's2', 'students', 's7']);
request.type = ApiRequest_1.ApiRequestType.Delete;
const query = api.buildQuery(request);
t.equal(query.steps.length, 6, 'should build a 6 step query');
t.ok(query.steps[0] instanceof builder.ExtendContextLiveQueryStep, 'EXTEND');
t.ok(query.steps[1] instanceof builder.QueryEdgeQueryStep, 'QUERY /schools');
t.ok(query.steps[2] instanceof builder.RelateQueryStep, 'RELATE schoolId');
t.ok(query.steps[3] instanceof builder.ExtendContextLiveQueryStep, 'EXTEND CONTEXT');
t.ok(query.steps[4] instanceof builder.ExtendContextQueryStep, 'APPLY PARAMS');
t.ok(query.steps[5] instanceof builder.QueryEdgeQueryStep, 'QUERY /students');
query.execute()
.then(resp => {
t.equal(resp.metadata, null);
t.end();
})
.catch(() => {
t.ok(false, "a valid query should not fail");
t.end();
});
}));
//# sourceMappingURL=related-change-queries.js.map