-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathramekin.test.js
130 lines (104 loc) · 3.45 KB
/
ramekin.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
const moment = require('moment')
const Ramekin = require('./ramekin')
const util = require('./lib/util')
const testDoc = { id: '123', body: 'Random text string', date: new Date() }
const testDoc2 = { id: '456', body: 'Antoher random string', date: new Date() }
const noIdTestDoc = { body: 'Another random string', date: new Date() }
const noDateTestDoc = { id: '123', body: 'Another random string' }
const usedPhrases = [
'antoh',
'antoh,random',
'antoh,random,string',
'random',
'random,string',
'random,text',
'random,text,string',
'string',
'text',
'text,string']
const findDocOptions = {
start: moment().subtract(1, 'months'),
end: new Date()
}
const pastHistoryOptions = {
start: moment().subtract(2, 'year'),
end: moment().subtract(1, 'year')
}
test('normalise: normalise a string', () => {
const r = new Ramekin()
expect(r.normalise('My name is Ian')).toEqual('name ian')
})
test('Set intersection code snippet test - to be refactored into a function test', () => {
const a = [1, 2, 3]
const b = [4, 3, 2]
expect(util.intersection(a, b)).toEqual([2, 3])
})
test('constructor: test default options are set', () => {
const r = new Ramekin()
expect(r.options.minTrendFreq).toEqual(3)
expect(r.options.historyDays).toEqual(90)
})
// test combined defaults an standards
test('constructor: test default and specified options are set', () => {
const r = new Ramekin({ minTrendFreq: 5 })
expect(r.options.minTrendFreq).toEqual(5)
expect(r.options.historyDays).toEqual(90)
})
// test date as a string
// test combined defaults an standards
test('ingest: test a document gets ingested', () => {
const r = new Ramekin()
expect(r.docs).toEqual({})
r.ingest(testDoc)
expect(r.docs).toEqual({ [testDoc.id]: testDoc })
})
test('ingest: ingest the same doc twice', () => {
const r = new Ramekin()
expect(r.docs).toEqual({})
r.ingest(testDoc)
expect(r.docs).toEqual({ [testDoc.id]: testDoc })
expect(() => r.ingest(testDoc)).toThrow()
})
test('ingest: no id specified', () => {
const r = new Ramekin()
expect(r.docs).toEqual({})
expect(() => r.ingest(noIdTestDoc)).toThrow()
})
test('ingest: no date specified', () => {
const r = new Ramekin()
expect(r.docs).toEqual({})
expect(() => r.ingest(noDateTestDoc)).toThrow()
})
test('ingestAll: test a document gets ingested', () => {
const r = new Ramekin()
expect(r.docs).toEqual({})
r.ingestAll([testDoc, testDoc2])
expect(r.docs).toEqual({ [testDoc.id]: testDoc, [testDoc2.id]: testDoc2 })
})
test('count: test the count returns the correct number', () => {
const r = new Ramekin()
r.ingestAll([testDoc, testDoc2])
expect(r.count('random', findDocOptions)).toEqual(2)
expect(r.count('random,string', findDocOptions)).toEqual(1)
expect(r.count('not,random,string', findDocOptions)).toEqual(0)
})
test('count: not in date range', () => {
const r = new Ramekin()
r.ingestAll([testDoc, testDoc2])
expect(r.count('random', pastHistoryOptions)).toEqual(0)
})
test('usedPhrases: returns the correct phrases', () => {
const r = new Ramekin()
expect(r.usedPhrases(findDocOptions)).toEqual([])
r.ingestAll([testDoc, testDoc2])
const computedUsedPhrases = r.usedPhrases(findDocOptions)
computedUsedPhrases.sort()
usedPhrases.sort()
expect(computedUsedPhrases).toEqual(usedPhrases)
expect(r.usedPhrases(pastHistoryOptions)).toEqual([])
})
test('trending: no data', () => {
const r = new Ramekin()
const trends = r.trending()
expect(trends).toEqual([])
})