summaryrefslogtreecommitdiff
path: root/src/__tests__/VyOSElasticModernFulltextStorageEngineTestCase.php
blob: 95558a199d69a090bf9734ff0341d81c5d9ffaac (plain)
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
<?php

/**
 * Minimal concrete subclass for unit-testing the abstract engine.
 * Only the abstract stubs from PhabricatorFulltextStorageEngine are
 * satisfied here; no real Elasticsearch I/O occurs in tests.
 */
final class VyOSElasticModernFulltextStorageEngineTestDouble
  extends VyOSElasticModernFulltextStorageEngine {

  public function reindexAbstractDocument(
    PhabricatorSearchAbstractDocument $doc) {
    // no-op in tests
  }

  public function executeSearch(PhabricatorSavedQuery $query) {
    return array();
  }

  public function indexExists() {
    return false;
  }

  public function getIndexStats() {
    return array();
  }

}

final class VyOSElasticModernFulltextStorageEngineTestCase
  extends PhutilTestCase {

  private function newEngine() {
    return new VyOSElasticModernFulltextStorageEngineTestDouble();
  }

  public function testSetVersionAcceptsSevenAndAbove() {
    foreach (array(7, 8, 9, 100) as $v) {
      $engine = $this->newEngine();
      $caught = null;
      try {
        $engine->setVersion($v);
      } catch (Exception $e) {
        $caught = $e;
      }
      $this->assertEqual(
        null,
        $caught,
        pht('Expected no exception for version=%d.', $v));
      $this->assertEqual($v, $engine->getVersion());
    }
  }

  public function testSetVersionRejectsBelowSeven() {
    foreach (array(0, 1, 2, 5, 6) as $v) {
      $engine = $this->newEngine();
      $caught = null;
      try {
        $engine->setVersion($v);
      } catch (Exception $e) {
        $caught = $e;
      }
      $this->assertTrue(
        $caught !== null,
        pht('Expected an exception for version=%d.', $v));
    }
  }

  public function testGetDocumentUri() {
    $engine = $this->newEngine()->setVersion(7);
    $uri = $engine->getDocumentUri('TASK', 'PHID-TASK-abc');
    $this->assertEqual('/_doc/PHID-TASK-abc', $uri);
  }

  public function testGetDocumentUriIgnoresType() {
    // The typeless API does not encode the doc type in the URL.
    $engine = $this->newEngine()->setVersion(7);
    $uri_a = $engine->getDocumentUri('TASK', 'PHID-TASK-abc');
    $uri_b = $engine->getDocumentUri('DREV', 'PHID-TASK-abc');
    $this->assertEqual($uri_a, $uri_b);
  }

  public function testGetSearchUri() {
    $engine = $this->newEngine()->setVersion(7);
    $this->assertEqual(
      '/_search',
      $engine->getSearchUri(array('TASK', 'DREV')));
    $this->assertEqual('/_search', $engine->getSearchUri(array('USER')));
    $this->assertEqual('/_search', $engine->getSearchUri(array()));
  }

  public function testBuildTypeFilter() {
    $engine = id(new VyOSElasticModernFulltextStorageEngine())
      ->setVersion(7);
    $filter = $engine->buildTypeFilter(array('TASK', 'DREV'));
    $expected = array(
      'terms' => array(
        'documentType' => array('TASK', 'DREV'),
      ),
    );
    $this->assertEqual($expected, $filter);
  }

  public function testBuildTypeFilterEmpty() {
    // Empty list still produces a filter; the caller is responsible
    // for normalizing "no types specified" to "all indexable types"
    // before calling this method. This matches the bundled engine's
    // pattern (executeSearch() normalizes before URL construction).
    $engine = id(new VyOSElasticModernFulltextStorageEngine())
      ->setVersion(7);
    $filter = $engine->buildTypeFilter(array());
    $this->assertEqual(
      array('terms' => array('documentType' => array())),
      $filter);
  }

}