Skip to content

Commit

Permalink
Use search with deepBy arg instead of inferring
Browse files Browse the repository at this point in the history
  • Loading branch information
minottic committed Apr 23, 2024
1 parent 9668762 commit c58a00d
Show file tree
Hide file tree
Showing 10 changed files with 175 additions and 142 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -240,13 +240,9 @@ describe('Basesnippet', function (this: Suite) {
});

it('Search with token should return 200 and matching body.tags', async () => {
const includeTags = {include: ['subsnippets']};
const includeTags = {where: {tags: 'aSearchableTag'}};
await client
.get(
`/basesnippets/search=${encodeURIComponent(
'#aSearchableTag',
)}?filter=${JSON.stringify(includeTags)}`,
)
.get(`/basesnippets/search=%00?filter=${JSON.stringify(includeTags)}`)
.set('Authorization', 'Bearer ' + token)
.set('Content-Type', 'application/json')
.expect(200)
Expand All @@ -262,12 +258,12 @@ describe('Basesnippet', function (this: Suite) {
});

it('Search with token should return 200 and matching body.tags + body.textcontent', async () => {
const includeTags = {include: ['subsnippets']};
const includeTags = {where: {tags: 'aSearchableTag'}};
await client
.get(
`/basesnippets/search=aSearchable Tex ${encodeURIComponent(
'#aSearchableTag',
)}?filter=${JSON.stringify(includeTags)}`,
`/basesnippets/search=aSearchable Tex?filter=${JSON.stringify(
includeTags,
)}`,
)
.set('Authorization', 'Bearer ' + token)
.set('Content-Type', 'application/json')
Expand All @@ -284,8 +280,9 @@ describe('Basesnippet', function (this: Suite) {
});

it('Search with token should return 200 and matching readACL', async () => {
const filter = {where: {readACL: 'basesnippetAcceptance'}};
await client
.get(`/basesnippets/search=@basesnippetAcceptance`)
.get(`/basesnippets/search=%00?filter=${JSON.stringify(filter)}`)
.set('Authorization', 'Bearer ' + token)
.set('Content-Type', 'application/json')
.expect(200)
Expand Down Expand Up @@ -329,7 +326,10 @@ describe('Basesnippet', function (this: Suite) {
});

it('Search with token should return 200 and matching subsnippets', async () => {
const includeTags = {include: ['subsnippets']};
const includeTags = {
where: {tags: 'aSubsnippetTag'},
include: ['subsnippets'],
};
await client
.post('/basesnippets')
.set('Authorization', 'Bearer ' + token)
Expand All @@ -342,9 +342,9 @@ describe('Basesnippet', function (this: Suite) {

await client
.get(
`/basesnippets/search=${encodeURIComponent(
'#aSubsnippetTag',
)}?filter=${JSON.stringify(includeTags)}`,
`/basesnippets/search=aSearch?filter=${JSON.stringify(
includeTags,
)}&deepby=tags`,
)
.set('Authorization', 'Bearer ' + token)
.set('Content-Type', 'application/json')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -187,13 +187,9 @@ describe('File controller services', function (this: Suite) {
});

it('Search index with token should return 200 and matching body.tags', async () => {
const includeTags = {fields: {tags: true}, include: ['subsnippets']};
const includeTags = {where: {tags: 'aSearchableTag'}};
await client
.get(
`/filesnippet/search=${encodeURIComponent(
'#aSearchableTag',
)}?filter=${JSON.stringify(includeTags)}`,
)
.get(`/filesnippet/search=%00?filter=${JSON.stringify(includeTags)}`)
.set('Authorization', 'Bearer ' + token)
.set('Content-Type', 'application/json')
.expect(200)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -191,13 +191,9 @@ describe('Location', function (this: Suite) {
});

it('Search index with token should return 200 and matching body.tags', async () => {
const includeTags = {fields: {tags: true}, include: ['subsnippets']};
const includeTags = {where: {tags: 'aSearchableTag'}};
await client
.get(
`/locations/search=${encodeURIComponent(
'#aSearchableTag',
)}?filter=${JSON.stringify(includeTags)}`,
)
.get(`/locations/search=%00?filter=${JSON.stringify(includeTags)}`)
.set('Authorization', 'Bearer ' + token)
.set('Content-Type', 'application/json')
.expect(200)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -184,13 +184,9 @@ describe('Logbook', function (this: Suite) {
});

it('Search index with token should return 200 and matching body.tags', async () => {
const includeTags = {fields: {tags: true}, include: ['subsnippets']};
const includeTags = {where: {tags: 'aSearchableTag'}};
await client
.get(
`/logbooks/search=${encodeURIComponent(
'#aSearchableTag',
)}?filter=${JSON.stringify(includeTags)}`,
)
.get(`/logbooks/search=%00?filter=${JSON.stringify(includeTags)}`)
.set('Authorization', 'Bearer ' + token)
.set('Content-Type', 'application/json')
.expect(200)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,13 +165,9 @@ describe('Paragraph', function (this: Suite) {
});

it('Search index with token should return 200 and matching body.tags', async () => {
const includeTags = {fields: {tags: true}, include: ['subsnippets']};
const includeTags = {where: {tags: 'aSearchableTag'}};
await client
.get(
`/paragraphs/search=${encodeURIComponent(
'#aSearchableTag',
)}?filter=${JSON.stringify(includeTags)}`,
)
.get(`/paragraphs/search=%00?filter=${JSON.stringify(includeTags)}`)
.set('Authorization', 'Bearer ' + token)
.set('Content-Type', 'application/json')
.expect(200)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,13 +162,9 @@ describe('TaskRepositorySnippet', function (this: Suite) {
});

it('Search index with token should return 200 and matching body.tags', async () => {
const includeTags = {fields: {tags: true}, include: ['subsnippets']};
const includeTags = {where: {tags: 'aSearchableTag'}};
await client
.get(
`/tasks/search=${encodeURIComponent(
'#aSearchableTag',
)}?filter=${JSON.stringify(includeTags)}`,
)
.get(`/tasks/search=%00?filter=${JSON.stringify(includeTags)}`)
.set('Authorization', 'Bearer ' + token)
.set('Content-Type', 'application/json')
.expect(200)
Expand Down
46 changes: 46 additions & 0 deletions sci-log-db/src/__tests__/unit/utils.misc.unit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ import {
standardiseIncludes,
sanitizeTextContent,
sanitizeTextContentInPlace,
removeDeepBy,
} from '../../utils/misc';
import {Where} from '@loopback/repository';

describe('Utils unit tests', function (this: Suite) {
it('Should filterEmptySubsnippets', () => {
Expand Down Expand Up @@ -316,4 +318,48 @@ describe('Utils unit tests', function (this: Suite) {
expect(t.input).to.be.eql(t.expected);
});
});

[
{input: [{key1: 'a', key2: 'b'}, ['key2']], expected: {key1: 'a'}},
{input: [{a: {key1: 'a'}, key2: 'b'}, ['key1', 'key2']], expected: {a: {}}},
{
input: [{a: {key1: 'a'}, key2: 'b'}, ['key1']],
expected: {a: {}, key2: 'b'},
},
{
input: [{a: {b: {key1: 'a'}}, key2: 'b'}, ['key1']],
expected: {a: {b: {}}, key2: 'b'},
},
].forEach((t, i) => {
it(`Should test removeDeepBy ${i}`, () => {
removeDeepBy(t.input[0] as Where, (key: string) =>
(t.input[1] as string[]).includes(key),
);
expect(t.input[0]).to.be.eql(t.expected);
});
});

[
{input: [{key1: 'a', key2: 'b'}, ['key2']], expected: {key2: 'b'}},
{
input: [{a: {key1: 'a'}, key2: 'b'}, ['a', 'key2']],
expected: {a: {}, key2: 'b'},
},
{
input: [{a: {key1: 'a'}, key2: 'b'}, ['key1']],
expected: {},
},
{
input: [{a: {b: {key1: 'a'}}, key2: 'b'}, ['key2']],
expected: {key2: 'b'},
},
].forEach((t, i) => {
it(`Should test removeDeepBy with negation ${i}`, () => {
removeDeepBy(
t.input[0] as Where,
(key: string) => !(t.input[1] as string[]).includes(key),
);
expect(t.input[0]).to.be.eql(t.expected);
});
});
});
2 changes: 2 additions & 0 deletions sci-log-db/src/controllers/basesnippet.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,11 +176,13 @@ export class BasesnippetController {
@param.path.string('search') search: string,
@param.filter(Basesnippet, {exclude: 'fields'})
filter?: Filter<Basesnippet>,
@param.array('deepby', 'query', {type: 'string'}) deepBy?: string[],
): Promise<Basesnippet[]> {
const snippetsFiltered = await this.basesnippetRepository.findWithSearch(
search,
this.user,
filter,
deepBy,
);
return snippetsFiltered;
}
Expand Down
Loading

0 comments on commit c58a00d

Please sign in to comment.