Skip to content

Commit

Permalink
Refactor search to apply recursively
Browse files Browse the repository at this point in the history
* Refactor search to reuse mongo/lb optimisation

* Add tests

* Delegate search composition to be
  • Loading branch information
minottic authored Mar 11, 2024
1 parent 0059f4a commit 5a587cf
Show file tree
Hide file tree
Showing 11 changed files with 355 additions and 150 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import {
setupApplication,
} from './test-helper';
import _ from 'lodash';
import {Basesnippet} from '../../models';

describe('Basesnippet', function (this: Suite) {
this.timeout(5000);
Expand All @@ -34,7 +33,7 @@ describe('Basesnippet', function (this: Suite) {
versionable: true,
name: 'aSearchableName',
description: 'aSearchableDescription',
textcontent: '<p>aSearchableTextContent</p>',
textcontent: '<p>aSearchable TextContent</p>',
};

before('setupApplication', async () => {
Expand Down Expand Up @@ -222,15 +221,15 @@ describe('Basesnippet', function (this: Suite) {

it('Search with token should return 200 and matching body.textcontent', async () => {
await client
.get(`/basesnippets/search=aSearchableText`)
.get(`/basesnippets/search=aSearchable TextCont`)
.set('Authorization', 'Bearer ' + token)
.set('Content-Type', 'application/json')
.expect(200)
.then(
result => (
expect(result.body.length).to.be.eql(1),
expect(result.body[0].textcontent).to.be.eql(
'<p>aSearchableTextContent</p>',
'<p>aSearchable TextContent</p>',
)
),
)
Expand All @@ -240,12 +239,34 @@ describe('Basesnippet', function (this: Suite) {
});

it('Search with token should return 200 and matching body.tags', async () => {
const includeTags = {fields: {tags: true}, include: ['subsnippets']};
const includeTags = {include: ['subsnippets']};
await client
.get(
`/basesnippets/search=aSearchabletag?filter=${JSON.stringify(
includeTags,
)}`,
`/basesnippets/search=${encodeURIComponent(
'#aSearchableTag',
)}?filter=${JSON.stringify(includeTags)}`,
)
.set('Authorization', 'Bearer ' + token)
.set('Content-Type', 'application/json')
.expect(200)
.then(
result => (
expect(result.body.length).to.be.eql(1),
expect(result.body[0].tags[0]).to.be.eql('aSearchableTag')
),
)
.catch(err => {
throw err;
});
});

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

it('Search with token should return 200 and matching readACL', async () => {
await client
.get(`/basesnippets/search=basesnippetAccept`)
.get(`/basesnippets/search=@basesnippetAcceptance`)
.set('Authorization', 'Bearer ' + token)
.set('Content-Type', 'application/json')
.expect(200)
Expand Down Expand Up @@ -299,33 +320,41 @@ describe('Basesnippet', function (this: Suite) {
name: 'aSearchExcludedName',
description: 'aSearchExcludedDescription',
textcontent:
'<p>aSearchExcludedTextContent</p><p>&aSearchableTextContent</p>',
'<p>aSearchExcludedTextContent</p><p>&aSearchable TextContent</p>',
ownerGroup: 'basesnippetAcceptance',
accessGroups: ['basesnippetAcceptance'],
})
.expect(204);
});

it('Search index with token should return 200 and matching subsnippets', async () => {
const includeTags = {fields: {tags: true}, include: ['subsnippets']};
it('Search with token should return 200 and matching subsnippets', async () => {
const includeTags = {include: ['subsnippets']};
await client
.post('/basesnippets')
.set('Authorization', 'Bearer ' + token)
.set('Content-Type', 'application/json')
.send({
...baseSnippet,
parentId: baseSnippetId,
tags: ['aSubsnippetTag'],
});

await client
.get(
`/basesnippets/search=aSearchabletag?filter=${JSON.stringify(
includeTags,
)}`,
`/basesnippets/search=${encodeURIComponent(
'#aSubsnippetTag',
)}?filter=${JSON.stringify(includeTags)}`,
)
.set('Authorization', 'Bearer ' + token)
.set('Content-Type', 'application/json')
.expect(200)
.then(
result => (
expect(result.body.length).to.be.eql(2),
result.body.map((res: Basesnippet) => {
if (res.subsnippets) {
expect(res.subsnippets[0].tags).to.be.eql(['aSearchableTag']);
expect(res.snippetType).to.be.eql('history');
} else expect(res.tags).to.be.eql(['aSearchableTag']);
})
expect(result.body[0].tags).to.be.eql(['aSubsnippetTag']),
expect(result.body[1].subsnippets[0].tags).to.be.eql([
'aSubsnippetTag',
])
),
)
.catch(err => {
Expand Down Expand Up @@ -745,7 +774,7 @@ describe('Basesnippet', function (this: Suite) {
name: 'aSearchExcludedName',
description: 'aSearchExcludedDescription',
textcontent:
'<p>aSearchExcludedTextContent</p><p>&aSearchableTextContent</p>',
'<p>aSearchExcludedTextContent</p><p>&aSearchable TextContent</p>',
...t.input,
})
.expect(t.expected)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -190,9 +190,9 @@ describe('File controller services', function (this: Suite) {
const includeTags = {fields: {tags: true}, include: ['subsnippets']};
await client
.get(
`/filesnippet/search=aSearchabletag?filter=${JSON.stringify(
includeTags,
)}`,
`/filesnippet/search=${encodeURIComponent(
'#aSearchableTag',
)}?filter=${JSON.stringify(includeTags)}`,
)
.set('Authorization', 'Bearer ' + token)
.set('Content-Type', 'application/json')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -194,9 +194,9 @@ describe('Location', function (this: Suite) {
const includeTags = {fields: {tags: true}, include: ['subsnippets']};
await client
.get(
`/locations/search=aSearchabletag?filter=${JSON.stringify(
includeTags,
)}`,
`/locations/search=${encodeURIComponent(
'#aSearchableTag',
)}?filter=${JSON.stringify(includeTags)}`,
)
.set('Authorization', 'Bearer ' + token)
.set('Content-Type', 'application/json')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,9 @@ describe('Logbook', function (this: Suite) {
const includeTags = {fields: {tags: true}, include: ['subsnippets']};
await client
.get(
`/logbooks/search=aSearchabletag?filter=${JSON.stringify(includeTags)}`,
`/logbooks/search=${encodeURIComponent(
'#aSearchableTag',
)}?filter=${JSON.stringify(includeTags)}`,
)
.set('Authorization', 'Bearer ' + token)
.set('Content-Type', 'application/json')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,9 +167,9 @@ describe('Paragraph', function (this: Suite) {
const includeTags = {fields: {tags: true}, include: ['subsnippets']};
await client
.get(
`/paragraphs/search=aSearchabletag?filter=${JSON.stringify(
includeTags,
)}`,
`/paragraphs/search=${encodeURIComponent(
'#aSearchableTag',
)}?filter=${JSON.stringify(includeTags)}`,
)
.set('Authorization', 'Bearer ' + token)
.set('Content-Type', 'application/json')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,11 @@ 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']};
await client
.get(`/tasks/search=aSearchabletag?filter=${JSON.stringify(includeTags)}`)
.get(
`/tasks/search=${encodeURIComponent(
'#aSearchableTag',
)}?filter=${JSON.stringify(includeTags)}`,
)
.set('Authorization', 'Bearer ' + token)
.set('Content-Type', 'application/json')
.expect(200)
Expand Down
114 changes: 114 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 @@ -9,9 +9,95 @@ import {
defaultSequentially,
concatOwnerAccessGroups,
arrayOfUniqueFrom,
filterEmptySubsnippets,
standardiseIncludes,
} from '../../utils/misc';

describe('Utils unit tests', function (this: Suite) {
it('Should filterEmptySubsnippets', () => {
const snippet = {
subsnippets: [
{
subsnippets: [
undefined,
{subsnippets: [1, undefined]},
{subsnippets: [undefined, undefined]},
{subsnippets: undefined},
{subsnippets: []},
],
},
],
} as Basesnippet;
filterEmptySubsnippets(snippet);
expect(snippet).to.eql({
subsnippets: [{subsnippets: [{subsnippets: [1]}]}],
});
});

[
{
subsnippets: [
{
subsnippets: [
undefined,
{subsnippets: [1, undefined]},
{subsnippets: [undefined, undefined]},
{subsnippets: undefined},
{subsnippets: []},
],
},
undefined,
],
},
{
subsnippets: [
{
subsnippets: [
undefined,
{subsnippets: [1, undefined]},
{subsnippets: [undefined, undefined]},
{subsnippets: undefined},
{subsnippets: []},
],
},
],
},
{
subsnippets: [
{
subsnippets: [
{subsnippets: [1, undefined]},
{subsnippets: [undefined, undefined]},
{subsnippets: undefined},
{subsnippets: []},
],
},
],
},
{
subsnippets: [{subsnippets: [{subsnippets: [1]}]}],
},
].forEach((t, i) => {
it(`Should filterEmptySubsnippets with maxDept ${i}`, () => {
const snippet = {
subsnippets: [
{
subsnippets: [
undefined,
{subsnippets: [1, undefined]},
{subsnippets: [undefined, undefined]},
{subsnippets: undefined},
{subsnippets: []},
],
},
undefined,
],
} as Basesnippet;
filterEmptySubsnippets(snippet, i);
expect(snippet).to.eql(t);
});
});

it('Should add deprecated to delete', () => {
const withDeprecated = getModelSchemaRefWithDeprecated(Basesnippet, {
deprecated: ['deleted'],
Expand Down Expand Up @@ -158,4 +244,32 @@ describe('Utils unit tests', function (this: Suite) {
expect(arrayOfUniqueFrom(...t.input)).to.be.eql(t.expected);
});
});

[
{
input: {include: ['a']},
expected: {include: [{relation: 'a'}]},
},
{
input: {include: [{relation: 'a'}]},
expected: {include: [{relation: 'a'}]},
},
{
input: {},
expected: {},
},
{
input: {include: ['a'], where: 'b'},
expected: {include: [{relation: 'a'}], where: 'b'},
},
{
input: {include: [{relation: 'a', include: ['c']}]},
expected: {include: [{relation: 'a', include: [{relation: 'c'}]}]},
},
].forEach((t, i) => {
it(`Should test standardiseIncludes ${i}`, () => {
standardiseIncludes(t.input);
expect(t.input).to.be.eql(t.expected);
});
});
});
Loading

0 comments on commit 5a587cf

Please sign in to comment.