test: cov

This commit is contained in:
Marc Cataford 2020-10-01 18:41:27 -04:00
parent fa66296ca8
commit db7146bb31
10 changed files with 269 additions and 64 deletions

0
src/__init__.py Normal file
View file

View file

@ -0,0 +1,184 @@
# name: test_base_tree_has_a_root_node
<class 'dict'> {
'children': <class 'list'> [
],
'mappings': <class 'list'> [
],
'value': None,
}
---
# name: test_insert_multiple_keys_same_string
<class 'dict'> {
'children': <class 'list'> [
<class 'dict'> {
'children': <class 'list'> [
<class 'dict'> {
'children': <class 'list'> [
<class 'dict'> {
'children': <class 'list'> [
<class 'dict'> {
'children': <class 'list'> [
],
'mappings': <class 'list'> [
'key_1',
'key_2',
],
'value': 'd',
},
],
'mappings': <class 'list'> [
],
'value': 'c',
},
],
'mappings': <class 'list'> [
],
'value': 'b',
},
],
'mappings': <class 'list'> [
],
'value': 'a',
},
],
'mappings': <class 'list'> [
],
'value': None,
}
---
# name: test_insert_overlapping_strings
<class 'dict'> {
'children': <class 'list'> [
<class 'dict'> {
'children': <class 'list'> [
<class 'dict'> {
'children': <class 'list'> [
<class 'dict'> {
'children': <class 'list'> [
<class 'dict'> {
'children': <class 'list'> [
],
'mappings': <class 'list'> [
'key_1',
],
'value': 'd',
},
<class 'dict'> {
'children': <class 'list'> [
],
'mappings': <class 'list'> [
'key_2',
],
'value': 'e',
},
],
'mappings': <class 'list'> [
],
'value': 'c',
},
],
'mappings': <class 'list'> [
],
'value': 'b',
},
],
'mappings': <class 'list'> [
],
'value': 'a',
},
],
'mappings': <class 'list'> [
],
'value': None,
}
---
# name: test_insert_single_character_
<class 'dict'> {
'children': <class 'list'> [
<class 'dict'> {
'children': <class 'list'> [
],
'mappings': <class 'list'> [
'key_1',
],
'value': 'a',
},
],
'mappings': <class 'list'> [
],
'value': None,
}
---
# name: test_insert_single_string
<class 'dict'> {
'children': <class 'list'> [
<class 'dict'> {
'children': <class 'list'> [
<class 'dict'> {
'children': <class 'list'> [
<class 'dict'> {
'children': <class 'list'> [
],
'mappings': <class 'list'> [
'key_1',
],
'value': 'c',
},
],
'mappings': <class 'list'> [
],
'value': 'b',
},
],
'mappings': <class 'list'> [
],
'value': 'a',
},
],
'mappings': <class 'list'> [
],
'value': None,
}
---
# name: test_insert_strings_subsets_of_each_other
<class 'dict'> {
'children': <class 'list'> [
<class 'dict'> {
'children': <class 'list'> [
<class 'dict'> {
'children': <class 'list'> [
<class 'dict'> {
'children': <class 'list'> [
<class 'dict'> {
'children': <class 'list'> [
],
'mappings': <class 'list'> [
'key_1',
],
'value': 'd',
},
],
'mappings': <class 'list'> [
'key_2',
],
'value': 'c',
},
],
'mappings': <class 'list'> [
],
'value': 'b',
},
],
'mappings': <class 'list'> [
],
'value': 'a',
},
],
'mappings': <class 'list'> [
],
'value': None,
}
---
# name: test_serializes_to_json
'{"value": null, "mappings": [], "children": [{"value": "a", "mappings": [], "children": [{"value": "b", "mappings": [], "children": [{"value": "c", "mappings": [], "children": [{"value": "d", "mappings": ["key_1"], "children": []}]}]}]}]}'
---

View file

@ -1,4 +1,4 @@
from base import IndexerBase
from .base import IndexerBase
from pathlib import Path
from typing import Dict, List
import re
@ -8,13 +8,13 @@ import mmap
import attr
from settings import settings
from .settings import settings
from process_utils import chunkify_content
from document_models import Corpus
from trigram_index import TrigramIndex
from line_index import LineIndex
from logger import get_logger
from .process_utils import chunkify_content
from .document_models import Corpus
from .trigram_index import TrigramIndex
from .line_index import LineIndex
from .logger import get_logger
logger = get_logger(__name__)

View file

@ -1,7 +1,7 @@
from base import IndexBase
from .base import IndexBase
import attr
from logger import get_logger
from .logger import get_logger
logger = get_logger(__name__)

View file

@ -2,7 +2,7 @@ import logging
import sys
import attr
from colors import highlight
from .colors import highlight
@attr.s

View file

@ -1,5 +1,6 @@
import attr
import json
import attr
@attr.s
class PrefixTree:
@ -43,9 +44,19 @@ class PrefixTree:
if next_child:
return self.get(rest, next_child)
def to_dict(self):
return self.root.to_dict()
def to_json(self):
return json.dumps(self.to_dict())
@attr.s
class PrefixTreeNode:
value = attr.ib()
mappings = attr.ib(default=attr.Factory(list))
children = attr.ib(default=attr.Factory(dict))
def to_dict(self):
return {"value": self.value, "mappings": self.mappings, "children": [child.to_dict() for child in self.children.values()]}

View file

@ -3,7 +3,7 @@ import json
from pathlib import Path
import attr
from constants import SETTINGS_KEYS
from .constants import SETTINGS_KEYS
SETTINGS_PATH = "~/.codesearchrc"

View file

@ -1,49 +0,0 @@
import pytest
from indexer import Indexer
@pytest.fixture()
def indexer():
return Indexer()
def test_indexer_builds_trigram_set_for_given_document(indexer):
mock_document = "now that's a doc"
mock_path = "/home/documents/cool_doc"
indexer.index(path=mock_path, content=mock_document)
expected_trigrams = [
"now",
"ow ",
"w t",
" th",
"tha",
"hat",
"at'",
"t's",
"'s ",
"s a",
" a ",
"a d",
" do",
"doc",
]
assert indexer.trigrams == {mock_path: set(expected_trigrams)}
def test_indexer_preserves_previous_trigram_sets_on_index(indexer):
mock_document_1 = "wow"
mock_document_2 = "woa"
mock_path_1 = "/home"
mock_path_2 = "/somewhere_else"
indexer.index(path=mock_path_1, content=mock_document_1)
assert indexer.trigrams == {mock_path_1: set(["wow"])}
indexer.index(path=mock_path_2, content=mock_document_2)
assert indexer.trigrams == {mock_path_1: set(["wow"]), mock_path_2: set(["woa"])}

59
src/test_prefix_tree.py Normal file
View file

@ -0,0 +1,59 @@
import pytest
from .prefix_tree import PrefixTree
@pytest.fixture
def prefix_tree():
return PrefixTree.initialize()
def test_base_tree_has_a_root_node(prefix_tree, snapshot):
assert prefix_tree.to_dict() == snapshot
def test_insert_single_string(prefix_tree, snapshot):
mock_value = 'abc'
mock_key = 'key_1'
prefix_tree.insert(value=mock_value, key=mock_key)
assert prefix_tree.to_dict() == snapshot
assert prefix_tree.get(value=mock_value) == [mock_key]
def test_insert_single_character_(prefix_tree, snapshot):
mock_value = 'a'
mock_key = 'key_1'
prefix_tree.insert(value=mock_value, key=mock_key)
assert prefix_tree.to_dict() == snapshot
assert prefix_tree.get(value=mock_value) == [mock_key]
def test_insert_overlapping_strings(prefix_tree, snapshot):
mock_value_1 = 'abcd'
mock_key_1 = 'key_1'
mock_value_2 = 'abce'
mock_key_2 = 'key_2'
prefix_tree.insert(value=mock_value_1, key=mock_key_1)
prefix_tree.insert(value=mock_value_2, key=mock_key_2)
assert prefix_tree.to_dict() == snapshot
assert prefix_tree.get(value=mock_value_1) == [mock_key_1]
assert prefix_tree.get(value=mock_value_2) == [mock_key_2]
def test_insert_multiple_keys_same_string(prefix_tree, snapshot):
mock_value = 'abcd'
mock_key_1 = 'key_1'
mock_key_2 = 'key_2'
prefix_tree.insert(value=mock_value, key=mock_key_1)
prefix_tree.insert(value=mock_value, key=mock_key_2)
assert prefix_tree.to_dict() == snapshot
assert prefix_tree.get(value=mock_value) == [mock_key_1, mock_key_2]
def test_insert_strings_subsets_of_each_other(prefix_tree, snapshot):
mock_value_1 = 'abcd'
mock_key_1 = 'key_1'
mock_value_2 = 'abc'
mock_key_2 = 'key_2'
prefix_tree.insert(value=mock_value_1, key=mock_key_1)
prefix_tree.insert(value=mock_value_2, key=mock_key_2)
assert prefix_tree.to_dict() == snapshot
assert prefix_tree.get(value=mock_value_1) == [mock_key_1]
assert prefix_tree.get(value=mock_value_2) == [mock_key_2]
def test_serializes_to_json(prefix_tree, snapshot):
prefix_tree.insert(value="abcd", key="key_1")
assert prefix_tree.to_json() == snapshot

View file

@ -1,9 +1,9 @@
from typing import List, Optional
import attr
from settings import settings
from base import IndexBase
from prefix_tree import PrefixTree
from .settings import settings
from .base import IndexBase
from .prefix_tree import PrefixTree
@attr.s