mirror of
https://github.com/baserow/baserow.git
synced 2026-12-05 08:48:35 +00:00
* fix(database): skip filters on unknown fields when matching rows Matching rows client-side threw when a view filter referenced a field missing from the field list, which broke adding, editing and realtime updates of rows. * fix(database): clarify skipped filter comment and changelog * fix(database): ignore groups without checkable filters in OR matching A group whose filters were all skipped made its OR parent match, unlike the backend, which ignores empty groups.
492 lines
14 KiB
JavaScript
492 lines
14 KiB
JavaScript
import {
|
|
TreeGroupNode,
|
|
canRowsBeOptimisticallyUpdatedInView,
|
|
createFiltersTree,
|
|
getOrderBy,
|
|
matchSearchFilters,
|
|
reportHiddenRows,
|
|
serializeGroupBys,
|
|
} from '@baserow/modules/database/utils/view'
|
|
import { TestApp } from '@baserow/test/helpers/testApp'
|
|
import _ from 'lodash'
|
|
import { vi } from 'vitest'
|
|
|
|
describe('TreeGroupNode', () => {
|
|
it('should initialize correctly', () => {
|
|
const node = new TreeGroupNode('AND')
|
|
expect(node.filterType).toBe('AND')
|
|
expect(node.parent).toBeNull()
|
|
expect(node.filters).toEqual([])
|
|
expect(node.children).toEqual([])
|
|
})
|
|
|
|
it('should add child nodes', () => {
|
|
const parentNode = new TreeGroupNode('AND')
|
|
const childNode = new TreeGroupNode('OR', parentNode)
|
|
expect(parentNode.children[0]).toBe(childNode)
|
|
})
|
|
})
|
|
|
|
describe('createFiltersTree', () => {
|
|
it('should create a tree with root node', () => {
|
|
const rootNode = createFiltersTree('AND', [], [])
|
|
expect(rootNode.filterType).toBe('AND')
|
|
expect(rootNode.hasFilters()).toBe(false)
|
|
expect(rootNode.filters).toEqual([])
|
|
expect(rootNode.children).toEqual([])
|
|
})
|
|
|
|
it('should correctly add filters to the tree', () => {
|
|
const filters = [
|
|
{ field: 1, type: 'type1', value: 'value1', group: 1 },
|
|
{ field: 2, type: 'type2', value: 'value2', group: 2 },
|
|
{ field: 3, type: 'type3', value: 'value3' },
|
|
]
|
|
const filterGroups = [
|
|
{ id: 1, filter_type: 'AND' },
|
|
{ id: 2, filter_type: 'OR' },
|
|
{ id: 3, filter_type: 'AND' },
|
|
]
|
|
const rootNode = createFiltersTree('AND', filters, filterGroups)
|
|
expect(rootNode.filterType).toBe('AND')
|
|
expect(rootNode.hasFilters()).toBe(true)
|
|
expect(rootNode.filters).toEqual([
|
|
{ field: 3, type: 'type3', value: 'value3' },
|
|
])
|
|
expect(rootNode.children).toHaveLength(3)
|
|
expect(rootNode.children[0].filterType).toBe('AND')
|
|
expect(rootNode.children[0].filters).toEqual([
|
|
{ field: 1, type: 'type1', value: 'value1', group: 1 },
|
|
])
|
|
expect(rootNode.children[0].children).toEqual([])
|
|
expect(rootNode.children[1].filterType).toBe('OR')
|
|
expect(rootNode.children[1].filters).toEqual([
|
|
{ field: 2, type: 'type2', value: 'value2', group: 2 },
|
|
])
|
|
expect(rootNode.children[2].filters).toEqual([])
|
|
expect(rootNode.children[2].hasFilters()).toBe(false)
|
|
})
|
|
|
|
it('should correctly nest groups into the tree', () => {
|
|
const filterGroups = [
|
|
{ id: 1, filter_type: 'AND' },
|
|
{ id: 2, filter_type: 'OR', parent_group: 1 },
|
|
{ id: 3, filter_type: 'AND', parent_group: 1 },
|
|
{ id: 4, filter_type: 'OR', parent_group: 3 },
|
|
]
|
|
const rootNode = createFiltersTree('AND', [], filterGroups)
|
|
expect(rootNode.hasFilters()).toBe(false)
|
|
expect(rootNode.children).toHaveLength(1)
|
|
expect(rootNode.children[0].filterType).toBe('AND')
|
|
expect(rootNode.children[0].children).toHaveLength(2)
|
|
expect(rootNode.children[0].children[0].filterType).toBe('OR')
|
|
expect(rootNode.children[0].children[0].children).toEqual([])
|
|
expect(rootNode.children[0].children[1].filterType).toBe('AND')
|
|
expect(rootNode.children[0].children[1].children).toHaveLength(1)
|
|
expect(rootNode.children[0].children[1].children[0].filterType).toBe('OR')
|
|
expect(rootNode.children[0].children[1].children[0].children).toEqual([])
|
|
})
|
|
|
|
it('should correctly add filters to nested groups', () => {
|
|
const filterGroups = [
|
|
{ id: 1, filter_type: 'AND' },
|
|
{ id: 2, filter_type: 'OR', parent_group: 1 },
|
|
{ id: 3, filter_type: 'OR', parent_group: 2 },
|
|
]
|
|
const rootNode = createFiltersTree('AND', [], filterGroups)
|
|
expect(rootNode.hasFilters()).toBe(false)
|
|
rootNode.children[0].children[0].children[0].addFilter({
|
|
field: 1,
|
|
type: 'type1',
|
|
value: 'value1',
|
|
group: 4,
|
|
})
|
|
expect(rootNode.hasFilters()).toBe(true)
|
|
})
|
|
})
|
|
|
|
describe('matchSearchFilters', () => {
|
|
let testApp = null
|
|
let registry = null
|
|
|
|
beforeEach(() => {
|
|
testApp = new TestApp()
|
|
registry = testApp.getRegistry()
|
|
})
|
|
|
|
afterEach(async () => {
|
|
await testApp.afterEach()
|
|
})
|
|
|
|
it('should return true with no filters', () => {
|
|
const filters = []
|
|
const filterGroups = []
|
|
const fields = {}
|
|
const rowValues = {}
|
|
expect(
|
|
matchSearchFilters(
|
|
registry,
|
|
'AND',
|
|
filters,
|
|
filterGroups,
|
|
fields,
|
|
rowValues
|
|
)
|
|
).toBe(true)
|
|
})
|
|
|
|
it('should match a single filter', () => {
|
|
const filters = [{ field: 1, type: 'equal', value: 'a' }]
|
|
const filterGroups = []
|
|
const fields = [{ id: 1, type: 'text' }]
|
|
expect(
|
|
matchSearchFilters(registry, 'AND', filters, filterGroups, fields, {
|
|
field_1: 'a',
|
|
})
|
|
).toBe(true)
|
|
|
|
expect(
|
|
matchSearchFilters(registry, 'AND', filters, filterGroups, fields, {
|
|
field_1: 'b',
|
|
})
|
|
).toBe(false)
|
|
})
|
|
|
|
it('should match a single filter in a group', () => {
|
|
const filters = [{ field: 1, type: 'equal', value: 'a', group: 1 }]
|
|
const filterGroups = [{ filter_type: 'OR', id: 1 }]
|
|
const fields = [{ id: 1, type: 'text' }]
|
|
expect(
|
|
matchSearchFilters(registry, 'AND', filters, filterGroups, fields, {
|
|
field_1: 'a',
|
|
})
|
|
).toBe(true)
|
|
expect(
|
|
matchSearchFilters(registry, 'AND', filters, filterGroups, fields, {
|
|
field_1: 'b',
|
|
})
|
|
).toBe(false)
|
|
})
|
|
|
|
it('should skip a filter on a field that is not in the fields', () => {
|
|
const filters = [
|
|
{ field: 1, type: 'equal', value: 'a' },
|
|
{ field: 2, type: 'equal', value: 'x' },
|
|
]
|
|
const fields = [{ id: 1, type: 'text' }]
|
|
expect(
|
|
matchSearchFilters(registry, 'AND', filters, [], fields, {
|
|
field_1: 'a',
|
|
})
|
|
).toBe(true)
|
|
expect(
|
|
matchSearchFilters(registry, 'AND', filters, [], fields, {
|
|
field_1: 'b',
|
|
})
|
|
).toBe(false)
|
|
})
|
|
|
|
it('should not let a group with only unknown-field filters satisfy an OR', () => {
|
|
const filters = [
|
|
{ field: 1, type: 'equal', value: 'a' },
|
|
{ field: 2, type: 'equal', value: 'x', group: 1 },
|
|
]
|
|
const filterGroups = [{ filter_type: 'AND', id: 1 }]
|
|
const fields = [{ id: 1, type: 'text' }]
|
|
expect(
|
|
matchSearchFilters(registry, 'OR', filters, filterGroups, fields, {
|
|
field_1: 'b',
|
|
})
|
|
).toBe(false)
|
|
expect(
|
|
matchSearchFilters(registry, 'OR', filters, filterGroups, fields, {
|
|
field_1: 'a',
|
|
})
|
|
).toBe(true)
|
|
})
|
|
|
|
it('should match filters correctly with OR and AND', () => {
|
|
// Match rows where field_1=Ada OR (field_1=John AND field_2=Turing)
|
|
const filters = [
|
|
{ field: 1, type: 'equal', value: 'Ada' },
|
|
{ field: 1, type: 'equal', value: 'John', group: 1 },
|
|
{ field: 2, type: 'equal', value: 'Turing', group: 1 },
|
|
]
|
|
const filterGroups = [{ filter_type: 'AND', id: 1 }]
|
|
const fields = [
|
|
{ id: 1, type: 'text' },
|
|
{ id: 2, type: 'text' },
|
|
]
|
|
|
|
expect(
|
|
matchSearchFilters(registry, 'OR', filters, filterGroups, fields, {
|
|
field_1: 'Ada',
|
|
field_2: 'Lovelace',
|
|
})
|
|
).toBe(true)
|
|
|
|
expect(
|
|
matchSearchFilters(registry, 'OR', filters, filterGroups, fields, {
|
|
field_1: 'Alan',
|
|
field_2: 'Turing',
|
|
})
|
|
).toBe(false)
|
|
|
|
expect(
|
|
matchSearchFilters(registry, 'OR', filters, filterGroups, fields, {
|
|
field_1: 'John',
|
|
field_2: 'Travolta',
|
|
})
|
|
).toBe(false)
|
|
|
|
expect(
|
|
matchSearchFilters(registry, 'OR', filters, filterGroups, fields, {
|
|
field_1: 'John',
|
|
field_2: 'Turing',
|
|
})
|
|
).toBe(true)
|
|
})
|
|
|
|
it('should match filters correctly with AND and OR', () => {
|
|
// Match rows where field_1=John AND (field_2=Travolta OR field_2=Turing)
|
|
const filters = [
|
|
{ field: 1, type: 'equal', value: 'John' },
|
|
{ field: 2, type: 'equal', value: 'Travolta', group: 1 },
|
|
{ field: 2, type: 'equal', value: 'Turing', group: 1 },
|
|
]
|
|
const filterGroups = [{ filter_type: 'OR', id: 1 }]
|
|
const fields = [
|
|
{ id: 1, type: 'text' },
|
|
{ id: 2, type: 'text' },
|
|
]
|
|
|
|
expect(
|
|
matchSearchFilters(registry, 'AND', filters, filterGroups, fields, {
|
|
field_1: 'John',
|
|
field_2: 'Lennon',
|
|
})
|
|
).toBe(false)
|
|
|
|
expect(
|
|
matchSearchFilters(registry, 'AND', filters, filterGroups, fields, {
|
|
field_1: 'Alan',
|
|
field_2: 'Turing',
|
|
})
|
|
).toBe(false)
|
|
|
|
expect(
|
|
matchSearchFilters(registry, 'AND', filters, filterGroups, fields, {
|
|
field_1: 'John',
|
|
field_2: 'Travolta',
|
|
})
|
|
).toBe(true)
|
|
|
|
expect(
|
|
matchSearchFilters(registry, 'AND', filters, filterGroups, fields, {
|
|
field_1: 'John',
|
|
field_2: 'Turing',
|
|
})
|
|
).toBe(true)
|
|
})
|
|
|
|
it('should serialize the filter tree correctly', () => {
|
|
const filters = [
|
|
{ field: 1, type: 'equal', value: 'a' },
|
|
{ field: 2, type: 'equal', value: 'b', group: 1 },
|
|
{ field: 3, type: 'equal', value: 'c', group: 1 },
|
|
]
|
|
const filterGroups = [{ filter_type: 'OR', id: 1 }]
|
|
const rootNode = createFiltersTree('AND', filters, filterGroups)
|
|
expect(
|
|
_.isEqual(rootNode.getFiltersTreeSerialized(), {
|
|
filter_type: 'AND',
|
|
filters: [{ type: 'equal', field: 1, value: 'a' }],
|
|
groups: [
|
|
{
|
|
filter_type: 'OR',
|
|
filters: [
|
|
{ type: 'equal', field: 2, value: 'b' },
|
|
{ type: 'equal', field: 3, value: 'c' },
|
|
],
|
|
groups: [],
|
|
},
|
|
],
|
|
})
|
|
).toBe(true)
|
|
})
|
|
})
|
|
|
|
describe('canRowsBeOptimisticallyUpdatedInView', () => {
|
|
let testApp = null
|
|
|
|
beforeAll(() => {
|
|
testApp = new TestApp()
|
|
})
|
|
|
|
afterEach(() => {
|
|
testApp.afterEach()
|
|
})
|
|
|
|
const view = { sortings: [], filters: [], group_bys: [] }
|
|
|
|
it('gives up on optimistic updates when a searched view has a read-only field whose value the backend computes', () => {
|
|
const fields = [{ id: 1, type: 'formula' }]
|
|
expect(
|
|
canRowsBeOptimisticallyUpdatedInView(
|
|
testApp._app.$registry,
|
|
view,
|
|
fields,
|
|
'search'
|
|
)
|
|
).toBe(false)
|
|
})
|
|
|
|
it('keeps optimistic updates when the only read-only field holds no value', () => {
|
|
// A button field is read only, but it has no cell value for the search to
|
|
// match, so the backend is not the source of truth for the result.
|
|
const fields = [{ id: 1, type: 'button' }]
|
|
expect(
|
|
canRowsBeOptimisticallyUpdatedInView(
|
|
testApp._app.$registry,
|
|
view,
|
|
fields,
|
|
'search'
|
|
)
|
|
).toBe(true)
|
|
})
|
|
|
|
it('still gives up when a read-only field is sorted on', () => {
|
|
const fields = [{ id: 1, type: 'button' }]
|
|
expect(
|
|
canRowsBeOptimisticallyUpdatedInView(
|
|
testApp._app.$registry,
|
|
{ ...view, sortings: [{ field: 1 }] },
|
|
fields,
|
|
null
|
|
)
|
|
).toBe(false)
|
|
})
|
|
})
|
|
|
|
describe('getOrderBy', () => {
|
|
it('returns null when adhocSorting is false', () => {
|
|
const view = { sortings: [{ field: 1, order: 'ASC', type: 'default' }] }
|
|
expect(getOrderBy(view, false)).toBeNull()
|
|
})
|
|
|
|
it('returns only sortings, not group_bys', () => {
|
|
const view = {
|
|
sortings: [{ field: 2, order: 'ASC', type: 'default' }],
|
|
group_bys: [{ field: 1, order: 'DESC', type: 'default' }],
|
|
}
|
|
expect(getOrderBy(view, true)).toBe('field_2')
|
|
})
|
|
|
|
it('returns empty string when no sortings exist', () => {
|
|
const view = {
|
|
sortings: [],
|
|
group_bys: [{ field: 1, order: 'ASC', type: 'default' }],
|
|
}
|
|
expect(getOrderBy(view, true)).toBe('')
|
|
})
|
|
|
|
it('serializes DESC and non-default sort type', () => {
|
|
const view = {
|
|
sortings: [
|
|
{ field: 3, order: 'DESC', type: 'default' },
|
|
{ field: 4, order: 'ASC', type: 'numeric' },
|
|
],
|
|
group_bys: [],
|
|
}
|
|
expect(getOrderBy(view, true)).toBe('-field_3,field_4[numeric]')
|
|
})
|
|
})
|
|
|
|
describe('serializeGroupBys', () => {
|
|
it('returns empty string when no group_bys', () => {
|
|
expect(serializeGroupBys({ group_bys: [] })).toBe('')
|
|
expect(serializeGroupBys({})).toBe('')
|
|
})
|
|
|
|
it('serializes group_bys correctly', () => {
|
|
const view = {
|
|
group_bys: [
|
|
{ field: 1, order: 'ASC', type: 'default' },
|
|
{ field: 2, order: 'DESC', type: 'default' },
|
|
],
|
|
}
|
|
expect(serializeGroupBys(view)).toBe('field_1,-field_2')
|
|
})
|
|
|
|
it('includes non-default sort type', () => {
|
|
const view = {
|
|
group_bys: [{ field: 5, order: 'ASC', type: 'numeric' }],
|
|
}
|
|
expect(serializeGroupBys(view)).toBe('field_5[numeric]')
|
|
})
|
|
})
|
|
|
|
describe('reportHiddenRows', () => {
|
|
const i18n = { t: (key, { count }) => `${key} - ${count}` }
|
|
|
|
it('returns the hidden row ids, shows one toast and closes their row modal', () => {
|
|
const dispatch = vi.fn()
|
|
const data = {
|
|
items: [],
|
|
metadata: { hidden_row_ids: [1, 2] },
|
|
}
|
|
|
|
const hidden = reportHiddenRows(data, {
|
|
dispatch,
|
|
i18n,
|
|
tableId: 10,
|
|
created: true,
|
|
})
|
|
|
|
expect([...hidden]).toEqual([1, 2])
|
|
expect(dispatch.mock.calls).toEqual([
|
|
[
|
|
'toast/info',
|
|
{
|
|
title: 'hiddenRows.title - 2',
|
|
message: 'hiddenRows.createdMessage - 2',
|
|
},
|
|
{ root: true },
|
|
],
|
|
[
|
|
'rowModal/rowsHiddenByBackend',
|
|
{ tableId: 10, rowIds: [1, 2] },
|
|
{ root: true },
|
|
],
|
|
])
|
|
})
|
|
|
|
it('uses the updated message for updated rows', () => {
|
|
const dispatch = vi.fn()
|
|
const data = { items: [], metadata: { hidden_row_ids: [1] } }
|
|
|
|
reportHiddenRows(data, { dispatch, i18n, tableId: 10, created: false })
|
|
|
|
expect(dispatch).toHaveBeenCalledWith(
|
|
'toast/info',
|
|
{
|
|
title: 'hiddenRows.title - 1',
|
|
message: 'hiddenRows.updatedMessage - 1',
|
|
},
|
|
{ root: true }
|
|
)
|
|
})
|
|
|
|
it('does nothing when the backend reports no hidden rows', () => {
|
|
const dispatch = vi.fn()
|
|
|
|
const hidden = reportHiddenRows(
|
|
{ items: [], metadata: {} },
|
|
{ dispatch, i18n, tableId: 10, created: false }
|
|
)
|
|
|
|
expect(hidden.size).toBe(0)
|
|
expect(dispatch).not.toHaveBeenCalled()
|
|
})
|
|
})
|