Commit 697f49d5 authored by Michael Hanselmann's avatar Michael Hanselmann
Browse files

ht: Add new check for numbers



Places which receive floats can usually also deal with integers, e.g.
OpTestDelay. Tests are added and the new check function is used for the
aforementioned opcode and verifying query results.
Signed-off-by: default avatarMichael Hanselmann <hansmi@google.com>
Reviewed-by: default avatarIustin Pop <iustin@google.com>
parent 3c88bf36
......@@ -178,6 +178,9 @@ TPositiveInt = TAnd(TInt, lambda v: v >= 0)
#: a strictly positive integer
TStrictPositiveInt = TAnd(TInt, lambda v: v > 0)
#: Number
TNumber = TOr(TInt, TFloat)
def TListOf(my_type):
"""Checks if a given value is a list with all elements of the same type.
......
......@@ -1206,7 +1206,7 @@ class OpTestDelay(OpCode):
"""
OP_DSC_FIELD = "duration"
OP_PARAMS = [
("duration", ht.NoDefault, ht.TFloat),
("duration", ht.NoDefault, ht.TNumber),
("on_master", True, ht.TBool),
("on_nodes", ht.EmptyList, ht.TListOf(ht.TNonEmptyString)),
("repeat", 0, ht.TPositiveInt)
......
......@@ -101,7 +101,7 @@ _VERIFY_FN = {
QFT_BOOL: ht.TBool,
QFT_NUMBER: ht.TInt,
QFT_UNIT: ht.TInt,
QFT_TIMESTAMP: ht.TOr(ht.TInt, ht.TFloat),
QFT_TIMESTAMP: ht.TNumber,
QFT_OTHER: lambda _: True,
}
......
......@@ -53,6 +53,7 @@ class TestTypeChecks(unittest.TestCase):
def testInt(self):
for val in [-100, -3, 0, 16, 128, 923874]:
self.assertTrue(ht.TInt(val))
self.assertTrue(ht.TNumber(val))
for val in [False, True, None, "", [], "Hello", 0.0, 0.23, -3818.163]:
self.assertFalse(ht.TInt(val))
......@@ -76,10 +77,19 @@ class TestTypeChecks(unittest.TestCase):
def testFloat(self):
for val in [-100.21, -3.0, 0.0, 16.12, 128.3433, 923874.928]:
self.assertTrue(ht.TFloat(val))
self.assertTrue(ht.TNumber(val))
for val in [False, True, None, "", [], "Hello", 0, 28, -1, -3281]:
self.assertFalse(ht.TFloat(val))
def testNumber(self):
for val in [-100, -3, 0, 16, 128, 923874,
-100.21, -3.0, 0.0, 16.12, 128.3433, 923874.928]:
self.assertTrue(ht.TNumber(val))
for val in [False, True, None, "", [], "Hello", "1"]:
self.assertFalse(ht.TNumber(val))
def testString(self):
for val in ["", "abc", "Hello World", "123",
u"", u"\u272C", u"abc"]:
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment