summaryrefslogtreecommitdiff
path: root/test-cli/test/tests/qamper.py
blob: 7a31615de18a6a8cd7878c4d1b4c9335a3019973 (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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import unittest
from test.helpers.amper import Amper


class Qamper(unittest.TestCase):
    params = None
    __resultlist = None  # resultlist is a python list of python dictionaries

    # 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
        self.__resultlist = []

    def execute(self):
        amp = Amper()
        # open serial connection
        if not amp.open():
            self.__resultlist.append(
                {
                    "desc": "Test result",
                    "data": "FAILED: can not open a serial port",
                    "type": "string"
                }
            )
            self.fail("Error: can not open a serial port")
        # check if the amperimeter is connected and working
        # 2 ATTEMTS
        if not amp.hello():
            if not amp.hello():
                self.__resultlist.append(
                    {
                        "desc": "Test result",
                        "data": "FAILED: can not communicate with the amperimeter",
                        "type": "string"
                    }
                )
                self.fail("Error: can not communicate")
        # get current value (in Amperes)
        current = amp.getCurrent()
        # close serial connection
        amp.close()
        # Check current range
        if float(current) > float(self._overcurrent):
            # Overcurrent detected
            self.__resultlist.append(
                {
                    "desc": "Test result",
                    "data": "FAILED: Overcurrent detected ( {} A)".format(current),
                    "type": "string"
                }
            )
            self.fail("failed: Overcurrent detected ( {} )".format(current))
        elif float(current) < float(self._undercurrent):
            # Undercurrent detected
            self.__resultlist.append(
                {
                    "desc": "Test result",
                    "data": "FAILED: Undercurrent detected ( {} A)".format(current),
                    "type": "string"
                }
            )
            self.fail("failed: Undercurrent detected ( {} )".format(current))

        # Test successful
        self.__resultlist.append(
            {
                "desc": "Test result",
                "data": "OK: Current {} A".format(current),
                "type": "string"
            }
        )

    def getresults(self):
        return self.__resultlist