WAKAYAMA shirou avatar WAKAYAMA shirou committed eda99ed Draft

add config and docstore tests.

Comments (0)

Files changed (2)

test/test_config.py

+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+
+import os
+import shutil
+import pytest
+import simplejson
+
+from dipus import config
+
+def write_config(data):
+    path = "/tmp/dipus_config.test"
+    with open(path, "w") as fp:
+        fp.write(simplejson.dumps(data))
+    return path
+
+def test_config_default():
+    path = write_config({})
+    c = config.Config(path)
+    assert c is not None
+    assert c.port == 9876
+    assert c.indexroot == "/tmp/dipus/index"
+    assert c.host == "0.0.0.0"
+    
+    os.remove(path)
+
+def test_config():
+    test_dir = "/tmp/dipus_true"
+    path = write_config({
+        "port": 9090,
+        "host": "127.0.0.1",
+        "indexroot": test_dir
+        })
+    c = config.Config(path)
+    assert c.port == 9090
+    assert c.indexroot == test_dir
+    assert c.host == "127.0.0.1"
+
+    os.remove(path)

test/test_docstore.py

+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+
+import os
+import shutil
+import time
+import pytest
+
+from dipus import docstore
+
+INDEXROOT="/tmp/dipustest/index"
+posted = {
+    "_index": "test_index",
+    "path": "test_path".decode("utf-8"),
+    "title": "test_title".decode("utf-8"),
+    "doc_url": "test_doc_url".decode("utf-8"),
+    "message": "test_message".decode("utf-8")
+    }
+    
+def test_openindex():
+    indexdir = os.path.join(INDEXROOT, "test_index")
+    ix = docstore.open_index(indexdir)
+    assert ix.last_modified != -1
+    assert 'title' in ix.schema.names()
+    assert 'path' in ix.schema.names()
+    assert 'doc_url' in ix.schema.names()
+    assert 'content' in ix.schema.names()
+    assert 'date' in ix.schema.names()
+    shutil.rmtree(INDEXROOT)
+
+
+def test_listindex():
+    with pytest.raises(OSError):
+        docstore.listindex(INDEXROOT)
+    indexdir = os.path.join(INDEXROOT, "test_index")
+    docstore.open_index(indexdir)
+    assert len(docstore.listindex(INDEXROOT)) == 1
+    indexdir = os.path.join(INDEXROOT, "test_index2")
+    docstore.open_index(indexdir)
+    assert len(docstore.listindex(INDEXROOT)) == 2
+    indexdir = os.path.join(INDEXROOT, "test_index3")
+    docstore.open_index(indexdir)
+    assert len(docstore.listindex(INDEXROOT)) == 3
+
+    shutil.rmtree(INDEXROOT)
+
+def test_register():
+    r = docstore.register(posted, INDEXROOT)
+
+    assert r['ok'] == True
+    assert r['_index'] == posted['_index']
+    assert r['_path'] == posted['path']
+    
+    time.sleep(1)  # FIXME
+    
+    r = docstore.search(posted["_index"], "test", INDEXROOT)
+
+    assert len(r) > 0
+
+    shutil.rmtree(INDEXROOT)
+    
+
+def test_search():
+    r = docstore.register(posted, INDEXROOT)
+    time.sleep(1)  # FIXME
+
+    r = docstore.search(posted["_index"], "hoge", INDEXROOT)
+    assert len(r) == 0
+    r = docstore.search(posted["_index"], "test", INDEXROOT)
+    assert len(r) > 0
+    r = docstore.search(posted["_index"], None, INDEXROOT)
+    assert len(r) == 0
+
+    shutil.rmtree(INDEXROOT)
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.