Beispiel #1
0
 def test_cl_return_message01(self) -> None:
     """Test behaviour of _return_message() on good and bad messages.
     """
     testy_lst = [([("ME", "hello")], "hello"), ([("AA", "funny")], None)]
     for test_lst, resp_exp in testy_lst:
         clresp = commlink.CLResponse(test_lst)
         resp_got = clresp._return_message()
         assert resp_got == resp_exp, "unexpected RespTuple"
Beispiel #2
0
 def test_clresp01(self) -> None:
     """A return code of an CLResponse instance with an empty response list
     or faulty message returns RC_FAULTY."""
     for bad_lst in [[], ['bl'], [('UB', '1')]]:
         print("BADLST: '{}'".format(bad_lst))
         clresp = commlink.CLResponse(bad_lst)
         retcode = clresp.return_code()
         assert retcode == commlink.BaseCommLink.RC_FAULTY, "RC faulty expected"
Beispiel #3
0
 def test_cl_return_code01(self):
     """Test return_code() on a number of valid/ invalid response messages."""
     testy_lst = [([("ME", "hello"),
                    ('OK', "")], commlink.BaseCommLink.RC_OK),
                  ([("ME", "hello"), ('ER', "099")], 99)]
     for test_lst, resp_exp in testy_lst:
         clresp = commlink.CLResponse(test_lst)
         resp_got = clresp.return_code()
         assert resp_got == resp_exp, "unexpected respcode"
Beispiel #4
0
 def test_radardata01(self):
     """Invalid radar data (RI field) should result in RunningAve._radar_data()
        returning None."""
     rone = [('CS', '.iv'), ('EP', '000000000000000000001242'),
             ('RI', 'BLA'), ('OK', '')]
     for testlst in [rone]:
         testin = commlink.CLResponse(testlst)
         gotval = TLSAscii.RunningAve._radar_data(self.logger, testin)
         assert gotval is None, "expected None"
Beispiel #5
0
 def test_clresponse02(self) -> None:
     """Test CLResponse when the serial connection has timed
     out (return list is None)"""
     clresp = commlink.CLResponse(None)
     assert clresp is not None, "Failed to instantiate CLResponse"
     cslst = clresp["CS"]
     assert cslst is None, "Non expected"
     rc = clresp.return_code()
     assert rc == commlink.BaseCommLink.RC_TIMEOUT
     cmt_dct = clresp.get_comment_dct()
     assert cmt_dct is None, "expected None"
Beispiel #6
0
 def test_clresponse01(self):
     """Test the CLResponse class on a number of legit/ non legit
     RFID reader response strings."""
     csval = "iv. "
     test_lst = [("CS", csval), ("ME", "hello")]
     clresp = commlink.CLResponse(test_lst)
     assert clresp is not None, "Failed to instantiate CLResponse"
     cslst = clresp["CS"]
     assert isinstance(cslst, list), "List expected"
     assert len(cslst) == 1, "expected length 1"
     csgot = cslst[0]
     assert isinstance(csgot, str), "Str expected"
     assert csgot == csval, "CS mismatch"
Beispiel #7
0
 def test_runningave02(self):
     """Feeding RFID scan data into a TLSAscii.RunningAve instance should
     produce the expected average value."""
     ave_len = 2
     rone = [('CS', '.iv'), ('EP', '000000000000000000001242'),
             ('RI', '-65'), ('OK', '')]
     testin = commlink.CLResponse(rone)
     ra = TLSAscii.RunningAve(self.logger, ave_len)
     for i in range(2 * ave_len):
         ra.add_clresp(testin)
         assert len(ra._dlst) <= ave_len, "unexpected ave list"
         run_ave = ra.get_runningave()
         if i < ave_len - 1:
             assert run_ave is None, "running ave not None for i = {} < ave_len={}".format(
                 i, ave_len)
         else:
             assert run_ave is not None, "running ave is None for i> ave_len"
Beispiel #8
0
 def raw_read_response(self) -> commlink.CLResponse:
     rlst = []
     done = len(self.resplst) == 0
     while not done:
         cur_line = self.resplst.pop(0).strip()
         if len(cur_line) > 0:
             resp_tup = commlink.BaseCommLink._line_2_resptup(cur_line)
             if resp_tup is not None:
                 rlst.append(resp_tup)
             else:
                 self.logger.error(
                     "line_2_resptup failed '{}'".format(cur_line))
         else:
             # we have reached a 'terminal' message (OK or ER)
             done = True
         done = len(self.resplst) == 0
     # --
     return commlink.CLResponse(rlst)
Beispiel #9
0
    def test_convert_msg(self):
        "Test  _convert_message() with a number of legal and illegal RFID messages"
        rone = [('CS', '.iv'),
                ('EP', '000000000000000000001242'), ('RI', '-64'),
                ('EP', '000000000000000000001243'), ('RI', '-55'),
                ('EP', '000000000000000000001241'), ('RI', '-62'),
                ('EP', '000000000000000000001236'), ('RI', '-59'),
                ('EP', '000000000000000000001234'), ('RI', '-59'),
                ('EP', '000000000000000000001237'), ('RI', '-62'),
                ('EP', '000000000000000000001240'), ('RI', '-65'),
                ('EP', '000000000000000000001235'), ('RI', '-66'),
                ('EP', '000000000000000000001239'), ('RI', '-59'), ('OK', '')]
        len_rone = len(rone)
        rtwo = [('CS', '.bc'), ('ME', 'No barcode found'), ('ER', '006')]
        len_rtwo = len(rtwo)
        rthree = [('CS', '.iv'), ('ME', 'No Transponder found'), ('ER', '005')]
        len_rthree = len(rthree)
        # rfour: remove RI entries from rone.
        rfour = [t for t in rone if t[0] != 'RI']
        len_rfour = len(rfour)
        rfive = [('CS', '.iv A{"MSG":"31","CMT":"RAD"}B'),
                 ('EP', '000000000000000000001237'), ('RI', '-63'),
                 ('EP', '000000000000000000001235'), ('RI', '-60'),
                 ('EP', '000000000000000000001238'), ('RI', '-68'),
                 ('EP', '000000000000000000001239'), ('RI', '-70'),
                 ('EP', '000000000000000000001243'), ('RI', '-52'),
                 ('EP', '000000000000000000001236'), ('RI', '-60'),
                 ('EP', '000000000000000000001242'), ('RI', '-67'),
                 ('EP', '000000000000000000001234'), ('RI', '-66'), ('OK', '')]
        # len_rfive = len(rfive)
        rsix = [t for t in rfive if t[0] != 'RI']
        radar_mode = TLSAscii.TlsMode(TLSAscii.TlsMode.radar)
        stock_mode = TLSAscii.TlsMode(TLSAscii.TlsMode.stock)
        # undef_mode = TLSAscii.TlsMode(TLSAscii.TlsMode.undef)

        rad_dat = CommonMSG.MSG_RF_RADAR_DATA
        # stk_dat = CommonMSG.MSG_RF_STOCK_DATA
        stk_dat = CommonMSG.MSG_RF_CMD_RESP
        # cmd_dat = CommonMSG.MSG_RF_CMD_RESP
        testvals = [
            (rone, radar_mode, rad_dat, 9),
            (rone, stock_mode, stk_dat, len_rone),
            # (rone, undef_mode, cmd_dat, len(rone)),
            (rtwo, radar_mode, rad_dat, 0),
            (rtwo, stock_mode, stk_dat, len_rtwo),
            # (rtwo, undef_mode, cmd_dat, len(rtwo)),
            (rthree, radar_mode, rad_dat, 0),
            (rthree, stock_mode, stk_dat, len_rthree),
            # (rthree, undef_mode, cmd_dat, len(rthree)),
            (rfour, radar_mode, rad_dat, None),
            (rfour, stock_mode, stk_dat, len_rfour),
            # (rfour, undef_mode, cmd_dat, len(rfour)),
            (rfive, radar_mode, rad_dat, 8),
            (rfive, stock_mode, rad_dat, 8),
            # (rfive, undef_mode, rad_dat, 8),
            (rsix, radar_mode, rad_dat, None),
            (rsix, stock_mode, stk_dat, None)
        ]
        # (rsix, undef_mode, cmd_dat, None)]
        #
        # pass in messages with a radarsetup comment
        # one OK, one error message
        # ivcmd = '.iv A{"MSG":"31","CMT":"radarsetup"}B'
        # rad1 = [('CS', ivcmd), ('ME', 'No Transponder found'), ('ER', '005')]
        # rad2 = [('CS', ivcmd),
        #        ('EP', '000000000000000000001242'), ('RI', '-67'),
        #        ('EP', '000000000000000000001234'), ('RI', '-66'), ('OK', '')]
        # testvals.append((rad1, undef_mode, cmd_dat, 3))
        # testvals.append((rad2, undef_mode, rad_dat, None))

        #
        # pass in messages with an IVreset comment
        # ivcmd = '.iv A{"MSG":"31","CMT":"IVreset"}B'
        # rad1 = [('CS', ivcmd), ('ME', 'No Transponder found'), ('ER', '005')]
        # rad2 = [('CS', ivcmd),
        #        ('EP', '000000000000000000001242'), ('RI', '-67'),
        #        ('EP', '000000000000000000001234'), ('RI', '-66'), ('OK', '')]
        # testvals.append((rad1, undef_mode, cmd_dat, 3))
        # testvals.append((rad2, undef_mode, rad_dat, None))

        # pass in messages with an unknown commentstring
        # should return None in both cases
        # ivcmd = '.iv A{"MSG":"31","CMT":"WONKYCOMMENT"}B'
        # rad1 = [('CS', ivcmd), ('ME', 'No Transponder found'), ('ER', '005')]
        # rad2 = [('CS', ivcmd),
        #        ('EP', '000000000000000000001242'), ('RI', '-67'),
        #        ('EP', '000000000000000000001234'), ('RI', '-66'), ('OK', '')]
        # testvals.append((rad1, undef_mode, cmd_dat, None))
        # testvals.append((rad2, undef_mode, rad_dat, None))

        for testlst, test_mode, expected_msg, exp_val in testvals:
            print("\n\n-------------------------")
            testin = commlink.CLResponse(testlst)
            self.tls.mode = test_mode
            # we must reset the running average in order to avoid side effects...
            self.tls.runningave.reset_average()
            got_val = self.tls._convert_message(testin)
            if got_val is not None:
                assert isinstance(got_val,
                                  CommonMSG), "expected a CommonMSG instance"
            if exp_val is None:
                if got_val is not None:
                    print(
                        "got_val: inp: {}, mode: {}, exp_msg: {}, exp_val: {}".
                        format(testlst, test_mode, expected_msg, exp_val))
                    raise RuntimeError(
                        "expected None: testin {}, expected: {}, got: {}".
                        format(testin, exp_val, got_val))
            else:
                # we do a relaxed test of correctnes
                if got_val is None:
                    print("inp: {}, mode: {}, exp_msg: {}, exp_val: {}".format(
                        testlst, test_mode, expected_msg, exp_val))
                    raise RuntimeError('got None, expected {}'.format(exp_val))
                else:
                    dct = got_val.as_dict()
                    assert dct['msg'] == expected_msg, "unexpected msg type"
                    dat = dct['data']
                    if not isinstance(dat, list) or len(dat) != exp_val:
                        print("expected data as a list of length exp_val = {}".
                              format(exp_val))
                        print("data is '{}'".format(dat))
                        fmtstr = "inp: {},\n mode: {},\n exp_msg: {},\n exp_val(length): {}, actual length: {}"
                        print(
                            fmtstr.format(testlst, test_mode, expected_msg,
                                          exp_val, len(dat)))
                        raise RuntimeError("unexpected output")

        # an illegal TLS mode should raise an exception..
        # THis is no longer true... instead we expect None
        testin = commlink.CLResponse(rone)
        self.tls.mode = 'bla'
        # with pytest.raises(RuntimeError):
        resp = self.tls._convert_message(testin)
        if resp is not None:
            raise RuntimeError("expected None")