Skip to content

Commit

Permalink
test: add mongo deep-merge test
Browse files Browse the repository at this point in the history
  • Loading branch information
imnotjames committed Jun 17, 2021
1 parent 5eafd07 commit 1877423
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 3 deletions.
@@ -0,0 +1,19 @@
import {Column} from "../../../../../../src/decorator/columns/Column";
import {Entity} from "../../../../../../src/decorator/entity/Entity";
import {User} from "./User";

@Entity()
export class Counters {

@Column({ name: "_likes" })
likes: number;

@Column({ name: "_comments" })
comments: number;

@Column({ name: "_favorites" })
favorites: number;

@Column(() => User)
viewer: User;
}
@@ -1,4 +1,5 @@
import {Entity} from "../../../../../../src/decorator/entity/Entity";
import {Counters} from "./Counters";
import {Column} from "../../../../../../src/decorator/columns/Column";
import {ObjectIdColumn} from "../../../../../../src/decorator/columns/ObjectIdColumn";
import {ObjectID} from "../../../../../../src/driver/mongodb/typings";
Expand All @@ -18,7 +19,6 @@ export class Post {
@Column()
index: number;

// @Column(() => Counters)
// counters: Counters;

@Column(() => Counters)
counters: Counters;
}
@@ -0,0 +1,9 @@
import {Column} from "../../../../../../src/decorator/columns/Column";
import {Entity} from "../../../../../../src/decorator/entity/Entity";

@Entity()
export class User {

@Column()
name: string;
}
Expand Up @@ -3,6 +3,8 @@ import {expect} from "chai";
import {Connection} from "../../../../../src/connection/Connection";
import {closeTestingConnections, createTestingConnections, reloadTestingDatabases} from "../../../../utils/test-utils";
import {Post} from "./entity/Post";
import {Counters} from "./entity/Counters";
import {User} from "./entity/User";

describe("mongodb > basic repository actions", () => {

Expand Down Expand Up @@ -46,6 +48,32 @@ describe("mongodb > basic repository actions", () => {
mergedPost.text.should.be.equal("And its text is updated as well");
})));

it("merge should merge all given recursive partial objects into given source entity", () => Promise.all(connections.map(async connection => {
const postRepository = connection.getRepository(Post);
const counter1 = new Counters();
counter1.likes = 5;
const counter2 = new Counters();
counter2.likes = 2;
counter2.viewer = new User();
counter2.viewer.name = "Hello World";
const post = postRepository.create({
title: "This is created post",
text: "All about this post",
counters: counter1
});
const mergedPost = postRepository.merge(post,
{ title: "This is updated post" },
{ text: "And its text is updated as well" },
{ counters: counter2 }
);
mergedPost.should.be.instanceOf(Post);
mergedPost.should.be.equal(post);
mergedPost.title.should.be.equal("This is updated post");
mergedPost.text.should.be.equal("And its text is updated as well");
mergedPost.counters.likes.should.be.equal(2);
mergedPost.counters.viewer.name.should.be.equal("Hello World");
})));

it("target should be valid", () => Promise.all(connections.map(async connection => {
const postRepository = connection.getRepository(Post);
expect(postRepository.target).not.to.be.undefined;
Expand Down

0 comments on commit 1877423

Please sign in to comment.