def testTrySubscribeFail(self, init_sub, get_class, get_type, ns, get_param):
        tm = TriggerMonitor() 
        tm.load_config()
          
        assert tm.try_subscribe("var1") == False

        init_sub.assert_not_called()
 def testNoPrevChange(self, get_param, ns):
     tm = TriggerMonitor()
     tm.load_config()
     tm.exporting = True
     tm.variable_changed("test", "var1")
     assert tm.prev_values["var1"] == "test"
     assert "var1" not in tm.prev_change
     assert len(tm.prev_values.items()) == 1
    def testTrySubscribeSucceed(self, init_sub, get_class, get_type, ns, get_param):
        tm = TriggerMonitor() 
        tm.load_config()
          
        assert tm.try_subscribe("var1") == True

        init_sub.assert_called_once()
        assert init_sub.call_args[0][0] == 'var1'
    def testSubscribe(self, sleep, shutdown, sub, rate, ns, get_param):
        tm = TriggerMonitor() 
        tm.load_config()
        TestTriggerMonitor.is_shutdown = 4

        tm.subscribe('var1')
          
        assert sleep.call_count == 4
        sub.assert_has_calls([call('var1'), call('var1')])
    def testExportPublishUploadCalled(self, export, sp, upload, publish, ns, get_param):
        tm = TriggerMonitor() 
        tm.load_config()
          
        tm.export("test", "var1")

        publish.assert_called_once()
        publish.assert_has_calls([call('test', 'var1', 'test', 0, 5)])

        upload.assert_called_once()
        upload.assert_has_calls([call('test', 'test', 'var1')])
    def testConditionSyntaxError(self, log, ns, get_param):
        tm = TriggerMonitor()
        tm.load_config()
        log.reset_mock()
        tm.conditions["var1"] = "a.b.c"
        
        tm.variable_changed(1, "var1")

        log.assert_called_once()
        log.assert_has_calls([call('w', "<type 'exceptions.NameError'> name 'a' is not defined")])
        assert tm.exporting == False
    def testConditionTrue(self, export, ns, get_param):
        tm = TriggerMonitor()
        tm.load_config()
        tm.conditions["var1"] = "data > 2 and data < 5"
        tm.trigger_delay = 0
        
        for i in range(6):
            tm.variable_changed(i, "var1")

        assert export.call_count == 2
        assert tm.exporting == False
    def testLoadConfig(self, ns, get_param, logwarn, logerr):
        tm = TriggerMonitor()
        tm.load_config()
        assert tm.conditions == {}
        assert tm.on_change_only == False
        assert tm.trigger_delay == 1000
        assert tm.bag_from == 5
        assert tm.bag_to == 5
        assert tm.bag_path == '/ns/bag_path:None'

        logwarn.assert_not_called()
        logerr.assert_not_called()
    def testTriggerDelayLess(self, get_param, ns):
        tm = TriggerMonitor()
        tm.load_config()
        tm.on_change_only = True
        tm.trigger_delay = 100
        
        tm.variable_changed("test", "var1")
        t1 = tm.prev_change["var1"]
        tm.variable_changed("test2", "var1")

        assert tm.prev_values["var1"] == "test2"
        assert tm.prev_change["var1"] == t1
 def testNoPrevChange(self, get_param, ns):
     tm = TriggerMonitor()
     tm.load_config()
     assert tm.exporting == False
     assert tm.on_change_only == False
     
     tm.variable_changed("test", "var1")
     tm.variable_changed("test", "var2")
     assert tm.prev_values["var1"] == "test"
     assert tm.prev_values["var2"] == "test"
     assert tm.prev_change["var1"] != None
     assert tm.prev_change["var2"] != None
     assert len(tm.prev_values.items()) == 2
    def testLoadConfig_RecommendedMissing(self, ns, get_param, logwarn, logerr, _exit):
        tm = TriggerMonitor()
        tm.load_config()

        logwarn.assert_not_called()
        logerr.assert_not_called()
        _exit.assert_not_called()

        logwarn.reset_mock()
        TestTriggerMonitor.missing_key = "/ns/bag_path"
        tm.load_config()

        calls = [call('[BBTM] Mandatory config item not set: bag_path'), call('[BBTM] Mandatory config item(s) missing, exiting.')]
        logerr.assert_has_calls(calls)
        logwarn.assert_not_called()
        _exit.assert_called_once()
    def testNoPrevChange(self, get_param, ns):
        tm = TriggerMonitor()
        tm.load_config()
        tm.on_change_only = True
        tm.trigger_delay = 0
        
        tm.variable_changed("test", "var1")
        tm.variable_changed("test", "var2")

        t1 = tm.prev_change["var1"]
        t2 = tm.prev_change["var2"]

        tm.variable_changed("test2", "var1")
        tm.variable_changed("test", "var2")

        assert tm.prev_values["var1"] == "test2"
        assert tm.prev_values["var2"] == "test"
        assert tm.prev_change["var2"] == t2
        assert tm.prev_change["var1"] != t1