Coverage for test/d7a/alp/operations/test_operation.py: 94%

34 statements  

« prev     ^ index     » next       coverage.py v7.5.0, created at 2024-05-24 08:03 +0200

1# 

2# Copyright (c) 2015-2021 University of Antwerp, Aloxy NV. 

3# 

4# This file is part of pyd7a. 

5# See https://github.com/Sub-IoT/pyd7a for further info. 

6# 

7# Licensed under the Apache License, Version 2.0 (the "License"); 

8# you may not use this file except in compliance with the License. 

9# You may obtain a copy of the License at 

10# 

11# http://www.apache.org/licenses/LICENSE-2.0 

12# 

13# Unless required by applicable law or agreed to in writing, software 

14# distributed under the License is distributed on an "AS IS" BASIS, 

15# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 

16# See the License for the specific language governing permissions and 

17# limitations under the License. 

18# 

19 

20# author: Christophe VG <contact@christophe.vg> 

21# unit tests for the Operation base class 

22 

23import unittest 

24 

25from d7a.alp.operations.operation import Operation 

26 

27class OperandX(object): pass 

28class OperandY(object): pass 

29 

30def make_operation(operand=None, op=None): 

31 class MyOperation(Operation): 

32 def __init__(self, *args, **kwargs): 

33 self.operand_class = operand 

34 self.op = op 

35 super(MyOperation, self).__init__(*args, **kwargs) 

36 return MyOperation 

37 

38class TestOperation(unittest.TestCase): 

39 

40 def test_not_implemented_operand(self): 

41 def bad(): Operation() 

42 self.assertRaises(AttributeError, bad) 

43 

44 def test_missing_operand(self): 

45 MyOperation = make_operation(operand=OperandX) 

46 def bad(): MyOperation() 

47 self.assertRaises(ValueError, bad) 

48 

49 def test_unexpected_operand(self): 

50 MyOperation = make_operation() 

51 def bad(): MyOperation(OperandY()) 

52 self.assertRaises(ValueError, bad) 

53 

54 

55 def test_incorrect_operand(self): 

56 MyOperation = make_operation(operand=OperandX) 

57 def bad(): MyOperation(OperandY()) 

58 self.assertRaises(ValueError, bad) 

59 

60 def test_op_property(self): 

61 MyOperation = make_operation(operand=None, op=123) 

62 op = MyOperation() 

63 self.assertEqual(op.op, 123) 

64 

65if __name__ == '__main__': 

66 suite = unittest.TestLoader().loadTestsFromTestCase(TestOperation) 

67 unittest.TextTestRunner(verbosity=2).run(suite)