-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathsearch.test.js
More file actions
146 lines (127 loc) · 5.38 KB
/
Copy pathsearch.test.js
File metadata and controls
146 lines (127 loc) · 5.38 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
const cds = require('../../cds.js')
const bookshop = require('path').resolve(__dirname, '../../bookshop')
const admin = {
auth: {
username: 'alice',
},
}
describe.skip('Bookshop - Search', () => {
const { expect, GET } = cds.test(bookshop)
// Skipping $search tests as the github action HANA version does not support SCORE
test('Search book', async () => {
const res = await GET('/admin/Books?$search=cat', admin)
expect(res.status).to.be.eq(200)
expect(res.data.value.length).to.be.eq(1)
expect(res.data.value[0].title).to.be.eq('Catweazle')
})
test('Search book with space and quotes', async () => {
const res = await GET('/admin/Books?$search="e R"', admin)
expect(res.status).to.be.eq(200)
expect(res.data.value.length).to.be.eq(2)
expect(res.data.value[0].title).to.be.eq('The Raven')
expect(res.data.value[1].descr).to.include('e r')
})
test('Search book with filter', async () => {
const res = await GET('/admin/Books?$search="e R"&$filter=ID eq 251 or ID eq 271', admin)
expect(res.status).to.be.eq(200)
expect(res.data.value.length).to.be.eq(2)
expect(res.data.value[0].title).to.be.eq('The Raven')
expect(res.data.value[1].descr).to.include('e r')
expect(res.data.value[0].ID).to.be.eq(251)
expect(res.data.value[1].ID).to.be.eq(271)
})
// search expression operating on aggregated results, must be put into the having clause
describe('with aggregate function', () => {
test('min', async () => {
const { Books } = cds.entities
let res = await SELECT.from(Books)
.columns({ args: [{ ref: ['title'] }], as: 'firstInAlphabet', func: 'MIN' })
.groupBy('title')
.search('Cat')
expect(res.length).to.be.eq(1)
})
})
describe('with path expressions', () => {
// reset search terms and cache before each test
beforeEach(async () => {
const { Books, Authors } = cds.entities
resetSearchTerms(Books)
resetSearchTerms(Authors)
function resetSearchTerms(entity) {
delete entity.__searchableColumns
Object.keys(entity).forEach(key => {
if (key.startsWith('@cds.search')) {
delete entity[key]
}
})
}
})
test('Search books and author.name', async () => {
const res = await GET('/admin/Books?$search="Emily"', admin)
expect(res.status).to.be.eq(200)
expect(res.data.value.length).to.be.eq(2)
expect(res.data.value[0].title).to.be.eq('Wuthering Heights')
})
test('Search authors via books', async () => {
const { Books } = cds.entities
// ad-hoc search expression
Books['@cds.search.author'] = true
let res = await SELECT.from(Books).columns('author.name', 'title').search('Brontë')
expect(res.length).to.be.eq(2) // Emily and Charlotte
})
test('Search authors address through calculated element in books', async () => {
const { Books } = cds.entities
// ad-hoc search expression
Books['@cds.search.authorsAddress'] = true
let res = await SELECT.from(Books).columns('author.name as author', 'title').search('"1 Main Street, Bradford"')
// author name in res[0] must match "Emily Brontë"
expect(res.length).to.be.eq(1)
expect(res[0].author).to.be.eq('Emily Brontë')
})
test('Search authors calculated element via books', async () => {
const { Books } = cds.entities
const { Authors } = cds.entities
// ad-hoc search expression
Books['@cds.search.author'] = true
Authors['@cds.search.address'] = true // address is a calculated element
let res = await SELECT.from(Books).columns('author.name as author', 'title').search('"1 Main Street, Bradford"')
// author name in res[0] must match "Emily Brontë"
expect(res.length).to.be.eq(1)
expect(res[0].author).to.be.eq('Emily Brontë')
})
test('Search escaped character in search literal', async () => {
const { Books } = cds.entities
const { Authors } = cds.entities
// ad-hoc search expression
Books['@cds.search.author'] = true
Authors['@cds.search.address'] = true // address is a calculated element
let res = await SELECT.from(Books).columns('author.name as author', 'title').search('"\\"\\\\"')
expect(res.length).to.be.eq(0)
})
test('Search improperly escaped character in search literal', async () => {
const { Books } = cds.entities
const { Authors } = cds.entities
// ad-hoc search expression
Books['@cds.search.author'] = true
Authors['@cds.search.address'] = true // address is a calculated element
let res = await SELECT.from(Books).columns('author.name as author', 'title').search('"\\q"')
expect(res.length).to.be.eq(0)
})
test('search on result of subselect', async () => {
const res = await cds.run(
SELECT.from(SELECT.from({ ref: ['sap.capire.bookshop.Books'] }).columns('title'))
.columns('title')
.search('Wuthering'),
)
expect(res.length).to.be.eq(1)
})
test('search on result of subselect via path expression', async () => {
const query = SELECT.from(SELECT.from({ ref: ['sap.capire.bookshop.Books'] }).columns('title', 'author'))
.columns('title', 'author')
.search('Brontë')
query.SELECT.from['@cds.search.author'] = true
const res = await cds.run(query)
expect(res.length).to.be.eq(2)
})
})
})