|
| 1 | +import "reflect-metadata"; |
| 2 | +import { expect } from "chai"; |
| 3 | +import { Connection } from "../../../../src/connection/Connection"; |
| 4 | +import { |
| 5 | + closeTestingConnections, |
| 6 | + createTestingConnections, |
| 7 | + reloadTestingDatabases |
| 8 | +} from "../../../utils/test-utils"; |
| 9 | +import { Post } from "./entity/Post"; |
| 10 | + |
| 11 | +describe("cube-postgres", () => { |
| 12 | + let connections: Connection[]; |
| 13 | + before(async () => { |
| 14 | + connections = await createTestingConnections({ |
| 15 | + entities: [__dirname + "/entity/*{.js,.ts}"], |
| 16 | + enabledDrivers: ["postgres"] |
| 17 | + }); |
| 18 | + }); |
| 19 | + beforeEach(() => reloadTestingDatabases(connections)); |
| 20 | + after(() => closeTestingConnections(connections)); |
| 21 | + |
| 22 | + it("should create correct schema with Postgres' cube type", () => |
| 23 | + Promise.all( |
| 24 | + connections.map(async connection => { |
| 25 | + const queryRunner = connection.createQueryRunner(); |
| 26 | + const schema = await queryRunner.getTable("post"); |
| 27 | + await queryRunner.release(); |
| 28 | + expect(schema).not.to.be.undefined; |
| 29 | + const cubeColumn = schema!.columns.find( |
| 30 | + tableColumn => |
| 31 | + tableColumn.name === "color" && |
| 32 | + tableColumn.type === "cube" |
| 33 | + ); |
| 34 | + expect(cubeColumn).to.not.be.undefined; |
| 35 | + }) |
| 36 | + )); |
| 37 | + |
| 38 | + it("should persist cube correctly", () => |
| 39 | + Promise.all( |
| 40 | + connections.map(async connection => { |
| 41 | + const color = [255, 0, 0]; |
| 42 | + const postRepo = connection.getRepository(Post); |
| 43 | + const post = new Post(); |
| 44 | + post.color = color; |
| 45 | + const persistedPost = await postRepo.save(post); |
| 46 | + const foundPost = await postRepo.findOne(persistedPost.id); |
| 47 | + expect(foundPost).to.exist; |
| 48 | + expect(foundPost!.color).to.deep.equal(color); |
| 49 | + }) |
| 50 | + )); |
| 51 | + |
| 52 | + it("should update cube correctly", () => |
| 53 | + Promise.all( |
| 54 | + connections.map(async connection => { |
| 55 | + const color = [255, 0, 0]; |
| 56 | + const color2 = [0, 255, 0]; |
| 57 | + const postRepo = connection.getRepository(Post); |
| 58 | + const post = new Post(); |
| 59 | + post.color = color; |
| 60 | + const persistedPost = await postRepo.save(post); |
| 61 | + |
| 62 | + await postRepo.update( |
| 63 | + { id: persistedPost.id }, |
| 64 | + { color: color2 } |
| 65 | + ); |
| 66 | + |
| 67 | + const foundPost = await postRepo.findOne(persistedPost.id); |
| 68 | + expect(foundPost).to.exist; |
| 69 | + expect(foundPost!.color).to.deep.equal(color2); |
| 70 | + }) |
| 71 | + )); |
| 72 | + |
| 73 | + it("should re-save cube correctly", () => |
| 74 | + Promise.all( |
| 75 | + connections.map(async connection => { |
| 76 | + const color = [255, 0, 0]; |
| 77 | + const color2 = [0, 255, 0]; |
| 78 | + const postRepo = connection.getRepository(Post); |
| 79 | + const post = new Post(); |
| 80 | + post.color = color; |
| 81 | + const persistedPost = await postRepo.save(post); |
| 82 | + |
| 83 | + persistedPost.color = color2; |
| 84 | + await postRepo.save(persistedPost); |
| 85 | + |
| 86 | + const foundPost = await postRepo.findOne(persistedPost.id); |
| 87 | + expect(foundPost).to.exist; |
| 88 | + expect(foundPost!.color).to.deep.equal(color2); |
| 89 | + }) |
| 90 | + )); |
| 91 | + |
| 92 | + it("should be able to order cube by euclidean distance", () => |
| 93 | + Promise.all( |
| 94 | + connections.map(async connection => { |
| 95 | + const color1 = [255, 0, 0]; |
| 96 | + const color2 = [255, 255, 0]; |
| 97 | + const color3 = [255, 255, 255]; |
| 98 | + |
| 99 | + const post1 = new Post(); |
| 100 | + post1.color = color1; |
| 101 | + const post2 = new Post(); |
| 102 | + post2.color = color2; |
| 103 | + const post3 = new Post(); |
| 104 | + post3.color = color3; |
| 105 | + await connection.manager.save([post1, post2, post3]); |
| 106 | + |
| 107 | + const posts = await connection.manager |
| 108 | + .createQueryBuilder(Post, "post") |
| 109 | + .orderBy("color <-> '(0, 255, 0)'", "DESC") |
| 110 | + .getMany(); |
| 111 | + |
| 112 | + const postIds = posts.map(post => post.id); |
| 113 | + expect(postIds).to.deep.equal([post1.id, post3.id, post2.id]); |
| 114 | + }) |
| 115 | + )); |
| 116 | +}); |
0 commit comments