odata-v4-server
Version:
OData V4 Server
880 lines • 91.8 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
/// <reference types="mocha" />
const test_model_1 = require("./test.model");
const model_1 = require("./model/model");
const metadata_spec_1 = require("./metadata.spec");
const ModelsForPromise_1 = require("./model/ModelsForPromise");
const ModelsForGenerator_1 = require("./model/ModelsForGenerator");
const mongodb_1 = require("mongodb");
const { expect } = require("chai");
const extend = require("extend");
let categories = require("./model/categories");
let products = require("./model/products");
let streamBuffers = require("stream-buffers");
function testFactory(createTest) {
describe("OData CRUD", () => {
createTest("should return entity set result", test_model_1.TestServer, "GET /EntitySet", {
statusCode: 200,
body: {
"@odata.context": "http://localhost/$metadata#EntitySet",
value: [{
"@odata.id": "http://localhost/EntitySet(1)",
"@odata.editLink": "http://localhost/EntitySet(1)",
id: 1,
a: 1
}]
},
elementType: test_model_1.Foobar,
contentType: "application/json"
});
createTest("should return entity set result using generator function", test_model_1.TestServer, "GET /GeneratorEntitySet", {
statusCode: 200,
body: {
"@odata.context": "http://localhost/$metadata#GeneratorEntitySet",
value: [{
"@odata.id": "http://localhost/GeneratorEntitySet(1)",
id: 1,
a: 1
}]
},
elementType: test_model_1.Foobar,
contentType: "application/json"
});
createTest("should return entity set result using async function", test_model_1.TestServer, "GET /AsyncEntitySet", {
statusCode: 200,
body: {
"@odata.context": "http://localhost/$metadata#AsyncEntitySet",
value: [{
"@odata.id": "http://localhost/AsyncEntitySet(1)",
id: 1,
a: 1
}]
},
elementType: test_model_1.Foobar,
contentType: "application/json"
});
createTest("should return entity set result by key using async function", test_model_1.TestServer, "GET /AsyncEntitySet(1)", {
statusCode: 200,
body: {
"@odata.context": "http://localhost/$metadata#AsyncEntitySet/$entity",
"@odata.id": "http://localhost/AsyncEntitySet(1)",
id: 1
},
elementType: test_model_1.Foobar,
contentType: "application/json"
});
createTest("should return entity set result with inline count", test_model_1.TestServer, "GET /InlineCountEntitySet?$count=true", {
statusCode: 200,
body: {
"@odata.context": "http://localhost/$metadata#InlineCountEntitySet",
"@odata.count": 1,
value: [{
"@odata.id": "http://localhost/InlineCountEntitySet(1)",
id: 1,
a: 1
}]
},
elementType: test_model_1.Foobar,
contentType: "application/json"
});
createTest("should return entity by key", test_model_1.TestServer, "GET /EntitySet(1)", {
statusCode: 200,
body: {
"@odata.context": "http://localhost/$metadata#EntitySet/$entity",
"@odata.id": "http://localhost/EntitySet(1)",
"@odata.editLink": "http://localhost/EntitySet(1)",
id: 1,
"foo": "bar"
},
elementType: test_model_1.Foobar,
contentType: "application/json"
});
createTest("should insert new entity", test_model_1.TestServer, "POST /EntitySet", {
statusCode: 201,
body: {
"@odata.context": "http://localhost/$metadata#EntitySet/$entity",
"@odata.id": "http://localhost/EntitySet(1)",
"@odata.editLink": "http://localhost/EntitySet(1)",
id: 1,
foo: "bar"
},
elementType: test_model_1.Foobar,
contentType: "application/json"
}, {
foo: "bar"
});
createTest("should update entity", test_model_1.TestServer, "PUT /EntitySet(1)", {
statusCode: 204
}, {
foo: "foobar"
});
createTest("should update entity using delta", test_model_1.TestServer, "PATCH /EntitySet(1)", {
statusCode: 204
}, {
bar: "foo"
});
createTest("should delete entity", test_model_1.TestServer, "DELETE /EntitySet(1)", {
statusCode: 204
});
createTest("should return result count", test_model_1.TestServer, "GET /EntitySet/$count", {
statusCode: 200,
body: 1,
elementType: Number,
contentType: "text/plain"
});
createTest("should return entity property", test_model_1.TestServer, "GET /EntitySet(1)/foo", {
statusCode: 200,
body: {
"@odata.context": "http://localhost/$metadata#EntitySet(1)/foo",
value: "bar"
},
elementType: "Edm.String",
contentType: "application/json"
});
createTest("should return entity property value", test_model_1.TestServer, "GET /EntitySet(1)/foo/$value", {
statusCode: 200,
body: "bar",
elementType: String,
contentType: "text/plain"
});
createTest("should return entity key property value", test_model_1.TestServer, "GET /EntitySet(1)/id/$value", {
statusCode: 200,
body: 1,
elementType: Number,
contentType: "text/plain"
});
createTest("should return entity value", test_model_1.TestServer, "GET /EntitySet(1)/$value", {
statusCode: 200,
body: {
id: 1,
foo: "bar"
},
elementType: test_model_1.Foobar,
contentType: "application/json"
});
createTest("should call action import", test_model_1.TestServer, "POST /ActionImport", {
statusCode: 204
});
createTest("should call action import with parameters", test_model_1.TestServer, "POST /ActionImportParams", {
statusCode: 204
}, {
value: 42
});
createTest("should call function import", test_model_1.TestServer, "GET /FunctionImport(value=42)", {
statusCode: 200,
body: {
"@odata.context": "http://localhost/$metadata#Edm.String",
value: "The number is 42."
},
elementType: "Edm.String",
contentType: "application/json"
});
createTest("should call function import using parameter alias", test_model_1.TestServer, "GET /FunctionImport(value=@value)?@value=42", {
statusCode: 200,
body: {
"@odata.context": "http://localhost/$metadata#Edm.String",
value: "The number is 42."
},
elementType: "Edm.String",
contentType: "application/json"
});
createTest("should call function import using different key and parameter name", test_model_1.TestServer, "GET /FunctionImport(param=@test)?@test=42", {
statusCode: 200,
body: {
"@odata.context": "http://localhost/$metadata#Edm.String",
value: "The number is undefined."
},
elementType: "Edm.String",
contentType: "application/json"
});
createTest("should call function import with multiple parameters", test_model_1.TestServer, "GET /FunctionImportMore(value=42,message='hello world')", {
statusCode: 200,
body: {
"@odata.context": "http://localhost/$metadata#Edm.String",
value: "The number is 42 and your message was hello world."
},
elementType: "Edm.String",
contentType: "application/json"
});
createTest("should call function import with multiple parameters using parameter alias", test_model_1.TestServer, "GET /FunctionImportMore(value=@value,message=@message)?@value=42&@message='hello world'", {
statusCode: 200,
body: {
"@odata.context": "http://localhost/$metadata#Edm.String",
value: "The number is 42 and your message was hello world."
},
elementType: "Edm.String",
contentType: "application/json"
});
createTest("should call function import with multiple parameters (different ordering)", test_model_1.TestServer, "GET /FunctionImportMore(message='hello world',value=42)", {
statusCode: 200,
body: {
"@odata.context": "http://localhost/$metadata#Edm.String",
value: "The number is 42 and your message was hello world."
},
elementType: "Edm.String",
contentType: "application/json"
});
createTest("should call bound collection action", test_model_1.TestServer, "POST /BoundOperationEntitySet/Default.Action", {
statusCode: 204
});
createTest("should call bound entity action", test_model_1.TestServer, "POST /BoundOperationEntitySet(1)/Default.Foo", {
statusCode: 204
});
createTest("should call bound collection function", test_model_1.TestServer, "GET /BoundOperationEntitySet/Default.Function(value=42)", {
statusCode: 200,
body: {
"@odata.context": "http://localhost/$metadata#Edm.String",
value: "foobar:42"
},
elementType: "Edm.String",
contentType: "application/json"
});
createTest("should call bound entity function", test_model_1.TestServer, "GET /BoundOperationEntitySet(1)/Default.Bar()", {
statusCode: 200,
body: {
"@odata.context": "http://localhost/$metadata#Edm.String",
value: "foobar"
},
elementType: "Edm.String",
contentType: "application/json"
});
createTest("should call bound entity function with parameter", test_model_1.TestServer, "GET /BoundOperationEntitySet(1)/Echo.echo(message='my message')", {
statusCode: 200,
body: {
"@odata.context": "http://localhost/$metadata#Edm.String",
value: "my message"
},
elementType: "Edm.String",
contentType: "application/json"
});
createTest("should call bound entity function with parameter", test_model_1.TestServer, "GET /BoundOperationEntitySet(1)/Echo.echoMany(message='my message')", {
statusCode: 200,
body: {
"@odata.context": "http://localhost/$metadata#Collection(Edm.String)",
value: ["my message"]
},
elementType: "Edm.String",
contentType: "application/json"
});
createTest("should call bound collection function using parameter alias", test_model_1.TestServer, "GET /BoundOperationEntitySet/Default.Function(value=@value)?@value=42", {
statusCode: 200,
body: {
"@odata.context": "http://localhost/$metadata#Edm.String",
value: "foobar:42"
},
elementType: "Edm.String",
contentType: "application/json"
});
createTest("should call function import with multiple parameters", test_model_1.TestServer, "GET /BoundOperationEntitySet/Default.FunctionMore(value=42,message='hello world')", {
statusCode: 200,
body: {
"@odata.context": "http://localhost/$metadata#Edm.String",
value: "The number is 42 and your message was hello world."
},
elementType: "Edm.String",
contentType: "application/json"
});
createTest("should call function import with multiple parameters using parameter alias", test_model_1.TestServer, "GET /BoundOperationEntitySet/Default.FunctionMore(value=@value,message=@message)?@value=42&@message='hello world'", {
statusCode: 200,
body: {
"@odata.context": "http://localhost/$metadata#Edm.String",
value: "The number is 42 and your message was hello world."
},
elementType: "Edm.String",
contentType: "application/json"
});
createTest("should call function import with multiple parameters (different ordering)", test_model_1.TestServer, "GET /BoundOperationEntitySet/Default.FunctionMore(message='hello world',value=42)", {
statusCode: 200,
body: {
"@odata.context": "http://localhost/$metadata#Edm.String",
value: "The number is 42 and your message was hello world."
},
elementType: "Edm.String",
contentType: "application/json"
});
createTest("should return entity collection navigation property result", test_model_1.TestServer, "GET /Categories('578f2baa12eaebabec4af290')/Products", {
statusCode: 200,
body: {
"@odata.context": "http://localhost/$metadata#Categories('578f2baa12eaebabec4af290')/Products",
value: products.filter(product => product.CategoryId && product.CategoryId.toString() == "578f2baa12eaebabec4af290").map(product => Object.assign({
"@odata.id": `http://localhost/Products('${product._id}')`
}, product))
},
elementType: model_1.Product,
contentType: "application/json"
});
createTest("should return entity collection navigation property result with filter", test_model_1.TestServer, "GET /Categories('578f2baa12eaebabec4af290')/Products?$filter=Name eq 'Pavlova'", {
statusCode: 200,
body: {
"@odata.context": "http://localhost/$metadata#Categories('578f2baa12eaebabec4af290')/Products",
value: products.filter(product => product.CategoryId && product.CategoryId.toString() == "578f2baa12eaebabec4af290" && product.Name == "Pavlova").map(product => Object.assign({
"@odata.id": "http://localhost/Products('578f2b8c12eaebabec4af248')"
}, product))
},
elementType: model_1.Product,
contentType: "application/json"
});
createTest("should return entity collection navigation property result", test_model_1.TestServer, "GET /Categories('578f2baa12eaebabec4af289')/Products('578f2b8c12eaebabec4af23c')", {
statusCode: 200,
body: Object.assign({
"@odata.context": "http://localhost/$metadata#Products/$entity",
"@odata.id": "http://localhost/Products('578f2b8c12eaebabec4af23c')"
}, products.filter(product => product._id.toString() == "578f2b8c12eaebabec4af23c")[0]),
elementType: model_1.Product,
contentType: "application/json"
});
createTest("should return stream result set of Categories using generator function", test_model_1.TestServer, "GET /Categories2", {
statusCode: 200,
body: {
"@odata.count": 8,
"@odata.context": "http://localhost/$metadata#Categories2",
value: categories.map(category => {
return Object.assign({ "@odata.id": `http://localhost/Categories2('${category._id}')` }, category);
})
},
elementType: test_model_1.Category2,
contentType: "application/json"
});
createTest("should return stream result set of Products using generator function", test_model_1.TestServer, "GET /Products2", {
statusCode: 200,
body: {
"@odata.context": "http://localhost/$metadata#Products2",
value: products.map(product => {
return Object.assign({ "@odata.id": `http://localhost/Products2('${product._id}')` }, product);
})
},
elementType: test_model_1.Product2,
contentType: "application/json"
});
createTest("should return exact Category using generator function", test_model_1.TestServer, "GET /Categories2('578f2baa12eaebabec4af289')", {
statusCode: 200,
body: {
"@odata.context": "http://localhost/$metadata#Categories2/$entity",
"@odata.id": "http://localhost/Categories2('578f2baa12eaebabec4af289')",
"Description": "Soft drinks",
"Name": "Beverages",
"_id": new mongodb_1.ObjectID("578f2baa12eaebabec4af289")
},
elementType: test_model_1.Category2,
contentType: "application/json"
});
createTest("should return stream result stream of Products referenced to Category using generator function", test_model_1.TestServer, "GET /Categories2('578f2baa12eaebabec4af290')/Products2", {
statusCode: 200,
body: {
"@odata.context": "http://localhost/$metadata#Categories2('578f2baa12eaebabec4af290')/Products2",
value: products.filter(product => product && product.CategoryId.toString() === "578f2baa12eaebabec4af290")
.map(product => {
return Object.assign({ "@odata.id": `http://localhost/Products2('${product._id}')` }, product);
})
},
elementType: test_model_1.Product2,
contentType: "application/json"
});
createTest("should return product referenced to category using generator function", test_model_1.TestServer, "GET /Categories2('578f2baa12eaebabec4af289')/Products2('578f2b8c12eaebabec4af23c')", {
statusCode: 200,
body: {
"@odata.context": "http://localhost/$metadata#Products2/$entity",
"value": [{
"@odata.id": "http://localhost/Products2('578f2b8c12eaebabec4af23c')",
"Discontinued": false, "Name": "Chai", "QuantityPerUnit": "10 boxes x 20 bags",
"UnitPrice": 39,
"_id": new mongodb_1.ObjectID("578f2b8c12eaebabec4af23c"),
"CategoryId": new mongodb_1.ObjectID("578f2baa12eaebabec4af289")
}]
},
elementType: test_model_1.Product2,
contentType: "application/json"
});
createTest("should return stream result stream of Categories referenced to Product using generator function", test_model_1.TestServer, "GET /Products2('578f2b8c12eaebabec4af23c')/Category2", {
statusCode: 200,
body: {
"@odata.context": "http://localhost/$metadata#Categories2/$entity",
"value": [{
"@odata.id": "http://localhost/Categories2('578f2baa12eaebabec4af289')",
"Description": "Soft drinks",
"Name": "Beverages",
"_id": new mongodb_1.ObjectID("578f2baa12eaebabec4af289")
}]
},
elementType: test_model_1.Category2,
contentType: "application/json"
});
/**
*
*
* ADVANCED GENERATOR W/ PROMISE
*
*
*
*/
createTest("should return promise of products using generator function", test_model_1.TestServer, "GET /AdvancedProducts", {
statusCode: 200,
body: {
"@odata.count": 76,
"@odata.context": "http://localhost/$metadata#AdvancedProducts",
value: products.map((product) => {
return Object.assign({ "@odata.id": `http://localhost/AdvancedProducts('${product._id}')` }, product);
})
},
elementType: ModelsForPromise_1.ProductPromise,
contentType: "application/json"
});
createTest("should return promise of categories using generator function", test_model_1.TestServer, "GET /AdvancedCategories", {
statusCode: 200,
body: {
"@odata.context": "http://localhost/$metadata#AdvancedCategories",
value: categories.map((category) => {
return Object.assign({ "@odata.id": `http://localhost/AdvancedCategories('${category._id}')` }, category);
})
},
elementType: ModelsForPromise_1.CategoryPromise,
contentType: "application/json"
});
createTest("should return promise of product using generator function", test_model_1.TestServer, "GET /AdvancedProducts('578f2b8c12eaebabec4af23c')", {
statusCode: 200,
body: {
"@odata.context": "http://localhost/$metadata#AdvancedProducts/$entity",
"@odata.id": "http://localhost/AdvancedProducts('578f2b8c12eaebabec4af23c')",
"Discontinued": false,
"Name": "Chai",
"QuantityPerUnit": "10 boxes x 20 bags",
"UnitPrice": 39,
"_id": new mongodb_1.ObjectID("578f2b8c12eaebabec4af23c"),
"CategoryId": new mongodb_1.ObjectID("578f2baa12eaebabec4af289")
},
elementType: ModelsForPromise_1.ProductPromise,
contentType: "application/json"
});
createTest("should return promise of category using generator function", test_model_1.TestServer, "GET /AdvancedCategories('578f2baa12eaebabec4af289')", {
statusCode: 200,
body: {
"@odata.context": "http://localhost/$metadata#AdvancedCategories/$entity",
"@odata.id": "http://localhost/AdvancedCategories('578f2baa12eaebabec4af289')",
"Description": "Soft drinks",
"Name": "Beverages",
"_id": new mongodb_1.ObjectID("578f2baa12eaebabec4af289")
},
elementType: ModelsForPromise_1.CategoryPromise,
contentType: "application/json"
});
createTest("should return promise of category referenced to product using generator function", test_model_1.TestServer, "GET /AdvancedProducts('578f2b8c12eaebabec4af23c')/CategoryPromise", {
statusCode: 200,
body: {
"@odata.context": "http://localhost/$metadata#AdvancedCategories/$entity",
"value": [{
"@odata.id": "http://localhost/AdvancedCategories('578f2baa12eaebabec4af289')",
"Description": "Soft drinks",
"Name": "Beverages",
"_id": new mongodb_1.ObjectID("578f2baa12eaebabec4af289")
}]
},
elementType: ModelsForPromise_1.CategoryPromise,
contentType: "application/json"
});
createTest("should return promise of products referenced to category using generator function", test_model_1.TestServer, "GET /AdvancedCategories('578f2baa12eaebabec4af289')/ProductPromises", {
statusCode: 200,
body: {
"@odata.context": "http://localhost/$metadata#AdvancedCategories('578f2baa12eaebabec4af289')/ProductPromises",
"value": products.filter((product) => product && product.CategoryId.toString() === "578f2baa12eaebabec4af289")
.map(product => {
return Object.assign({ "@odata.id": `http://localhost/AdvancedProducts('${product._id}')` }, product);
})
},
elementType: ModelsForPromise_1.ProductPromise,
contentType: "application/json"
});
/**
*
*
* ADVANCED GENERATOR W/ ANOTHER GENERATOR
*
*
*
*/
createTest("should return products using generator that calls another generator", test_model_1.TestServer, "GET /GeneratorProducts", {
statusCode: 200,
body: {
"@odata.context": "http://localhost/$metadata#GeneratorProducts",
value: products.map((product) => {
return Object.assign({ "@odata.id": `http://localhost/GeneratorProducts('${product._id}')` }, product);
})
},
elementType: ModelsForGenerator_1.GeneratorProduct,
contentType: "application/json"
});
createTest("should return categories using generator that calls another generator", test_model_1.TestServer, "GET /GeneratorCategories", {
statusCode: 200,
body: {
"@odata.context": "http://localhost/$metadata#GeneratorCategories",
value: categories.map((category) => {
return Object.assign({ "@odata.id": `http://localhost/GeneratorCategories('${category._id}')` }, category);
})
},
elementType: ModelsForGenerator_1.GeneratorCategory,
contentType: "application/json"
});
createTest("should return single product using generator function that calls another generator function", test_model_1.TestServer, "GET /GeneratorProducts('578f2b8c12eaebabec4af23c')", {
statusCode: 200,
body: {
"@odata.context": "http://localhost/$metadata#GeneratorProducts/$entity",
"@odata.id": "http://localhost/GeneratorProducts('578f2b8c12eaebabec4af23c')",
"Discontinued": false,
"Name": "Chai",
"QuantityPerUnit": "10 boxes x 20 bags",
"UnitPrice": 39,
"_id": new mongodb_1.ObjectID("578f2b8c12eaebabec4af23c"),
"CategoryId": new mongodb_1.ObjectID("578f2baa12eaebabec4af289")
},
elementType: ModelsForGenerator_1.GeneratorProduct,
contentType: "application/json"
});
createTest("should return single category using generator function that calls another generator function", test_model_1.TestServer, "GET /GeneratorCategories('578f2baa12eaebabec4af289')", {
statusCode: 200,
body: {
"@odata.context": "http://localhost/$metadata#GeneratorCategories/$entity",
"@odata.id": "http://localhost/GeneratorCategories('578f2baa12eaebabec4af289')",
"Description": "Soft drinks",
"Name": "Beverages",
"_id": new mongodb_1.ObjectID("578f2baa12eaebabec4af289")
},
elementType: ModelsForGenerator_1.GeneratorCategory,
contentType: "application/json"
});
createTest("should return single category referenced to product using generator function that calls another generator function", test_model_1.TestServer, "GET /GeneratorProducts('578f2b8c12eaebabec4af23c')/GeneratorCategory", {
statusCode: 200,
body: {
"@odata.context": "http://localhost/$metadata#GeneratorCategories/$entity",
"@odata.id": "http://localhost/GeneratorCategories('578f2baa12eaebabec4af289')",
"Description": "Soft drinks",
"Name": "Beverages",
"_id": new mongodb_1.ObjectID("578f2baa12eaebabec4af289")
},
elementType: ModelsForGenerator_1.GeneratorCategory,
contentType: "application/json"
});
createTest("should return products referenced to category using generator function that calls another generator function", test_model_1.TestServer, "GET /GeneratorCategories('578f2baa12eaebabec4af289')/GeneratorProducts", {
statusCode: 200,
body: {
"@odata.context": "http://localhost/$metadata#GeneratorCategories('578f2baa12eaebabec4af289')/GeneratorProducts",
"value": products.filter((product) => product && product.CategoryId.toString() === "578f2baa12eaebabec4af289")
.map(product => {
return Object.assign({ "@odata.id": `http://localhost/GeneratorProducts('${product._id}')` }, product);
})
},
elementType: ModelsForGenerator_1.GeneratorProduct,
contentType: "application/json"
});
createTest("should return products referenced to category using generator function that calls another generator function", test_model_1.TestServer, "GET /GeneratorCategories('578f2baa12eaebabec4af289')/GeneratorProducts", {
statusCode: 200,
body: {
"@odata.context": "http://localhost/$metadata#GeneratorCategories('578f2baa12eaebabec4af289')/GeneratorProducts",
"value": products.filter((product) => product && product.CategoryId.toString() === "578f2baa12eaebabec4af289")
.map(product => {
return Object.assign({ "@odata.id": `http://localhost/GeneratorProducts('${product._id}')` }, product);
})
},
elementType: ModelsForGenerator_1.GeneratorProduct,
contentType: "application/json"
});
// createTest("should return product reference on category", TestServer, "GET /Categories('578f2baa12eaebabec4af28e')/Products('578f2b8c12eaebabec4af242')/$ref", {
// statusCode: 200,
// body: {
// "@odata.context": "http://localhost/$metadata#$ref",
// "@odata.id": "http://localhost/Categories('578f2baa12eaebabec4af28e')/Products"
// },
// elementType: Product,
// contentType: "application/json"
// });
createTest("should return products with inline count", test_model_1.TestServer, "GET /Products?$count=true", {
statusCode: 200,
body: {
"@odata.context": "http://localhost/$metadata#Products",
"@odata.count": 76,
value: products.map(product => {
return Object.assign({ "@odata.id": `http://localhost/Products('${product._id}')` }, product);
})
},
elementType: model_1.Product,
contentType: "application/json"
});
createTest("should return product with only name and category property", test_model_1.TestServer, "GET /Products('578f2b8c12eaebabec4af23c')?$select=Name,CategoryId", {
statusCode: 200,
body: {
"@odata.context": "http://localhost/$metadata#Products(Name,CategoryId)/$entity",
"@odata.id": "http://localhost/Products('578f2b8c12eaebabec4af23c')",
"Name": "Chai",
"CategoryId": new mongodb_1.ObjectID("578f2baa12eaebabec4af289")
},
elementType: model_1.Product,
contentType: "application/json"
});
createTest("should return filtered product with only name and category property", test_model_1.TestServer, "GET /Products?$filter=_id eq '578f2b8c12eaebabec4af23c'&$select=Name,CategoryId", {
statusCode: 200,
body: {
"@odata.context": "http://localhost/$metadata#Products(Name,CategoryId)",
"value": [{
"@odata.id": "http://localhost/Products('578f2b8c12eaebabec4af23c')",
"Name": "Chai",
"CategoryId": new mongodb_1.ObjectID("578f2baa12eaebabec4af289")
}]
},
elementType: model_1.Product,
contentType: "application/json"
});
createTest("should return entity navigation property result", test_model_1.TestServer, "GET /Products('578f2b8c12eaebabec4af23c')/Category", {
statusCode: 200,
body: Object.assign({
"@odata.context": "http://localhost/$metadata#Categories/$entity",
"@odata.id": "http://localhost/Categories('578f2baa12eaebabec4af289')",
}, categories.filter(category => category._id.toString() == "578f2baa12eaebabec4af289")[0]),
elementType: model_1.Category,
contentType: "application/json"
});
createTest("should return Category entity with navigation property", test_model_1.TestServer, "GET /Categories('578f2baa12eaebabec4af289')/Products('578f2b8c12eaebabec4af23c')/Category", {
statusCode: 200,
body: {
"@odata.context": "http://localhost/$metadata#Categories/$entity",
"@odata.id": "http://localhost/Categories('578f2baa12eaebabec4af289')",
"Description": "Soft drinks",
"Name": "Beverages",
"_id": new mongodb_1.ObjectID("578f2baa12eaebabec4af289")
},
elementType: model_1.Category,
contentType: "application/json"
});
createTest("should return Category name with navigation property", test_model_1.TestServer, "GET /Categories('578f2baa12eaebabec4af289')/Products('578f2b8c12eaebabec4af23c')/Category/Name", {
statusCode: 200,
body: {
"@odata.context": "http://localhost/$metadata#Categories/$entity/Name",
"value": "Beverages"
},
elementType: "Edm.String",
contentType: "application/json"
});
describe("Expand tests", () => {
createTest("should return products expanded with category", test_model_1.TestServer, "GET /Products?$expand=Category", {
statusCode: 200,
body: {
"@odata.context": "http://localhost/$metadata#Products",
"@odata.count": 76,
"value": products.map(product => {
return Object.assign({ "@odata.id": `http://localhost/Products('${product._id}')` }, product, {
"Category": Object.assign({ "@odata.id": `http://localhost/Categories('${product.CategoryId}')` }, categories.find(c => c._id.toString() === product.CategoryId.toString()))
});
})
},
elementType: model_1.Product,
contentType: "application/json"
});
createTest("should return categories expanded with products", test_model_1.TestServer, "GET /Categories?$expand=Products", {
statusCode: 200,
body: {
"@odata.context": "http://localhost/$metadata#Categories",
"value": categories.map(category => {
return Object.assign({ "@odata.id": `http://localhost/Categories('${category._id}')` }, category, { Products: products.filter(p => p.CategoryId.toString() === `${category._id}`)
.map(p => Object.assign({ "@odata.id": `http://localhost/Products('${p._id}')` }, p))
});
})
},
elementType: model_1.Category,
contentType: "application/json"
});
createTest("should return single product expanded with categories", test_model_1.TestServer, "GET /Products('578f2b8c12eaebabec4af23c')?$expand=Category", {
statusCode: 200,
body: Object.assign({
"@odata.context": "http://localhost/$metadata#Products/$entity",
"@odata.id": "http://localhost/Products('578f2b8c12eaebabec4af23c')",
}, products.find(product => product._id.toString() === "578f2b8c12eaebabec4af23c"), {
Category: Object.assign({
"@odata.id": "http://localhost/Categories('578f2baa12eaebabec4af289')",
}, categories.find(category => category._id.toString() === "578f2baa12eaebabec4af289"))
}),
elementType: model_1.Product,
contentType: "application/json"
});
createTest("should return single category expanded with products", test_model_1.TestServer, "GET /Categories('578f2baa12eaebabec4af289')?$expand=Products", {
statusCode: 200,
body: Object.assign({
"@odata.context": "http://localhost/$metadata#Categories/$entity",
"@odata.id": "http://localhost/Categories('578f2baa12eaebabec4af289')",
}, categories.find(c => c._id.toString() === "578f2baa12eaebabec4af289"), {
Products: products.filter(p => p.CategoryId.toString() === "578f2baa12eaebabec4af289")
.map(p => Object.assign({ "@odata.id": `http://localhost/Products('${p._id}')` }, p))
}),
elementType: model_1.Category,
contentType: "application/json"
});
createTest("should return categories expanded with products", test_model_1.TestServer, "GET /Categories?$expand=Products", {
statusCode: 200,
body: {
"@odata.context": "http://localhost/$metadata#Categories",
"value": categories.map(category => {
return Object.assign({ "@odata.id": `http://localhost/Categories('${category._id}')` }, category, { Products: products.filter(p => p.CategoryId.toString() === `${category._id}`)
.map(p => Object.assign({ "@odata.id": `http://localhost/Products('${p._id}')` }, p))
});
})
},
elementType: model_1.Category,
contentType: "application/json"
});
createTest("should return GeneratorProducts expanded with GeneratorCategory result using generator function", test_model_1.TestServer, "GET /GeneratorProducts?$expand=GeneratorCategory", {
statusCode: 200,
body: {
"@odata.context": "http://localhost/$metadata#GeneratorProducts",
"value": products.map(product => {
return Object.assign({ "@odata.id": `http://localhost/GeneratorProducts('${product._id}')` }, product, {
"GeneratorCategory": Object.assign({ "@odata.id": `http://localhost/GeneratorCategories('${product.CategoryId}')` }, categories.find(c => c._id.toString() === product.CategoryId.toString()))
});
})
},
elementType: ModelsForGenerator_1.GeneratorProduct,
contentType: "application/json"
});
createTest("should return GeneratorCategories expanded with GeneratorProducts using generator function", test_model_1.TestServer, "GET /GeneratorCategories?$expand=GeneratorProducts", {
statusCode: 200,
body: {
"@odata.context": "http://localhost/$metadata#GeneratorCategories",
"value": categories.map(category => {
return Object.assign({ "@odata.id": `http://localhost/GeneratorCategories('${category._id}')` }, category, {
GeneratorProducts: products.filter(p => p.CategoryId.toString() === `${category._id}`)
.map(p => Object.assign({ "@odata.id": `http://localhost/GeneratorProducts('${p._id}')` }, p))
});
})
},
elementType: ModelsForGenerator_1.GeneratorCategory,
contentType: "application/json"
});
createTest("should return GeneratorProduct expanded with GeneratorCategories using generator function and navigation property", test_model_1.TestServer, "GET /GeneratorProducts('578f2b8c12eaebabec4af23c')?$expand=GeneratorCategory", {
statusCode: 200,
body: Object.assign({
"@odata.context": "http://localhost/$metadata#GeneratorProducts/$entity",
"@odata.id": "http://localhost/GeneratorProducts('578f2b8c12eaebabec4af23c')",
}, products.find(product => product._id.toString() === "578f2b8c12eaebabec4af23c"), {
GeneratorCategory: Object.assign({
"@odata.id": "http://localhost/GeneratorCategories('578f2baa12eaebabec4af289')",
}, categories.find(category => category._id.toString() === "578f2baa12eaebabec4af289"))
}),
elementType: ModelsForGenerator_1.GeneratorProduct,
contentType: "application/json"
});
createTest("should return GeneratorCategory expanded with GeneratorProducts using generator function and navigation property", test_model_1.TestServer, "GET /GeneratorCategories('578f2baa12eaebabec4af289')?$expand=GeneratorProducts", {
statusCode: 200,
body: Object.assign({
"@odata.context": "http://localhost/$metadata#GeneratorCategories/$entity",
"@odata.id": "http://localhost/GeneratorCategories('578f2baa12eaebabec4af289')",
}, categories.find(c => c._id.toString() == "578f2baa12eaebabec4af289"), {
GeneratorProducts: products.filter(p => p.CategoryId.toString() === "578f2baa12eaebabec4af289")
.map(p => Object.assign({ "@odata.id": `http://localhost/GeneratorProducts('${p._id}')` }, p))
}),
elementType: ModelsForGenerator_1.GeneratorCategory,
contentType: "application/json"
});
createTest("should return Categories2 expanded with Products2 using generator function", test_model_1.TestServer, "GET /Categories2?$expand=Products2", {
statusCode: 200,
body: {
"@odata.context": "http://localhost/$metadata#Categories2",
"@odata.count": 8,
"value": categories.map(category => {
return Object.assign({ "@odata.id": `http://localhost/Categories2('${category._id}')` }, category, {
Products2: products.filter(p => p.CategoryId.toString() === category._id.toString())
.map(p => Object.assign({ "@odata.id": `http://localhost/Products2('${p._id}')` }, p))
});
})
},
elementType: test_model_1.Category2,
contentType: "application/json"
});
createTest("should return stream result of Products2 expanded with Category2 using generator function", test_model_1.TestServer, "GET /Products2?$expand=Category2", {
statusCode: 200,
body: {
"@odata.context": "http://localhost/$metadata#Products2",
"value": products.map(product => {
return Object.assign({ "@odata.id": `http://localhost/Products2('${product._id}')` }, product, {
Category2: {
"value": categories.filter(c => c._id.toString() === product.CategoryId.toString())
.map(c => Object.assign({ "@odata.id": `http://localhost/Categories2('${c._id}')` }, c))
}
});
})
},
elementType: test_model_1.Product2,
contentType: "application/json"
});
createTest("should return stream result of Products2 expanded with Category2 using generator function and navigation property", test_model_1.TestServer, "GET /Products2('578f2b8c12eaebabec4af23c')?$expand=Category2", {
statusCode: 200,
body: {
"@odata.context": "http://localhost/$metadata#Products2/$entity",
"@odata.id": "http://localhost/Products2('578f2b8c12eaebabec4af23c')",
"Discontinued": false,
"Name": "Chai",
"QuantityPerUnit": "10 boxes x 20 bags",
"UnitPrice": 39,
"_id": new mongodb_1.ObjectID("578f2b8c12eaebabec4af23c"),
"CategoryId": new mongodb_1.ObjectID("578f2baa12eaebabec4af289"),
"Category2": {
"value": [
{
"@odata.id": "http://localhost/Categories2('578f2baa12eaebabec4af289')",
"Description": "Soft drinks",
"Name": "Beverages",
"_id": new mongodb_1.ObjectID("578f2baa12eaebabec4af289")
}
]
}
},
elementType: test_model_1.Product2,
contentType: "application/json"
});
createTest("should return stream result of Category2 and expanded with Products2 using generator function and navigation property", test_model_1.TestServer, "GET /Categories2('578f2baa12eaebabec4af289')?$expand=Products2", {
statusCode: 200,
body: {
"@odata.context": "http://localhost/$metadata#Categories2/$entity",
"@odata.id": "http://localhost/Categories2('578f2baa12eaebabec4af289')",
"Description": "Soft drinks",
"Name": "Beverages",
"_id": new mongodb_1.ObjectID("578f2baa12eaebabec4af289"),
"Products2": products.filter(p => p.CategoryId.toString() === "578f2baa12eaebabec4af289")
.map(p => Object.assign({ "@odata.id": `http://localhost/Products2('${p._id}')` }, p))
},
elementType: test_model_1.Category2,
contentType: "application/json"
});
createTest("should return Category filtered by Name and expanded with Products", test_model_1.TestServer, "GET /Categories?$expand=Products&$filter=Name eq 'Beverages'", {
statusCode: 200,
body: {
"@odata.context": "http://localhost/$metadata#Categories",
"value": [
{
"@odata.id": "http://localhost/Categories('578f2baa12eaebabec4af289')",
"Description": "Soft drinks",
"Name": "Beverages",
"_id": new mongodb_1.ObjectID("578f2baa12eaebabec4af289"),
"Products": products
.filter(p => p.CategoryId.toString() === "578f2baa12eaebabec4af289")
.map(p => Object.assign({ "@odata.id": `http://localhost/Products('${p._id}')` }, p))
}
]
},
elementType: model_1.Category,
contentType: "application/json"
});
createTest("should return Category2 filtered by Name and expanded with Products2 using generator function", test_model_1.TestServer, "GET /Categories2?$expand=Products2&$filter=Name eq 'Beverages'", {
statusCode: 200,
body: {
"@odata.context": "http://localhost/$metadata#Categories2",
"@odata.count": 1,
"value": [
{
"@odata.id": "http://localhost/Categories2('578f2baa12eaebabec4af289')",
"Description": "Soft drinks",
"Name": "Beverages",
"_id": "578f2baa12eaebabec4af289",
"Products2": products
.filter(p => p.CategoryId.toString() === "578f2baa12eaebabec4af289")
.map(p => Object.assign({ "@odata.id": `http://localhost/Products2('${p._id}')` }, p))
}
]
},
elementType: test_model_1.Category2,
contentType: "application/j