aboutsummaryrefslogtreecommitdiffstats
path: root/config-model/src/test/java/com/yahoo/schema/processing/NGramTestCase.java
blob: 551542a84babf009d17e7ae867b8d1eb4df3a199 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.schema.processing;

import com.yahoo.schema.Schema;
import com.yahoo.schema.ApplicationBuilder;
import com.yahoo.schema.AbstractSchemaTestCase;
import com.yahoo.schema.document.MatchType;
import com.yahoo.schema.document.SDField;
import com.yahoo.schema.document.Stemming;
import com.yahoo.schema.parser.ParseException;
import org.junit.jupiter.api.Test;

import java.io.IOException;
import java.util.List;

import static org.junit.jupiter.api.Assertions.*;

/**
 * @author bratseth
 */
public class NGramTestCase extends AbstractSchemaTestCase {

    @Test
    void testNGram() throws IOException, ParseException {
        Schema schema = ApplicationBuilder.buildFromFile("src/test/examples/ngram.sd");
        assertNotNull(schema);

        SDField gram1 = schema.getConcreteField("gram_1");
        assertEquals(MatchType.GRAM, gram1.getMatching().getType());
        assertEquals(1, gram1.getMatching().getGramSize().getAsInt());

        SDField gram2 = schema.getConcreteField("gram_2");
        assertEquals(MatchType.GRAM, gram2.getMatching().getType());
        assertTrue(gram2.getMatching().getGramSize().isEmpty());

        SDField gram3 = schema.getConcreteField("gram_3");
        assertEquals(MatchType.GRAM, gram3.getMatching().getType());
        assertEquals(3, gram3.getMatching().getGramSize().getAsInt());

        assertEquals("input gram_1 | ngram 1 | index gram_1 | summary gram_1", gram1.getIndexingScript().iterator().next().toString());
        assertEquals("input gram_2 | ngram 2 | attribute gram_2 | index gram_2", gram2.getIndexingScript().iterator().next().toString());
        assertEquals("input gram_3 | ngram 3 | index gram_3", gram3.getIndexingScript().iterator().next().toString());

        assertFalse(gram1.getNormalizing().doRemoveAccents());
        assertEquals(Stemming.NONE, gram1.getStemming());

        List<String> queryCommands = gram1.getQueryCommands();
        assertEquals(2, queryCommands.size());
        assertEquals("ngram 1", queryCommands.get(1));
    }

    @Test
    void testInvalidNGramSetting1() throws IOException, ParseException {
        try {
            ApplicationBuilder.buildFromFile("src/test/examples/invalidngram1.sd");
            fail("Should cause an exception");
        }
        catch (IllegalArgumentException e) {
            assertEquals("gram-size can only be set when the matching mode is 'gram'", e.getMessage());
        }
    }

    @Test
    void testInvalidNGramSetting2() throws IOException, ParseException {
        try {
            ApplicationBuilder.buildFromFile("src/test/examples/invalidngram2.sd");
            fail("Should cause an exception");
        }
        catch (IllegalArgumentException e) {
            assertEquals("gram-size can only be set when the matching mode is 'gram'", e.getMessage());
        }
    }

    @Test
    void testInvalidNGramSetting3() throws IOException, ParseException {
        try {
            ApplicationBuilder.buildFromFile("src/test/examples/invalidngram3.sd");
            fail("Should cause an exception");
        }
        catch (IllegalArgumentException e) {
            assertEquals("gram matching is not supported with attributes, use 'index' in indexing", e.getMessage());
        }
    }

}