forked from loafle/openapi-generator-original
[TS-Refactor] Added tests for Object Serializer
This commit is contained in:
parent
525f48d694
commit
c2b7422a81
File diff suppressed because it is too large
Load Diff
@ -2,7 +2,9 @@
|
||||
"private": true,
|
||||
"dependencies": {
|
||||
"@types/form-data": "^2.2.1",
|
||||
"@types/rewire": "^2.5.28",
|
||||
"form-data": "^2.3.2",
|
||||
"rewire": "^4.0.1",
|
||||
"ts-node": "^3.3.0",
|
||||
"ts-petstore-client": "file:../../builds/default"
|
||||
},
|
||||
@ -13,10 +15,10 @@
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/chai": "^4.0.1",
|
||||
"chai": "^4.1.0",
|
||||
"@types/isomorphic-fetch": "0.0.34",
|
||||
"@types/mocha": "^2.2.41",
|
||||
"@types/node": "^8.0.14",
|
||||
"@types/node": "^8.10.38",
|
||||
"chai": "^4.1.0",
|
||||
"mocha": "^5.2.0",
|
||||
"ts-loader": "^2.3.0",
|
||||
"typescript": "^2.4.1"
|
||||
|
@ -0,0 +1,223 @@
|
||||
const rewire = require("rewire")
|
||||
import { expect} from "chai";
|
||||
import {Pet, Category, Tag} from "ts-petstore-client"
|
||||
|
||||
|
||||
const objectSerializerFile = rewire("../../node_modules/ts-petstore-client/dist/models/ObjectSerializer.js")
|
||||
|
||||
const ObjectSerializer = objectSerializerFile.__get__("ObjectSerializer")
|
||||
//console.log(ObjectSerializer)
|
||||
|
||||
|
||||
describe("ObjectSerializer", () => {
|
||||
describe("Serialize", () => {
|
||||
it("String", () => {
|
||||
const input = "test string"
|
||||
expect(ObjectSerializer.serialize(input, "string")).to.equal("test string")
|
||||
});
|
||||
|
||||
it("Number", () => {
|
||||
const input = 1337
|
||||
expect(ObjectSerializer.serialize(input, "number")).to.equal(1337)
|
||||
});
|
||||
|
||||
it("String Array", () => {
|
||||
const input = ["a", "b", "c"]
|
||||
expect(ObjectSerializer.serialize(input, "Array<string>")).to.deep.equal(["a", "b", "c"])
|
||||
})
|
||||
|
||||
it("Number Array", () => {
|
||||
const input = [ 1337, 42, 0]
|
||||
expect(ObjectSerializer.serialize(input, "Array<number>")).to.deep.equal([1337, 42, 0])
|
||||
})
|
||||
|
||||
it("Date", () => {
|
||||
const input = new Date(1543777609696)
|
||||
expect(ObjectSerializer.serialize(input, "Date")).to.equal(input.toISOString())
|
||||
})
|
||||
|
||||
it("Object", () => {
|
||||
const input = {"a": "test", "b": { "test": 5}}
|
||||
expect(ObjectSerializer.serialize(input, "Object")).to.deep.equal({ a: "test", "b": { "test": 5}})
|
||||
})
|
||||
|
||||
it("Class", () => {
|
||||
const input = new Category()
|
||||
input.id = 4
|
||||
input.name = "Test"
|
||||
expect(ObjectSerializer.serialize(input, "Category")).to.deep.equal({ "id": input.id, "name": input.name})
|
||||
});
|
||||
|
||||
it ("Enum", () => {
|
||||
const input = Pet.StatusEnum.Available
|
||||
expect(ObjectSerializer.serialize(input, "Pet.StatusEnum")).to.equal("available")
|
||||
})
|
||||
|
||||
it("Complex Class", () => {
|
||||
const tags = []
|
||||
const tagResult = []
|
||||
for (let i = 0; i < 2; i++) {
|
||||
const tag = new Tag()
|
||||
tag.id = i
|
||||
tag.name = "Tag" + i
|
||||
tags.push(tag)
|
||||
tagResult.push({
|
||||
"id": tag.id,
|
||||
"name": tag.name
|
||||
})
|
||||
}
|
||||
|
||||
const category = new Category()
|
||||
category.id = 4
|
||||
category.name = "TestCat"
|
||||
const pet = new Pet()
|
||||
pet.id = 145
|
||||
pet.category = category
|
||||
pet.name = "PetName"
|
||||
pet.photoUrls = [ "url", "other url"]
|
||||
pet.status = Pet.StatusEnum.Available
|
||||
pet.tags = tags
|
||||
|
||||
expect(ObjectSerializer.serialize(pet, "Pet")).to.deep.equal({
|
||||
"id": pet.id,
|
||||
"name": pet.name,
|
||||
"category": {
|
||||
"id": category.id,
|
||||
"name": category.name
|
||||
},
|
||||
"photoUrls": [ "url", "other url"],
|
||||
"status": "available",
|
||||
"tags": tagResult
|
||||
})
|
||||
})
|
||||
it("Array of Class", () => {
|
||||
const categories = []
|
||||
const result = []
|
||||
for (let i = 0; i < 2; i++) {
|
||||
const category = new Category()
|
||||
category.id = i
|
||||
category.name = "Cat" + i
|
||||
categories.push(category)
|
||||
result.push({
|
||||
"id": category.id,
|
||||
"name": category.name
|
||||
})
|
||||
}
|
||||
|
||||
expect(ObjectSerializer.serialize(categories, "Array<Category>")).to.deep.equal(result)
|
||||
})
|
||||
})
|
||||
|
||||
describe("Deserialize", () => {
|
||||
it("String", () => {
|
||||
const input = "test string"
|
||||
expect(ObjectSerializer.deserialize(input, "string")).to.equal("test string")
|
||||
});
|
||||
|
||||
it("Number", () => {
|
||||
const input = 1337
|
||||
expect(ObjectSerializer.deserialize(input, "number")).to.equal(1337)
|
||||
});
|
||||
|
||||
it("String Array", () => {
|
||||
const input = ["a", "b", "c"]
|
||||
expect(ObjectSerializer.deserialize(input, "Array<string>")).to.deep.equal(["a", "b", "c"])
|
||||
})
|
||||
|
||||
it("Number Array", () => {
|
||||
const input = [ 1337, 42, 0]
|
||||
expect(ObjectSerializer.deserialize(input, "Array<number>")).to.deep.equal([1337, 42, 0])
|
||||
})
|
||||
|
||||
it("Date", () => {
|
||||
const input = new Date(1543777609696)
|
||||
expect(ObjectSerializer.deserialize(input.toISOString(), "Date").getTime()).to.equal(input.getTime())
|
||||
})
|
||||
|
||||
it("Object", () => {
|
||||
const input = {"a": "test", "b": { "test": 5}}
|
||||
expect(ObjectSerializer.deserialize(input, "Object")).to.deep.equal({ a: "test", "b": { "test": 5}})
|
||||
})
|
||||
|
||||
it("Class", () => {
|
||||
const input = new Category()
|
||||
input.id = 4
|
||||
input.name = "Test"
|
||||
const deserialized = ObjectSerializer.deserialize({ "id": 4, "name": "Test"}, "Category")
|
||||
|
||||
expect(deserialized.constructor.name).to.equal("Category")
|
||||
expect(deserialized).to.deep.equal(input)
|
||||
});
|
||||
|
||||
it ("Enum", () => {
|
||||
const input = Pet.StatusEnum.Available
|
||||
expect(ObjectSerializer.deserialize("available", "Pet.StatusEnum")).to.equal(input)
|
||||
})
|
||||
|
||||
it("Complex Class", () => {
|
||||
const tags = []
|
||||
const tagResult = []
|
||||
for (let i = 0; i < 2; i++) {
|
||||
const tag = new Tag()
|
||||
tag.id = i
|
||||
tag.name = "Tag" + i
|
||||
tags.push(tag)
|
||||
tagResult.push({
|
||||
"id": tag.id,
|
||||
"name": tag.name
|
||||
})
|
||||
}
|
||||
|
||||
const category = new Category()
|
||||
category.id = 4
|
||||
category.name = "TestCat"
|
||||
const pet = new Pet()
|
||||
pet.id = 145
|
||||
pet.category = category
|
||||
pet.name = "PetName"
|
||||
pet.photoUrls = [ "url", "other url"]
|
||||
pet.status = Pet.StatusEnum.Available
|
||||
pet.tags = tags
|
||||
|
||||
const deserialized = ObjectSerializer.deserialize({
|
||||
"id": pet.id,
|
||||
"name": pet.name,
|
||||
"category": {
|
||||
"id": category.id,
|
||||
"name": category.name
|
||||
},
|
||||
"photoUrls": [ "url", "other url"],
|
||||
"status": "available",
|
||||
"tags": tagResult
|
||||
}, "Pet") as Pet
|
||||
|
||||
expect(deserialized.constructor.name).to.equal("Pet")
|
||||
expect(deserialized.category.constructor.name).to.equal("Category")
|
||||
for (let i = 0; i < deserialized.tags.length; i++){
|
||||
expect(deserialized.tags[i].constructor.name).to.equal("Tag")
|
||||
}
|
||||
expect(deserialized).to.deep.equal(pet)
|
||||
})
|
||||
|
||||
it("Array of Class", () => {
|
||||
const categories = []
|
||||
const result = []
|
||||
for (let i = 0; i < 2; i++) {
|
||||
const category = new Category()
|
||||
category.id = i
|
||||
category.name = "Cat" + i
|
||||
categories.push(category)
|
||||
result.push({
|
||||
"id": category.id,
|
||||
"name": category.name
|
||||
})
|
||||
}
|
||||
|
||||
const deserialized = ObjectSerializer.deserialize(result, "Array<Category>")
|
||||
for (let i = 0; i < categories.length; i++) {
|
||||
expect(deserialized[i].constructor.name).to.equal("Category")
|
||||
}
|
||||
expect(deserialized).to.deep.equal(categories)
|
||||
})
|
||||
})
|
||||
})
|
@ -6,7 +6,8 @@
|
||||
"sourceMap": false,
|
||||
"outDir": "dist",
|
||||
"types": [
|
||||
"mocha"
|
||||
"mocha",
|
||||
"node"
|
||||
],
|
||||
"lib": [
|
||||
"es6",
|
||||
|
Loading…
x
Reference in New Issue
Block a user