Repository navigation
Expand file tree
/
Copy path02-insert-flat-data.test.ts
More file actions
190 lines (152 loc) · 4.46 KB
/
Copy path02-insert-flat-data.test.ts
File metadata and controls
190 lines (152 loc) · 4.46 KB
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
import _ from "lodash";
import { Database } from "../src/database";
import { CsvLoader } from "../src/data/csv-loader";
import {
selectActorByName,
selectCount,
selectKeyword,
selectDirector,
selectGenre,
selectProductionCompany,
selectMovie
} from "../src/queries/select";
import {
ACTORS,
KEYWORDS,
DIRECTORS,
GENRES,
PRODUCTION_COMPANIES,
MOVIES
} from "../src/table-names";
import { Movie } from "../src/data/types";
import { escape } from "../src/utils";
import { minutes } from "./utils";
const insertActors = (actors: string[]) => {
return (
`insert into actors (full_name) values` +
actors.map(actor => `('${escape(actor)}')`).join(",")
);
};
const insertKeywords = (keywords: string[]) => {
throw new Error(`todo`);
};
const insertDirectors = (directors: string[]) => {
throw new Error(`todo`);
};
const insertGenres = (genres: string[]) => {
throw new Error(`todo`);
};
const insertProductionCompanies = (companies: string[]) => {
throw new Error(`todo`);
};
const insertMovies = (movies: Movie[]) => {
throw new Error(`todo`);
};
describe("Insert Flat Data", () => {
let db: Database;
beforeAll(async () => {
db = await Database.fromExisting("01", "02");
await CsvLoader.load();
}, minutes(1));
it(
"should insert actors",
async done => {
const actors = await CsvLoader.actors();
const chunks = _.chunk(actors, 500);
for (const ch of chunks) {
await db.insert(insertActors(ch));
}
const count = await db.selectSingleRow(selectCount(ACTORS));
expect(count.c).toBe(7617);
const actor = await db.selectSingleRow(selectActorByName("Tom Hardy"));
expect(actor.id).not.toBeNaN();
expect(actor.full_name).toEqual("Tom Hardy");
done();
},
minutes(1)
);
it(
"should insert keywords",
async done => {
const keywords = await CsvLoader.keywords();
const chunks = _.chunk(keywords, 500);
for (const ch of chunks) {
await db.insert(insertKeywords(ch));
}
const count = await db.selectSingleRow(selectCount(KEYWORDS));
expect(count.c).toBe(3700);
const row = await db.selectSingleRow(selectKeyword("teddy bear"));
expect(row.id).not.toBeNaN();
expect(row.keyword).toEqual("teddy bear");
done();
},
minutes(1)
);
it(
"should insert directors",
async done => {
const directors = await CsvLoader.directors();
const chunks = _.chunk(directors, 500);
for (const ch of chunks) {
await db.insert(insertDirectors(ch));
}
const count = await db.selectSingleRow(selectCount(DIRECTORS));
expect(count.c).toBe(2499);
const row = await db.selectSingleRow(selectDirector("Alan Taylor"));
expect(row.id).not.toBeNaN();
expect(row.full_name).toEqual("Alan Taylor");
done();
},
minutes(1)
);
it(
"should insert genres",
async done => {
const genres = await CsvLoader.genres();
await db.insert(insertGenres(genres));
const count = await db.selectSingleRow(selectCount(GENRES));
expect(count.c).toBe(20);
const row = await db.selectSingleRow(selectGenre("Fantasy"));
expect(row.id).not.toBeNaN();
expect(row.genre).toEqual("Fantasy");
done();
},
minutes(1)
);
it(
"should insert production companies",
async done => {
const productionCompanies = await CsvLoader.productionCompanies();
const chunks = _.chunk(productionCompanies, 500);
for (const ch of chunks) {
await db.insert(insertProductionCompanies(ch));
}
const count = await db.selectSingleRow(selectCount(PRODUCTION_COMPANIES));
expect(count.c).toBe(3429);
const row = await db.selectSingleRow(
selectProductionCompany("Universal Pictures")
);
expect(row.id).not.toBeNaN();
expect(row.company_name).toEqual("Universal Pictures");
done();
},
minutes(1)
);
it(
"should insert movies",
async done => {
const movies = await CsvLoader.movies();
const chunks = _.chunk(movies, 500);
for (const ch of chunks) {
await db.insert(insertMovies(ch));
}
const count = await db.selectSingleRow(selectCount(MOVIES));
expect(count.c).toBe(2998);
const row = await db.selectSingleRow(selectMovie("tt0369610"));
expect(row.id).not.toBeNaN();
expect(row.original_title).toEqual("Jurassic World");
done();
},
minutes(1)
);
});