Skip to content

Commit

Permalink
Merge pull request #54 from Capitains/better-cli
Browse files Browse the repository at this point in the history
Added more support for test of Manager
  • Loading branch information
PonteIneptique committed Mar 28, 2017
2 parents efc46a1 + bf45331 commit 0c9c4a6
Show file tree
Hide file tree
Showing 7 changed files with 155 additions and 44 deletions.
7 changes: 0 additions & 7 deletions capitains_nautilus/cts/resolver.py
Expand Up @@ -149,13 +149,6 @@ def read(self, identifier, path=None):
o = Text(urn=identifier, resource=self.xmlparse(f))
return o

def flush(self):
""" Flush current resolver objects and cache
"""
for text in self.texts:
self.cache.delete(_cache_key(self.texts_parsed_cache_key, str(text.id)))
self.cache.delete(self.inventory_cache_key)

def parse(self, resource=None):
""" Parse a list of directories ans
:param resource: List of folders
Expand Down
4 changes: 3 additions & 1 deletion capitains_nautilus/flask_ext.py
Expand Up @@ -50,7 +50,9 @@ class FlaskNautilus(object):
# CTS
"_r_GetCapabilities", "_r_GetPassage", "_r_GetPassagePlus",
"_r_GetValidReff", "_r_GetPrevNext", "_r_GetFirstUrn", "_r_GetLabel",
"cts_error"
"cts_error",
# DTS
"r_dts_collections", "r_dts_collection", "dts_error"
]

def __init__(self, prefix="", app=None, name=None,
Expand Down
18 changes: 8 additions & 10 deletions capitains_nautilus/manager.py
Expand Up @@ -73,10 +73,14 @@ def flush_http_cache():
flask_nautilus.flaskcache.clear()

@CLI.command()
def flush():
""" Flush all caches """
flush_http_cache()
flush_resolver()
def flush_both():
""" Flush all caches
"""
if resolver.cache.clear() is True:
click.echo("Caching of Resolver Cleared")
if flask_nautilus.flaskcache.clear() is True:
click.echo("Caching of HTTP Cleared")

@CLI.command()
def parse():
Expand All @@ -97,10 +101,4 @@ def process_reffs(threads):
del future
click.echo("References parsed")

@CLI.command()
def reset():
""" Clean then parse the inventory """
flush()
parse()

return CLI
2 changes: 1 addition & 1 deletion setup.py
Expand Up @@ -2,7 +2,7 @@

setup(
name='capitains_nautilus',
version="1.0.0b6",
version="1.0.0b7",
description='Resolver for Capitains Guidelines Repository',
url='http://github.com/Capitains/nautilus',
author='Thibault Clerice',
Expand Down
35 changes: 19 additions & 16 deletions tests/test_cli/app.py
Expand Up @@ -3,7 +3,7 @@
from flask import Flask
from werkzeug.contrib.cache import FileSystemCache
from flask_caching import Cache
from tests.test_cli.config import subprocess_repository, subprocess_cache_dir
from tests.test_cli.config import subprocess_repository, subprocess_cache_dir, http_cache_dir


from MyCapytain.resources.prototypes.cts.inventory import TextInventoryCollection, PrototypeTextInventory
Expand All @@ -12,32 +12,35 @@

from capitains_nautilus.flask_ext import FlaskNautilus

tic = TextInventoryCollection()
latin = PrototypeTextInventory("urn:perseus:latinLit", parent=tic)
latin.set_label("Classical Latin", "eng")
latin.set_label("Latin Classique", "fre")

def make_dispatcher():
tic = TextInventoryCollection()
latin = PrototypeTextInventory("urn:perseus:latinLit", parent=tic)
latin.set_label("Classical Latin", "eng")
latin.set_label("Latin Classique", "fre")
dispatcher = CollectionDispatcher(tic)

@dispatcher.inventory("urn:perseus:latinLit")
def dispatchLatinLit(collection, path=None, **kwargs):
if collection.id.startswith("urn:cts:latinLit:"):
return True
return False

return dispatcher


http_cache = Cache(
config={
'CACHE_TYPE': "filesystem",
"CACHE_DIR": subprocess_cache_dir,
"CACHE_DIR": http_cache_dir,
"CACHE_DEFAULT_TIMEOUT": 0
}
)
nautilus_cache = FileSystemCache(subprocess_cache_dir, default_timeout=0)
dispatcher = CollectionDispatcher(tic)


@dispatcher.inventory("urn:perseus:latinLit")
def dispatchLatinLit(collection, path=None, **kwargs):
if collection.id.startswith("urn:cts:latinLit:"):
return True
return False


resolver = NautilusCTSResolver(
subprocess_repository,
dispatcher=dispatcher,
dispatcher=make_dispatcher(),
cache=nautilus_cache
)

Expand Down
3 changes: 2 additions & 1 deletion tests/test_cli/config.py
@@ -1,2 +1,3 @@
subprocess_repository = ["./tests/test_data/latinLit"]
subprocess_cache_dir = "cache_dir"
subprocess_cache_dir = "cache_dir"
http_cache_dir = "http_cache_dir"
130 changes: 122 additions & 8 deletions tests/test_cli/test_cli.py
@@ -1,11 +1,19 @@
from unittest import TestCase
from subprocess import call
from sys import executable
import glob
import os
import mock
import shutil
from tests.test_cli.config import subprocess_cache_dir
from tests.test_cli.app import resolver, nautilus_cache, http_cache
from click.testing import CliRunner
from capitains_nautilus.manager import FlaskNautilusManager
from tests.test_cli.config import subprocess_cache_dir, http_cache_dir, subprocess_repository
from tests.test_cli.app import resolver, nautilus_cache, make_dispatcher, app, nautilus
from flask import Flask
from flask_caching import Cache
from werkzeug.contrib.cache import FileSystemCache
from capitains_nautilus.cts.resolver import NautilusCTSResolver
from capitains_nautilus.flask_ext import FlaskNautilus


cwd = os.getcwd()
Expand All @@ -14,6 +22,7 @@


class TestManager(TestCase):

class ParsingCalled(Exception):
pass

Expand All @@ -25,34 +34,139 @@ def cli(self, *args):
def setUp(self):
self.cache = nautilus_cache
self.resolver = resolver
self.resolver.dispatcher = make_dispatcher()
self.resolver.__inventory__ = None
self.resolver.logger.disabled = True
self.former_parse = self.resolver.parse
self.nautilus = nautilus
self.app = app

def x(*k, **kw):
raise TestManager.ParsingCalled("Parse should not be called")
raise self.ParsingCalled("Parse should not be called")
self.resolver.parse = x

def tearDown(self):
#shutil.rmtree(subprocess_cache_dir, ignore_errors=True)
shutil.rmtree(subprocess_cache_dir, ignore_errors=True)
shutil.rmtree(http_cache_dir, ignore_errors=True)
self.resolver.parse = self.former_parse

def test_parse(self):
""" Check that parsing works """
self.cli("parse")
self.assertEqual(len(self.resolver.texts), 2, "There should be 2 texts preprocessed")

def test_flush_inventory(self):
output = self.cli("parse")
""" Check that parsing works and that flushing removes the cache """
self.cli("parse")
files = glob.glob(subprocess_cache_dir+"/*")
self.assertGreater(len(files), 0, "There should be caching operated by resolver")

self.cli("flush_resolver")
with self.assertRaises(TestManager.ParsingCalled): # It is called because it will parse
self.assertEqual(len(self.resolver.texts), 0, "There should be 0 texts preprocessed")
files = glob.glob(subprocess_cache_dir+"/*")
self.assertEqual(len(files), 0, "Resolver Cache should be flushed")

def test_flush_http(self):
""" Check that parsing works, that flushing removes the http cache """
self.cli("parse")

with self.app.app_context():
x = nautilus._r_GetCapabilities()
self.assertIn(
'<label xml:lang="eng">Epigrammata</label>', x[0],
"Response should be correctly produced"
)

files = glob.glob(http_cache_dir+"/*")
self.assertGreater(len(files), 0, "There should be caching operated by flask-caching")

self.cli("flush_http_cache")
files = glob.glob(http_cache_dir+"/*")
self.assertEqual(len(files), 0, "There should be flushing of flask-caching")

files = glob.glob(subprocess_cache_dir+"/*")
self.assertGreater(len(files), 0, "But not of Resolver Cache")

def test_flush_both(self):
""" Check that parsing works, that both flushing removes the http cache and resolver cache"""
self.cli("parse")

with self.app.app_context():
x = nautilus._r_GetCapabilities()
self.assertIn(
'<label xml:lang="eng">Epigrammata</label>', x[0],
"Response should be correctly produced"
)

files = glob.glob(http_cache_dir+"/*")
self.assertGreater(len(files), 0, "There should be caching operated by flask-caching")
files = glob.glob(subprocess_cache_dir+"/*")
self.assertGreater(len(files), 0, "There should be caching operated by resolver")

self.cli("flush_both")
files = glob.glob(http_cache_dir+"/*")
self.assertEqual(len(files), 0, "There should be flushing of flask-caching")
files = glob.glob(subprocess_cache_dir+"/*")
self.assertEqual(len(files), 0, "There should be flushing of resolver")

def test_references(self):
output = self.cli("parse")
output_2 = self.cli("process_reffs")
self.assertEqual(len(self.resolver.texts), 2, "There should be 2 texts preprocessed")
with mock.patch("capitains_nautilus.cts.resolver.CTSCapitainsLocalResolver.getReffs") as getReffs:
self.assertEqual(
self.resolver.getReffs(textId=self.resolver.texts[0].id, level=2)[:5],
self.resolver.getReffs(textId="urn:cts:latinLit:phi1294.phi002.perseus-lat2", level=2)[:5],
['1.pr', '1.1', '1.2', '1.3', '1.4']
)
getReffs.assert_not_called()


class TestManagerClickMethod(TestManager):
""" Rerun the same tests but in the context of unit test """

def setUp(self):
# Full creation of app
self.http_cache = Cache(
config={
'CACHE_TYPE': "filesystem",
"CACHE_DIR": http_cache_dir,
"CACHE_DEFAULT_TIMEOUT": 0
}
)
self.cache = FileSystemCache(subprocess_cache_dir, default_timeout=0)
self.resolver = NautilusCTSResolver(
subprocess_repository,
dispatcher=make_dispatcher(),
cache=self.cache
)
self.app = Flask("Nautilus")
self.nautilus = FlaskNautilus(
app=self.app,
prefix="/api",
name="nautilus",
resolver=self.resolver,
flask_caching=self.http_cache
)
self.http_cache.init_app(self.app)

# Option to ensure cache works
self.former_parse = self.resolver.parse

def x(*k, **kw):
raise self.ParsingCalled("Parse should not be called")
self.resolver.parse = x

def cli(self, *args):
""" Run command using CliRunner
:param args:
:return:
"""
manager = FlaskNautilusManager(self.resolver, self.nautilus)
runner = CliRunner()
raising_parse = self.resolver.parse
# Ensure Parsing can work
self.resolver.parse = self.former_parse
invoked = runner.invoke(manager, list(args))
self.resolver.parse = raising_parse
self.__inventory__ = None
return invoked

0 comments on commit 0c9c4a6

Please sign in to comment.