Get existing tests passing.
This commit is contained in:
parent
cb41861d13
commit
17e946845e
File diff suppressed because it is too large
Load Diff
|
@ -1,8 +1,13 @@
|
|||
import { ViewV2 } from "@budibase/types"
|
||||
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) {
|
||||
if (!view.queryUI && view.query) {
|
||||
if (!view.queryUI && view.query && !isEmptyObject(view.query)) {
|
||||
if (!Array.isArray(view.query)) {
|
||||
// In practice this should not happen. `view.query`, at the time this code
|
||||
// goes into the codebase, only contains LegacyFilter[] in production.
|
||||
|
@ -24,7 +29,7 @@ export function ensureQueryUISet(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)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -10,7 +10,9 @@ import { Expectations, TestAPI } from "./base"
|
|||
|
||||
export class ViewV2API extends TestAPI {
|
||||
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
|
||||
): Promise<ViewV2> => {
|
||||
const exp: Expectations = {
|
||||
|
|
|
@ -319,9 +319,6 @@ const buildCondition = (expression: LegacyFilter) => {
|
|||
return
|
||||
}
|
||||
|
||||
const isHbs =
|
||||
typeof value === "string" && (value.match(HBS_REGEX) || []).length > 0
|
||||
|
||||
if (operator === "allOr") {
|
||||
query.allOr = true
|
||||
return
|
||||
|
@ -338,40 +335,45 @@ const buildCondition = (expression: LegacyFilter) => {
|
|||
value = null
|
||||
}
|
||||
|
||||
if (
|
||||
type === "datetime" &&
|
||||
!isHbs &&
|
||||
operator !== "empty" &&
|
||||
operator !== "notEmpty"
|
||||
) {
|
||||
// Ensure date value is a valid date and parse into correct format
|
||||
if (!value) {
|
||||
return
|
||||
}
|
||||
try {
|
||||
value = new Date(value).toISOString()
|
||||
} catch (error) {
|
||||
return
|
||||
}
|
||||
}
|
||||
if (type === "number" && typeof value === "string" && !isHbs) {
|
||||
if (operator === "oneOf") {
|
||||
value = value.split(",").map(item => parseFloat(item))
|
||||
} else {
|
||||
value = parseFloat(value)
|
||||
}
|
||||
}
|
||||
if (type === "boolean") {
|
||||
value = `${value}`?.toLowerCase() === "true"
|
||||
}
|
||||
if (
|
||||
["contains", "notContains", "containsAny"].includes(
|
||||
operator.toLocaleString()
|
||||
) &&
|
||||
type === "array" &&
|
||||
typeof value === "string"
|
||||
) {
|
||||
value = value.split(",")
|
||||
const isHbs =
|
||||
typeof value === "string" && (value.match(HBS_REGEX) || []).length > 0
|
||||
|
||||
// Parsing value depending on what the type is.
|
||||
switch (type) {
|
||||
case FieldType.DATETIME:
|
||||
if (!isHbs && operator !== "empty" && operator !== "notEmpty") {
|
||||
if (!value) {
|
||||
return
|
||||
}
|
||||
try {
|
||||
value = new Date(value).toISOString()
|
||||
} catch (error) {
|
||||
return
|
||||
}
|
||||
}
|
||||
break
|
||||
case FieldType.NUMBER:
|
||||
if (typeof value === "string" && !isHbs) {
|
||||
if (operator === "oneOf") {
|
||||
value = value.split(",").map(parseFloat)
|
||||
} else {
|
||||
value = parseFloat(value)
|
||||
}
|
||||
}
|
||||
break
|
||||
case FieldType.BOOLEAN:
|
||||
value = `${value}`.toLowerCase() === "true"
|
||||
break
|
||||
case FieldType.ARRAY:
|
||||
if (
|
||||
["contains", "notContains", "containsAny"].includes(
|
||||
operator.toLocaleString()
|
||||
) &&
|
||||
typeof value === "string"
|
||||
) {
|
||||
value = value.split(",")
|
||||
}
|
||||
break
|
||||
}
|
||||
|
||||
if (isRangeSearchOperator(operator)) {
|
||||
|
@ -398,17 +400,17 @@ const buildCondition = (expression: LegacyFilter) => {
|
|||
...query.range[field],
|
||||
low: value,
|
||||
}
|
||||
} else if (isLogicalSearchOperator(operator)) {
|
||||
// TODO
|
||||
} else if (
|
||||
isBasicSearchOperator(operator) ||
|
||||
isArraySearchOperator(operator) ||
|
||||
isRangeSearchOperator(operator)
|
||||
) {
|
||||
if (type === "boolean") {
|
||||
// Transform boolean filters to cope with null.
|
||||
// "equals false" needs to be "not equals true"
|
||||
// "not equals false" needs to be "equals true"
|
||||
// TODO(samwho): I suspect this boolean transformation isn't needed anymore,
|
||||
// write some tests to confirm.
|
||||
|
||||
// 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) {
|
||||
query.notEqual = query.notEqual || {}
|
||||
query.notEqual[field] = true
|
||||
|
@ -423,6 +425,8 @@ const buildCondition = (expression: LegacyFilter) => {
|
|||
query[operator] ??= {}
|
||||
query[operator][field] = value
|
||||
}
|
||||
} else {
|
||||
throw new Error(`Unsupported operator: ${operator}`)
|
||||
}
|
||||
|
||||
return query
|
||||
|
|
Loading…
Reference in New Issue