Skip to content

Instantly share code, notes, and snippets.

@adamabernathy
Created February 6, 2017 16:14
Show Gist options
  • Save adamabernathy/b698c60faf727736d2db4fabb1a85928 to your computer and use it in GitHub Desktop.
Save adamabernathy/b698c60faf727736d2db4fabb1a85928 to your computer and use it in GitHub Desktop.
Simple assertion test
# encoding: utf-8
"""
TESTING SCRIPT for Time Rate of Change - QC Flag Test
"""
def assert_(statement, result, caption='*'):
"""
Simple assertion test
"""
red = '\033[91m'
green = '\033[92m'
cstop = '\033[0m'
test_result = green + 'Pass' + cstop \
if statement == result \
else red + 'Fail' + cstop
print test_result, ' ==> ', caption
return statement == result
if __name__ == '__main__':
# pylint: disable=I0011, E0401, W0212
if __package__ is None:
import sys
from os import path
sys.path.append(path.dirname(path.dirname(path.abspath(__file__))))
from qc_flags import time_rate_of_change as FC
else:
from ..qc_flags import time_rate_of_change as FC
print '\n---\nTime Rate of Change - Build Test\n---'
F = FC.TimeRateOfChange()
print '\nVERSION: ', F.__version__
print "\nStarting static analysis"
F._run_pylint()
# Evaluate parts of `get_rules`
print '\nTesting `get_rules()` Hourly Values'
assert_(F.rules[60]['TSOI'], 2.77, 'TSOI')
assert_(F.rules[60]['TMPF'], 19.44, 'TMPF')
# Evaluate `get_previous_ob()`
print '\nTesting `get_previous_ob()`'
assert_(F.get_previous_ob(123456, 60), 42)
# Test the response of `close_segment()`
# Note: this test is really just to validate that the method is a member
# of the class. As we can not really test the execution as it would
# affect the database.
print '\nTesting `close_segment()'
assert_(F.close_segment(), True)
# Evaluate `test()`
print '\nTesting `test()`'
assert_(F.test(1, 2, 3, 4), 0)
# Evaluate `_test()`
print '\nTesting `_test()`'
assert_(F._test('TMPF', 42), False, 'T-60, TMPF, v=42, d=0 ')
assert_(F._test('TMPF', 21), True, 'T-60, TMPF, v=21, d=-20')
assert_(F._test('TMPF', 63), True, 'T-60, TMPF, v=61, d=+20')
print '\nTesting complete!'
@adamabernathy
Copy link
Author

adamabernathy commented Feb 6, 2017

you also need this in your class that you are going to test

def _run_pylint(self):
    import pylint.lint as lint
    from pylint.reporters.text import ParseableTextReporter

    pylint_output = ""
    lint.Run(
        ["-r", "n", __file__],
        reporter=ParseableTextReporter(pylint_output),
        exit=False
    )
    print pylint_output

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment