コード例 #1
0
    def test_match(self):

        parser = QueryLanguage()

        #------------------------------------------------------------------------------------------------------
        # Check that when field is outside range (less than lower bound), match() returns false
        #------------------------------------------------------------------------------------------------------

        field = 'voltage'
        lower_bound = 5
        upper_bound = 10
        instrument = 'instrument_1'
        search_string1 = "SEARCH '%s' VALUES FROM %s TO %s FROM '%s'"\
        % (field, lower_bound, upper_bound, instrument)
        query = parser.parse(search_string1)

        event = ExampleDetectableEvent('TestEvent', voltage=4)
        self.assertFalse(QueryLanguage.match(event, query['query']))

        #------------------------------------------------------------------------------------------------------
        # Check that when field is outside range (is higher than upper bound), match() returns false
        #------------------------------------------------------------------------------------------------------

        event = ExampleDetectableEvent('TestEvent', voltage=11)
        self.assertFalse(QueryLanguage.match(event, query['query']))

        #------------------------------------------------------------------------------------------------------
        # Check that when field is inside range, match() returns true
        #------------------------------------------------------------------------------------------------------

        event = ExampleDetectableEvent('TestEvent', voltage=6)
        self.assertTrue(QueryLanguage.match(event, query['query']))

        #------------------------------------------------------------------------------------------------------
        # Check that when field is exactly of the value mentioned in a query, match() returns true
        #------------------------------------------------------------------------------------------------------

        value = 15
        search_string2 = "search '%s' is '%s' from '%s'" % (field, value, instrument)
        query = parser.parse(search_string2)

        event = ExampleDetectableEvent('TestEvent', voltage=15)
        self.assertTrue(QueryLanguage.match(event, query['query']))

        #------------------------------------------------------------------------------------------------------
        # Check that when value is not exactly what is mentioned in a query, match() returns false
        #------------------------------------------------------------------------------------------------------

        event = ExampleDetectableEvent('TestEvent', voltage=14)
        self.assertFalse(QueryLanguage.match(event, query['query']))