Skip to content

Commit

Permalink
Merge c672df4 into 35fadaa
Browse files Browse the repository at this point in the history
  • Loading branch information
Micah Hausler committed Aug 19, 2014
2 parents 35fadaa + c672df4 commit d86db73
Show file tree
Hide file tree
Showing 17 changed files with 161 additions and 16 deletions.
4 changes: 3 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ python:
- '3.3'
- '3.4'
install:
- pip install flake8 nose>=1.3.0
- pip install coveralls flake8 nose>=1.3.0
- python setup.py install
script:
- flake8 .
- python setup.py nosetests
after_success:
coveralls
File renamed without changes.
2 changes: 1 addition & 1 deletion MANIFEST.in
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
include README.rst
include CONTRIBUTORS.txt
include CONTRIBUTORS
include LICENSE
20 changes: 16 additions & 4 deletions README.rst
Original file line number Diff line number Diff line change
@@ -1,8 +1,20 @@
.. image:: https://travis-ci.org/ambitioninc/kmatch.png
:target: https://travis-ci.org/ambitioninc/kmatch

.. image:: https://coveralls.io/repos/ambitioninc/kmatch/badge.png?branch=develop
:target: https://coveralls.io/r/ambitioninc/kmatch?branch=develop

.. image:: https://pypip.in/v/kmatch/badge.png
:target: https://crate.io/packages/kmatch/
:alt: Latest PyPI version

.. image:: https://pypip.in/d/kmatch/badge.png
:target: https://crate.io/packages/kmatch/
:alt: Number of PyPI downloads


kmatch
===============
======

A language for matching/validating/filtering Python dictionaries

Expand All @@ -17,10 +29,10 @@ To install the latest code directly from source, type::
pip install git+git://github.com/ambitioninc/kmatch.git

Documentation
=============
-------------

Full documentation is available at http://kmatch.readthedocs.org

License
=======
MIT License (see LICENSE)
-------
MIT License (see LICENSE)
9 changes: 6 additions & 3 deletions docs/contributing.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@ Contributing
============

Contributions and issues are most welcome! All issues and pull requests are
handled through github on the `ambitioninc repository`_. Please check for any
existing issues before filing a new one!
handled through github on the `ambitioninc repository`_. Also, please check for
any existing issues before filing a new one. If you have a great idea but it
involves big changes, please file a ticket before making a pull request! We
want to make sure you don't spend your time coding something that might not fit
the scope of the project.

.. _ambitioninc repository: https://github.com/ambitioninc/kmatch
.. _ambitioninc repository: https://github.com/ambitioninc/kmatch/issues

Running the tests
-----------------
Expand Down
2 changes: 1 addition & 1 deletion docs/ref/kmatch.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Code documentation
==================

K
------
-

.. automodule:: kmatch
.. autoclass:: kmatch.K
Expand Down
11 changes: 11 additions & 0 deletions docs/ref/test_mixin.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
.. _ref-test_mixin:

Test Mixin
==========

KmatchTestMixin
---------------

.. automodule:: kmatch
.. autoclass:: kmatch.KmatchTestMixin
:members:
4 changes: 4 additions & 0 deletions docs/release_notes.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
Release Notes
=============

v0.1.3
------
* Added KmatchTestMixin for tests

v0.1.2
------

Expand Down
1 change: 1 addition & 0 deletions docs/toc.rst
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@ Table of Contents
language_overview
usage_examples
ref/kmatch
ref/test_mixin
contributing
release_notes
25 changes: 25 additions & 0 deletions docs/usage_examples.rst
Original file line number Diff line number Diff line change
Expand Up @@ -126,3 +126,28 @@ Take our previous example, except with ``suppress_key_errors`` set to ``True``.
print K(['==', 'k1', 5], suppress_key_errors=True).match({'k2': 1})
False
Using the test mixin
--------------------

The ``KmatchTestMixin`` can be used for test classes when you want to verify a dictionary matches a particular pattern.

.. code-block:: python
from unittest import TestCase
from kmatch import KmatchTestMixin
class MyTestClass(KmatchTestMixin, TestCase):
def my_test(self):
self.assertKmatches(['<=', 'f', 0], {'f': -1})
def my_opposite_test(self):
with self.assertRaises(AssertionError):
self.assertNotKmatches(['<=', 'f', 0], {'f': -1})
self.assertNotKmatches(['<=', 'f', 0], {'g': 1})
.. note:: The ``suppress_key_errors`` parameter is set to ``False`` by default for ``.assertKmatches()``, and ``True``
for ``.assertNotKmatches()``.
3 changes: 2 additions & 1 deletion kmatch/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# flake8: noqa
from .version import __version__
from .kmatch import K
from .kmatch import K
from .mixins import KmatchTestMixin
6 changes: 3 additions & 3 deletions kmatch/kmatch.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def __init__(self, p, suppress_key_errors=False):
:type p: list
:param suppress_key_errors: Suppress KeyError exceptions on filters and return False instead
:type suppress_key_errors: bool
:raises: ValueError on an invalid pattern or regex
:raises: :class:`ValueError <exceptions.ValueError>` on an invalid pattern or regex
"""
self._raw_pattern = deepcopy(p)
self._compiled_pattern = deepcopy(p)
Expand Down Expand Up @@ -148,7 +148,7 @@ def match(self, value):
:type value: dict
:rtype: bool
:returns: True if the value matches the pattern, False otherwise
:raises: KeyError if key from pattern does not exist in input value and the suppress_key_errors class variable
is False
:raises: :class:`KeyError <exceptions.KeyError>` if key from pattern does not exist in input value and the
suppress_key_errors class variable is False
"""
return self._match(self._compiled_pattern, value)
46 changes: 46 additions & 0 deletions kmatch/mixins.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
from .kmatch import K


class KmatchTestMixin(object):
"""
A mixin for test classes to perform kmatch validation on dictionaries
"""
def assertKmatches(self, pattern, value, suppress_key_errors=False):
"""
Assert that the value matches the kmatch pattern.
:type pattern: list
:param pattern: The kmatch pattern
:type value: dict
:param value: The dictionary to evaluate
:type suppress_key_errors: bool
:param suppress_key_errors: Suppress KeyError exceptions on filters and return False instead. False by default
:raises:
* :class:`KeyError <exceptions.KeyError>` if key from pattern does not exist in input value and the \
suppress_key_errors class variable is False
* :class:`AssertionError <exceptions.AssertionError>` if the value **does not** match the pattern
"""
assert K(pattern, suppress_key_errors=suppress_key_errors).match(value)

def assertNotKmatches(self, pattern, value, suppress_key_errors=True):
"""
Assert that the value does **not** matches the kmatch pattern.
:type pattern: list
:param pattern: The kmatch pattern
:type value: dict
:param value: The dictionary to evaluate
:type suppress_key_errors: bool
:param suppress_key_errors: Suppress KeyError exceptions on filters and return False instead. True by default
:raises:
* :class:`KeyError <exceptions.KeyError>` if key from pattern does not exist in input value and the \
suppress_key_errors class variable is False
* :class:`AssertionError <exceptions.AssertionError>` if the value **does match** the pattern
"""
assert not K(pattern, suppress_key_errors=suppress_key_errors).match(value)
File renamed without changes.
39 changes: 39 additions & 0 deletions kmatch/tests/mixin_tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import unittest

from kmatch import KmatchTestMixin


class MixinTestUsingMixin(KmatchTestMixin, unittest.TestCase):

def test_matches(self):
"""
Test .assertMatches() using the mixin on a true match
"""
self.assertKmatches(['<=', 'f', 0], {'f': -1})

def test_matches_raises_error(self):
"""
Test .assertMatches() using the mixin on a false match
"""
with self.assertRaises(AssertionError):
self.assertKmatches(['<=', 'f', 0], {'f': 1})

def test_not_matches(self):
"""
Test .assertNotMatches() using the mixin on a false match
"""
self.assertNotKmatches(['<=', 'f', 0], {'f': 1})

def test_not_matches_no_key_error(self):
"""
Test .assertNotMatches() using the mixin on a false match
"""
self.assertNotKmatches(['<=', 'f', 0], {'g': 1})
self.assertNotKmatches(['<=', 'f', 0], {'f': 1})

def test_not_matches_raises_error(self):
"""
Test .assertNotMatches() using the mixin raises an error on a match
"""
with self.assertRaises(AssertionError):
self.assertNotKmatches(['<=', 'f', 0], {'f': -1})
2 changes: 1 addition & 1 deletion kmatch/version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = '0.1.2'
__version__ = '0.1.3'
3 changes: 2 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,9 @@ def get_version():
'Programming Language :: Python :: 3.3',
'Programming Language :: Python :: 3.4',
'Intended Audience :: Developers',
'License :: OSI Approved :: BSD License',
'License :: OSI Approved :: MIT License',
'Operating System :: OS Independent',
'Development Status :: 5 - Production/Stable',
],
license='MIT',
install_requires=[],
Expand Down

0 comments on commit d86db73

Please sign in to comment.