tests

This file contains test cases for Privex’s Python Helper’s (privex-helpers).

Before running the tests:

  • Ensure you have any mandatory requirements installed (see setup.py’s install_requires)

  • You may wish to install any optional requirements listed in README.md for best results

  • Python 3.7 is recommended at the time of writing this. See README.md in-case this has changed.

To run the tests, simply execute ./tests.py in your shell:

user@the-matrix ~/privex-helpers $ ./tests.py
............................
----------------------------------------------------------------------
Ran 28 tests in 0.001s

OK

If for some reason you don’t have the executable python3.7 in your PATH, try running by hand with python3

user@the-matrix ~/privex-helpers $ python3 tests.py
............................
----------------------------------------------------------------------
Ran 28 tests in 0.001s

OK

For more verbosity, simply add -v to the end of the command:

user@the-matrix ~/privex-helpers $ ./tests.py -v
test_empty_combined (__main__.TestBoolHelpers) ... ok
test_isfalse_truthy (__main__.TestBoolHelpers) ... ok
test_v4_arpa_boundary_16bit (__main__.TestIPReverseDNS)
Test generating 16-bit v4 boundary ... ok
test_v4_arpa_boundary_24bit (__main__.TestIPReverseDNS)
Test generating 24-bit v4 boundary ... ok
test_kval_single (__main__.TestParseHelpers)
Test that a single value still returns a list ... ok
test_kval_spaced (__main__.TestParseHelpers)
Test key:val csv parsing with excess outer whitespace, and value whitespace ... ok
# Truncated excess output in this PyDoc example, as there are many more lines showing 
# the results of each individual testcase, wasting space and adding bloat...
----------------------------------------------------------------------
Ran 28 tests in 0.001s

OK

You can also use the pytest tool (used by default for our Travis CI):

user@host: ~/privex-helpers $ pip3 install pytest
# You can add `-v` for more detailed output, just like when running tests.py directly.
user@host: ~/privex-helpers $ pytest tests.py

===================================== test session starts =====================================
platform darwin -- Python 3.7.0, pytest-5.0.1, py-1.8.0, pluggy-0.12.0
rootdir: /home/user/privex-helpers
collected 33 items                                                                            

tests.py .................................                                              [100%]

====================================== warnings summary =======================================
/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/jinja2/utils.py:485
/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/jinja2/utils.py:485: 
DeprecationWarning: Using or importing the ABCs from 'collections' instead of from 'collections.abc' 
is deprecated, and in 3.8 it will stop working
    from collections import MutableMapping
============================ 33 passed, 2 warnings in 0.17 seconds ============================

Copyright:

Copyright 2019         Privex Inc.   ( https://www.privex.io )
License: X11 / MIT     Github: https://github.com/Privex/python-helpers

Classes

EmptyIter

A mock iterable object with zero length for testing empty()

TestBoolHelpers([methodName])

Test the boolean check functions is_true, is_false, as well as empty()

TestIPReverseDNS([methodName])

Unit testing for the reverse DNS functions in privex.helpers.net

TestParseHelpers([methodName])

Test the parsing functions parse_csv and parse_keyval

PrivexBaseCase([methodName])

Base test-case for module test cases to inherit.

class tests.EmptyIter[source]

A mock iterable object with zero length for testing empty()

class tests.PrivexBaseCase(methodName='runTest')[source]

Base test-case for module test cases to inherit.

Contains useful class attributes such as falsey and empty_vals that are used across different unit tests.

falsey = ['false', 'FALSE', False, 0, '0', 'no']

Normal False-y values, as various types

falsey_empty = ['false', 'FALSE', False, 0, '0', 'no', None, '', 'null']

False-y values, plus ‘empty’ values like ‘’ and None

truthy = [True, 'TRUE', 'true', 'yes', 'y', '1', 1]

Truthful values, as various types

class tests.TestBoolHelpers(methodName='runTest')[source]

Test the boolean check functions is_true, is_false, as well as empty()

class tests.TestIPReverseDNS(methodName='runTest')[source]

Unit testing for the reverse DNS functions in privex.helpers.net

Covers:
  • positive resolution tests (generate standard rDNS domain from clean input)

  • positive boundary tests (confirm valid results with range of boundaries)

  • negative address tests (ensure errors thrown for invalid v4/v6 addresses)

  • negative boundary tests (ensure errors thrown for invalid v4/v6 rDNS boundaries)

test_v4_arpa_boundary_16bit()[source]

Test generating 16-bit v4 boundary

test_v4_arpa_boundary_24bit()[source]

Test generating 24-bit v4 boundary

test_v4_inv_boundary()[source]

Raise if IPv4 boundary isn’t divisable by 8

test_v4_inv_boundary_2()[source]

Raise if IPv4 boundary is too short

test_v4_invalid()[source]

Raise if IPv4 address has < 4 octets

test_v4_invalid_2()[source]

Raise if IPv4 address has octet out of range

test_v4_to_arpa()[source]

Test generating rDNS for standard v4

test_v6_arpa_boundary_16bit()[source]

Test generating 16-bit v6 boundary

test_v6_arpa_boundary_32bit()[source]

Test generating 32-bit v6 boundary

test_v6_inv_boundary()[source]

Raise if IPv6 boundary isn’t dividable by 4

test_v6_inv_boundary_2()[source]

Raise if IPv6 boundary is too short

test_v6_invalid()[source]

Raise if IPv6 address has invalid block formatting

test_v6_invalid_2()[source]

Raise if v6 address has invalid chars

test_v6_to_arpa()[source]

Test generating rDNS for standard v6

class tests.TestParseHelpers(methodName='runTest')[source]

Test the parsing functions parse_csv and parse_keyval

test_csv_single()[source]

Test that a single value still returns a list

test_csv_spaced()[source]

Test csv parsing with excess outer whitespace, and value whitespace

test_env_bool_false()[source]

Test env_bool returns False boolean with valid env var

test_env_bool_true()[source]

Test env_bool returns True boolean with valid env var

test_env_nonexist_bool()[source]

Test env_bool returns default with non-existant env var

test_kval_clean()[source]

Test that a clean key:val csv is parsed correctly

test_kval_custom_clean()[source]

Test that a clean key:val csv with custom split characters is parsed correctly (pipe for kv, semi-colon for pair separation)

test_kval_custom_spaced()[source]

Test key:val csv parsing with excess outer/value whitespace, and custom split characters.

test_kval_single()[source]

Test that a single value still returns a list

test_kval_spaced()[source]

Test key:val csv parsing with excess outer whitespace, and value whitespace

class tests.TestRedisCache(methodName='runTest')[source]

Unit tests which verify that the decorator privex.helpers.decorators.r_test() caches correctly, and also verifies dynamic cache key generation works as expected.

setUp()[source]

Hook method for setting up the test fixture before exercising it.

tearDown()[source]

Remove any Redis keys used during test, to avoid failure on re-run

test_rcache_callable()[source]

Decorate random string function - use a lambda callable to determine a cache key

test_rcache_rand()[source]

Decorate random string function with r_cache - test that two calls return the same string

test_rcache_rand_dynamic()[source]

Decorate random string function with r_cache and use format_args for dynamic cache string testing