2024-09-16 16:50:14 +00:00
const dbgateApi = require ( 'dbgate-api/src/shell' ) ;
// const jsonLinesWriter = require('dbgate-api/src/shell/jsonLinesWriter');
const tmp = require ( 'tmp' ) ;
// const dbgatePluginCsv = require('dbgate-plugin-csv/src/backend');
const fs = require ( 'fs' ) ;
const requirePlugin = require ( 'dbgate-api/src/shell/requirePlugin' ) ;
const CSV _DATA = ` Issue Number; Title; Github URL; Labels; State; Created At; Updated At; Reporter; Assignee
801 ; "Does it 'burst' the database on startup or first lUI load ? " ; https : //github.com/dbgate/dbgate/issues/801; ""; open; 05/23/2024; 05/23/2024; rgarrigue;
799 ; "BUG: latest AppImage crashes on opening in Fedora 39" ; https : //github.com/dbgate/dbgate/issues/799; ""; open; 05/21/2024; 05/24/2024; BenGraham-Git;
798 ; "MongoDB write operations fail" ; https : //github.com/dbgate/dbgate/issues/798; "bug,solved"; open; 05/21/2024; 05/24/2024; mahmed0715;
797 ; "BUG: Unable to open SQL files" ; https : //github.com/dbgate/dbgate/issues/797; "bug"; open; 05/20/2024; 05/21/2024; cesarValdivia;
795 ; "BUG: MS SQL Server connection error (KEY_USAGE_BIT_INCORRECT)" ; https : //github.com/dbgate/dbgate/issues/795; ""; open; 05/20/2024; 05/20/2024; keskinonur;
794 ; "GLIBC_2.29' not found and i have 2.31" ; https : //github.com/dbgate/dbgate/issues/794; ""; closed; 05/20/2024; 05/21/2024; MFdanGM;
793 ; "BUG: PostgresSQL doesn't show tables when connected" ; https : //github.com/dbgate/dbgate/issues/793; ""; open; 05/20/2024; 05/22/2024; stomper013;
792 ; "FEAT: Wayland support" ; https : //github.com/dbgate/dbgate/issues/792; ""; closed; 05/19/2024; 05/21/2024; VosaXalo;
` ;
test ( 'csv import test' , async ( ) => {
const dbgatePluginCsv = requirePlugin ( 'dbgate-plugin-csv' ) ;
const csvFileName = tmp . tmpNameSync ( ) ;
const jsonlFileName = tmp . tmpNameSync ( ) ;
fs . writeFileSync ( csvFileName , CSV _DATA ) ;
const reader = await dbgatePluginCsv . shellApi . reader ( {
fileName : csvFileName ,
} ) ;
const writer = await dbgateApi . jsonLinesWriter ( {
fileName : jsonlFileName ,
} ) ;
await dbgateApi . copyStream ( reader , writer ) ;
const jsonData = fs . readFileSync ( jsonlFileName , 'utf-8' ) ;
const rows = jsonData
. split ( '\n' )
. filter ( x => x . trim ( ) !== '' )
. map ( x => JSON . parse ( x ) ) ;
2024-09-17 07:59:47 +00:00
expect ( rows [ 0 ] . columns ) . toEqual ( [
{ columnName : 'Issue Number' } ,
{ columnName : 'Title' } ,
{ columnName : 'Github URL' } ,
{ columnName : 'Labels' } ,
{ columnName : 'State' } ,
{ columnName : 'Created At' } ,
{ columnName : 'Updated At' } ,
{ columnName : 'Reporter' } ,
{ columnName : 'Assignee' } ,
] ) ;
2024-09-16 16:50:14 +00:00
expect ( rows . length ) . toEqual ( 9 ) ;
2024-09-17 07:59:47 +00:00
expect ( rows [ 1 ] ) . toEqual ( {
'Issue Number' : '801' ,
Title : "Does it 'burst' the database on startup or first lUI load ? " ,
'Github URL' : 'https://github.com/dbgate/dbgate/issues/801' ,
Labels : '' ,
State : 'open' ,
'Created At' : '05/23/2024' ,
'Updated At' : '05/23/2024' ,
Reporter : 'rgarrigue' ,
Assignee : '' ,
} ) ;
2024-09-16 16:50:14 +00:00
} ) ;
2024-09-17 10:32:03 +00:00
test ( 'JSON array import test' , async ( ) => {
const jsonFileName = tmp . tmpNameSync ( ) ;
const jsonLinesFileName = tmp . tmpNameSync ( ) ;
fs . writeFileSync (
jsonFileName ,
JSON . stringify ( [
{ id : 1 , val : 'v1' } ,
{ id : 2 , val : 'v2' } ,
] )
) ;
const reader = await dbgateApi . jsonReader ( {
fileName : jsonFileName ,
} ) ;
const writer = await dbgateApi . jsonLinesWriter ( {
fileName : jsonLinesFileName ,
} ) ;
await dbgateApi . copyStream ( reader , writer ) ;
const jsonData = fs . readFileSync ( jsonLinesFileName , 'utf-8' ) ;
const rows = jsonData
. split ( '\n' )
. filter ( x => x . trim ( ) !== '' )
. map ( x => JSON . parse ( x ) ) ;
expect ( rows . length ) . toEqual ( 2 ) ;
expect ( rows [ 0 ] ) . toEqual ( {
id : 1 ,
val : 'v1' ,
} ) ;
} ) ;