trader-server
Version:
OData server for testing strategies, simulating and real trading.
1,009 lines (930 loc) • 91.1 kB
text/typescript
/// <reference types="mocha" />
import { TestServer, Foobar, AuthenticationServer, Image, Image2, User, Location, Music, DefTest, DefTestServer, Product2, Category2, UpsertTestEntity, ProductServer, CategoryServer, CategoryStream } from './test.model';
import { Edm, odata, NotImplementedError } from "../lib/index";
import { Product, Category } from "./model/model";
import { Meta, Media, TestEntity, MetaTestServer, CompoundKey, EmptyEntity, BaseMeta, Genre } from './metadata.spec';
import { ProductPromise, CategoryPromise } from "./model/ModelsForPromise";
import { GeneratorProduct, GeneratorCategory } from "./model/ModelsForGenerator";
import { ObjectID } from "mongodb";
const { expect } = require("chai");
const extend = require("extend");
let categories = require("./model/categories");
let products = require("./model/products");
let streamBuffers = require("stream-buffers");
export function testFactory(createTest: any) {
describe("OData CRUD", () => {
createTest("should return entity set result", 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: Foobar,
contentType: "application/json"
});
createTest("should return entity set result using generator function", TestServer, "GET /GeneratorEntitySet", {
statusCode: 200,
body: {
"@odata.context": "http://localhost/$metadata#GeneratorEntitySet",
value: [{
"@odata.id": "http://localhost/GeneratorEntitySet(1)",
id: 1,
a: 1
}]
},
elementType: Foobar,
contentType: "application/json"
});
createTest("should return entity set result using async function", TestServer, "GET /AsyncEntitySet", {
statusCode: 200,
body: {
"@odata.context": "http://localhost/$metadata#AsyncEntitySet",
value: [{
"@odata.id": "http://localhost/AsyncEntitySet(1)",
id: 1,
a: 1
}]
},
elementType: Foobar,
contentType: "application/json"
});
createTest("should return entity set result by key using async function", TestServer, "GET /AsyncEntitySet(1)", {
statusCode: 200,
body: {
"@odata.context": "http://localhost/$metadata#AsyncEntitySet/$entity",
"@odata.id": "http://localhost/AsyncEntitySet(1)",
id: 1
},
elementType: Foobar,
contentType: "application/json"
});
createTest("should return entity set result with inline count", 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: Foobar,
contentType: "application/json"
});
createTest("should return entity by key", 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: Foobar,
contentType: "application/json"
});
createTest("should insert new entity", 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: Foobar,
contentType: "application/json"
}, {
foo: "bar"
});
createTest("should update entity", TestServer, "PUT /EntitySet(1)", {
statusCode: 204
}, {
foo: "foobar"
});
createTest("should update entity using delta", TestServer, "PATCH /EntitySet(1)", {
statusCode: 204
}, {
bar: "foo"
});
createTest("should delete entity", TestServer, "DELETE /EntitySet(1)", {
statusCode: 204
});
createTest("should return result count", TestServer, "GET /EntitySet/$count", {
statusCode: 200,
body: 1,
elementType: Number,
contentType: "text/plain"
});
createTest("should return entity property", 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", TestServer, "GET /EntitySet(1)/foo/$value", {
statusCode: 200,
body: "bar",
elementType: String,
contentType: "text/plain"
});
createTest("should return entity key property value", TestServer, "GET /EntitySet(1)/id/$value", {
statusCode: 200,
body: 1,
elementType: Number,
contentType: "text/plain"
});
createTest("should return entity value", TestServer, "GET /EntitySet(1)/$value", {
statusCode: 200,
body: {
id: 1,
foo: "bar"
},
elementType: Foobar,
contentType: "application/json"
});
createTest("should call action import", TestServer, "POST /ActionImport", {
statusCode: 204
});
createTest("should call action import with parameters", TestServer, "POST /ActionImportParams", {
statusCode: 204
}, {
value: 42
});
createTest("should call function import", 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", 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", 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", 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", 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)", 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", TestServer, "POST /BoundOperationEntitySet/Default.Action", {
statusCode: 204
});
createTest("should call bound entity action", TestServer, "POST /BoundOperationEntitySet(1)/Default.Foo", {
statusCode: 204
});
createTest("should call bound collection function", 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", 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", 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", 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", 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", 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", 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)", 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", 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: Product,
contentType: "application/json"
});
createTest("should return entity collection navigation property result with filter", 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: Product,
contentType: "application/json"
});
createTest("should return entity collection navigation property result", 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: Product,
contentType: "application/json"
});
createTest("should return stream result set of Categories using generator function", 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: Category2,
contentType: "application/json"
});
createTest("should return stream result set of Products using generator function", 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: Product2,
contentType: "application/json"
});
createTest("should return exact Category using generator function", 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 ObjectID("578f2baa12eaebabec4af289")
},
elementType: Category2,
contentType: "application/json"
});
createTest("should return stream result stream of Products referenced to Category using generator function", 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: Product2,
contentType: "application/json"
});
createTest("should return product referenced to category using generator function", 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 ObjectID("578f2b8c12eaebabec4af23c"),
"CategoryId": new ObjectID("578f2baa12eaebabec4af289")
}]
},
elementType: Product2,
contentType: "application/json"
});
createTest("should return stream result stream of Categories referenced to Product using generator function", 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 ObjectID("578f2baa12eaebabec4af289")
}]
},
elementType: Category2,
contentType: "application/json"
});
/**
*
*
* ADVANCED GENERATOR W/ PROMISE
*
*
*
*/
createTest("should return promise of products using generator function", 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: ProductPromise,
contentType: "application/json"
});
createTest("should return promise of categories using generator function", 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: CategoryPromise,
contentType: "application/json"
});
createTest("should return promise of product using generator function", 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 ObjectID("578f2b8c12eaebabec4af23c"),
"CategoryId": new ObjectID("578f2baa12eaebabec4af289")
},
elementType: ProductPromise,
contentType: "application/json"
});
createTest("should return promise of category using generator function", 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 ObjectID("578f2baa12eaebabec4af289")
},
elementType: CategoryPromise,
contentType: "application/json"
});
createTest("should return promise of category referenced to product using generator function", 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 ObjectID("578f2baa12eaebabec4af289")
}]
},
elementType: CategoryPromise,
contentType: "application/json"
});
createTest("should return promise of products referenced to category using generator function", 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: ProductPromise,
contentType: "application/json"
});
/**
*
*
* ADVANCED GENERATOR W/ ANOTHER GENERATOR
*
*
*
*/
createTest("should return products using generator that calls another generator", 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: GeneratorProduct,
contentType: "application/json"
});
createTest("should return categories using generator that calls another generator", 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: GeneratorCategory,
contentType: "application/json"
});
createTest("should return single product using generator function that calls another generator function", 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 ObjectID("578f2b8c12eaebabec4af23c"),
"CategoryId": new ObjectID("578f2baa12eaebabec4af289")
},
elementType: GeneratorProduct,
contentType: "application/json"
});
createTest("should return single category using generator function that calls another generator function", 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 ObjectID("578f2baa12eaebabec4af289")
},
elementType: GeneratorCategory,
contentType: "application/json"
});
createTest("should return single category referenced to product using generator function that calls another generator function", 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 ObjectID("578f2baa12eaebabec4af289")
},
elementType: GeneratorCategory,
contentType: "application/json"
});
createTest("should return products referenced to category using generator function that calls another generator function",
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: GeneratorProduct,
contentType: "application/json"
});
createTest("should return products referenced to category using generator function that calls another generator function",
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: 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", 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: Product,
contentType: "application/json"
});
createTest("should return product with only name and category property", 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 ObjectID("578f2baa12eaebabec4af289")
},
elementType: Product,
contentType: "application/json"
});
createTest("should return filtered product with only name and category property", 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 ObjectID("578f2baa12eaebabec4af289")
}]
},
elementType: Product,
contentType: "application/json"
});
createTest("should return entity navigation property result", 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: Category,
contentType: "application/json"
});
createTest("should return Category entity with navigation property", 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 ObjectID("578f2baa12eaebabec4af289")
},
elementType: Category,
contentType: "application/json"
});
createTest("should return Category name with navigation property", 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", 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: Product,
contentType: "application/json"
});
createTest("should return categories expanded with products", 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: Category,
contentType: "application/json"
});
createTest("should return single product expanded with categories", 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: Product,
contentType: "application/json"
});
createTest("should return single category expanded with products", 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: Category,
contentType: "application/json"
});
createTest("should return categories expanded with products", 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: Category,
contentType: "application/json"
});
createTest("should return GeneratorProducts expanded with GeneratorCategory result using generator function",
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: GeneratorProduct,
contentType: "application/json"
});
createTest("should return GeneratorCategories expanded with GeneratorProducts using generator function",
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: GeneratorCategory,
contentType: "application/json"
});
createTest("should return GeneratorProduct expanded with GeneratorCategories using generator function and navigation property",
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: GeneratorProduct,
contentType: "application/json"
});
createTest("should return GeneratorCategory expanded with GeneratorProducts using generator function and navigation property",
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: GeneratorCategory,
contentType: "application/json"
});
createTest("should return Categories2 expanded with Products2 using generator function", 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: Category2,
contentType: "application/json"
});
createTest("should return stream result of Products2 expanded with Category2 using generator function",
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: Product2,
contentType: "application/json"
});
createTest("should return stream result of Products2 expanded with Category2 using generator function and navigation property",
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 ObjectID("578f2b8c12eaebabec4af23c"),
"CategoryId": new ObjectID("578f2baa12eaebabec4af289"),
"Category2": {
"value": [
{
"@odata.id": "http://localhost/Categories2('578f2baa12eaebabec4af289')",
"Description": "Soft drinks",
"Name": "Beverages",
"_id": new ObjectID("578f2baa12eaebabec4af289")
}
]
}
},
elementType: Product2,
contentType: "application/json"
});
createTest("should return stream result of Category2 and expanded with Products2 using generator function and navigation property",
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 ObjectID("578f2baa12eaebabec4af289"),
"Products2": products.filter(p => p.CategoryId.toString() === "578f2baa12eaebabec4af289")
.map(p => Object.assign({ "@odata.id": `http://localhost/Products2('${p._id}')` }, p))
},
elementType: Category2,
contentType: "application/json"
});
createTest("should return Category filtered by Name and expanded with Products",
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 ObjectID("578f2baa12eaebabec4af289"),
"Products": products
.filter(p => p.CategoryId.toString() === "578f2baa12eaebabec4af289")
.map(p => Object.assign({ "@odata.id": `http://localhost/Products('${p._id}')` }, p))
}
]
},
elementType: Category,
contentType: "application/json"
});
createTest("should return Category2 filtered by Name and expanded with Products2 using generator function",
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.Catego