[1b162dfa] | 1 | """ |
---|
| 2 | Unit tests for dispersion functionality of |
---|
| 3 | C++ model classes |
---|
| 4 | """ |
---|
[ae60f86] | 5 | print "New style models are still under development" |
---|
[1b162dfa] | 6 | import unittest, math, numpy |
---|
| 7 | |
---|
| 8 | class TestModel(object): |
---|
| 9 | """ |
---|
| 10 | An old-style model |
---|
| 11 | """ |
---|
| 12 | def __init__(self): |
---|
| 13 | ## Parameter dictionary |
---|
| 14 | self.params = {} |
---|
| 15 | self.params['scale'] = 0.05 |
---|
| 16 | |
---|
| 17 | ## Parameter details [units, min, max] |
---|
| 18 | self.details = {} |
---|
| 19 | self.details['scale'] = ['', None, None] |
---|
| 20 | |
---|
| 21 | def run(self, x): |
---|
| 22 | return self.params['scale']*x |
---|
| 23 | |
---|
[8809e48] | 24 | from sans.models.BaseModel import BaseModel, Parameter, ParameterProperty |
---|
[1b162dfa] | 25 | class NewTestModel(BaseModel): |
---|
| 26 | scale = ParameterProperty('scale') |
---|
| 27 | |
---|
| 28 | def __init__(self): |
---|
| 29 | BaseModel.__init__(self) |
---|
| 30 | self.parameters = {} |
---|
| 31 | self.parameters['scale'] = Parameter('scale', 4.0) |
---|
| 32 | |
---|
[8809e48] | 33 | |
---|
| 34 | def runXY(self, x): |
---|
[1b162dfa] | 35 | return self.scale*x |
---|
| 36 | |
---|
[8809e48] | 37 | class TestBogusModel(unittest.TestCase): |
---|
| 38 | def setUp(self): |
---|
| 39 | self.model = NewTestModel() |
---|
| 40 | |
---|
| 41 | def test_call(self): |
---|
| 42 | self.assertEqual(self.model(1), 4.0) |
---|
| 43 | self.model.scale = 1.0 |
---|
| 44 | self.assertEqual(self.model(1), 1.0) |
---|
| 45 | self.model.setParam('scale',2.0) |
---|
| 46 | self.assertEqual(self.model(1), 2.0) |
---|
| 47 | self.assertEqual(self.model.getParam('scale'), 2.0) |
---|
| 48 | |
---|
[1b162dfa] | 49 | class TestBaseModel(unittest.TestCase): |
---|
| 50 | """ |
---|
| 51 | Testing C++ Cylinder model |
---|
| 52 | """ |
---|
| 53 | def setUp(self): |
---|
| 54 | from sans.models.BaseModel import BaseModel |
---|
| 55 | self.model = BaseModel() |
---|
| 56 | |
---|
| 57 | |
---|
| 58 | def test_removed_attrs(self): |
---|
| 59 | def setop(): |
---|
| 60 | print self.model.operateOn |
---|
| 61 | self.assertRaises(AttributeError, setop) |
---|
| 62 | def setother(): |
---|
| 63 | print self.model.other |
---|
| 64 | self.assertRaises(AttributeError, setother) |
---|
| 65 | |
---|
| 66 | class TestAdaptor(unittest.TestCase): |
---|
| 67 | """ |
---|
| 68 | Testing C++ Cylinder model |
---|
| 69 | """ |
---|
| 70 | def setUp(self): |
---|
[ae60f86] | 71 | from sans.models.NewCylinderModel import CylinderModel |
---|
| 72 | self.model = CylinderModel() |
---|
[1b162dfa] | 73 | |
---|
| 74 | |
---|
| 75 | def test_setparam(self): |
---|
[ae60f86] | 76 | self.model.setParam("length", 151.0) |
---|
| 77 | self.assertEquals(self.model.getParam("length"), 151.0) |
---|
| 78 | |
---|
| 79 | |
---|
| 80 | |
---|
| 81 | print self.model(0.001) |
---|
[8809e48] | 82 | print self.model.parameters['length'] |
---|
[ae60f86] | 83 | self.model.setParam("length", 250.0) |
---|
[8809e48] | 84 | self.assertEquals(self.model.getParam("length"), 250.0) |
---|
| 85 | |
---|
| 86 | print self.model.parameters['length'] |
---|
[ae60f86] | 87 | print self.model(0.001) |
---|
[1b162dfa] | 88 | |
---|
| 89 | |
---|
| 90 | |
---|
| 91 | if __name__ == '__main__': |
---|
| 92 | unittest.main() |
---|
| 93 | |
---|