xref: /dokuwiki/_test/tests/Search/Index/TupleOpsTest.php (revision ede4646658cf51245060332d97a319a39c788ea1)
1*ede46466SAndreas Gohr<?php
2*ede46466SAndreas Gohr
3*ede46466SAndreas Gohrnamespace dokuwiki\test\Search\Index;
4*ede46466SAndreas Gohr
5*ede46466SAndreas Gohruse dokuwiki\Search\Index\TupleOps;
6*ede46466SAndreas Gohr
7*ede46466SAndreas Gohrclass TupleOpsTest extends \DokuWikiTest
8*ede46466SAndreas Gohr{
9*ede46466SAndreas Gohr    /**
10*ede46466SAndreas Gohr     * @see testUpdateTuple
11*ede46466SAndreas Gohr     */
12*ede46466SAndreas Gohr    public function provideUpdateTuple()
13*ede46466SAndreas Gohr    {
14*ede46466SAndreas Gohr        return [
15*ede46466SAndreas Gohr            ['', 'foo', 3, 'foo*3'],
16*ede46466SAndreas Gohr            ['', 17, 3, '17*3'],
17*ede46466SAndreas Gohr            ['foo*2', 'foo', 3, 'foo*3'],
18*ede46466SAndreas Gohr            ['17*2', 17, 3, '17*3'],
19*ede46466SAndreas Gohr            ['bar*2:foo*2', 'foo', 3, 'foo*3:bar*2'],
20*ede46466SAndreas Gohr            ['18*2:17*2', 17, 3, '17*3:18*2'],
21*ede46466SAndreas Gohr            // Test tuples without explicit count (implicit count of 1)
22*ede46466SAndreas Gohr            ['foo', 'foo', 3, 'foo*3'],
23*ede46466SAndreas Gohr            ['17', 17, 3, '17*3'],
24*ede46466SAndreas Gohr            ['bar:foo', 'foo', 3, 'foo*3:bar'],
25*ede46466SAndreas Gohr            ['bar*2:foo', 'foo', 5, 'foo*5:bar*2'],
26*ede46466SAndreas Gohr            ['18:17', 17, 3, '17*3:18'],
27*ede46466SAndreas Gohr            ['18:17', 19, 1, '19:18:17'],
28*ede46466SAndreas Gohr            // existing 1 counts are not updated unless touched directly
29*ede46466SAndreas Gohr            ['foo*4:bar*1:baz*3', 'uff', 2, 'uff*2:foo*4:bar*1:baz*3'],
30*ede46466SAndreas Gohr            ['foo*4:bar*1:baz*3', 'bar', 1, 'bar:foo*4:baz*3'],
31*ede46466SAndreas Gohr            // 0 is a valid entity
32*ede46466SAndreas Gohr            ['', 0, 1, '0'],
33*ede46466SAndreas Gohr            ['0*7', 0, 6, '0*6'],
34*ede46466SAndreas Gohr            ['foo:bar*3', 0, 1, '0:foo:bar*3'],
35*ede46466SAndreas Gohr            // frequency of 0 deletes
36*ede46466SAndreas Gohr            ['', 7, 0, ''],
37*ede46466SAndreas Gohr            ['0', 0, 0, ''],
38*ede46466SAndreas Gohr            ['foo*6:bar*3:baz', 'bar', 0, 'foo*6:baz'],
39*ede46466SAndreas Gohr        ];
40*ede46466SAndreas Gohr    }
41*ede46466SAndreas Gohr
42*ede46466SAndreas Gohr    /**
43*ede46466SAndreas Gohr     * @dataProvider provideUpdateTuple
44*ede46466SAndreas Gohr     */
45*ede46466SAndreas Gohr    public function testUpdateTuple($record, $key, $count, $expect)
46*ede46466SAndreas Gohr    {
47*ede46466SAndreas Gohr        $result = TupleOps::updateTuple($record, $key, $count);
48*ede46466SAndreas Gohr        $this->assertEquals($result, $expect);
49*ede46466SAndreas Gohr    }
50*ede46466SAndreas Gohr
51*ede46466SAndreas Gohr    /**
52*ede46466SAndreas Gohr     * @see testAggregateTupleCounts
53*ede46466SAndreas Gohr     */
54*ede46466SAndreas Gohr    public function provideAggregateTupleCounts()
55*ede46466SAndreas Gohr    {
56*ede46466SAndreas Gohr        return [
57*ede46466SAndreas Gohr            ['5*10:foo*2:14*100::bar*7', 119],
58*ede46466SAndreas Gohr            // Test with tuples without explicit count (implicit count of 1)
59*ede46466SAndreas Gohr            ['5:foo:14::bar', 4],
60*ede46466SAndreas Gohr            ['5*10:foo:14*100', 111],
61*ede46466SAndreas Gohr            ['key1:key2:key3', 3],
62*ede46466SAndreas Gohr            ['', 0],
63*ede46466SAndreas Gohr            ['single', 1],
64*ede46466SAndreas Gohr        ];
65*ede46466SAndreas Gohr    }
66*ede46466SAndreas Gohr
67*ede46466SAndreas Gohr    /**
68*ede46466SAndreas Gohr     * @dataProvider provideAggregateTupleCounts
69*ede46466SAndreas Gohr     */
70*ede46466SAndreas Gohr    public function testAggregateTupleCounts($record, $expected)
71*ede46466SAndreas Gohr    {
72*ede46466SAndreas Gohr        $result = TupleOps::aggregateTupleCounts($record);
73*ede46466SAndreas Gohr        $this->assertEquals($expected, $result);
74*ede46466SAndreas Gohr    }
75*ede46466SAndreas Gohr
76*ede46466SAndreas Gohr    /**
77*ede46466SAndreas Gohr     * @see testParseTuples
78*ede46466SAndreas Gohr     */
79*ede46466SAndreas Gohr    public function provideParseTuples()
80*ede46466SAndreas Gohr    {
81*ede46466SAndreas Gohr        return [
82*ede46466SAndreas Gohr            // Original test case
83*ede46466SAndreas Gohr            [
84*ede46466SAndreas Gohr                '5*10:foo*2:14*100::bar*7',
85*ede46466SAndreas Gohr                [5 => 'first', 'bar' => 'second', 'foo' => null],
86*ede46466SAndreas Gohr                ['first' => 10, 'second' => 7]
87*ede46466SAndreas Gohr            ],
88*ede46466SAndreas Gohr            // Test with tuples without explicit count (implicit count of 1)
89*ede46466SAndreas Gohr            [
90*ede46466SAndreas Gohr                '5:foo:14::bar',
91*ede46466SAndreas Gohr                [5 => 'first', 'bar' => 'second', 'foo' => null],
92*ede46466SAndreas Gohr                ['first' => 1, 'second' => 1]
93*ede46466SAndreas Gohr            ],
94*ede46466SAndreas Gohr            // Mixed: some with count, some without
95*ede46466SAndreas Gohr            [
96*ede46466SAndreas Gohr                '5*10:foo:14*100',
97*ede46466SAndreas Gohr                [5 => 'first', 'foo' => 'second', 14 => 'third'],
98*ede46466SAndreas Gohr                ['first' => 10, 'second' => 1, 'third' => 100]
99*ede46466SAndreas Gohr            ],
100*ede46466SAndreas Gohr            // No filter map (returns all with original keys)
101*ede46466SAndreas Gohr            [
102*ede46466SAndreas Gohr                '5*10:foo*2:bar',
103*ede46466SAndreas Gohr                null,
104*ede46466SAndreas Gohr                [5 => 10, 'foo' => 2, 'bar' => 1]
105*ede46466SAndreas Gohr            ],
106*ede46466SAndreas Gohr            // Empty record
107*ede46466SAndreas Gohr            [
108*ede46466SAndreas Gohr                '',
109*ede46466SAndreas Gohr                [5 => 'first'],
110*ede46466SAndreas Gohr                []
111*ede46466SAndreas Gohr            ],
112*ede46466SAndreas Gohr        ];
113*ede46466SAndreas Gohr    }
114*ede46466SAndreas Gohr
115*ede46466SAndreas Gohr    /**
116*ede46466SAndreas Gohr     * @dataProvider provideParseTuples
117*ede46466SAndreas Gohr     */
118*ede46466SAndreas Gohr    public function testParseTuples($record, $keys, $expect)
119*ede46466SAndreas Gohr    {
120*ede46466SAndreas Gohr        $result = TupleOps::parseTuples($record, $keys);
121*ede46466SAndreas Gohr        $this->assertEquals($expect, $result);
122*ede46466SAndreas Gohr    }
123*ede46466SAndreas Gohr
124*ede46466SAndreas Gohr}
125