summaryrefslogtreecommitdiff
path: root/test-cli/test/tests/qamper.py
blob: 51aa469c9d350a99d872e3e79a2d3758f2d4cef9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import unittest
from test.helpers.amper import Amper


class Qamper(unittest.TestCase):
    params = None
    __current = None

    # varlist: undercurrent, overcurrent
    def __init__(self, testname, testfunc, varlist):
        self.params = varlist
        super(Qamper, self).__init__(testfunc)

        if "undercurrent" in varlist:
            self._undercurrent = varlist["undercurrent"]
        else:
            raise Exception('undercurrent param inside Qamp must be defined')
        if "overcurrent" in varlist:
            self._overcurrent = varlist["overcurrent"]
        else:
            raise Exception('overcurrent param inside Qamp must be defined')
        self._testMethodDoc = testname

    def execute(self):
        amp = Amper()
        # open serial connection
        if not amp.open():
            self.fail("failed: can not open serial port")
            return
        # check if the amperimeter is connected and working
        # 2 ATTEMTS
        if not amp.hello():
            if not amp.hello():
                self.fail("failed: can not communicate")
                return
        # get current value (in Amperes)
        self.__current = amp.getCurrent()
        # close serial connection
        amp.close()
        # Check current range
        if float(self.__current) > float(self._overcurrent):
            self.fail("failed: Overcurrent detected ( {} )".format(self.__current))
        if float(self.__current) < float(self._undercurrent):
            self.fail("failed: Undercurrent detected ( {} )".format(self.__current))

    def getresults(self):
        # resultlist is a python list of python dictionaries
        resultlist = [
            {
                "desc": "current (Ampers)",
                "data": self.__current,
                "type": "string"
            }
        ]
        return resultlist