Skip to content

Commit

Permalink
Merge pull request #301 from PermanentOrg/PER-9353-search-with-qutati…
Browse files Browse the repository at this point in the history
…on-marks
  • Loading branch information
crisnicandrei authored Oct 24, 2023
2 parents 058fab5 + f782698 commit a9efe56
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 27 deletions.
54 changes: 29 additions & 25 deletions src/app/search/services/search.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,14 +81,6 @@ describe('SearchService', () => {
expectSearchToBe(service.parseSearchTerm('Potato'), 'Potato', []);
});

it('should remove tag tokens from search', () => {
expectSearchToBe(
service.parseSearchTerm('tag:"NonExistentTag"'),
undefined,
[]
);
});

it('should load tag tokens into the tags array', () => {
tags.setTags([{ name: 'Potato' }]);
expectSearchToBe(service.parseSearchTerm('tag:"Potato"'), undefined, [
Expand All @@ -110,36 +102,31 @@ describe('SearchService', () => {
);
});

it('removes quotation marks from tag names', () => {
/*
This test probably represents erroneous behavior that we want to change
at some point. For now, this represents the original functionality of
the SearchService as it was written.
*/
it('handles quotation marks in tag names properly', () => {
tags.setTags([
{ name: 'Potato', tagId: 0 },
{ name: '"Potato"', tagId: 1 },
]);
const searchTokens = service.parseSearchTerm('tag:""Potato""');
expect(searchTokens[1].length).toBe(1);
expect(searchTokens[1][0].tagId).toBe(0);
expect(searchTokens[1][0].tagId).toBe(1);
});

it('handles tag edge cases', () => {
tags.setTags([{ name: 'tag:Test' }]);
expectSearchToBe(
service.parseSearchTerm('tag:"tag:"Test""'),
'tag:"tag:"Test""',
[]
);
});

it('handles returns the tags correctly if they have quotation marks', () => {
tags.setTags([{ name: 'tag:"Test"' }]);
expectSearchToBe(service.parseSearchTerm('tag:"tag:"Test""'), undefined, [
{ name: 'tag:Test' },
{ name: 'tag:"Test"' },
]);
});

function expectSearchToBe(
search: [string, TagVOData[]],
expectedSearch: string,
expectedTags: TagVOData[]
) {
expect(search[0]).toBe(expectedSearch);
expect(search[1]).toEqual(expectedTags);
}
});

describe('getResultsInCurrentFolder', () => {
Expand Down Expand Up @@ -236,4 +223,21 @@ describe('SearchService', () => {
service.getResultsInPublicArchive('Test', [], '1');
expect(apiSpy).toHaveBeenCalled();
});

it('cannot handle tags with quotation marks followed by search terms with quotation marks', () => {
tags.setTags([{ name: '"A Multiword Tag"', tagId: 0 }]);
const searchTokens = service.parseSearchTerm(
'tag:""A Multiword Tag"" "potato"'
);
expect(searchTokens[1].length).toBe(0);
});

function expectSearchToBe(
search: [string, TagVOData[]],
expectedSearch: string,
expectedTags: TagVOData[]
) {
expect(search[0]).toBe(expectedSearch);
expect(search[1]).toEqual(expectedTags);
}
});
10 changes: 8 additions & 2 deletions src/app/search/services/search.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,16 @@ export class SearchService {
const tagNames = part.match(getTagName) || [];
for (const tagName of tagNames) {
if (tagName) {
const name = tagName.replace(/"/g, '');
const name = this.removeFirstAndLastQuotes(tagName);
const tag = this.tags.getTagByName(name);
if (tag) {
parsedTags.push(tag);
}
}
}
if (!parsedTags.length) {
queryString = termString;
}
} else {
queryParts.push(part);
}
Expand All @@ -80,10 +83,13 @@ export class SearchService {
} else {
queryString = termString;
}

return [queryString, parsedTags];
}

private removeFirstAndLastQuotes(str: string): string {
return str.replace(/^"|"$/g, '');
}

public getResultsInCurrentArchive(
searchTerm: string,
tags: TagVOData[],
Expand Down

0 comments on commit a9efe56

Please sign in to comment.