From 5eafd07367f97bde29ad5705a3882da301910943 Mon Sep 17 00:00:00 2001 From: fer22f Date: Mon, 25 Nov 2019 09:25:16 -0300 Subject: [PATCH] test: add a complex type value-transformer test --- .../columns/value-transformer/entity/Post.ts | 34 +++++++++++ .../value-transformer/value-transformer.ts | 58 ++++++++++++++++++- 2 files changed, 91 insertions(+), 1 deletion(-) diff --git a/test/functional/columns/value-transformer/entity/Post.ts b/test/functional/columns/value-transformer/entity/Post.ts index 5077452d66f..3daca45fca4 100644 --- a/test/functional/columns/value-transformer/entity/Post.ts +++ b/test/functional/columns/value-transformer/entity/Post.ts @@ -15,6 +15,38 @@ class TagTransformer implements ValueTransformer { } +export class Complex { + x: number; + y: number; + circularReferenceToMySelf: { + complex: Complex + }; + + constructor(from: String) { + this.circularReferenceToMySelf = { complex: this }; + const [x, y] = from.split(" "); + this.x = +x; + this.y = +y; + } + + toString() { + return `${this.x} ${this.y}`; + } +} + +class ComplexTransformer implements ValueTransformer { + + to (value: Complex | null): string | null { + if (value == null) { return value; } + return value.toString(); + } + + from (value: string | null): Complex | null { + if (value == null) { return value; } + return new Complex(value); + } +} + @Entity() export class Post { @@ -27,4 +59,6 @@ export class Post { @Column({ type: String, transformer: new TagTransformer() }) tags: string[]; + @Column({ type: String, transformer: new ComplexTransformer(), nullable: true }) + complex: Complex | null; } diff --git a/test/functional/columns/value-transformer/value-transformer.ts b/test/functional/columns/value-transformer/value-transformer.ts index 02f5d49bc90..9e49b166aab 100644 --- a/test/functional/columns/value-transformer/value-transformer.ts +++ b/test/functional/columns/value-transformer/value-transformer.ts @@ -4,7 +4,7 @@ import {closeTestingConnections, createTestingConnections, reloadTestingDatabase import {Connection} from "../../../../src/connection/Connection"; import {PhoneBook} from "./entity/PhoneBook"; -import {Post} from "./entity/Post"; +import {Complex, Post} from "./entity/Post"; import {User} from "./entity/User"; importĀ {Category} from "./entity/Category"; import {View} from "./entity/View"; @@ -95,4 +95,60 @@ describe("columns > value-transformer functionality", () => { dbView && dbView.title.should.be.eql(title); }))); + + it("should marshal data using a complex value-transformer", () => Promise.all(connections.map(async connection => { + + const postRepository = connection.getRepository(Post); + + // create and save a post first + const post = new Post(); + post.title = "Complex transformers!"; + post.tags = ["complex", "transformer"]; + await postRepository.save(post); + + let loadedPost = await postRepository.findOne(post.id); + expect(loadedPost!.complex).to.eq(null); + + // then update all its properties and save again + post.title = "Complex transformers2!"; + post.tags = ["very", "complex", "actually"]; + post.complex = new Complex("3 2.5"); + await postRepository.save(post); + + // check if all columns are updated except for readonly columns + loadedPost = await postRepository.findOne(post.id); + expect(loadedPost!.title).to.be.equal("Complex transformers2!"); + expect(loadedPost!.tags).to.deep.eq(["very", "complex", "actually"]); + expect(loadedPost!.complex!.x).to.eq(3); + expect(loadedPost!.complex!.y).to.eq(2.5); + + // then update all its properties and save again + post.title = "Complex transformers3!"; + post.tags = ["very", "lacking", "actually"]; + post.complex = null; + await postRepository.save(post); + + loadedPost = await postRepository.findOne(post.id); + expect(loadedPost!.complex).to.eq(null); + + // then update all its properties and save again + post.title = "Complex transformers4!"; + post.tags = ["very", "here", "again!"]; + post.complex = new Complex("0.5 0.5"); + await postRepository.save(post); + + loadedPost = await postRepository.findOne(post.id); + expect(loadedPost!.complex!.x).to.eq(0.5); + expect(loadedPost!.complex!.y).to.eq(0.5); + + // then update all its properties and save again + post.title = "Complex transformers5!"; + post.tags = ["now", "really", "lacking!"]; + post.complex = new Complex("1.05 2.3"); + await postRepository.save(post); + + loadedPost = await postRepository.findOne(post.id); + expect(loadedPost!.complex!.x).to.eq(1.05); + expect(loadedPost!.complex!.y).to.eq(2.3); + }))); });