From d47274fb8eed849791c6ee6010e8699cd1e6351f Mon Sep 17 00:00:00 2001 From: Moriz Wahl Date: Mon, 16 Mar 2020 01:50:36 +0100 Subject: [PATCH] linting --- backend/src/schema/resolvers/searches.spec.js | 97 ++++++++++--------- .../schema/resolvers/searches/queryString.js | 57 ++++++----- .../resolvers/searches/queryString.spec.js | 29 +++--- 3 files changed, 98 insertions(+), 85 deletions(-) diff --git a/backend/src/schema/resolvers/searches.spec.js b/backend/src/schema/resolvers/searches.spec.js index 6ef6e48b0..42ffd6f98 100644 --- a/backend/src/schema/resolvers/searches.spec.js +++ b/backend/src/schema/resolvers/searches.spec.js @@ -138,27 +138,55 @@ describe('resolvers', () => { }) describe('testing different post content', () => { - const addPost = post => { - return Factory.build( - 'post', - { - id: post.id, - title: post.title, - content: post.content, - }, - factoryOptions, - ) - } - - describe('adding a post which content contains the title of the first post', () => { - describe('query contains the title of the first post', () => { - it('finds both posts', async () => { - await addPost({ - __typename: 'Post', + beforeAll(async () => { + return Promise.all([ + Factory.build( + 'post', + { id: 'b-post', title: 'Aufruf', content: 'Jeder sollte seinen Beitrag leisten.', - }) + }, + factoryOptions, + ), + Factory.build( + 'post', + { + id: 'g-post', + title: 'Zusammengesetzte Wörter', + content: `Ein Bindestrich kann zwischen zwei Substantiven auch dann gesetzt werden, wenn drei gleichlautende Buchstaben aufeinandertreffen. Das ist etwa bei einem „Teeei“ der Fall, das so korrekt geschrieben ist. Möglich ist hier auch die Schreibweise mit Bindestrich: Tee-Ei.`, + }, + factoryOptions, + ), + Factory.build( + 'post', + { + id: 'c-post', + title: 'Die binomischen Formeln', + content: `1. binomische Formel: (a + b)² = a² + 2ab + b² +2. binomische Formel: (a - b)² = a² - 2ab + b² +3. binomische Formel: (a + b)(a - b) = a² - b²`, + }, + factoryOptions, + ), + Factory.build( + 'post', + { + id: 'd-post', + title: 'Der Panther', + content: `Sein Blick ist vom Vorübergehn der Stäbe +so müd geworden, daß er nichts mehr hält. +Ihm ist, als ob es tausend Stäbe gäbe +und hinter tausend Stäben keine Welt.`, + }, + factoryOptions, + ), + ]) + }) + + describe('a post which content contains the title of the first post', () => { + describe('query contains the title of the first post', () => { + it('finds both posts', async () => { variables = { query: 'beitrag' } await expect(query({ query: searchQuery, variables })).resolves.toMatchObject({ data: { @@ -182,14 +210,9 @@ describe('resolvers', () => { }) }) - describe('adding a post that contains a hyphen between two words and German quotation marks', () => { + describe('a post that contains a hyphen between two words and German quotation marks', () => { describe('hyphens in query', () => { it('will be treated as ordinary characters', async () => { - await addPost({ - id: 'g-post', - title: 'Zusammengesetzte Wörter', - content: `Ein Bindestrich kann zwischen zwei Substantiven auch dann gesetzt werden, wenn drei gleichlautende Buchstaben aufeinandertreffen. Das ist etwa bei einem „Teeei“ der Fall, das so korrekt geschrieben ist. Möglich ist hier auch die Schreibweise mit Bindestrich: Tee-Ei.`, - }) variables = { query: 'tee-ei' } await expect(query({ query: searchQuery, variables })).resolves.toMatchObject({ data: { @@ -225,16 +248,9 @@ describe('resolvers', () => { }) }) - describe('adding a post that contains a simple mathematical exprssion and linebreaks', () => { + describe('a post that contains a simple mathematical exprssion and linebreaks', () => { describe('query a part of the mathematical expression', () => { it('finds that post', async () => { - await addPost({ - id: 'c-post', - title: 'Die binomischen Formeln', - content: `1. binomische Formel: (a + b)² = a² + 2ab + b² -2. binomische Formel: (a - b)² = a² - 2ab + b² -3. binomische Formel: (a + b)(a - b) = a² - b²`, - }) variables = { query: '(a - b)²' } await expect(query({ query: searchQuery, variables })).resolves.toMatchObject({ data: { @@ -294,17 +310,9 @@ describe('resolvers', () => { }) }) - describe('adding a post that contains a poem', () => { + describe('a post that contains a poem', () => { describe('query for more than one word, e.g. the title of the poem', () => { it('finds the poem and another post that contains only one word but with lower score', async () => { - await addPost({ - id: 'd-post', - title: 'Der Panther', - content: `Sein Blick ist vom Vorübergehn der Stäbe -so müd geworden, daß er nichts mehr hält. -Ihm ist, als ob es tausend Stäbe gäbe -und hinter tausend Stäben keine Welt.`, - }) variables = { query: 'der panther' } await expect(query({ query: searchQuery, variables })).resolves.toMatchObject({ data: { @@ -400,13 +408,6 @@ und hinter tausend Stäben keine Welt.`, }) }) }) - - /* - it('finds Russian text', async () => { - variables = { query: 'Калашникова' } - const expected = createExpectedObject([gPost]) - await expect(query({ query: searchQuery, variables })).resolves.toMatchObject(expected) - }) */ }) }) }) diff --git a/backend/src/schema/resolvers/searches/queryString.js b/backend/src/schema/resolvers/searches/queryString.js index 3e53bfec9..e53405c8e 100644 --- a/backend/src/schema/resolvers/searches/queryString.js +++ b/backend/src/schema/resolvers/searches/queryString.js @@ -1,7 +1,7 @@ export function queryString(str) { const normalizedString = normalizeWhitespace(str) - const escapedString = escapeSpecialCharacters(normalizedString) - return ` + const escapedString = escapeSpecialCharacters(normalizedString) + return ` ${matchWholeText(escapedString)} ${matchEachWordExactly(escapedString)} ${matchSomeWordsExactly(escapedString)} @@ -10,39 +10,50 @@ ${matchBeginningOfWords(escapedString)} } const matchWholeText = (str, boost = 8) => { - return `"${str}"^${boost}` + return `"${str}"^${boost}` } const matchEachWordExactly = (str, boost = 4) => { - if (str.includes(' ')) { - let tmp = str.split(' ').map((s, i) => i === 0 ? `"${s}"` : `AND "${s}"`).join(' ') - return `(${tmp})^${boost}` - } else { - return '' - } + if (str.includes(' ')) { + const tmp = str + .split(' ') + .map((s, i) => (i === 0 ? `"${s}"` : `AND "${s}"`)) + .join(' ') + return `(${tmp})^${boost}` + } else { + return '' + } } const matchSomeWordsExactly = (str, boost = 2) => { - if (str.includes(' ')) { - return str.split(' ').map(s => `"${s}"^${boost}`).join(' ') - } else { - return '' - } + if (str.includes(' ')) { + return str + .split(' ') + .map(s => `"${s}"^${boost}`) + .join(' ') + } else { + return '' + } } const matchBeginningOfWords = str => { - return normalizeWhitespace(str.split(' ').map(s => { - if (s.length > 3) { - // at least 4 letters. So AND, OR and NOT are never used unquoted - return s + '*' - } else { - return '' - } - }).join(' ')) + return normalizeWhitespace( + str + .split(' ') + .map(s => { + if (s.length > 3) { + // at least 4 letters. So AND, OR and NOT are never used unquoted + return s + '*' + } else { + return '' + } + }) + .join(' '), + ) } export function normalizeWhitespace(str) { - return str.replace(/\s+/g, ' ').trim() + return str.replace(/\s+/g, ' ').trim() } export function escapeSpecialCharacters(str) { diff --git a/backend/src/schema/resolvers/searches/queryString.spec.js b/backend/src/schema/resolvers/searches/queryString.spec.js index e431df90f..8f646ce25 100644 --- a/backend/src/schema/resolvers/searches/queryString.spec.js +++ b/backend/src/schema/resolvers/searches/queryString.spec.js @@ -1,20 +1,20 @@ import { queryString, escapeSpecialCharacters, normalizeWhitespace } from './queryString' describe('queryString', () => { - describe('special characters', () => { - it('does escaping correctly', () => { - expect(escapeSpecialCharacters('+ - && || ! ( ) { } [ ] ^ " ~ * ? : \\ / ')) - .toEqual('\\+ \\- \\&\\& \\|\\| \\! \\( \\) \\{ \\} \\[ \\] \\^ \\" \\~ \\* \\? \\: \\\\ \\/ ') - }) + describe('special characters', () => { + it('does escaping correctly', () => { + expect(escapeSpecialCharacters('+ - && || ! ( ) { } [ ] ^ " ~ * ? : \\ / ')).toEqual( + '\\+ \\- \\&\\& \\|\\| \\! \\( \\) \\{ \\} \\[ \\] \\^ \\" \\~ \\* \\? \\: \\\\ \\/ ', + ) }) + }) - describe('whitespace', () => { - it('is normalized correctly', () => { - expect(normalizeWhitespace(' a \t \n b \n ')) - .toEqual('a b') - }) + describe('whitespace', () => { + it('is normalized correctly', () => { + expect(normalizeWhitespace(' a \t \n b \n ')).toEqual('a b') }) - + }) + describe('exact match', () => { it('boosts score by factor 8', () => { expect(queryString('a couple of words')).toContain('"a couple of words"^8') @@ -23,7 +23,9 @@ describe('queryString', () => { describe('match all words exactly', () => { it('boosts score by factor 4', () => { - expect(queryString('a couple of words')).toContain('("a" AND "couple" AND "of" AND "words")^4') + expect(queryString('a couple of words')).toContain( + '("a" AND "couple" AND "of" AND "words")^4', + ) }) }) @@ -33,10 +35,9 @@ describe('queryString', () => { }) }) - describe('globbing for longer words', () => { + describe('globbing for longer words', () => { it('globs words with more than three characters', () => { expect(queryString('a couple of words')).toContain('couple* words*') }) }) - })