Skip to content

Commit

Permalink
Added unit tests for algorithm
Browse files Browse the repository at this point in the history
Refs #11414
  • Loading branch information
DanNixon committed Mar 20, 2015
1 parent 25db142 commit faeeb44
Show file tree
Hide file tree
Showing 2 changed files with 123 additions and 0 deletions.
@@ -0,0 +1,122 @@
import unittest
from mantid.simpleapi import *
from mantid.api import *


class AddSampleLogMultipleTest(unittest.TestCase):

def setUp(self):
"""
Crates a small sample workspace to test with.
"""
CreateSampleWorkspace(OutputWorkspace='__AddSampleLogMultiple_test',
NumBanks=1,
BankPixelWidth=1,
XMax=10,
BinWidth=1)
self._workspace = mtd['__AddSampleLogMultiple_test']


def tearDown(self):
"""
Removes sample workspaces.
"""
DeleteWorkspace(self._workspace)


def _validate_sample_logs(self, names, values, types):
"""
Validates sample logs set on workspace.
@param names List of sample log names
@param values List of sample log values
@param types List of sample log types
"""
logs = self._workspace.getSampleDetails().getLogData()
matched_logs = list()

for log in logs:
if log.name in names:
matched_logs.append(log.name)
idx = names.index(log.name)

self.assertEqual(log.value, values[idx])
self.assertEqual(log.type, types[idx])

self.assertEqual(matched_logs, names)


def test_strings(self):
"""
Tests adding multiple strings.
"""
names = ['a', 'b', 'c']
values = ['one', 'two', 'three']
types = ['string', 'string', 'string']

AddSampleLogMultiple(Workspace=self._workspace,
LogNames=names,
LogValues=values)

self._validate_sample_logs(names, values, types)


def test_strings_and_numbers(self):
"""
Tests adding multiple strings and numbers.
"""
names = ['a', 'b', 'c', 'd', 'e', 'f']
values = ['one', 'two', 'three', 4, 5.5, 6e2]
types = ['string', 'string', 'string', 'number', 'number', 'number']

AddSampleLogMultiple(Workspace=self._workspace,
LogNames=names,
LogValues=values)

self._validate_sample_logs(names, values, types)


def test_validation_no_names(self):
"""
Test validation for no log names.
"""
names = []
values = ['one', 'two', 'three']

self.assertRaises(RuntimeError,
AddSampleLogMultiple,
Workspace=self._workspace,
LogNames=names,
LogValues=values)


def test_validation_no_values(self):
"""
Test validation for no log values.
"""
names = ['a', 'b', 'c']
values = []

self.assertRaises(RuntimeError,
AddSampleLogMultiple,
Workspace=self._workspace,
LogNames=names,
LogValues=values)


def test_validation_differing_counts(self):
"""
Test validation for differing numbers of log names and log values.
"""
names = ['a', 'b', 'c']
values = ['one', 'two']

self.assertRaises(RuntimeError,
AddSampleLogMultiple,
Workspace=self._workspace,
LogNames=names,
LogValues=values)


if __name__ == '__main__':
unittest.main()
Expand Up @@ -3,6 +3,7 @@
##

set ( TEST_PY_FILES
AddSampleLogMultipleTest.py
CalculateSampleTransmissionTest.py
CheckForSampleLogsTest.py
ConjoinSpectraTest.py
Expand Down

0 comments on commit faeeb44

Please sign in to comment.