-
-
Notifications
You must be signed in to change notification settings - Fork 41
Expand file tree
/
Copy pathAreaHistoryDataSource.ts
More file actions
169 lines (127 loc) · 6.64 KB
/
AreaHistoryDataSource.ts
File metadata and controls
169 lines (127 loc) · 6.64 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
import muuid from 'uuid-mongodb'
import MutableAreaDataSource from '../MutableAreaDataSource.js'
import ChangeLogDataSource from '../ChangeLogDataSource.js'
import { OperationType } from '../../db/AreaTypes.js'
import inMemoryDB from '../../utils/inMemoryDB.js'
import waitForExpect from 'wait-for-expect'
import jest from 'jest-mock'
describe('Area history', () => {
let areas: MutableAreaDataSource
let onChange: jest.Mock
const testUser = muuid.v4()
beforeAll(async () => {
onChange = jest.fn()
await inMemoryDB.connect(onChange)
await ChangeLogDataSource.getInstance()._testRemoveAll()
areas = MutableAreaDataSource.getInstance()
})
afterAll(async () => {
try {
await inMemoryDB.close()
} catch (e) {
console.log('closing mongoose', e)
}
})
beforeEach(async () => {
await ChangeLogDataSource.getInstance()._testRemoveAll()
onChange.mockClear()
})
it('should create history records for new subareas', async () => {
const usa = await areas.addCountry('usa')
const newArea = await areas.findOneAreaByUUID(usa.metadata.area_id)
expect(newArea.area_name).toEqual(usa.area_name)
const or = await areas.addArea(testUser, { areaName: 'oregon', parentUuid: usa.metadata.area_id })
const nv = await areas.addArea(testUser, { areaName: 'nevada', parentUuid: usa.metadata.area_id })
expect(nv?._id).toBeTruthy()
expect(or?._id).toBeTruthy()
await waitForExpect(() => expect(onChange).toHaveBeenCalledTimes(5))
const areaHistory = await ChangeLogDataSource.getInstance().getAreaChangeSets()
expect(areaHistory).toHaveLength(2)
// verify changes in most recent order
expect(areaHistory[0].operation).toEqual(OperationType.addArea)
expect(areaHistory[1].operation).toEqual(OperationType.addArea)
// Verify NV history
const nvAreaHistory = areaHistory[0].changes
expect(nvAreaHistory).toHaveLength(2)
// history is shown most recent first
expect(nvAreaHistory[0].dbOp).toEqual('insert') // insert new area
expect(nvAreaHistory[0].fullDocument.area_name).toEqual(nv?.area_name) // area added to the right parent?
// verify change history linking
expect(nvAreaHistory[0].fullDocument._change?.historyId).toEqual(areaHistory[0]._id) // should point to current change
expect(nvAreaHistory[0].fullDocument._change?.prevHistoryId).not.toBeDefined() // new document -> no previous history
expect(nvAreaHistory[1].dbOp).toEqual('update') // add area to country.children[]
expect(nvAreaHistory[1].fullDocument.area_name).toEqual(usa?.area_name)
expect(nvAreaHistory[1].fullDocument.children).toHaveLength(2)
expect(nvAreaHistory[1].fullDocument.children[1]).toEqual(nv?._id) // area added to parent.children[]?
// verify change history linking
// 2nd change record: parent (country)
expect(nvAreaHistory[1].fullDocument._change?.historyId).toEqual(areaHistory[0]._id) // should point to current change
expect(nvAreaHistory[1].fullDocument._change?.prevHistoryId).toEqual(areaHistory[1]._id) // should point to previous Add new area
// Verify OR history
const orAreaHistory = areaHistory[1].changes
expect(orAreaHistory).toHaveLength(2)
const randomHistory = await ChangeLogDataSource.getInstance().getAreaChangeSets(muuid.v4())
expect(randomHistory).toHaveLength(0)
// Verify USA history
const usaHistory = await ChangeLogDataSource.getInstance().getAreaChangeSets(usa.metadata.area_id)
expect(usaHistory).toHaveLength(2)
expect(usaHistory[0].operation).toEqual('addArea')
expect(usaHistory[1].operation).toEqual('addArea')
// Verify USA history links
expect(usaHistory[0].changes[0])
})
it('should record multiple Areas.setDestination() calls ', async () => {
const canada = await areas.addCountry('can')
const squamish = await areas.addArea(testUser, { areaName: 'squamish', parentUuid: canada.metadata.area_id })
expect(squamish?._id).toBeTruthy()
if (squamish != null) {
const areaUuid = squamish.metadata.area_id
await expect(areas.setDestinationFlag(testUser, muuid.v4(), true)).rejects.toThrow() // non-existent area id. Trx won't be recorded
await areas.setDestinationFlag(testUser, areaUuid, true)
await areas.setDestinationFlag(testUser, areaUuid, false)
await waitForExpect(() => expect(onChange).toHaveBeenCalledTimes(5))
const changset = await ChangeLogDataSource.getInstance().getAreaChangeSets(areaUuid)
expect(changset).toHaveLength(3)
expect(changset[0].operation).toEqual('updateDestination')
expect(changset[1].operation).toEqual('updateDestination')
expect(changset[2].operation).toEqual('addArea')
expect(changset[0].changes[0].fullDocument.metadata.isDestination).toStrictEqual(false)
expect(changset[1].changes[0].fullDocument.metadata.isDestination).toStrictEqual(true)
expect(changset[2].changes[0].fullDocument.metadata.isDestination).toStrictEqual(false) // default
}
})
it('should record an Areas.deleteArea() call', async () => {
const greece = await areas.addCountry('grc')
const leonidio = await areas.addArea(testUser, { areaName: 'Leonidio', parentUuid: greece.metadata.area_id })
if (leonidio == null) fail()
await areas.deleteArea(testUser, leonidio.metadata.area_id)
await waitForExpect(() => expect(onChange).toHaveBeenCalledTimes(5))
const history = await ChangeLogDataSource.getInstance().getAreaChangeSets(leonidio.metadata.area_id)
expect(history).toHaveLength(2)
expect(history[0].operation).toEqual('deleteArea')
expect(history[1].operation).toEqual('addArea')
expect(history[0].changes[0].fullDocument._id).toEqual(leonidio._id)
})
it('should not record a failed Areas.deleteArea() call', async () => {
const spain = await areas.addCountry('esp')
const margalef = await areas.addArea(testUser, { areaName: 'margalef', parentUuid: spain.metadata.area_id })
if (margalef == null) fail()
const newChild = await areas.addArea(testUser, { areaName: 'One', parentUuid: margalef.metadata.area_id })
if (newChild == null) fail()
let deleted = false
try {
await areas.deleteArea(testUser, margalef.metadata.area_id)
fail('Shouldn\'t allow deletion when the area still has subareas')
} catch (e) {
deleted = true
}
expect(deleted).toBeTruthy()
await waitForExpect(() => expect(onChange).toHaveBeenCalledTimes(5))
const history = await ChangeLogDataSource.getInstance().getAreaChangeSets(spain.metadata.area_id)
// should only have 2 entries:
// 1. Add country
// 2. Add child to country
expect(history).toHaveLength(1)
expect(history[0].operation).toEqual('addArea')
})
})