Skip to content
This repository has been archived by the owner on Apr 12, 2022. It is now read-only.

Commit

Permalink
more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
fopina committed Apr 4, 2018
1 parent 925bdb0 commit aad4ce2
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 13 deletions.
4 changes: 3 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ language: python
python:
- 2.7
- 3.6.4
env:
- FLASK_APP=quickcat
install:
- pip install -r requirements_test.txt
- pip install coveralls
script:
- coverage run test_quickcat.py
after_success:
- coveralls
- coverage report -m
- coverage report -m
4 changes: 4 additions & 0 deletions dev_source_me
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/bin/bash

export FLASK_APP=$(pwd)/quickcat/__init__.py
export FLASK_DEBUG=1
3 changes: 2 additions & 1 deletion quickcat/cli.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import click
from .routes import app
from . import models


@app.cli.command()
Expand All @@ -14,7 +15,7 @@ def load_file(infile, bulk):
]
c_b = models.Image.objects.count()
try:
models.Image.objects.insert(imgs, write_concern={'continue_on_error': False})
models.Image.objects.insert(imgs, write_concern={'continue_on_error': True})
except models.errors.NotUniqueError as e:
pass
c_f = models.Image.objects.count()
Expand Down
92 changes: 87 additions & 5 deletions test_quickcat.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,27 @@
import unittest
import json
import re
import click
from click.testing import CliRunner
import warnings

# UGLY WARNING: refactor app setup to make this prettier
import os
os.environ['MONGODB_URI'] = 'mongomock://localhost/test'

import quickcat.routes # load routes
quickcat.routes.app.config['TESTING'] = True
from quickcat.routes import app
app.config['TESTING'] = True
from quickcat.models import Image, Category, db
from quickcat import cli


class QuickCatTestCase(unittest.TestCase):

def setUp(self):
self.app = quickcat.routes.app.test_client()
self.app = app.test_client()

def tearDown(self):
with quickcat.routes.app.app_context():
with app.app_context():
db.connection.drop_database('test')

def test_index(self):
Expand Down Expand Up @@ -152,7 +156,85 @@ def test_stats(self):
[('b', '0'), ('c', '0')]
)


def test_cli_load_file(self):
with warnings.catch_warnings():
# required to avoid DeprecationWarnings in the output
warnings.filterwarnings("ignore",category=DeprecationWarning)
import mongoengine

runner = CliRunner()
with runner.isolated_filesystem():
with open('hello.txt', 'w') as f:
f.write('urlA\n')
f.write('urlB\n')

result = runner.invoke(cli.load_file, ['hello.txt'])
self.assertEqual(result.exit_code, 0)
self.assertEqual(result.output, '2 added, 0 already existed\n')

result = runner.invoke(cli.load_file, ['hello.txt'])
self.assertEqual(result.exit_code, 0)
self.assertEqual(result.output, '0 added, 2 already existed\n')

self.assertEqual(Image.objects.count(), 2)

def test_cli_load_file_bulk(self):
with warnings.catch_warnings():
# required to avoid DeprecationWarnings in the output
warnings.filterwarnings("ignore",category=DeprecationWarning)
import mongoengine

runner = CliRunner()
with runner.isolated_filesystem():
with open('hello.txt', 'w') as f:
f.write('urlA\n')
f.write('urlB\n')

result = runner.invoke(cli.load_file, ['hello.txt', '-b'])
self.assertEqual(result.exit_code, 0)
self.assertEqual(result.output, '2 added\n')
self.assertEqual(Image.objects.count(), 2)

# TODO re-running same file adds images even though they have same URL
# bug in mongomock (ignoring unique)???

def test_cli_categories(self):
runner = CliRunner()

self.assertEqual(Category.objects.count(), 0)

result = runner.invoke(cli.categories, ['cat A'], input='y\n')
self.assertEqual(result.exit_code, 0)
self.assertEqual(result.output, '''\
Existing categories will be removed and these will be added
cat A
Continue? [y/N]: y
Categories recreated
''')
self.assertEqual(Category.objects.count(), 1)
self.assertEqual(Category.objects.first().name, 'cat A')

result = runner.invoke(cli.categories, ['cat B', 'cat C'], input='n\n')
self.assertEqual(result.exit_code, 0)
self.assertEqual(result.output, '''\
Existing categories will be removed and these will be added
cat B, cat C
Continue? [y/N]: n
''')
self.assertEqual(Category.objects.count(), 1)
self.assertEqual(Category.objects.first().name, 'cat A')

result = runner.invoke(cli.categories, ['cat B', 'cat C'], input='y\n')
self.assertEqual(result.exit_code, 0)
self.assertEqual(result.output, '''\
Existing categories will be removed and these will be added
cat B, cat C
Continue? [y/N]: y
Categories recreated
''')
self.assertEqual(Category.objects.count(), 2)
self.assertEqual(Category.objects.all()[0].name, 'cat B')
self.assertEqual(Category.objects.all()[1].name, 'cat C')


if __name__ == '__main__':
Expand Down
6 changes: 0 additions & 6 deletions wrapper

This file was deleted.

0 comments on commit aad4ce2

Please sign in to comment.