Skip to content

Conversation

@wlach
Copy link
Contributor

@wlach wlach commented Feb 5, 2024

Subject: Add a unit test for querying functionality in search

Feature or Bugfix

  • Bugfix / Tests

Purpose

The search query function is currently untested, though it's a key piece of functionality.

Intended to eventually be updated after #11942 is landed (but seperating out for now for ease-of-review).

Detail

On investigation, there were a number of minor bugs in search term functionality, which were fixed along with this PR.

Relates


// print the results
_displayNextItem(results, results.length, searchTerms, highlightTerms);
return { results, searchTerms, highlightTerms };
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Refactored this to be seperate from displaying the results, which was necessary to add the test.

if (word.length > 2) {
const escapedWord = _escapeRegExp(word);
Object.keys(terms).forEach((term) => {
if (term.match(escapedWord) && !terms[word])
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

terms[word] is a problem, since if it it's referring to the 0th item, it will have a false value.

This is probably rare in production unless you happen to be unlucky, but can easily happen in a test (see below)

if (term.match(escapedWord) && !titleTerms[word])
arr.push({ files: titleTerms[word], score: Scorer.partialTitle });
});
if (!terms.hasOwnProperty(word)) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Checking for hasOwnProperty before iterating over all the terms seems like an easy optimization, so I just did it.


// create the mapping
files.forEach((file) => {
if (fileMap.has(file) && fileMap.get(file).indexOf(word) === -1)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another fun little bug -- if the file was already present, but the word wasn't there, it would immediately jump to the else clause to reinitialize the map from scratch. Another edge case that would be more likely to happen in a test.

"",
null,
2,
5,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The score was artificially low in this test because we were overwriting the main match with a partial match due to this bug:

https://github.com/sphinx-doc/sphinx/pull/11950/files#r1478405652

Fix a number of small bugs that this picked up
along the way.

Intended to eventually be updated after sphinx-doc#11942 is
landed (but seperating out for now for ease-of-review).
@wlach wlach force-pushed the html-search-test-search-query branch from 8ac1bd0 to 85fb222 Compare February 5, 2024 15:15
});

describe('query', function() {
it("should duplicate on title and content", function() {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Change this to "should not" later after #11942 lands

@wlach wlach marked this pull request as ready for review February 5, 2024 15:18
@wlach
Copy link
Contributor Author

wlach commented Feb 5, 2024

@jayaddison Another search ticket to look at, probably useful in the context of #11942 and #11944 (no rush obviously)

@wlach
Copy link
Contributor Author

wlach commented Feb 6, 2024

Moved this out into other PRs, closing.

@wlach wlach closed this Feb 6, 2024
@wlach wlach deleted the html-search-test-search-query branch February 6, 2024 16:11
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 9, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants