summaryrefslogtreecommitdiffstats
path: root/container-search/src/test/java/com/yahoo/prelude/fastsearch/JsonFieldTestCase.java
blob: 497da70f5ca547aec3ce16b3619de7d80b777728 (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
86
87
88
89
90
91
92
93
94
95
96
97
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.prelude.fastsearch;

import static org.junit.Assert.*;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import com.yahoo.data.access.*;
import com.yahoo.data.access.simple.*;

public class JsonFieldTestCase {

    @Before
    public void setUp() throws Exception {
    }

    @After
    public void tearDown() throws Exception {
    }

    @Test
    public final void requireThatWeightedSetsItemsAreConvertedToStrings() {
        Value.ArrayValue topArr = new Value.ArrayValue();
        topArr.add(new Value.ArrayValue()
                   .add(new Value.DoubleValue(17.5))
                   .add(new Value.LongValue(10)));
        topArr.add(new Value.ArrayValue()
                   .add(new Value.DoubleValue(0.25))
                   .add(new Value.DoubleValue(20)));

        Inspector c = JSONField.convertTop(topArr);

        assertEquals(Type.STRING, c.entry(0).entry(0).type());
        assertEquals(Type.LONG,   c.entry(0).entry(1).type());
        assertEquals(Type.STRING, c.entry(1).entry(0).type());
        assertEquals(Type.DOUBLE, c.entry(1).entry(1).type());

        assertEquals("17.5", c.entry(0).entry(0).asString());
        assertEquals(10,     c.entry(0).entry(1).asLong());
        assertEquals("0.25", c.entry(1).entry(0).asString());
        assertEquals(20.0,   c.entry(1).entry(1).asDouble(), 0.01);
    }

    @Test
    public final void requireThatNewWeightedSetsAreConvertedToOldFormat() {
        Value.ArrayValue topArr = new Value.ArrayValue();
        topArr.add(new Value.ObjectValue()
                   .put("item", new Value.DoubleValue(17.5))
                   .put("weight", new Value.LongValue(10)));
        topArr.add(new Value.ObjectValue()
                   .put("item", new Value.DoubleValue(0.25))
                   .put("weight", new Value.DoubleValue(20)));
        topArr.add(new Value.ObjectValue()
                   .put("item", new Value.StringValue("foob"))
                   .put("weight", new Value.DoubleValue(30)));

        Inspector c = JSONField.convertTop(topArr);

        assertEquals(Type.STRING, c.entry(0).entry(0).type());
        assertEquals(Type.LONG,   c.entry(0).entry(1).type());
        assertEquals(Type.STRING, c.entry(1).entry(0).type());
        assertEquals(Type.DOUBLE, c.entry(1).entry(1).type());
        assertEquals(Type.STRING, c.entry(2).entry(0).type());
        assertEquals(Type.DOUBLE, c.entry(2).entry(1).type());

        assertEquals("17.5", c.entry(0).entry(0).asString());
        assertEquals(10,     c.entry(0).entry(1).asLong());
        assertEquals("0.25", c.entry(1).entry(0).asString());
        assertEquals(20.0,   c.entry(1).entry(1).asDouble(), 0.01);
        assertEquals("foob", c.entry(2).entry(0).asString());
        assertEquals(30.0,   c.entry(2).entry(1).asDouble(), 0.01);
    }

    @Test
    public final void requireThatArrayValuesAreConvertedToStrings() {
        Value.ArrayValue topArr = new Value.ArrayValue();
        topArr.add(new Value.DoubleValue(17.5));
        topArr.add(new Value.DoubleValue(0.25));
        topArr.add(new Value.LongValue(10));
        topArr.add(new Value.DoubleValue(20));

        Inspector c = JSONField.convertTop(topArr);

        assertEquals(Type.STRING, c.entry(0).type());
        assertEquals(Type.STRING, c.entry(1).type());
        assertEquals(Type.STRING, c.entry(2).type());
        assertEquals(Type.STRING, c.entry(3).type());

        assertEquals("17.5", c.entry(0).asString());
        assertEquals("0.25", c.entry(1).asString());
        assertEquals("10",   c.entry(2).asString());
        assertEquals("20.0", c.entry(3).asString());
    }

}