Some checks failed
Build & Test / Analyze (javascript, blocksuite) (push) Failing after 3s
Build & Test / Rust test filter (push) Failing after 3s
Build & Test / E2E BlockSuite Test (2) (push) Failing after 3s
Build & Test / E2E BlockSuite Test (3) (push) Failing after 4s
Build & Test / E2E BlockSuite Test (4) (push) Failing after 3s
Build & Test / E2E BlockSuite Cross Browser Test (push) Failing after 3s
Build & Test / E2E Mobile Test (1) (push) Failing after 3s
Build & Test / E2E Mobile Test (2) (push) Failing after 3s
Build & Test / Build AFFiNE native (x86_64-unknown-linux-gnu) (push) Failing after 4s
Build & Test / Unit Test (1) (push) Has been skipped
Build & Test / Unit Test (2) (push) Has been skipped
Build & Test / Unit Test (3) (push) Has been skipped
Build & Test / Build Server native (push) Failing after 3s
Build & Test / Check Git Status (push) Has been skipped
Build & Test / Server Test (0, 4) (push) Has been skipped
Build & Test / Server Test (1, 4) (push) Has been skipped
Build & Test / Analyze (javascript, affine) (push) Failing after 3s
Build & Test / Analyze (typescript, affine) (push) Failing after 3s
Build & Test / Analyze (typescript, blocksuite) (push) Failing after 3s
Build & Test / Mobile native build filter (push) Failing after 3s
Build & Test / Check yarn binary (push) Failing after 3s
Build & Test / E2E BlockSuite Test (1) (push) Failing after 4s
Build & Test / E2E BlockSuite Test (5) (push) Failing after 3s
Build & Test / Unit Test (4) (push) Has been skipped
Build & Test / Unit Test (5) (push) Has been skipped
Build & Test / Native Unit Test (push) Has been skipped
Build & Test / Server Test (2, 4) (push) Has been skipped
Build & Test / Server Test (3, 4) (push) Has been skipped
Build & Test / Server Test with Elasticsearch (push) Has been skipped
Build & Test / Server Test with Embedded Search (push) Has been skipped
Build & Test / Server Test with Manticore Search (push) Has been skipped
Build & Test / Server E2E Test (push) Has been skipped
Build & Test / Cloud Desktop E2E Test (push) Has been skipped
Build & Test / Cloud E2E Test 1/2 (push) Has been skipped
Build & Test / Cloud E2E Test 2/2 (push) Has been skipped
Build & Test / Build @affine/electron renderer (push) Failing after 3s
Build & Test / Copilot test filter (push) Failing after 4s
Build & Test / Lint Rust (push) Has been skipped
Build & Test / Build Android app (push) Has been skipped
Build & Test / Test Android app (push) Has been skipped
Build & Test / Run native tests (push) Has been skipped
Build & Test / Server Copilot Api Test (push) Has been skipped
Build & Test / Frontend Copilot E2E Test (3, 5) (push) Has been skipped
Build & Test / Frontend Copilot E2E Test (4, 5) (push) Has been skipped
Build & Test / Frontend Copilot E2E Test (1, 5) (push) Has been skipped
Build & Test / Frontend Copilot E2E Test (2, 5) (push) Has been skipped
Build & Test / Frontend Copilot E2E Test (5, 5) (push) Has been skipped
Build & Test / Desktop Test (ubuntu-latest, linux, x64, x86_64-unknown-linux-gnu, true) (push) Has been cancelled
Build & Test / Desktop Test (windows-latest, windows, x64, x86_64-pc-windows-msvc, true) (push) Has been cancelled
Build & Test / 3, 2, 1 Launch (push) Has been cancelled
Build & Test / Lint (push) Has been cancelled
Build & Test / Typecheck (push) Has been cancelled
Build & Test / Build iOS app (push) Has been cancelled
Build & Test / Build AFFiNE native (aarch64-apple-darwin) (push) Has been cancelled
Build & Test / E2E Test (1) (push) Has been cancelled
Build & Test / E2E Test (2) (push) Has been cancelled
Build & Test / E2E Test (3) (push) Has been cancelled
Build & Test / E2E Test (4) (push) Has been cancelled
Build & Test / E2E Test (5) (push) Has been cancelled
Build & Test / Build AFFiNE native (x86_64-apple-darwin) (push) Has been cancelled
Build & Test / Build AFFiNE native (aarch64-pc-windows-msvc) (push) Has been cancelled
Build & Test / Build AFFiNE native (x86_64-pc-windows-msvc) (push) Has been cancelled
Build & Test / Desktop Test (macos-latest, macos, arm64, aarch64-apple-darwin, true) (push) Has been cancelled
Build & Test / Desktop Test (macos-latest, macos, x64, x86_64-apple-darwin, false) (push) Has been cancelled
237 lines
5.5 KiB
TypeScript
237 lines
5.5 KiB
TypeScript
import {
|
|
addProperty,
|
|
copyCellsByProperty,
|
|
databaseBlockProperties,
|
|
deleteColumn,
|
|
getCell,
|
|
getProperty,
|
|
updateCell,
|
|
} from '@blocksuite/affine-block-database';
|
|
import {
|
|
type CellDataType,
|
|
type ColumnDataType,
|
|
type DatabaseBlockModel,
|
|
DatabaseBlockSchemaExtension,
|
|
NoteBlockSchemaExtension,
|
|
ParagraphBlockSchemaExtension,
|
|
RootBlockSchemaExtension,
|
|
} from '@blocksuite/affine-model';
|
|
import { propertyModelPresets } from '@blocksuite/data-view/property-pure-presets';
|
|
import type { BlockModel, Store } from '@blocksuite/store';
|
|
import { Text } from '@blocksuite/store';
|
|
import {
|
|
createAutoIncrementIdGenerator,
|
|
TestWorkspace,
|
|
} from '@blocksuite/store/test';
|
|
import { beforeEach, describe, expect, test } from 'vitest';
|
|
|
|
const extensions = [
|
|
RootBlockSchemaExtension,
|
|
NoteBlockSchemaExtension,
|
|
ParagraphBlockSchemaExtension,
|
|
DatabaseBlockSchemaExtension,
|
|
];
|
|
|
|
function createTestOptions() {
|
|
const idGenerator = createAutoIncrementIdGenerator();
|
|
return { id: 'test-collection', idGenerator };
|
|
}
|
|
|
|
function createTestDoc(docId = 'doc0') {
|
|
const options = createTestOptions();
|
|
const collection = new TestWorkspace(options);
|
|
collection.meta.initialize();
|
|
const doc = collection.createDoc(docId);
|
|
doc.load();
|
|
return doc.getStore({ extensions });
|
|
}
|
|
|
|
describe('DatabaseManager', () => {
|
|
let doc: Store;
|
|
let db: DatabaseBlockModel;
|
|
|
|
let rootId: BlockModel['id'];
|
|
let noteBlockId: BlockModel['id'];
|
|
let databaseBlockId: BlockModel['id'];
|
|
let p1: BlockModel['id'];
|
|
let p2: BlockModel['id'];
|
|
let col1: ColumnDataType['id'];
|
|
let col2: ColumnDataType['id'];
|
|
let col3: ColumnDataType['id'];
|
|
|
|
const selection = [
|
|
{ id: '1', value: 'Done', color: 'var(--affine-tag-white)' },
|
|
{ id: '2', value: 'TODO', color: 'var(--affine-tag-pink)' },
|
|
{ id: '3', value: 'WIP', color: 'var(--affine-tag-blue)' },
|
|
];
|
|
|
|
beforeEach(() => {
|
|
doc = createTestDoc();
|
|
|
|
rootId = doc.addBlock('affine:page', {
|
|
title: new Text('database test'),
|
|
});
|
|
noteBlockId = doc.addBlock('affine:note', {}, rootId);
|
|
|
|
databaseBlockId = doc.addBlock(
|
|
'affine:database',
|
|
{
|
|
columns: [],
|
|
titleColumn: 'Title',
|
|
},
|
|
noteBlockId
|
|
);
|
|
|
|
const databaseModel = doc.getModelById(
|
|
databaseBlockId
|
|
) as DatabaseBlockModel;
|
|
db = databaseModel;
|
|
|
|
col1 = addProperty(
|
|
db,
|
|
'end',
|
|
databaseBlockProperties.numberColumnConfig.create('Number')
|
|
);
|
|
col2 = addProperty(
|
|
db,
|
|
'end',
|
|
propertyModelPresets.selectPropertyModelConfig.create('Single Select', {
|
|
options: selection,
|
|
})
|
|
);
|
|
col3 = addProperty(
|
|
db,
|
|
'end',
|
|
databaseBlockProperties.richTextColumnConfig.create('Rich Text')
|
|
);
|
|
|
|
doc.updateBlock(databaseModel, {
|
|
columns: [col1, col2, col3],
|
|
});
|
|
|
|
p1 = doc.addBlock(
|
|
'affine:paragraph',
|
|
{
|
|
text: new Text('text1'),
|
|
},
|
|
databaseBlockId
|
|
);
|
|
p2 = doc.addBlock(
|
|
'affine:paragraph',
|
|
{
|
|
text: new Text('text2'),
|
|
},
|
|
databaseBlockId
|
|
);
|
|
|
|
updateCell(db, p1, {
|
|
columnId: col1,
|
|
value: 0.1,
|
|
});
|
|
updateCell(db, p2, {
|
|
columnId: col2,
|
|
value: [selection[1]],
|
|
});
|
|
});
|
|
|
|
test('getColumn', () => {
|
|
const column = {
|
|
...databaseBlockProperties.numberColumnConfig.create('testColumnId'),
|
|
id: 'testColumnId',
|
|
};
|
|
addProperty(db, 'end', column);
|
|
|
|
const result = getProperty(db, column.id);
|
|
expect(result).toEqual(column);
|
|
});
|
|
|
|
test('addColumn', () => {
|
|
const column =
|
|
databaseBlockProperties.numberColumnConfig.create('Test Column');
|
|
const id = addProperty(db, 'end', column);
|
|
const result = getProperty(db, id);
|
|
|
|
expect(result).toMatchObject(column);
|
|
expect(result).toHaveProperty('id');
|
|
});
|
|
|
|
test('deleteColumn', () => {
|
|
const column = {
|
|
...databaseBlockProperties.numberColumnConfig.create('Test Column'),
|
|
id: 'testColumnId',
|
|
};
|
|
addProperty(db, 'end', column);
|
|
expect(getProperty(db, column.id)).toEqual(column);
|
|
|
|
deleteColumn(db, column.id);
|
|
expect(getProperty(db, column.id)).toBeUndefined();
|
|
});
|
|
|
|
test('getCell', () => {
|
|
const modelId = doc.addBlock(
|
|
'affine:paragraph',
|
|
{
|
|
text: new Text('paragraph'),
|
|
},
|
|
noteBlockId
|
|
);
|
|
const column = {
|
|
...databaseBlockProperties.numberColumnConfig.create('Test Column'),
|
|
id: 'testColumnId',
|
|
};
|
|
const cell: CellDataType = {
|
|
columnId: column.id,
|
|
value: 42,
|
|
};
|
|
|
|
addProperty(db, 'end', column);
|
|
updateCell(db, modelId, cell);
|
|
|
|
const model = doc.getModelById(modelId);
|
|
|
|
expect(model).not.toBeNull();
|
|
|
|
const result = getCell(db, model!.id, column.id);
|
|
expect(result).toEqual(cell);
|
|
});
|
|
|
|
test('updateCell', () => {
|
|
const newRowId = doc.addBlock(
|
|
'affine:paragraph',
|
|
{
|
|
text: new Text('text3'),
|
|
},
|
|
databaseBlockId
|
|
);
|
|
|
|
updateCell(db, newRowId, {
|
|
columnId: col2,
|
|
value: [selection[2]],
|
|
});
|
|
|
|
const cell = getCell(db, newRowId, col2);
|
|
expect(cell).toEqual({
|
|
columnId: col2,
|
|
value: [selection[2]],
|
|
});
|
|
});
|
|
|
|
test('copyCellsByColumn', () => {
|
|
const newColId = addProperty(
|
|
db,
|
|
'end',
|
|
propertyModelPresets.selectPropertyModelConfig.create('Copied Select', {
|
|
options: selection,
|
|
})
|
|
);
|
|
|
|
copyCellsByProperty(db, col2, newColId);
|
|
|
|
const cell = getCell(db, p2, newColId);
|
|
expect(cell).toEqual({
|
|
columnId: newColId,
|
|
value: [selection[1]],
|
|
});
|
|
});
|
|
});
|