Skip to content

Commit

Permalink
Updating all existing tests
Browse files Browse the repository at this point in the history
  • Loading branch information
stephen-bunn committed Nov 3, 2017
1 parent 33e35e5 commit fe8046a
Show file tree
Hide file tree
Showing 27 changed files with 229 additions and 433 deletions.
1 change: 0 additions & 1 deletion tests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,4 @@
# MIT License <https://opensource.org/licenses/MIT>

from .test_sandpaper import (SandPaperTest,)
from .test_utils import (UtilTest,)
from .rules import *
5 changes: 2 additions & 3 deletions tests/rules/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,8 @@
from .strip import (StripRuleTest,)
from .increment import (IncrementRuleTest,)
from .decrement import (DecrementRuleTest,)
from .substitute import (SubstituteRuleTest,)
from .translate_text import (TranslateTextRuleTest,)
from .translate_date import (TranslateDateRuleTest,)

from .add_column import (AddColumnRuleTest,)
from .remove_column import (RemoveColumnRuleTest,)
from .add_columns import (AddColumnsRuleTest,)
from .remove_columns import (RemoveColumnsRuleTest,)
81 changes: 45 additions & 36 deletions tests/rules/_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@

import os
import abc
import types
import hashlib
import glob
import filecmp
import unittest

import sandpaper

import six
import braceexpand


class BaseRuleTest(six.with_metaclass(abc.ABCMeta, unittest.TestCase)):
Expand All @@ -26,6 +27,13 @@ def rule_name(self):

raise NotImplementedError()

@abc.abstractproperty
def rule_arguments(self):
""" Required rule arguments as a tuple (*args, **kwargs).
"""

raise NotImplementedError()

@abc.abstractproperty
def rule_group(self):
""" Required rule group for static path discovery.
Expand All @@ -45,6 +53,13 @@ def static_dir(self):
))
return self._static_dir

def _evaluate_glob(self, pattern):
discovered = set()
for variation in braceexpand.braceexpand(pattern):
for path in glob.glob(variation):
discovered.add(path)
return discovered

def _get_static_glob(self, post=False):
""" Gets a glob for testing static files.
"""
Expand All @@ -54,19 +69,6 @@ def _get_static_glob(self, post=False):
'{flag}.{{xls{{,x}},{{c,t}}sv}}'
).format(flag=('post' if post else 'pre'))

def _get_file_hash(self, filepath, chunk_size=4096):
""" Hashes a filepath for equality validation.
"""

hasher = hashlib.md5()
with open(filepath, 'rb') as fp:
while True:
chunk = fp.read(chunk_size)
if not chunk:
break
hasher.update(chunk)
return hasher.hexdigest()

def setUp(self):
""" Setup the test.
"""
Expand All @@ -89,32 +91,39 @@ def test_addition(self):
""" Test the addition of the rule.
"""

self.assertIsInstance(getattr(self.paper, self.rule_group), list)
self.assertEqual(getattr(self.paper, self.rule_group), [])
self.assertIsInstance(getattr(self.paper, 'rules'), list)
self.assertEqual(len(getattr(self.paper, 'rules')), 0)
self.assertIsInstance(getattr(self.paper, self.rule_group), set)
self.assertEqual(getattr(self.paper, self.rule_group), set())
getattr(self.paper, self.rule_name)()
self.assertIsInstance(getattr(self.paper, self.rule_group), list)
self.assertGreater(len(getattr(self.paper, 'rules')), 0)
self.assertIsInstance(getattr(self.paper, self.rule_group), set)
self.assertGreater(len(getattr(self.paper, self.rule_group)), 0)

del getattr(self.paper, self.rule_group)[:]
del self.paper.rules[:]

def test_application(self):
""" Tests the implementation of the rule.
"""

getattr(self.paper, self.rule_name)()

applied = self.paper.apply(self._get_static_glob(post=False))
self.assertIsInstance(applied, types.GeneratorType)
applied = list(applied)
self.assertIsInstance(applied, list)

for (evaluation, processed) in zip(
sandpaper.utils.fancy_glob(self._get_static_glob(post=True)),
applied
):
print((evaluation, processed[0],))
self.assertEqual(
self._get_file_hash(evaluation),
self._get_file_hash(processed[0])
)
self.assertIsInstance(processed[-1], dict)
getattr(self.paper, self.rule_name)(
*self.rule_arguments[0],
**self.rule_arguments[-1]
)

(pre_paths, sanded_paths, post_paths,) = (
self._evaluate_glob(self._get_static_glob(post=False)),
[],
self._evaluate_glob(self._get_static_glob(post=True)),
)
for path in pre_paths:
(name, ext,) = os.path.splitext(path)
sanded_paths.append(('{name}.sanded{ext}').format(**locals()))

for (from_file, to_file, result_file,) in \
zip(pre_paths, sanded_paths, post_paths):
print((from_file, to_file, result_file,))
applied = self.paper.apply(from_file, to_file)
print(applied)
self.assertEqual(applied, to_file)
self.assertTrue(filecmp.cmp(to_file, result_file))
53 changes: 0 additions & 53 deletions tests/rules/add_column.py

This file was deleted.

33 changes: 33 additions & 0 deletions tests/rules/add_columns.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#!/usr/bin/env python
# -*- encoding: utf-8 -*-
#
# Copyright (c) 2017 Stephen Bunn (stephen@bunn.io)
# MIT License <https://opensource.org/licenses/MIT>

from ._common import BaseRuleTest


class AddColumnsRuleTest(BaseRuleTest):
""" Tests the ``add_columns`` rule.
"""

@property
def rule_name(self):
""" The name of the rule.
"""

return 'add_columns'

@property
def rule_arguments(self):
""" The arguments for this rule's application.
"""

return ([{'test': 'test_value'}], {},)

@property
def rule_group(self):
""" The group type of the rule.
"""

return 'record_rules'
7 changes: 7 additions & 0 deletions tests/rules/capitalize.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,13 @@ def rule_name(self):

return 'capitalize'

@property
def rule_arguments(self):
""" The arguments for this rule's application.
"""

return ([], {},)

@property
def rule_group(self):
""" The group type of the rule.
Expand Down
31 changes: 7 additions & 24 deletions tests/rules/decrement.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,7 @@
# Copyright (c) 2017 Stephen Bunn (stephen@bunn.io)
# MIT License <https://opensource.org/licenses/MIT>

import types

from ._common import BaseRuleTest
import sandpaper


class DecrementRuleTest(BaseRuleTest):
Expand All @@ -22,29 +19,15 @@ def rule_name(self):
return 'decrement'

@property
def rule_group(self):
""" The group type of the rule.
def rule_arguments(self):
""" The arguments for this rule's application.
"""

return 'value_rules'
return ([], {'amount': 1},)

def test_application(self):
""" Overridden rule applciation test.
@property
def rule_group(self):
""" The group type of the rule.
"""

getattr(self.paper, self.rule_name)(amount=1)
applied = self.paper.apply(self._get_static_glob(post=False))
self.assertIsInstance(applied, types.GeneratorType)
applied = list(applied)
self.assertIsInstance(applied, list)

for (evaluation, processed) in zip(
sandpaper.utils.fancy_glob(self._get_static_glob(post=True)),
applied
):
print((evaluation, processed[0],))
self.assertEqual(
self._get_file_hash(evaluation),
self._get_file_hash(processed[0])
)
self.assertIsInstance(processed[-1], dict)
return 'value_rules'
31 changes: 7 additions & 24 deletions tests/rules/increment.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,7 @@
# Copyright (c) 2017 Stephen Bunn (stephen@bunn.io)
# MIT License <https://opensource.org/licenses/MIT>

import types

from ._common import BaseRuleTest
import sandpaper


class IncrementRuleTest(BaseRuleTest):
Expand All @@ -22,29 +19,15 @@ def rule_name(self):
return 'increment'

@property
def rule_group(self):
""" The group type of the rule.
def rule_arguments(self):
""" The arguments for this rule's application.
"""

return 'value_rules'
return ([], {'amount': 1},)

def test_application(self):
""" Overridden rule applciation test.
@property
def rule_group(self):
""" The group type of the rule.
"""

getattr(self.paper, self.rule_name)(amount=1)
applied = self.paper.apply(self._get_static_glob(post=False))
self.assertIsInstance(applied, types.GeneratorType)
applied = list(applied)
self.assertIsInstance(applied, list)

for (evaluation, processed) in zip(
sandpaper.utils.fancy_glob(self._get_static_glob(post=True)),
applied
):
print((evaluation, processed[0],))
self.assertEqual(
self._get_file_hash(evaluation),
self._get_file_hash(processed[0])
)
self.assertIsInstance(processed[-1], dict)
return 'value_rules'
7 changes: 7 additions & 0 deletions tests/rules/lower.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,13 @@ def rule_name(self):

return 'lower'

@property
def rule_arguments(self):
""" The arguments for this rule's application.
"""

return ([], {},)

@property
def rule_group(self):
""" The group type of the rule.
Expand Down
7 changes: 7 additions & 0 deletions tests/rules/lstrip.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,13 @@ def rule_name(self):

return 'lstrip'

@property
def rule_arguments(self):
""" The arguments for this rule's application.
"""

return ([], {},)

@property
def rule_group(self):
""" The group type of the rule.
Expand Down

0 comments on commit fe8046a

Please sign in to comment.