Commit d5ee944d authored by Javier Quinteros's avatar Javier Quinteros

Add tests using tox

Files to be compared are not provided until a small TDMS file can be included.
The original is too big for the time being.
parent 521a22a4
#!/usr/bin/env python3
"""Tests to check that dasconv working
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
any later version.
:Copyright:
2019-2020 Javier Quinteros, Deutsches GFZ Potsdam <javier@gfz-potsdam.de>
:License:
GPLv3
:Platform:
Linux
.. moduleauthor:: Javier Quinteros <javier@gfz-potsdam.de>, GEOFON, GFZ Potsdam
"""
import sys
from datetime import datetime
from datetime import timedelta
import unittest
from unittestTools import WITestRunner
from obspy import Trace
from obspy import Stream
from obspy import read
from dasscripts.tdms import TDMS
class ConversionTests(unittest.TestCase):
"""Test the functionality of tdms.py
"""
def testOneChannel(self):
"""One record from one channel"""
directory = '/Users/javier/Nextcloud/share_Philippe'
# Start of the time window close to the beginning of the file
stt = datetime(2018, 9, 5, 9, 53, 34)
# Take only 250 samples
ett = stt + timedelta(milliseconds=250)
orig = read('testOneChannel.mseed')
t = TDMS('PDN_1km', directory, chstart=100, chstop=100, starttime=stt, endtime=ett)
with t:
for data in t:
aux = Trace(data=data[0], header=data[1])
conv = Stream([aux])
# print(orig[0].stats)
# print(conv[0].stats)
self.assertTrue((orig[0].data==conv[0].data).all(), 'Wrong comparison for one record and one channel!')
# self.assertEqual(orig[0].stats, conv[0].stats, 'Wrong comparison for one record and one channel!')
def testTwoChannels(self):
"""One record from two channels"""
directory = '/Users/javier/Nextcloud/share_Philippe'
# Start of the time window close to the beginning of the file
stt = datetime(2018, 9, 5, 9, 53, 34)
# Take only 250 samples
ett = stt + timedelta(milliseconds=250)
orig = read('testTwoChannels.mseed')
t = TDMS('PDN_1km', directory, chstart=100, chstop=101, starttime=stt, endtime=ett)
with t:
conv = Stream()
for data in t:
aux = Trace(data=data[0], header=data[1])
conv += aux
self.assertTrue((orig[0].data==conv[0].data).all(), 'Wrong comparison for channel 100!')
self.assertTrue((orig[1].data==conv[1].data).all(), 'Wrong comparison for channel 101!')
# self.assertEqual(orig[0].stats, conv[0].stats, 'Wrong comparison for one record and one channel!')
def testTwoRecords(self):
"""Two records from one channel"""
directory = '/Users/javier/Nextcloud/share_Philippe'
# Start of the time window close to the beginning of the file
stt = datetime(2018, 9, 5, 9, 53, 34)
# Take only 250 samples
ett = stt + timedelta(seconds=4)
orig = read('testTwoRecords.mseed')
t = TDMS('PDN_1km', directory, chstart=100, chstop=100, starttime=stt, endtime=ett)
with t:
conv = Stream()
for data in t:
aux = Trace(data=data[0], header=data[1])
conv += aux
self.assertTrue((orig[0].data==conv[0].data).all(), 'Wrong comparison for channel 100!')
# self.assertEqual(orig[0].stats, conv[0].stats, 'Wrong comparison for one record and one channel!')
# ----------------------------------------------------------------------
def usage():
print('testRoute [-h] [-p]')
if __name__ == '__main__':
# 0=Plain mode (good for printing); 1=Colourful mode
mode = 1
for ind in range(len(sys.argv)-1, -1, -1):
if ind == 0:
break
if sys.argv[ind] in ('-p', '--plain'):
sys.argv.pop(ind)
mode = 0
elif sys.argv[ind] in ('-h', '--help'):
usage()
sys.exit(0)
unittest.main(testRunner=WITestRunner(mode=mode))
"""Classes to be used in the tests.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
any later version.
:Copyright:
2019-2020 Javier Quinteros, Deutsches GFZ Potsdam <javier@gfz-potsdam.de>
:License:
GPLv3
:Platform:
Linux
.. moduleauthor:: Javier Quinteros <javier@gfz-potsdam.de>, GEOFON, GFZ Potsdam
"""
import sys
import unittest
class WITestRunner(object):
"""Class in charge of running the tests."""
def __init__(self, outstream=sys.stderr, mode=1):
"""Save the stream where the output will be printed."""
self.mode = mode
if mode:
# Colours escape command
self.HEADER = '\033[95m'
self.OKBLUE = '\033[94m'
self.OKGREEN = '\033[92m'
self.WARNING = '\033[93m'
self.FAIL = '\033[91m'
self.ENDC = '\033[0m'
else:
# Colours escape command
self.HEADER = ''
self.OKBLUE = ''
self.OKGREEN = ''
self.WARNING = ''
self.FAIL = ''
self.ENDC = ''
self.outStream = outstream
self.write(self.HEADER + '\nRunning test...\n' + self.ENDC)
def write(self, message):
"""Redirect output to the stream."""
self.outStream.write(message)
def run(self, test):
"""Run the given test case."""
result = WITestResult(self, self.mode)
test(result)
if len(result.failures) + len(result.errors) > 0:
self.write(self.HEADER + '\nError/Failure details\n' + self.ENDC)
result.printErrors()
run = result.testsRun
return result
class WITestResult(unittest.TestResult):
"""A test result class that prints in colours to the console."""
def __init__(self, testRunner, mode=1):
"""Constructor of the WITestResult class."""
unittest.TestResult.__init__(self)
if mode:
# Colours escape command
self.HEADER = '\033[95m'
self.OKBLUE = '\033[94m'
self.OKGREEN = '\033[92m'
self.WARNING = '\033[93m'
self.FAIL = '\033[91m'
self.ENDC = '\033[0m'
else:
# Colours escape command
self.HEADER = ''
self.OKBLUE = ''
self.OKGREEN = ''
self.WARNING = ''
self.FAIL = ''
self.ENDC = ''
self.testRunner = testRunner
def startTest(self, test):
"""Wrapper to start the test and customize the output."""
unittest.TestResult.startTest(self, test)
self.testRunner.write('Checking %s... ' % test.shortDescription())
def addSuccess(self, test):
"""Wrapper to add a successful test and customize the output."""
unittest.TestResult.addSuccess(self, test)
self.testRunner.write('[' + self.OKGREEN + 'OK' + self.ENDC + ']\n')
def addError(self, test, err):
"""Wrapper to add a faulty test and customize the output."""
unittest.TestResult.addError(self, test, err)
self.testRunner.write('[' + self.WARNING + 'ERROR' + self.ENDC + ']\n')
def addFailure(self, test, err):
"""Wrapper to add a test with failure and customize the output."""
unittest.TestResult.addFailure(self, test, err)
self.testRunner.write('[' + self.FAIL + 'FAIL' + self.ENDC + ']\n')
def printErrors(self):
"""Wrapper to print a summary of errors customizing the output."""
self.printErrorList('Error', self.errors)
self.printErrorList('Failure', self.failures)
def printErrorList(self, errorType, errors):
"""Wrapper to print a list of errors customizing the output."""
for test, err in errors:
self.testRunner.write('%s checking %s\n' %
(errorType, test.shortDescription()))
self.testRunner.write((self.WARNING + ' %s' + self.ENDC) %
err.splitlines(True)[-1])
[tox]
envlist = py36
[testenv]
# install testing framework
# ... or install anything else you might need here
deps = pytest
# run the tests
# ... or run any other command line tool you need to run here
changedir = tests
commands = pytest
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment