-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjsonld-serializers.test.js
More file actions
176 lines (160 loc) · 5.53 KB
/
jsonld-serializers.test.js
File metadata and controls
176 lines (160 loc) · 5.53 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
const { expect } = require('chai')
const {
AggregationsSerializer,
ItemResultsSerializer,
ResourceSerializer
} = require('../lib/jsonld_serializers')
describe('JSONLD Serializers', () => {
describe('ResourceSerializer', () => {
it('enables hasItemDates when numItemsTotal is above thresshold', async () => {
const esDoc = {
uri: 'b123',
numItemDatesParsed: [8],
numItemsTotal: [10]
}
const serialized = await ResourceSerializer.serialize(esDoc)
// When 8 of 10 items have parsed dates, we meet the min 80% thresshold:
expect(serialized.hasItemDates).to.equal(true)
})
it('disables hasItemDates when numItemsTotal is below thresshold', async () => {
const esDoc = {
uri: 'b123',
numItemDatesParsed: [7],
numItemsTotal: [10]
}
const serialized = await ResourceSerializer.serialize(esDoc)
// When 7 of 10 items have parsed dates, we fail the min 80% thresshold:
expect(serialized.hasItemDates).to.equal(false)
})
it('enables hasItemDates when numItemsTotal is missing but numItems is above thresshold', async () => {
const esDoc = {
uri: 'b123',
numItemDatesParsed: [8],
numItems: [10]
}
const serialized = await ResourceSerializer.serialize(esDoc)
// When 8 of 10 items have parsed dates, we meet the min 80% thresshold:
expect(serialized.hasItemDates).to.equal(true)
})
it('disables hasItemDates when numItemsTotal is missing and numItems is below thresshold', async () => {
const esDoc = {
uri: 'b123',
numItemDatesParsed: [7],
numItems: [10]
}
const serialized = await ResourceSerializer.serialize(esDoc)
// When 7 of 10 items have parsed dates, we fail the min 80% thresshold:
expect(serialized.hasItemDates).to.equal(false)
})
it('disables hasItemDates when any counts are missing', async () => {
await Promise.all([
// No numItems or numItemsTotal set (unlikely):
{
uri: 'b123',
numItemDatesParsed: [7]
},
// No numItemDatesParsed set:
{
uri: 'b123',
numItems: [10]
}
]
.map(async (esDoc) => {
const serialized = await ResourceSerializer.serialize(esDoc)
expect(serialized.hasItemDates).to.equal(false)
})
)
})
describe('format', () => {
it('adds label to format', async () => {
const serialized = await ResourceSerializer.serialize({
formatId: 'a'
})
expect(serialized.format).to.deep.equal([{
'@id': 'a',
prefLabel: 'Book/Text'
}])
})
it('removes invalid format', async () => {
const serialized = await ResourceSerializer.serialize({
formatId: '!'
})
expect(serialized.format).to.be.a('null')
})
})
})
describe('ItemResultsSerializer', () => {
const esItems = [
{
accessMessage: [{ id: 'accessMessage:2', label: 'Request in advance' }],
catalogItemType: [{ id: 'catalogItemType:55', label: 'book, limited circ, MaRLI' }],
holdingLocation: [{ id: 'loc:rc2ma', label: 'Offsite' }],
identifier: [
'urn:bnum:b10807029',
'urn:shelfmark:JLE 83-2799',
'urn:barcode:33433056867710'
],
status: [{ id: 'status:a', label: 'Available' }],
type: ['bf:Item'],
uri: 'i12606717',
recapCustomerCode: ['NA'],
eddRequestable: true,
deliveryLocation: [
{
id: 'loc:mal23',
label: 'Schwarzman Building - Scholar Room 223',
sortPosition: 0
},
{
id: 'loc:mal',
label: 'Schwarzman Building - Main Reading Room 315',
sortPosition: 1
},
{
id: 'loc:mab',
label: 'Schwarzman Building - Art & Architecture Room 300',
sortPosition: 2
}
]
}
]
it('serializes delivery-lcoations-by-barcode response', async () => {
const serialized = await ItemResultsSerializer.serialize(esItems)
expect(serialized).to.nested.include({
'itemListElement[0].@id': 'res:i12606717',
'itemListElement[0].deliveryLocation[0].@id': 'loc:mal23',
'itemListElement[0].deliveryLocation[0].prefLabel': 'Schwarzman Building - Scholar Room 223'
})
})
})
describe('AggregationsSerializer', () => {
const esResponse = require('./fixtures/es-aggregations-response.json')
// Perform flattening of nested aggregations, same as lib/resources.js
// does (inline, unfortunately):
const transformedEsResponse = Object.assign(
{},
esResponse,
{
aggregations: Object.entries(esResponse.aggregations)
.reduce((transformed, [field, obj]) => {
transformed[field] = obj._nested ? obj._nested : obj
return transformed
}, {})
}
)
it('formats format agg', async () => {
const serialized = await AggregationsSerializer.serialize(transformedEsResponse)
const formatAgg = serialized.itemListElement
.find((agg) => agg.id === 'format')
expect(formatAgg).to.be.a('object')
expect(formatAgg).to.nested.include({
'values[0].value': 'a',
'values[0].count': 2324674,
'values[0].label': 'Book/Text'
})
const bucketsWithoutLabels = formatAgg.values
.filter((val) => !val.label)
expect(bucketsWithoutLabels).to.have.lengthOf(0)
})
})
})