-
Notifications
You must be signed in to change notification settings - Fork 531
dbeaver/pro#7990 qmdb csv export #4306
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
SychevAndrey
wants to merge
7
commits into
devel
Choose a base branch
from
dbeaver/pro#7990-qmdb-csv-export
base: devel
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+322
−5
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
500cb7d
dbeaver/pro#7990 refactor: make onRemove prop optional in Tag component
SychevAndrey ef62719
dbeaver/pro#7990 feat: add postForBlob helper function for using with…
SychevAndrey ca33434
Merge branch 'devel' into dbeaver/pro#7990-qmdb-csv-export
SychevAndrey 1424496
dbeaver/pro#7990 feat: replace postForBlob with submitForm for form s…
SychevAndrey 676d582
dbeaver/pro#7990 tests: add tests for submitForm and objectToFormFiel…
SychevAndrey 9e9842c
Merge branch 'devel' into dbeaver/pro#7990-qmdb-csv-export
EvgeniaBzzz e9232da
Merge branch 'devel' into dbeaver/pro#7990-qmdb-csv-export
SychevAndrey File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,253 @@ | ||
| /* | ||
| * CloudBeaver - Cloud Database Manager | ||
| * Copyright (C) 2020-2026 DBeaver Corp and others | ||
| * | ||
| * Licensed under the Apache License, Version 2.0. | ||
| * you may not use this file except in compliance with the License. | ||
| */ | ||
| import { afterEach, beforeEach, describe, expect, it, vitest } from 'vitest'; | ||
| import { objectToFormFields, submitForm } from './submitForm.js'; | ||
|
|
||
| describe('submitForm', () => { | ||
| let submitSpy: ReturnType<typeof vitest.spyOn<HTMLFormElement, 'submit'>>; | ||
| let capturedForm: HTMLFormElement | null; | ||
|
|
||
| // submit() is the moment when the form is fully populated and still attached to the DOM — | ||
| // submitForm removes it right after. We snapshot it from document.body here. | ||
| const getForm = (): HTMLFormElement => { | ||
| if (!capturedForm) { | ||
| throw new Error('submit() was not called'); | ||
| } | ||
| return capturedForm; | ||
| }; | ||
| const getInputs = (): HTMLInputElement[] => Array.from(getForm().querySelectorAll('input')); | ||
|
|
||
| beforeEach(() => { | ||
| document.body.innerHTML = ''; | ||
| capturedForm = null; | ||
| submitSpy = vitest.spyOn(HTMLFormElement.prototype, 'submit').mockImplementation(() => { | ||
| capturedForm = document.body.querySelector('form'); | ||
| }); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| submitSpy.mockRestore(); | ||
| }); | ||
|
|
||
| it('should create a form with POST method and target _blank', () => { | ||
| submitForm('/api/test', []); | ||
|
|
||
| const form = getForm(); | ||
| expect(form.tagName).toBe('FORM'); | ||
| expect(form.method.toLowerCase()).toBe('post'); | ||
| expect(form.action).toContain('/api/test'); | ||
| expect(form.target).toBe('_blank'); | ||
| expect(form.style.display).toBe('none'); | ||
| }); | ||
|
|
||
| it('should attach the form to document.body before submit', () => { | ||
| submitForm('/api/test', []); | ||
|
|
||
| // capturedForm is read inside submit(), so being non-null proves the form was in the DOM at that point | ||
| expect(capturedForm).not.toBeNull(); | ||
| }); | ||
|
|
||
| it('should append hidden inputs for each field', () => { | ||
| submitForm('/api/test', [ | ||
| ['name', 'John'], | ||
| ['age', '30'], | ||
| ]); | ||
|
|
||
| const inputs = getInputs(); | ||
| expect(inputs).toHaveLength(2); | ||
| expect(inputs[0]!.type).toBe('hidden'); | ||
| expect(inputs[0]!.name).toBe('name'); | ||
| expect(inputs[0]!.value).toBe('John'); | ||
| expect(inputs[1]!.type).toBe('hidden'); | ||
| expect(inputs[1]!.name).toBe('age'); | ||
| expect(inputs[1]!.value).toBe('30'); | ||
| }); | ||
|
|
||
| it('should submit the form and remove it from the DOM', () => { | ||
| submitForm('/api/test', [['key', 'value']]); | ||
|
|
||
| expect(submitSpy).toHaveBeenCalledTimes(1); | ||
| expect(document.body.querySelector('form')).toBeNull(); | ||
| }); | ||
|
|
||
| it('should support repeated keys (e.g. for array fields)', () => { | ||
| submitForm('/api/test', [ | ||
| ['ids', '1'], | ||
| ['ids', '2'], | ||
| ['ids', '3'], | ||
| ]); | ||
|
|
||
| const ids = getInputs().filter(i => i.name === 'ids'); | ||
| expect(ids).toHaveLength(3); | ||
| expect(ids.map(i => i.value)).toEqual(['1', '2', '3']); | ||
| }); | ||
|
|
||
| it('should accept any iterable of key/value pairs', () => { | ||
| const fields = new Map([ | ||
| ['a', '1'], | ||
| ['b', '2'], | ||
| ]); | ||
|
|
||
| submitForm('/api/test', fields); | ||
|
|
||
| const inputs = getInputs(); | ||
| expect(inputs).toHaveLength(2); | ||
| expect(inputs.map(i => [i.name, i.value])).toEqual([ | ||
| ['a', '1'], | ||
| ['b', '2'], | ||
| ]); | ||
| }); | ||
|
|
||
| it('should also accept a generator', () => { | ||
| function* gen(): Generator<readonly [string, string]> { | ||
| yield ['x', '1']; | ||
| yield ['y', '2']; | ||
| } | ||
|
|
||
| submitForm('/api/test', gen()); | ||
|
|
||
| expect(getInputs().map(i => [i.name, i.value])).toEqual([ | ||
| ['x', '1'], | ||
| ['y', '2'], | ||
| ]); | ||
| }); | ||
|
|
||
| it('should handle an empty fields iterable', () => { | ||
| submitForm('/api/test', []); | ||
|
|
||
| expect(getInputs()).toHaveLength(0); | ||
| expect(submitSpy).toHaveBeenCalledTimes(1); | ||
| expect(document.body.querySelector('form')).toBeNull(); | ||
| }); | ||
| }); | ||
|
|
||
| describe('objectToFormFields', () => { | ||
| it('should convert primitive values to string fields', () => { | ||
| expect( | ||
| objectToFormFields({ | ||
| name: 'John', | ||
| age: 30, | ||
| active: true, | ||
| }), | ||
| ).toEqual([ | ||
| ['name', 'John'], | ||
| ['age', '30'], | ||
| ['active', 'true'], | ||
| ]); | ||
| }); | ||
|
|
||
| it('should skip null, undefined, and empty string values', () => { | ||
| expect( | ||
| objectToFormFields({ | ||
| a: 'value', | ||
| b: null, | ||
| c: undefined, | ||
| d: '', | ||
| e: 'other', | ||
| }), | ||
| ).toEqual([ | ||
| ['a', 'value'], | ||
| ['e', 'other'], | ||
| ]); | ||
| }); | ||
|
|
||
| it('should keep falsy non-empty values (0, false)', () => { | ||
| expect( | ||
| objectToFormFields({ | ||
| zero: 0, | ||
| falseFlag: false, | ||
| }), | ||
| ).toEqual([ | ||
| ['zero', '0'], | ||
| ['falseFlag', 'false'], | ||
| ]); | ||
| }); | ||
|
|
||
| it('should expand arrays into repeated fields', () => { | ||
| expect( | ||
| objectToFormFields({ | ||
| tags: ['a', 'b', 'c'], | ||
| }), | ||
| ).toEqual([ | ||
| ['tags', 'a'], | ||
| ['tags', 'b'], | ||
| ['tags', 'c'], | ||
| ]); | ||
| }); | ||
|
|
||
| it('should skip null/undefined/empty entries inside arrays', () => { | ||
| expect( | ||
| objectToFormFields({ | ||
| ids: [1, null, 2, undefined, '', 3], | ||
| }), | ||
| ).toEqual([ | ||
| ['ids', '1'], | ||
| ['ids', '2'], | ||
| ['ids', '3'], | ||
| ]); | ||
| }); | ||
|
|
||
| it('should flatten nested objects, hoisting inner keys to top level', () => { | ||
| expect( | ||
| objectToFormFields({ | ||
| user: { | ||
| name: 'John', | ||
| age: 30, | ||
| }, | ||
| }), | ||
| ).toEqual([ | ||
| ['name', 'John'], | ||
| ['age', '30'], | ||
| ]); | ||
| }); | ||
|
|
||
| it('should flatten deeply nested objects', () => { | ||
| expect( | ||
| objectToFormFields({ | ||
| outer: { | ||
| middle: { | ||
| leaf: 'value', | ||
| }, | ||
| }, | ||
| }), | ||
| ).toEqual([['leaf', 'value']]); | ||
| }); | ||
|
|
||
| it('should handle nested objects mixed with arrays', () => { | ||
| expect( | ||
| objectToFormFields({ | ||
| meta: { | ||
| tags: ['x', 'y'], | ||
| name: 'foo', | ||
| }, | ||
| id: 1, | ||
| }), | ||
| ).toEqual([ | ||
| ['tags', 'x'], | ||
| ['tags', 'y'], | ||
| ['name', 'foo'], | ||
| ['id', '1'], | ||
| ]); | ||
| }); | ||
|
|
||
| it('should return an empty array for an empty object', () => { | ||
| expect(objectToFormFields({})).toEqual([]); | ||
| }); | ||
|
|
||
| it('should return an empty array when all values are skipped', () => { | ||
| expect( | ||
| objectToFormFields({ | ||
| a: null, | ||
| b: undefined, | ||
| c: '', | ||
| d: [], | ||
| e: [null, '', undefined], | ||
| }), | ||
| ).toEqual([]); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| /* | ||
| * CloudBeaver - Cloud Database Manager | ||
| * Copyright (C) 2020-2026 DBeaver Corp and others | ||
| * | ||
| * Licensed under the Apache License, Version 2.0. | ||
| * you may not use this file except in compliance with the License. | ||
| */ | ||
|
|
||
| export function submitForm(url: string, fields: Iterable<readonly [string, string]>): void { | ||
| const form = document.createElement('form'); | ||
| form.method = 'POST'; | ||
| form.action = url; | ||
| form.target = '_blank'; | ||
| form.style.display = 'none'; | ||
|
|
||
| for (const [name, value] of fields) { | ||
| const input = document.createElement('input'); | ||
| input.type = 'hidden'; | ||
| input.name = name; | ||
| input.value = value; | ||
| form.appendChild(input); | ||
| } | ||
|
|
||
| document.body.appendChild(form); | ||
| form.submit(); | ||
| document.body.removeChild(form); | ||
| } | ||
|
|
||
| /** | ||
| * Flattens an object into form-encoded key/value pairs, skipping falsy values: | ||
| * - arrays become repeated fields (`key=v1&key=v2`) | ||
| * - nested objects are flattened — their inner keys become top-level fields (!collision of inner keys is not handled) | ||
| */ | ||
| export function objectToFormFields(obj: Record<string, unknown>): Array<[string, string]> { | ||
| const fields: Array<[string, string]> = []; | ||
| for (const [key, value] of Object.entries(obj)) { | ||
| appendField(fields, key, value); | ||
| } | ||
| return fields; | ||
| } | ||
|
|
||
| function appendField(fields: Array<[string, string]>, key: string, value: unknown): void { | ||
| if (value == null || value === '') { | ||
| return; | ||
| } | ||
| if (Array.isArray(value)) { | ||
| for (const item of value) { | ||
| if (item != null && item !== '') { | ||
| fields.push([key, String(item)]); | ||
| } | ||
| } | ||
| return; | ||
| } | ||
| if (typeof value === 'object') { | ||
| for (const [innerKey, innerValue] of Object.entries(value)) { | ||
| appendField(fields, innerKey, innerValue); | ||
| } | ||
| return; | ||
| } | ||
| fields.push([key, String(value)]); | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it would be nice to cover it with unit tests
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes, I will add this