forked from popcorn-official/popcorn-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dredd.hooks.js
90 lines (83 loc) · 1.97 KB
/
dredd.hooks.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
// Import the neccesary modules.
// @flow
import 'dotenv/config'
// @flow-ignore
import hooks from 'hooks'
import pMap from 'p-map'
import { createWriteStream } from 'fs'
import {
Database,
PopApi
} from 'pop-api'
import { join } from 'path'
import testAnimeMovie from './test/data/animemovie'
import testAnimeShow from './test/data/animeshow'
import testMovie from './test/data/movie'
import testShow from './test/data/show'
import {
AnimeMovie,
AnimeShow,
Movie,
Show
} from './src/models'
import { name } from './package'
process.env.TEMP_DIR = process.env.TEMP_DIR || join(...['tmp'])
const tempDir = process.env.TEMP_DIR
const models = [{
c: AnimeMovie,
m: new AnimeMovie(testAnimeMovie)
}, {
c: AnimeShow,
m: new AnimeShow(testAnimeShow)
}, {
c: Movie,
m: new Movie(testMovie)
}, {
c: Show,
m: new Show(testShow)
}]
/**
* The database middleware to connect to MongoDb.
* @type {Database}
*/
let database: Database
hooks.beforeAll((t, done) => {
database = new Database(PopApi, {
database: name
})
createWriteStream(join(...[
tempDir,
`${name}.log`
])).end()
return database.connect().then(() => {
return pMap(models, model => {
return model.c.findOneAndUpdate({
_id: model.m.id
}, model.m, {
upsert: true,
new: true
}).then(res => hooks.log(`Inserted content: '${res.id}'`))
})
}).then(() => database.disconnect())
.then(() => hooks.log('beforeAll: ok'))
.then(() => done())
.catch(err => {
hooks.error(`beforeAll: '${err}'`)
done()
})
})
hooks.afterAll((t, done) => {
return database.connect().then(() => {
return pMap(models, model => {
return model.c.findOneAndRemove({
_id: model.m.id
}).then(res => hooks.log(`Removed content: '${res.id}'`))
})
}).then(() => database.disconnect())
.then(() => hooks.log('afterAll: ok'))
.then(() => done())
.catch(err => {
hooks.error(`afterAll: '${err}'`)
done()
})
})