@@ -3,26 +3,27 @@ import { faker } from '@faker-js/faker';
33import { INestApplication , ValidationPipe } from '@nestjs/common' ;
44import { Test } from '@nestjs/testing' ;
55import test from 'ava' ;
6+ import { ValidationError } from 'class-validator' ;
67import cookieParser from 'cookie-parser' ;
8+ import JSON5 from 'json5' ;
9+ import { MongoClient } from 'mongodb' ;
710import request from 'supertest' ;
811import { ApplicationModule } from '../../../src/app.module.js' ;
12+ import { CreateOrUpdateTableWidgetsDto } from '../../../src/entities/widget/dto/create-table-widget.dto.js' ;
13+ import { WidgetTypeEnum } from '../../../src/enums/widget-type.enum.js' ;
914import { AllExceptionsFilter } from '../../../src/exceptions/all-exceptions.filter.js' ;
15+ import { ValidationException } from '../../../src/exceptions/custom-exceptions/validation-exception.js' ;
1016import { Messages } from '../../../src/exceptions/text/messages.js' ;
1117import { Cacher } from '../../../src/helpers/cache/cacher.js' ;
1218import { DatabaseModule } from '../../../src/shared/database/database.module.js' ;
1319import { DatabaseService } from '../../../src/shared/database/database.service.js' ;
1420import { MockFactory } from '../../mock.factory.js' ;
1521import { compareTableWidgetsArrays } from '../../utils/compare-table-widgets-arrays.js' ;
22+ import { createTestTable } from '../../utils/create-test-table.js' ;
1623import { getTestData } from '../../utils/get-test-data.js' ;
24+ import { getTestKnex } from '../../utils/get-test-knex.js' ;
1725import { registerUserAndReturnUserInfo } from '../../utils/register-user-and-return-user-info.js' ;
1826import { TestUtils } from '../../utils/test.utils.js' ;
19- import { ValidationException } from '../../../src/exceptions/custom-exceptions/validation-exception.js' ;
20- import { ValidationError } from 'class-validator' ;
21- import { CreateOrUpdateTableWidgetsDto } from '../../../src/entities/widget/dto/create-table-widget.dto.js' ;
22- import { WidgetTypeEnum } from '../../../src/enums/widget-type.enum.js' ;
23- import { createTestTable } from '../../utils/create-test-table.js' ;
24- import { getTestKnex } from '../../utils/get-test-knex.js' ;
25- import JSON5 from 'json5' ;
2627
2728const mockFactory = new MockFactory ( ) ;
2829let app : INestApplication ;
@@ -782,3 +783,134 @@ test.serial(`${currentTest} should return created table widgets`, async (t) => {
782783 t . is ( getTableStructureRO . foreignKeys [ 0 ] . hasOwnProperty ( 'autocomplete_columns' ) , true ) ;
783784 t . is ( getTableStructureRO . foreignKeys [ 0 ] . autocomplete_columns . length , 5 ) ;
784785} ) ;
786+
787+ // Table widgets for mongodb database
788+ test . serial (
789+ `${ currentTest } should return created table widgets as foreign keys, when database is mongodb` ,
790+ async ( t ) => {
791+ const connectionToTestDB = getTestData ( mockFactory ) . mongoDbConnection ;
792+ const { token } = await registerUserAndReturnUserInfo ( app ) ;
793+
794+ const mongoConnectionString =
795+ `mongodb://${ connectionToTestDB . username } ` +
796+ `:${ connectionToTestDB . password } ` +
797+ `@${ connectionToTestDB . host } ` +
798+ `:${ connectionToTestDB . port } ` +
799+ `/${ connectionToTestDB . database } ` ;
800+
801+ const referencedOnTableTableName = `users` ;
802+ const referencedByTableName = `orders` ;
803+ const testTableColumnName = `user_name` ;
804+ const testReferencedColumnsName = `product_description` ;
805+ const referencedByColumnName = 'user_id' ;
806+ const referencedOnColumnName = '_id' ;
807+
808+ const client = new MongoClient ( mongoConnectionString ) ;
809+ await client . connect ( ) ;
810+ const db = client . db ( connectionToTestDB . database ) ;
811+ const referencedCollection = db . collection ( referencedOnTableTableName ) ;
812+ const referencedByCollection = db . collection ( referencedByTableName ) ;
813+
814+ await referencedCollection . drop ( ) ;
815+ await referencedByCollection . drop ( ) ;
816+
817+ const insertedReferencedIds = [ ] ;
818+ for ( let index = 0 ; index < 42 ; index ++ ) {
819+ const insertedReferencedId = await referencedCollection . insertOne ( {
820+ [ testTableColumnName ] : faker . person . fullName ( ) ,
821+ created_at : new Date ( ) ,
822+ updated_at : new Date ( ) ,
823+ } ) ;
824+ insertedReferencedIds . push ( insertedReferencedId . insertedId . toHexString ( ) ) ;
825+ }
826+
827+ for ( let index = 0 ; index < 42 ; index ++ ) {
828+ await referencedByCollection . insertOne ( {
829+ [ referencedByColumnName ] : insertedReferencedIds [ index ] ,
830+ [ testReferencedColumnsName ] : faker . lorem . lines ( ) ,
831+ created_at : new Date ( ) ,
832+ updated_at : new Date ( ) ,
833+ } ) ;
834+ }
835+
836+ const foreignKeyWidgetsDTO : CreateOrUpdateTableWidgetsDto = {
837+ widgets : [
838+ {
839+ widget_type : WidgetTypeEnum . Foreign_key ,
840+ widget_params : JSON . stringify ( {
841+ referenced_column_name : referencedOnColumnName ,
842+ referenced_table_name : referencedOnTableTableName ,
843+ constraint_name : 'manually_created_constraint' ,
844+ column_name : referencedByColumnName ,
845+ } ) ,
846+ field_name : referencedByColumnName ,
847+ description : 'User ID as foreign key' ,
848+ name : 'User ID' ,
849+ widget_options : JSON . stringify ( { } ) ,
850+ } ,
851+ ] ,
852+ } ;
853+
854+ const createConnectionResponse = await request ( app . getHttpServer ( ) )
855+ . post ( '/connection' )
856+ . send ( connectionToTestDB )
857+ . set ( 'Cookie' , token )
858+ . set ( 'Content-Type' , 'application/json' )
859+ . set ( 'Accept' , 'application/json' ) ;
860+ const createConnectionRO = JSON . parse ( createConnectionResponse . text ) ;
861+ t . is ( createConnectionResponse . status , 201 ) ;
862+ const connectionId = createConnectionRO . id ;
863+
864+ const createTableWidgetResponse = await request ( app . getHttpServer ( ) )
865+ . post ( `/widget/${ connectionId } ?tableName=${ referencedByTableName } ` )
866+ . send ( foreignKeyWidgetsDTO )
867+ . set ( 'Content-Type' , 'application/json' )
868+ . set ( 'Cookie' , token )
869+ . set ( 'Accept' , 'application/json' ) ;
870+ const createTableWidgetRO = JSON . parse ( createTableWidgetResponse . text ) ;
871+ console . log ( '🚀 ~ createTableWidgetRO:' , createTableWidgetRO ) ;
872+ t . is ( createTableWidgetResponse . status , 201 ) ;
873+
874+ const getTableWidgets = await request ( app . getHttpServer ( ) )
875+ . get ( `/widgets/${ connectionId } ?tableName=${ referencedByTableName } ` )
876+ . set ( 'Content-Type' , 'application/json' )
877+ . set ( 'Cookie' , token )
878+ . set ( 'Accept' , 'application/json' ) ;
879+ t . is ( getTableWidgets . status , 200 ) ;
880+ const getTableWidgetsRO = JSON . parse ( getTableWidgets . text ) ;
881+ t . is ( typeof getTableWidgetsRO , 'object' ) ;
882+ t . is ( getTableWidgetsRO . length , 1 ) ;
883+
884+ t . is ( getTableWidgetsRO [ 0 ] . widget_type , foreignKeyWidgetsDTO . widgets [ 0 ] . widget_type ) ;
885+
886+ const getTableStructureResponse = await request ( app . getHttpServer ( ) )
887+ . get ( `/table/structure/${ connectionId } ?tableName=${ referencedByTableName } ` )
888+ . set ( 'Content-Type' , 'application/json' )
889+ . set ( 'Cookie' , token )
890+ . set ( 'Accept' , 'application/json' ) ;
891+
892+ const getTableStructureRO = JSON . parse ( getTableStructureResponse . text ) ;
893+
894+ t . is ( getTableStructureResponse . status , 200 ) ;
895+ t . is ( getTableStructureRO . hasOwnProperty ( 'table_widgets' ) , true ) ;
896+ t . is ( getTableStructureRO . table_widgets . length , 1 ) ;
897+ t . is ( getTableStructureRO . table_widgets [ 0 ] . field_name , foreignKeyWidgetsDTO . widgets [ 0 ] . field_name ) ;
898+ t . is ( getTableStructureRO . table_widgets [ 0 ] . widget_type , foreignKeyWidgetsDTO . widgets [ 0 ] . widget_type ) ;
899+ t . is ( getTableStructureRO . hasOwnProperty ( 'foreignKeys' ) , true ) ;
900+ t . is ( getTableStructureRO . foreignKeys . length , 1 ) ;
901+ t . is ( getTableStructureRO . foreignKeys [ 0 ] . column_name , foreignKeyWidgetsDTO . widgets [ 0 ] . field_name ) ;
902+ t . is ( getTableStructureRO . foreignKeys [ 0 ] . referenced_table_name , referencedOnTableTableName ) ;
903+
904+ // check table rows received with foreign keys from widget
905+
906+ const getRowsResponse = await request ( app . getHttpServer ( ) )
907+ . get ( `/table/rows/${ connectionId } ?tableName=${ referencedByTableName } ` )
908+ . set ( 'Content-Type' , 'application/json' )
909+ . set ( 'Cookie' , token )
910+ . set ( 'Accept' , 'application/json' ) ;
911+ t . is ( getRowsResponse . status , 200 ) ;
912+ const getRowsRO = JSON . parse ( getRowsResponse . text ) ;
913+ t . is ( typeof getRowsRO . rows [ 0 ] , 'object' ) ;
914+ t . is ( getRowsRO . rows [ 0 ] . hasOwnProperty ( '_id' ) , true ) ;
915+ } ,
916+ ) ;
0 commit comments