-
-
Notifications
You must be signed in to change notification settings - Fork 6.2k
/
issue-5501.ts
112 lines (96 loc) · 4.95 KB
/
issue-5501.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
import "reflect-metadata";
import { Connection } from "../../../src/connection/Connection";
import { closeTestingConnections, createTestingConnections, reloadTestingDatabases } from "../../utils/test-utils";
import { Post } from "./entity/Post";
import { expect } from "chai";
describe("github issues > #5501 Incorrect data loading from JSON string for column type 'simple-json'", () => {
let connections: Connection[];
before(async () => {
connections = await createTestingConnections({
entities: [Post],
schemaCreate: true,
dropSchema: true
});
});
beforeEach(() => reloadTestingDatabases(connections));
after(() => closeTestingConnections(connections));
it("should correctly store simple-json field", () => Promise.all(connections.map(async (connection) => {
let id = 0;
const runTestCase = async (input: any, expected: any, message: string) => {
id++;
await connection.getRepository(Post).save({ id , jsonField: input });
const actual = (
await connection.createQueryBuilder()
.from("Post", "post")
.select("post.jsonField", "json")
.where("post.id = :id", {id})
.getRawOne()
)!.json;
expect(actual).to.be.equal(expected, message);
}
await runTestCase("hello world", "\"hello world\"", "normal string");
await runTestCase("", "\"\"", "empty string");
await runTestCase("null", "\"null\"", "string containing the word null");
await runTestCase( { "key": "value" }, "{\"key\":\"value\"}", "object containing a key and string value");
await runTestCase([ "hello" ], "[\"hello\"]", "array containing a string");
await runTestCase(null, null, "a null object value");
await runTestCase(1, "1", "the real number 1");
await runTestCase(0.3, "0.3", "the number 0.3");
await runTestCase(true, "true", "the boolean value true");
await runTestCase(
[ { hello: "earth", planet: true }, { hello: "moon", planet: false } ],
"[{\"hello\":\"earth\",\"planet\":true},{\"hello\":\"moon\",\"planet\":false}]",
"a complex object example"
);
})));
it("should correctly retrieve simple-json field", () => Promise.all(connections.map(async (connection) => {
let id = 0;
const runTestCase = async (input: string | null, expected: any, message: string) => {
id++;
await connection.createQueryBuilder()
.insert()
.into(Post)
.values({id, jsonField: () => ':field'} as any) // A bit of a hack to get the raw value inserting
.setParameter('field', input)
.execute();
const actual = (
await connection.getRepository(Post).findOne({ where: { id } })
)!.jsonField;
expect(actual).to.be.eql(expected, message);
}
await runTestCase("\"hello world\"", "hello world", "normal string");
await runTestCase("\"\"", "", "empty string");
await runTestCase("\"null\"", "null", "string containing the word null");
await runTestCase("{\"key\":\"value\"}", { "key": "value" }, "object containing a key and string value");
await runTestCase("[\"hello\"]", [ "hello" ], "array containing a string");
await runTestCase(null, null, "a null object value");;
await runTestCase("1", 1, "the real number 1");
await runTestCase("0.3", 0.3, "the number 0.3");
await runTestCase("true", true, "the boolean value true");
await runTestCase(
"[{\"hello\":\"earth\",\"planet\":true},{\"hello\":\"moon\",\"planet\":false}]",
[{"hello":"earth","planet":true},{"hello":"moon","planet":false}],
"a complex object example"
);
})));
it("should throw an error when the data in the database is invalid", () => Promise.all(connections.map(async (connection) => {
const insert = (id: number, value: string | null) =>
connection.createQueryBuilder()
.insert()
.into(Post)
.values({ id, jsonField: () => ':field' } as any) // A bit of a hack to get the raw value inserting
.setParameter('field', value)
.execute()
// This was the likely data within the database in #4440
// This will happen if you've tried to manually insert the data in ways where
// we aren't expecting you to - like switching the column type to a text &
// trying to push a value into it that is an object.
await insert(1, "[object Object]");
const repo = connection.getRepository(Post);
const getJson = async (id: number) =>
(
await repo.findOne({ where: { id } })
)!.jsonField;
await expect(getJson(1)).to.be.rejected;
})));
});