-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
model.test.js
158 lines (155 loc) · 4.59 KB
/
model.test.js
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
import { expect } from "chai";
import { D1Orm } from "../lib/database.js";
import { DataTypes } from "../lib/datatypes.js";
import { Model } from "../lib/model.js";
const fakeD1Database = {
prepare: () => {},
dump: () => {},
batch: () => {},
exec: () => {},
};
describe("Model Validation", () => {
const orm = new D1Orm(fakeD1Database);
describe("it should throw if the model has invalid options", () => {
it("should throw if an invalid D1Orm is provided", () => {
expect(() => new Model({ D1Orm: {} })).to.throw(
Error,
"Options.D1Orm is not an instance of D1Orm"
);
});
it("should throw if an invalid tableName is provided", () => {
expect(() => new Model({ D1Orm: orm, tableName: 1 })).to.throw(
Error,
"Options.tableName must be a string"
);
});
it("should throw an error if no columns are provided", () => {
expect(() => new Model({ D1Orm: orm, tableName: "test" })).to.throw(
Error,
"Model columns must be defined"
);
});
it("should throw an error if columns are an empty object", () => {
expect(() => new Model({ D1Orm: orm, tableName: "test" }, {})).to.throw(
Error,
"Model columns cannot be empty"
);
});
it("should throw an error if an autoincrement column is not an integer type", () => {
expect(
() =>
new Model(
{ D1Orm: orm, tableName: "test" },
{
id: { type: DataTypes.STRING, autoIncrement: true },
}
)
).to.throw(Error, `Column "id" is autoincrement but is not an integer`);
});
it("should throw an error if there is no primary key", () => {
expect(
() =>
new Model(
{ D1Orm: orm, tableName: "test" },
{
id: { type: DataTypes.INTEGER },
}
)
).to.throw(Error, "Model must have a primary key");
});
it("should not throw an error if there is more than 1 primary key", () => {
expect(
() =>
new Model(
{ D1Orm: orm, tableName: "test" },
{
id: { type: DataTypes.INTEGER, primaryKey: true },
id2: { type: DataTypes.INTEGER, primaryKey: true },
}
)
).to.not.throw();
});
it("should throw an error if 2 primary keys and autoincrement is true", () => {
expect(
() =>
new Model(
{ D1Orm: orm, tableName: "test" },
{
id: { type: DataTypes.INTEGER, primaryKey: true },
id2: {
type: DataTypes.INTEGER,
primaryKey: true,
autoIncrement: true,
},
}
)
).to.throw(
Error,
"Model cannot have more than 1 primary key if autoIncrement is true"
);
});
});
});
describe("Model > Create Tables", () => {
const orm = new D1Orm(fakeD1Database);
it("should return a create table statement", () => {
const model = new Model(
{ D1Orm: orm, tableName: "test" },
{
id: { type: DataTypes.INTEGER, primaryKey: true, autoIncrement: true },
name: { type: DataTypes.STRING },
}
);
expect(model.createTableDefinition).to.equal(
"CREATE TABLE `test` (id integer AUTOINCREMENT, name text, PRIMARY KEY (id));"
);
});
it("should return a create table statement with multiple primary keys", () => {
const model = new Model(
{ D1Orm: orm, tableName: "test" },
{
id: { type: DataTypes.INTEGER, primaryKey: true },
name: { type: DataTypes.STRING, primaryKey: true },
}
);
expect(model.createTableDefinition).to.equal(
"CREATE TABLE `test` (id integer, name text, PRIMARY KEY (id, name));"
);
});
it("should support a not null constraint", () => {
const model = new Model(
{ D1Orm: orm, tableName: "test" },
{
id: { type: DataTypes.INTEGER, primaryKey: true, autoIncrement: true },
name: { type: DataTypes.STRING, notNull: true },
}
);
expect(model.createTableDefinition).to.equal(
"CREATE TABLE `test` (id integer AUTOINCREMENT, name text NOT NULL, PRIMARY KEY (id));"
);
});
it("should support a unique constraint", () => {
const model = new Model(
{ D1Orm: orm, tableName: "test" },
{
id: { type: DataTypes.INTEGER, primaryKey: true, autoIncrement: true },
name: { type: DataTypes.STRING, unique: true },
}
);
expect(model.createTableDefinition).to.equal(
"CREATE TABLE `test` (id integer AUTOINCREMENT, name text UNIQUE, PRIMARY KEY (id));"
);
});
it("should support a default value", () => {
const model = new Model(
{ D1Orm: orm, tableName: "test" },
{
id: { type: DataTypes.INTEGER, primaryKey: true, autoIncrement: true },
name: { type: DataTypes.STRING, defaultValue: "test" },
}
);
expect(model.createTableDefinition).to.equal(
'CREATE TABLE `test` (id integer AUTOINCREMENT, name text DEFAULT "test", PRIMARY KEY (id));'
);
});
});