Skip to content

Commit 782a130

Browse files
authored
Tests for Advanced Formulas (baserow#4193)
* Initial batch of tests * Ensure castToInt/castToFloat is taken care of by parser for random int/float * Fix tests after rebase * Update Boolean arg type to use ensurer in test() * Add remaining tests for argument types * 'and' and 'or' should be operator types * Add 'and' and 'or' operators to formula visitor * Add frontend tests * Add integration tests for runtime formulas * Disable flakey test * Replace manual field name construction with db_column
1 parent b472bcc commit 782a130

File tree

11 files changed

+3722
-29
lines changed

11 files changed

+3722
-29
lines changed

backend/src/baserow/core/formula/argument_types.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ def parse(self, value):
2727
class NumberBaserowRuntimeFormulaArgumentType(BaserowRuntimeFormulaArgumentType):
2828
def __init__(self, *args, **kwargs):
2929
self.cast_to_int = kwargs.pop("cast_to_int", False)
30+
self.cast_to_float = kwargs.pop("cast_to_float", False)
3031
super().__init__(*args, **kwargs)
3132

3233
def test(self, value):
@@ -38,7 +39,12 @@ def test(self, value):
3839

3940
def parse(self, value):
4041
value = ensure_numeric(value)
41-
return int(value) if self.cast_to_int else value
42+
if self.cast_to_int:
43+
return int(value)
44+
elif self.cast_to_float:
45+
return float(value)
46+
else:
47+
return value
4248

4349

4450
class TextBaserowRuntimeFormulaArgumentType(BaserowRuntimeFormulaArgumentType):
@@ -79,7 +85,11 @@ def parse(self, value):
7985

8086
class BooleanBaserowRuntimeFormulaArgumentType(BaserowRuntimeFormulaArgumentType):
8187
def test(self, value):
82-
return isinstance(value, bool)
88+
try:
89+
ensure_boolean(value)
90+
return True
91+
except ValidationError:
92+
return False
8393

8494
def parse(self, value):
8595
return ensure_boolean(value)

backend/src/baserow/core/formula/runtime_formula_types.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -345,24 +345,24 @@ class RuntimeRandomInt(RuntimeFormulaFunction):
345345
type = "random_int"
346346

347347
args = [
348-
NumberBaserowRuntimeFormulaArgumentType(),
349-
NumberBaserowRuntimeFormulaArgumentType(),
348+
NumberBaserowRuntimeFormulaArgumentType(cast_to_int=True),
349+
NumberBaserowRuntimeFormulaArgumentType(cast_to_int=True),
350350
]
351351

352352
def execute(self, context: FormulaContext, args: FormulaArgs):
353-
return random.randint(int(args[0]), int(args[1])) # nosec: B311
353+
return random.randint(args[0], args[1]) # nosec: B311
354354

355355

356356
class RuntimeRandomFloat(RuntimeFormulaFunction):
357357
type = "random_float"
358358

359359
args = [
360-
NumberBaserowRuntimeFormulaArgumentType(),
361-
NumberBaserowRuntimeFormulaArgumentType(),
360+
NumberBaserowRuntimeFormulaArgumentType(cast_to_float=True),
361+
NumberBaserowRuntimeFormulaArgumentType(cast_to_float=True),
362362
]
363363

364364
def execute(self, context: FormulaContext, args: FormulaArgs):
365-
return random.uniform(float(args[0]), float(args[1])) # nosec: B311
365+
return random.uniform(args[0], args[1]) # nosec: B311
366366

367367

368368
class RuntimeRandomBool(RuntimeFormulaFunction):

backend/src/baserow/test_utils/helpers.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -624,6 +624,20 @@ def __eq__(self, other):
624624
return isinstance(other, int)
625625

626626

627+
class AnyFloat(float):
628+
"""A class that can be used to check if a value is a float."""
629+
630+
def __eq__(self, other):
631+
return isinstance(other, float)
632+
633+
634+
class AnyBool:
635+
"""A class that can be used to check if a value is a boolean."""
636+
637+
def __eq__(self, other):
638+
return isinstance(other, bool)
639+
640+
627641
class AnyStr(str):
628642
"""
629643
A class that can be used to check if a value is an str. Useful in tests when

0 commit comments

Comments
 (0)