Skip to content

Commit

Permalink
test: add a complex type value-transformer test
Browse files Browse the repository at this point in the history
  • Loading branch information
fer22f authored and imnotjames committed Jun 17, 2021
1 parent 82eac85 commit 5eafd07
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 1 deletion.
34 changes: 34 additions & 0 deletions test/functional/columns/value-transformer/entity/Post.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand All @@ -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;
}
58 changes: 57 additions & 1 deletion test/functional/columns/value-transformer/value-transformer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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);
})));
});

0 comments on commit 5eafd07

Please sign in to comment.