Get existing tests passing.

This commit is contained in:
Sam Rose 2024-10-17 17:16:54 +01:00
parent cb41861d13
commit 17e946845e
No known key found for this signature in database
4 changed files with 1280 additions and 1233 deletions

File diff suppressed because it is too large Load Diff

View File

@ -1,8 +1,13 @@
import { ViewV2 } from "@budibase/types" import { ViewV2 } from "@budibase/types"
import { utils, dataFilters } from "@budibase/shared-core" import { utils, dataFilters } from "@budibase/shared-core"
import { isPlainObject } from "lodash"
function isEmptyObject(obj: any) {
return obj && isPlainObject(obj) && Object.keys(obj).length === 0
}
export function ensureQueryUISet(view: ViewV2) { export function ensureQueryUISet(view: ViewV2) {
if (!view.queryUI && view.query) { if (!view.queryUI && view.query && !isEmptyObject(view.query)) {
if (!Array.isArray(view.query)) { if (!Array.isArray(view.query)) {
// In practice this should not happen. `view.query`, at the time this code // In practice this should not happen. `view.query`, at the time this code
// goes into the codebase, only contains LegacyFilter[] in production. // goes into the codebase, only contains LegacyFilter[] in production.
@ -24,7 +29,7 @@ export function ensureQueryUISet(view: ViewV2) {
} }
export function ensureQuerySet(view: ViewV2) { export function ensureQuerySet(view: ViewV2) {
if (!view.query && view.queryUI) { if (!view.query && view.queryUI && !isEmptyObject(view.queryUI)) {
view.query = dataFilters.buildQuery(view.queryUI) view.query = dataFilters.buildQuery(view.queryUI)
} }
} }

View File

@ -10,7 +10,9 @@ import { Expectations, TestAPI } from "./base"
export class ViewV2API extends TestAPI { export class ViewV2API extends TestAPI {
create = async ( create = async (
view: CreateViewRequest, // The frontend changed in v3 from sending query to sending only queryUI,
// making sure tests reflect that.
view: Omit<CreateViewRequest, "query">,
expectations?: Expectations expectations?: Expectations
): Promise<ViewV2> => { ): Promise<ViewV2> => {
const exp: Expectations = { const exp: Expectations = {

View File

@ -319,9 +319,6 @@ const buildCondition = (expression: LegacyFilter) => {
return return
} }
const isHbs =
typeof value === "string" && (value.match(HBS_REGEX) || []).length > 0
if (operator === "allOr") { if (operator === "allOr") {
query.allOr = true query.allOr = true
return return
@ -338,40 +335,45 @@ const buildCondition = (expression: LegacyFilter) => {
value = null value = null
} }
if ( const isHbs =
type === "datetime" && typeof value === "string" && (value.match(HBS_REGEX) || []).length > 0
!isHbs &&
operator !== "empty" && // Parsing value depending on what the type is.
operator !== "notEmpty" switch (type) {
) { case FieldType.DATETIME:
// Ensure date value is a valid date and parse into correct format if (!isHbs && operator !== "empty" && operator !== "notEmpty") {
if (!value) { if (!value) {
return return
} }
try { try {
value = new Date(value).toISOString() value = new Date(value).toISOString()
} catch (error) { } catch (error) {
return return
} }
} }
if (type === "number" && typeof value === "string" && !isHbs) { break
if (operator === "oneOf") { case FieldType.NUMBER:
value = value.split(",").map(item => parseFloat(item)) if (typeof value === "string" && !isHbs) {
} else { if (operator === "oneOf") {
value = parseFloat(value) value = value.split(",").map(parseFloat)
} } else {
} value = parseFloat(value)
if (type === "boolean") { }
value = `${value}`?.toLowerCase() === "true" }
} break
if ( case FieldType.BOOLEAN:
["contains", "notContains", "containsAny"].includes( value = `${value}`.toLowerCase() === "true"
operator.toLocaleString() break
) && case FieldType.ARRAY:
type === "array" && if (
typeof value === "string" ["contains", "notContains", "containsAny"].includes(
) { operator.toLocaleString()
value = value.split(",") ) &&
typeof value === "string"
) {
value = value.split(",")
}
break
} }
if (isRangeSearchOperator(operator)) { if (isRangeSearchOperator(operator)) {
@ -398,17 +400,17 @@ const buildCondition = (expression: LegacyFilter) => {
...query.range[field], ...query.range[field],
low: value, low: value,
} }
} else if (isLogicalSearchOperator(operator)) {
// TODO
} else if ( } else if (
isBasicSearchOperator(operator) || isBasicSearchOperator(operator) ||
isArraySearchOperator(operator) || isArraySearchOperator(operator) ||
isRangeSearchOperator(operator) isRangeSearchOperator(operator)
) { ) {
if (type === "boolean") { if (type === "boolean") {
// Transform boolean filters to cope with null. // TODO(samwho): I suspect this boolean transformation isn't needed anymore,
// "equals false" needs to be "not equals true" // write some tests to confirm.
// "not equals false" needs to be "equals true"
// Transform boolean filters to cope with null. "equals false" needs to
// be "not equals true" "not equals false" needs to be "equals true"
if (operator === "equal" && value === false) { if (operator === "equal" && value === false) {
query.notEqual = query.notEqual || {} query.notEqual = query.notEqual || {}
query.notEqual[field] = true query.notEqual[field] = true
@ -423,6 +425,8 @@ const buildCondition = (expression: LegacyFilter) => {
query[operator] ??= {} query[operator] ??= {}
query[operator][field] = value query[operator][field] = value
} }
} else {
throw new Error(`Unsupported operator: ${operator}`)
} }
return query return query