forked from typeorm/typeorm
/
view-entity-postgres.ts
89 lines (68 loc) · 3.46 KB
/
view-entity-postgres.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import {expect} from "chai";
import "reflect-metadata";
import {CockroachDriver} from "../../../../src/driver/cockroachdb/CockroachDriver";
import {Category} from "./entity/Category";
import {Connection} from "../../../../src";
import {closeTestingConnections, createTestingConnections, reloadTestingDatabases} from "../../../utils/test-utils";
import {Post} from "./entity/Post";
import {PostCategory} from "./entity/PostCategory";
import {PostByCategory} from "./entity/PostByCategory";
describe("view entity > postgres", () => {
let connections: Connection[];
before(async () => connections = await createTestingConnections({
entities: [__dirname + "/entity/*{.js,.ts}"],
enabledDrivers: ["postgres"]
}));
beforeEach(() => reloadTestingDatabases(connections));
after(() => closeTestingConnections(connections));
it("should create entity view from string definition", () => Promise.all(connections.map(async connection => {
const queryRunner = connection.createQueryRunner();
const postCategory = await queryRunner.getView("post_category");
expect(postCategory).to.be.exist;
await queryRunner.release();
})));
it("should not return data without refreshing the materialized view", () => Promise.all(connections.map(async connection => {
const queryRunner = connection.createQueryRunner();
const category1 = new Category();
category1.name = "Cars";
await queryRunner.manager.save(category1);
const post1 = new Post();
post1.name = "About BMW";
post1.categoryId = category1.id;
await queryRunner.manager.save(post1);
const emptyResult = await queryRunner.manager.find(PostByCategory);
emptyResult.length.should.be.equal(0);
await queryRunner.query("REFRESH MATERIALIZED VIEW post_by_category");
const resultWithData = await queryRunner.manager.find(PostByCategory);
resultWithData.length.should.be.equal(1);
expect(resultWithData[0].categoryName).to.eq(category1.name);
expect(Number(resultWithData[0].postCount)).to.eq(1);
await queryRunner.release();
})));
it("should correctly return data from View", () => Promise.all(connections.map(async connection => {
const category1 = new Category();
category1.name = "Cars";
await connection.manager.save(category1);
const category2 = new Category();
category2.name = "Airplanes";
await connection.manager.save(category2);
const post1 = new Post();
post1.name = "About BMW";
post1.categoryId = category1.id;
await connection.manager.save(post1);
const post2 = new Post();
post2.name = "About Boeing";
post2.categoryId = category2.id;
await connection.manager.save(post2);
const postCategories = await connection.manager.find(PostCategory);
postCategories.length.should.be.equal(2);
const postId1 = connection.driver instanceof CockroachDriver ? "1" : 1;
postCategories[0].id.should.be.equal(postId1);
postCategories[0].postName.should.be.equal("About BMW");
postCategories[0].categoryName.should.be.equal("Cars");
const postId2 = connection.driver instanceof CockroachDriver ? "2" : 2;
postCategories[1].id.should.be.equal(postId2);
postCategories[1].postName.should.be.equal("About Boeing");
postCategories[1].categoryName.should.be.equal("Airplanes");
})));
});