Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test cleanup #3

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -114,3 +114,7 @@ dmypy.json

# Pyre type checker
.pyre/

# Local dev python2 support, ignore for now
Pipfile*
test.sh
1 change: 1 addition & 0 deletions legacy/tk_legacy.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# try to use tkinter, if it fails for both python 2.x and 3.x use cli
gui = True
try:
from tkinter import filedialog, Toplevel, Tk
Expand Down
1 change: 1 addition & 0 deletions split.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ def process_file(self, inputfile, outputdir):
if searchObj:
continue

# TODO: handle case where filename is on same line as tag?
# Set the outfile name
searchObj = re.search(r'^FILENAME (.*)', line)
if searchObj:
Expand Down
Empty file added tests/fixtures/out/.gitkeep
Empty file.
18 changes: 0 additions & 18 deletions tests/fixtures/out/1.xml

This file was deleted.

8 changes: 7 additions & 1 deletion tests/fixtures/qdc1.xml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
FILENAME test1
<qualifieddc xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:dcterms="http://purl.org/dc/terms/" xmlns:marcrel="http://www.loc.gov/marc.relators/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.loc.gov/marc.relators/ http://imlsdcc2.grainger.illinois.edu/registry/marcrel.xsd" xsi:noNamespaceSchemaLocation="http://dublincore.org/schemas/xmls/qdc/2008/02/11/qualifieddc.xsd">
<dc:title>Carved flint macehead, Eastern tomb chamber, Knowth </dc:title>
<dc:description>Maesmawr-type ovoid flint macehead, with cylindrical perforation towards narrow end. All six surfaces are decorated. Discovered in the right-hand recess of the chamber of Knowth Tomb 1B East.</dc:description>
Expand All @@ -15,4 +16,9 @@
<dc:source>Photograph</dc:source>
<dc:format>Jpeg</dc:format>
<dc:rights>Copyright National Monuments Service, Department of Culture, Heritage and the Gaeltacht.</dc:rights>
</qualifieddc>
</qualifieddc>

FILENAME test2
<qualifieddc xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:dcterms="http://purl.org/dc/terms/" xmlns:marcrel="http://www.loc.gov/marc.relators/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.loc.gov/marc.relators/ http://imlsdcc2.grainger.illinois.edu/registry/marcrel.xsd" xsi:noNamespaceSchemaLocation="http://dublincore.org/schemas/xmls/qdc/2008/02/11/qualifieddc.xsd">
<dc:title>Foo</dc:title>
</qualifieddc>
6 changes: 2 additions & 4 deletions tests/support/stdout_helper.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import sys

# https://stackoverflow.com/questions/4219717/how-to-assert-output-with-nosetest-unittest-in-python
# output helper (hides pdb output too)


def get_stdout():
# used to test values sent to stdout (hides pdb output too)
if not hasattr(sys.stdout, "getvalue"):
self.fail("need to run in buffered mode")
self.fail("need to run in buffered mode (--buffer)")
return sys.stdout.getvalue().strip()
3 changes: 1 addition & 2 deletions tests/support/tkinter_test_case.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,9 @@
except ImportError:
from Tkinter import Tk

# https://stackoverflow.com/a/49028688/6305204


class TKinterTestCase(unittest.TestCase):
# https://stackoverflow.com/a/49028688/6305204
def setUp(self):
self.root = Tk()
self.pump_events()
Expand Down
39 changes: 30 additions & 9 deletions tests/test_split.py → tests/test_cli.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,31 @@
# functions being tested
from split import Split

import sys
import glob
import os
import argparse
import unittest

# pre 3.3 need to pip install mock
try:
from unittest.mock import patch
except ImportError:
# pre 3.3 need to pip install mock
from mock import patch

from support.stdout_helper import get_stdout


class TestSplit(unittest.TestCase):
class TestCli(unittest.TestCase):
def __init__(self, methodName="runTest"):
unittest.TestCase.__init__(self, methodName)
self.filename = "./tests/fixtures/qdc1.xml"
self.outputdir = "./tests/fixtures/out"
self.file1 = os.path.join('.', 'tests', 'fixtures', 'qdc1.xml')
self.outputdir = os.path.join('.', 'tests', 'fixtures', 'out')

# TODO: stub filesystem? python equivalent to ruby fakefs?
def tearDown(self):
# clean up, remove xml files from output after each test
for file in glob.glob(os.path.join(self.outputdir, '*.xml')):
os.remove(file)

def test_process_file_not_found(self):
with self.assertRaises(EnvironmentError) as context:
Expand All @@ -27,23 +34,37 @@ def test_process_file_not_found(self):
self.assertTrue("No such file or directory: 'asdf'" in str(context.exception))

def test_setup_params_cli_args(self):
args = argparse.Namespace(filename=self.filename, outputdir=self.outputdir)
args = argparse.Namespace(filename=self.file1, outputdir=self.outputdir)
with patch('argparse.ArgumentParser.parse_args', return_value=args):
self.assertEqual((self.filename, self.outputdir), Split().setup_params())
self.assertEqual((self.file1, self.outputdir), Split().setup_params())
output = get_stdout()
self.assertTrue("Processing input file " + self.filename in output)
self.assertTrue("Processing input file " + self.file1 in output)
self.assertTrue("Creating output xml files in " + self.outputdir in output)

def test_setup_params_invalid_params(self):
# if either the input file or output directory are invalid, print warning
for arg_list in [[self.filename, "asdf"], ["asdf", self.outputdir]]:
for arg_list in [[self.file1, "asdf"], ["asdf", self.outputdir]]:
args = argparse.Namespace(filename=arg_list[0], outputdir=arg_list[1])
with patch('argparse.ArgumentParser.parse_args', return_value=args):
with self.assertRaises(SystemExit):
Split().setup_params()
output = get_stdout()
self.assertTrue("Invalid input or output location" in output)

def test_split_with_filenames(self):
args = argparse.Namespace(filename=self.file1, outputdir=self.outputdir)
with patch('argparse.ArgumentParser.parse_args', return_value=args):
files = ['test1.xml', 'test2.xml']
for file in files:
file_path = os.path.join(self.outputdir, file)
self.assertFalse(os.path.isfile(file_path))

Split().main()

for file in files:
file_path = os.path.join(self.outputdir, file)
self.assertTrue(os.path.isfile(file_path))


if __name__ == '__main__':
unittest.main()
5 changes: 2 additions & 3 deletions tests/test_gui.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@

# pre 3.3 need to pip install mock
try:
from unittest.mock import patch, MagicMock
from unittest.mock import patch
from tkinter import *
except ImportError:
from mock import patch, MagicMock
from mock import patch
from Tkinter import *

from support.tkinter_test_case import TKinterTestCase
Expand All @@ -28,7 +28,6 @@ def test_setup_params_gui_args(self):
with self.assertRaises(SystemExit) as context:
split = Split(self.root)
self.pump_events()
# split.dialog = MagicMock(name='dialog', return_value='test')
split.setup_params()
output = get_stdout()
assertTrue(output, 'Invalid input or output location')
Expand Down