_binary From f752f87f3fc578c7e19f4d55ba81d42996297c2f Mon Sep 17 00:00:00 2001
From: TimmahC <tph.chow@gmail.com>
Date: Tue, 26 Mar 2019 01:45:44 -0700
Subject: [PATCH] Cleaned up IndexShardTest, a=chris

---
 tests/IndexShardTest.php | 97 ++++++++++++++++++++++++++++++++++++++++++++++--
 1 file changed, 93 insertions(+), 4 deletions(-)

diff --git a/tests/IndexShardTest.php b/tests/IndexShardTest.php
index 87e2f28..91c6ed6 100644
--- a/tests/IndexShardTest.php
+++ b/tests/IndexShardTest.php
@@ -120,6 +120,28 @@ class IndexShardTest extends UnitTest
             "Doc lookup by meta word works");
         $this->assertEqual(count($c_data), 1,
             "Doc lookup by meta word works has correct count");
+        // add a third document and check
+        $docid = "KKKKKKKK";
+        $doc_hash = "LLLLLLLL";
+        $doc_hosts_url = "MMMMMMMM";
+        $docid .= $doc_hash. $doc_hosts_url;
+        $offset = 10;
+        $word_counts = [
+            'BBBBBBBB' => [1, 3],
+            'DDDD' => [1, 4, 9],
+            'GG' => [6],
+        ];
+        $meta_ids = ["YYYYYYYY"];
+        $this->test_objects['shard']->addDocumentWords($docid,
+            $offset, $word_counts, $meta_ids, [], true);
+        $c_data = $this->test_objects['shard']->getPostingsSliceById(
+            L\crawlHashWord('BBBBBBBB', true), 5);
+        $this->assertTrue(isset($c_data["AAAAAAAABBBBBBBBCCCCCCCC"]),
+            "Doc lookup by meta word works");
+        $this->assertTrue(isset($c_data["KKKKKKKKLLLLLLLLMMMMMMMM"]),
+            "Doc lookup by meta word works");
+        $this->assertEqual(count($c_data), 2,
+            "Doc lookup by meta word works has correct count");
     }
     /**
      * Check if can store link documents into an index shard and retrieve them
@@ -162,6 +184,28 @@ class IndexShardTest extends UnitTest
             "Link Doc lookup by word works 2nd doc");
         $this->assertEqual(count($c_data), 2,
             "Link Doc lookup by word works has correct count");
+        $docid = "AAAAAAAA";
+        $doc_hash = "BBBBBBBB";
+        $docid .= $doc_hash."GGGGGGGG";
+        $offset = 21;
+        $word_counts = [
+            'DDDDDDDD' => [1, 4],
+            'LLLLLLLL' => [7, 9],
+            'NNNNNNNN' => [2],
+        ];
+        $meta_ids = ["EEEEEEEE", "FFFFFFFF"];
+        $this->test_objects['shard']->addDocumentWords($docid,
+            $offset, $word_counts, $meta_ids, true);
+        $c_data = $this->test_objects['shard']->getPostingsSliceById(
+            L\crawlHashWord('NNNNNNNN', true), 5);
+        $this->assertTrue(isset($c_data["AAAAAAAABBBBBBBBCCCCCCCC"]),
+            "Link Doc lookup by word works 1st of two");
+        $this->assertTrue(isset($c_data["AAAAAAAABBBBBBBBGGGGGGGG"]),
+            "Link Doc lookup by word works 2nd doc");
+            $this->assertFalse(isset($c_data["AAAAAAAABBBBBBBBEEEEEEEE"]),
+            "Link Doc lookup should not have third document");
+        $this->assertEqual(count($c_data), 2,
+            "Link Doc lookup by word works has correct count");
     }
     /**
      * Check that appending two index shards works correctly
@@ -177,7 +221,7 @@ class IndexShardTest extends UnitTest
             'DDDDDDDD' => [6],
         ];
         $meta_ids = ["EEEEEEEE", "FFFFFFFF"];
-        $this->test_objects['shard']->addDocumentWords($docid,
+        $this->test_objects['shard']->addDocumentWords($docid, //adding a document to the first shard
             $offset, $word_lists, $meta_ids, true);
         $docid = "KKKKKKKKGGGGGGGGHHHHHHHH";
         $offset = 20;
@@ -186,7 +230,7 @@ class IndexShardTest extends UnitTest
             'DDDDDDDD' => [4],
         ];
         $meta_ids = [];
-        $this->test_objects['shard2']->addDocumentWords($docid,
+        $this->test_objects['shard2']->addDocumentWords($docid, //adding a document to the second shard
             $offset, $word_lists, $meta_ids);
         $docid = "GGGGGGGG";
         $offset = 6;
@@ -196,7 +240,7 @@ class IndexShardTest extends UnitTest
             'JJJJJJJJ' => [5],
         ];
         $meta_ids = ["KKKKKKKK"];
-        $this->test_objects['shard2']->addDocumentWords($docid,
+        $this->test_objects['shard2']->addDocumentWords($docid, //adding another document to the second shard
             $offset, $word_lists, $meta_ids);
         $this->test_objects['shard']->appendIndexShard(
             $this->test_objects['shard2']);
@@ -240,6 +284,20 @@ class IndexShardTest extends UnitTest
             L\crawlHashWord('KKKKKKKK', true), 5);
         $this->assertTrue(isset($c_data["GGGGGGGG"]),
             "Data from third shard present 4");
+        $docid = "HHHHHHHHDDDDDDDD";
+        $offset = 50;
+        $word_lists = [
+            'YYYYYYYY' => [3, 7],
+        ];
+        $meta_ids = [];
+        $this->test_objects['shard3']->addDocumentWords($docid, //adding a document to the third shard
+            $offset, $word_lists, $meta_ids);
+        $this->test_objects['shard']->appendIndexShard(
+            $this->test_objects['shard3']);
+        $c_data = $this->test_objects['shard']->getPostingsSliceById(
+            L\crawlHashWord('YYYYYYYY', true), 5);
+        $this->assertTrue(isset($c_data["HHHHHHHHDDDDDDDD"]),
+            "Data from third shard present 5");
     }
     /**
      * Check that changing document offsets works
@@ -306,6 +364,7 @@ class IndexShardTest extends UnitTest
             "DDDDDDDD" => 7,
         ];
         $i = 0;
+        echo $this->test_objects['shard']->len_all_link_docs;
         foreach ($predicted_offsets as $key =>$offset) {
             $this->assertTrue(isset($c_data[$key]),
                 "Summary key matches predicted $i");
@@ -318,6 +377,36 @@ class IndexShardTest extends UnitTest
         $this->assertEqual($c_data['CCCCCCCCFFFFFFFF']
                 [CrawlConstants::SUMMARY_OFFSET],
                 0,  "Summary offset matches predicted second word");
+        // adding new document after changing offset once
+        $docid = "AAAAAAAASSSSSSSD";
+        $offset = 0;
+        $word_lists = [
+            'BBBBBBBB' => [1]
+        ];
+        $meta_ids = [];
+        $this->test_objects['shard']->addDocumentWords($docid,
+            $offset, $word_lists, $meta_ids);
+        $new_doc_offsets = [
+            "QQQQQQQQEEEEEEEEFFFFFFFF" => 10,
+            "DDDDDDDD" => 6,
+        ];
+        $this->test_objects['shard']->changeDocumentOffsets($new_doc_offsets);
+        $c_data = $this->test_objects['shard']->getPostingsSliceById(
+            L\crawlHashWord('BBBBBBBB', true), 6);
+        $predicted_offsets = [
+            "AAAAAAAASSSSSSSS" => 5,
+            "CCCCCCCCFFFFFFFF" => 0,
+            "AAAAAAAAEEEEEEEEFFFFFFFF" => 10,
+            "QQQQQQQQEEEEEEEEFFFFFFFF" => 10,
+            "DDDDDDDD" => 6,
+        ];
+        foreach ($predicted_offsets as $key =>$offset) {
+            $this->assertTrue(isset($c_data[$key]),
+                "Summary key matches predicted $i");
+            $this->assertEqual($c_data[$key][CrawlConstants::SUMMARY_OFFSET],
+                $offset,  "Summary offset matches predicted $i");
+            $i++;
+        }
     }
     /**
      * Check that save and load work
@@ -397,4 +486,4 @@ class IndexShardTest extends UnitTest
         $this->assertTrue(isset($c_data["AAAAAAAABBBBBBBBCCCCCCCC"]),
             "Save without dictionary test works");
     }
-}
+}
\ No newline at end of file
-- 
2.5.0.windows.1

