Skip to content

Commit

Permalink
Add tests and fixed Survey Data CLI
Browse files Browse the repository at this point in the history
Fixed #8
  • Loading branch information
PonteIneptique committed Mar 27, 2018
1 parent d97baf0 commit 33789c8
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 4 deletions.
12 changes: 9 additions & 3 deletions atservices/scripts/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,13 +81,19 @@ def cli():
def dump(dest=None):
if not dest:
dest = os.path.join(".", "misses.csv")
with open("dest", "w") as output:

with open(dest, "w") as output:
output.write(Miss.get_csv())
click.echo("CSV written")

@click.command("survey-clear")
@click.option("--until", help="Datetime until which data needs to be cleared")
def clear():
Miss.clear_up_to()
def clear(until=None):
if until:
click.echo("Clear missed data until " + until)
else:
click.echo("Clearing all missed data")
Miss.clear_up_to(until)

cli.add_command(dump)
cli.add_command(clear)
Expand Down
92 changes: 91 additions & 1 deletion tests/test_scripts/test_scripts.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
function should be done in their respective test modules (see .test_collatinus)
"""
import os

from unittest import TestCase
from click.testing import CliRunner
Expand All @@ -18,6 +19,9 @@
from atservices.models import Translation


from ..fixtures import insert_misses


class TestDataScript(TestCase):
""" Tests for the Data Scripts
"""
Expand Down Expand Up @@ -172,4 +176,90 @@ def test_db_recreate(self):
self.assertEqual(
len(Translation.query.all()), 0,
"There should have been 0 insert"
)
)


class TestSurveyScript(TestCase):
""" Tests for the Survey Scripts
"""

def setUp(self):
self.app, self.db = create_app("test")

self.cli = make_data_cli(db=self.db)
make_db_cli(cli=self.cli, db=self.db)
make_data_survey_cli(cli=self.cli, db=self.db)

self.runner = CliRunner()

with self.app.app_context():
self.db.create_all()
self.db.session.commit()

insert_misses(self.db)

def tearDown(self):
with self.app.app_context():
self.db.drop_all()

def invoke(self, commands):
with self.app.app_context():
return self.runner.invoke(self.cli, commands)

def test_dump(self):
""" Test that dumping to CSV works """
with self.runner.isolated_filesystem() as fs:

self.invoke(["survey-dump"])

with open(os.path.join(fs, "misses.csv")) as f:
self.assertEqual(
f.read(),
"""at lemma lemma_lang translation_lang client
2018-01-01 19:00:00 lascivus lat fre Collatinus-Lemmatize
2018-01-01 20:00:00 appétit fre eng Bocuse
2018-01-01 21:00:00 bbq eng fre They"""
)

def test_dump_specific_file(self):
""" Test that dumping to CSV works with a specified filename """
with self.runner.isolated_filesystem() as fs:

self.invoke(["survey-dump", "--dest", "fake.csv"])

with open(os.path.join(fs, "fake.csv")) as f:
self.assertEqual(
f.read(),
"""at lemma lemma_lang translation_lang client
2018-01-01 19:00:00 lascivus lat fre Collatinus-Lemmatize
2018-01-01 20:00:00 appétit fre eng Bocuse
2018-01-01 21:00:00 bbq eng fre They"""
)

def test_clear(self):
""" Test that clearing Misses works"""
with self.runner.isolated_filesystem() as fs:

self.invoke(["survey-clear"])
self.invoke(["survey-dump"])

with open(os.path.join(fs, "misses.csv")) as f:
self.assertEqual(
f.read(),
"""at lemma lemma_lang translation_lang client"""
)

def test_clear_until(self):
""" Test that clearing Misses until a specific datetime works"""
with self.runner.isolated_filesystem() as fs:

self.invoke(["survey-clear", '--until', '"2018-01-01 19:59:00"'])
self.invoke(["survey-dump"])

with open(os.path.join(fs, "misses.csv")) as f:
self.assertEqual(
f.read(),
"""at lemma lemma_lang translation_lang client
2018-01-01 20:00:00 appétit fre eng Bocuse
2018-01-01 21:00:00 bbq eng fre They"""
)

0 comments on commit 33789c8

Please sign in to comment.