1
0
Fork 0
mirror of https://gitlab.com/bramw/baserow.git synced 2025-02-24 05:26:23 +00:00

Cannot concat Array and String in AB Formula

This commit is contained in:
Tsering Paljor 2024-12-12 13:31:30 +00:00
parent 727ab462a8
commit 50179828e4
5 changed files with 64 additions and 2 deletions
backend
changelog/entries/unreleased/bug
web-frontend
modules/core
test/unit/core/formula

View file

@ -4,13 +4,14 @@ from baserow.core.formula.argument_types import (
)
from baserow.core.formula.registries import RuntimeFormulaFunction
from baserow.core.formula.types import FormulaArgs, FormulaContext
from baserow.core.formula.validator import ensure_string
class RuntimeConcat(RuntimeFormulaFunction):
type = "concat"
def execute(self, context: FormulaContext, args: FormulaArgs):
return "".join([str(a) for a in args])
return "".join([ensure_string(a) for a in args])
def validate_number_of_args(self, args):
return len(args) >= 2

View file

@ -0,0 +1,32 @@
from unittest.mock import MagicMock
import pytest
from baserow.core.formula.runtime_formula_types import RuntimeConcat
@pytest.mark.parametrize(
"formula_args,expected",
[
(
[[["Apple", "Banana"]], "Cherry"],
"Apple,BananaCherry",
),
(
[[["Apple", "Banana"]], ",Cherry"],
"Apple,Banana,Cherry",
),
(
[[["Apple", "Banana"]], ", Cherry"],
"Apple,Banana, Cherry",
),
],
)
def test_returns_concatenated_value(formula_args, expected):
"""
Ensure that formula_args and non-formula strings are concatenated correctly.
"""
context = MagicMock()
result = RuntimeConcat().execute(context, formula_args)
assert result == expected

View file

@ -0,0 +1,7 @@
{
"type": "bug",
"message": "[Builder] Fixed a bug in the Create/Update row workflow action when concatenating a formula field and a string.",
"issue_number": 2546,
"bullet_points": [],
"created_at": "2024-12-12"
}

View file

@ -8,6 +8,7 @@ import {
InvalidNumberOfArguments,
} from '@baserow/modules/core/formula/parser/errors'
import { Node, VueNodeViewRenderer } from '@tiptap/vue-2'
import { ensureString } from '@baserow/modules/core/utils/validator'
import GetFormulaComponent from '@baserow/modules/core/components/formula/GetFormulaComponent'
import { mergeAttributes } from '@tiptap/core'
import _ from 'lodash'
@ -139,7 +140,7 @@ export class RuntimeConcat extends RuntimeFormulaFunction {
}
execute(context, args) {
return args.join('')
return args.map((arg) => ensureString(arg)).join('')
}
validateNumberOfArgs(args) {

View file

@ -0,0 +1,21 @@
import { RuntimeConcat } from '@baserow/modules/core/runtimeFormulaTypes'
import { expect } from '@jest/globals'
/** Tests for the RuntimeConcat class. */
describe('RuntimeConcat', () => {
test.each([
{ args: [[['Apple', 'Banana']], 'Cherry'], expected: 'Apple,BananaCherry' },
{
args: [[['Apple', 'Banana']], ',Cherry'],
expected: 'Apple,Banana,Cherry',
},
{
args: [[['Apple', 'Banana']], ', Cherry'],
expected: 'Apple,Banana, Cherry',
},
])('should concatenate the runtime args correctly', ({ args, expected }) => {
const runtimeConcat = new RuntimeConcat()
const result = runtimeConcat.execute({}, args)
expect(result).toBe(expected)
})
})