diff --git a/apps/velo-external-db/project.json b/apps/velo-external-db/project.json index 8d5ca1b11..61b6eaa86 100644 --- a/apps/velo-external-db/project.json +++ b/apps/velo-external-db/project.json @@ -8,7 +8,7 @@ "outputs": [ "{options.outputPath}" ], "options": { "outputPath": "dist/apps/velo-external-db", - "main": "apps/velo-external-db/src/app.ts", + "main": "apps/velo-external-db/src/main.ts", "tsConfig": "apps/velo-external-db/tsconfig.app.json", "generatePackageJson": true, "progress": true, diff --git a/apps/velo-external-db/src/app.ts b/apps/velo-external-db/src/app.ts index 63b22f7fb..6968719de 100644 --- a/apps/velo-external-db/src/app.ts +++ b/apps/velo-external-db/src/app.ts @@ -2,16 +2,8 @@ import express = require('express') import { create, readCommonConfig } from '@wix-velo/external-db-config' import { ExternalDbRouter } from '@wix-velo/velo-external-db-core' import { engineConnectorFor } from './storage/factory' -import { Server } from 'http' -import { ConnectionCleanUp, ISchemaProvider } from '@wix-velo/velo-external-db-types' import { Hooks } from 'libs/velo-external-db-core/src/types' -let started = false -let server: Server, _schemaProvider: ISchemaProvider, _cleanup: ConnectionCleanUp -let externalDbRouter: ExternalDbRouter - - - const initConnector = async (hooks?: Hooks) => { const { vendor, type: adapterType } = readCommonConfig() const configReader = create() @@ -19,7 +11,7 @@ const initConnector = async (hooks?: Hooks) => { const { connector: engineConnector, providers, cleanup } = await engineConnectorFor(adapterType, dbConfig) - externalDbRouter = new ExternalDbRouter({ + const externalDbRouter = new ExternalDbRouter({ connector: engineConnector, config: { authorization: { @@ -33,21 +25,14 @@ const initConnector = async (hooks?: Hooks) => { hooks }) - _cleanup = async () => { - await cleanup() - } - _schemaProvider = providers.schemaProvider - - return { externalDbRouter } + return { externalDbRouter, cleanup: async () => await cleanup(), schemaProvider: providers.schemaProvider } } -initConnector().then(({ externalDbRouter }) => { +export const createApp = async () => { const app = express() - app.use(externalDbRouter.router) - - server = app.listen(8080, () => console.log('Connector listening on port 8080')) - - started = true -}) + const initConnectorResponse = await initConnector() + app.use(initConnectorResponse.externalDbRouter.router) + const server = app.listen(8080, () => console.log('Connector listening on port 8080')) -export const internals = () => ({ server, schemaProvider: _schemaProvider, cleanup: _cleanup, started, reload: initConnector, externalDbRouter }) + return { server, ...initConnectorResponse, reload: () => initConnector() } +} \ No newline at end of file diff --git a/apps/velo-external-db/src/main.ts b/apps/velo-external-db/src/main.ts new file mode 100644 index 000000000..9c828a5a9 --- /dev/null +++ b/apps/velo-external-db/src/main.ts @@ -0,0 +1,3 @@ +import { createApp } from "velo-external-db"; + +createApp() \ No newline at end of file diff --git a/apps/velo-external-db/test/e2e/app.e2e.spec.ts b/apps/velo-external-db/test/e2e/app.e2e.spec.ts index 86301d32e..d8f673173 100644 --- a/apps/velo-external-db/test/e2e/app.e2e.spec.ts +++ b/apps/velo-external-db/test/e2e/app.e2e.spec.ts @@ -1,4 +1,4 @@ -import { authOwner } from '../drivers/auth_test_support' +import { authOwner } from '@wix-velo/external-db-testkit' import { initApp, teardownApp, dbTeardown, setupDb, currentDbImplementationName } from '../resources/e2e_resources' const axios = require('axios').create({ diff --git a/apps/velo-external-db/test/e2e/app_auth.e2e.spec.ts b/apps/velo-external-db/test/e2e/app_auth.e2e.spec.ts index f1c0c8cd9..fd533b696 100644 --- a/apps/velo-external-db/test/e2e/app_auth.e2e.spec.ts +++ b/apps/velo-external-db/test/e2e/app_auth.e2e.spec.ts @@ -1,5 +1,5 @@ import { Uninitialized, gen } from '@wix-velo/test-commons' -import { authVisitor, authOwnerWithoutSecretKey, errorResponseWith } from '../drivers/auth_test_support' +import { authVisitor, authOwnerWithoutSecretKey, errorResponseWith } from '@wix-velo/external-db-testkit' import each from 'jest-each' import { initApp, teardownApp, dbTeardown, setupDb, currentDbImplementationName } from '../resources/e2e_resources' diff --git a/apps/velo-external-db/test/e2e/app_data.e2e.spec.ts b/apps/velo-external-db/test/e2e/app_data.e2e.spec.ts index a0909ceaf..a6c8fe32c 100644 --- a/apps/velo-external-db/test/e2e/app_data.e2e.spec.ts +++ b/apps/velo-external-db/test/e2e/app_data.e2e.spec.ts @@ -6,7 +6,7 @@ import * as gen from '../gen' import * as schema from '../drivers/schema_api_rest_test_support' import * as data from '../drivers/data_api_rest_test_support' import * as matchers from '../drivers/schema_api_rest_matchers' -import { authAdmin, authOwner, authVisitor } from '../drivers/auth_test_support' +import { authAdmin, authOwner, authVisitor } from '@wix-velo/external-db-testkit' import * as authorization from '../drivers/authorization_test_support' import Chance = require('chance') import { initApp, teardownApp, dbTeardown, setupDb, currentDbImplementationName, supportedOperations } from '../resources/e2e_resources' @@ -20,7 +20,6 @@ const axios = require('axios').create({ describe(`Velo External DB Data REST API: ${currentDbImplementationName()}`, () => { beforeAll(async() => { await setupDb() - await initApp() }, 20000) diff --git a/apps/velo-external-db/test/e2e/app_data_hooks.e2e.spec.ts b/apps/velo-external-db/test/e2e/app_data_hooks.e2e.spec.ts index d9bcbe283..b433232c8 100644 --- a/apps/velo-external-db/test/e2e/app_data_hooks.e2e.spec.ts +++ b/apps/velo-external-db/test/e2e/app_data_hooks.e2e.spec.ts @@ -1,4 +1,4 @@ -import { authOwner, errorResponseWith } from '../drivers/auth_test_support' +import { authOwner, errorResponseWith } from '@wix-velo/external-db-testkit' import { testSupportedOperations } from '@wix-velo/test-commons' const { UpdateImmediately, DeleteImmediately, Aggregate } = require('@wix-velo/velo-external-db-commons').SchemaOperations const each = require('jest-each').default diff --git a/apps/velo-external-db/test/e2e/app_schema.e2e.spec.ts b/apps/velo-external-db/test/e2e/app_schema.e2e.spec.ts index 33b69f032..975ef72ca 100644 --- a/apps/velo-external-db/test/e2e/app_schema.e2e.spec.ts +++ b/apps/velo-external-db/test/e2e/app_schema.e2e.spec.ts @@ -3,7 +3,7 @@ import { SchemaOperations } from '@wix-velo/velo-external-db-types' const { RemoveColumn } = SchemaOperations import * as schema from '../drivers/schema_api_rest_test_support' import * as matchers from '../drivers/schema_api_rest_matchers' -import { authOwner } from '../drivers/auth_test_support' +import { authOwner } from '@wix-velo/external-db-testkit' import * as gen from '../gen' import Chance = require('chance') import { initApp, teardownApp, dbTeardown, setupDb, currentDbImplementationName, supportedOperations } from '../resources/e2e_resources' diff --git a/apps/velo-external-db/test/e2e/app_schema_hooks.e2e.spec.ts b/apps/velo-external-db/test/e2e/app_schema_hooks.e2e.spec.ts index b85ad8092..d857a87eb 100644 --- a/apps/velo-external-db/test/e2e/app_schema_hooks.e2e.spec.ts +++ b/apps/velo-external-db/test/e2e/app_schema_hooks.e2e.spec.ts @@ -1,4 +1,4 @@ -import { authOwner, errorResponseWith } from '../drivers/auth_test_support' +import { authOwner, errorResponseWith } from '@wix-velo/external-db-testkit' const each = require('jest-each').default import { initApp, teardownApp, dbTeardown, setupDb, currentDbImplementationName, env } from '../resources/e2e_resources' import gen = require('../gen') @@ -7,6 +7,7 @@ import schema = require('../drivers/schema_api_rest_test_support') import hooks = require('../drivers/hooks_test_support') import { Uninitialized } from '@wix-velo/test-commons' + import { ServiceContext } from 'libs/velo-external-db-core/src/types' const axios = require('axios').create({ diff --git a/apps/velo-external-db/test/env/env.db.setup.js b/apps/velo-external-db/test/env/env.db.setup.js index ebce6a000..22b402c47 100644 --- a/apps/velo-external-db/test/env/env.db.setup.js +++ b/apps/velo-external-db/test/env/env.db.setup.js @@ -3,16 +3,16 @@ import { registerTsProject } from 'nx/src/utils/register' registerTsProject('.', 'tsconfig.base.json') -const postgres = require('../resources/engines/postgres_resources') -const mysql = require('../resources/engines/mysql_resources') -const spanner = require('../resources/engines/spanner_resources') -const firestore = require('../resources/engines/firestore_resources') -const mssql = require('../resources/engines/mssql_resources') -const mongo = require ('../resources/engines/mongo_resources') -const googleSheet = require('../resources/engines/google_sheets_resources') -const airtable = require ('../resources/engines/airtable_resources') -const dynamoDb = require ('../resources/engines/dynamodb_resources') -const bigquery = require ('../resources/engines/bigquery_resources') +const { testResources: postgres } = require ('@wix-velo/external-db-postgres') +const { testResources: mysql } = require ('@wix-velo/external-db-mysql') +const { testResources: spanner } = require ('@wix-velo/external-db-spanner') +const { testResources: firestore } = require ('@wix-velo/external-db-firestore') +const { testResources: mssql } = require ('@wix-velo/external-db-mssql') +const { testResources: mongo } = require ('@wix-velo/external-db-mongo') +const { testResources: googleSheet } = require('@wix-velo/external-db-google-sheets') +const { testResources: airtable } = require('@wix-velo/external-db-airtable') +const { testResources: dynamoDb } = require('@wix-velo/external-db-dynamodb') +const { testResources: bigquery } = require('@wix-velo/external-db-bigquery') const { sleep } = require('@wix-velo/test-commons') const ci = require('./ci_utils') diff --git a/apps/velo-external-db/test/env/env.db.teardown.js b/apps/velo-external-db/test/env/env.db.teardown.js index 6d97a0494..63a4e09c7 100644 --- a/apps/velo-external-db/test/env/env.db.teardown.js +++ b/apps/velo-external-db/test/env/env.db.teardown.js @@ -1,12 +1,14 @@ -const postgres = require('../resources/engines/postgres_resources') -const mysql = require('../resources/engines/mysql_resources') -const spanner = require('../resources/engines/spanner_resources') -const firestore = require('../resources/engines/firestore_resources') -const mssql = require('../resources/engines/mssql_resources') -const googleSheet = require('../resources/engines/google_sheets_resources') -const airtable = require('../resources/engines/airtable_resources') -const dynamo = require('../resources/engines/dynamodb_resources') -const mongo = require ('../resources/engines/mongo_resources') +const { testResources: postgres } = require ('@wix-velo/external-db-postgres') +const { testResources: mysql } = require ('@wix-velo/external-db-mysql') +const { testResources: spanner } = require ('@wix-velo/external-db-spanner') +const { testResources: firestore } = require ('@wix-velo/external-db-firestore') +const { testResources: mssql } = require ('@wix-velo/external-db-mssql') +const { testResources: mongo } = require ('@wix-velo/external-db-mongo') +const { testResources: googleSheet } = require('@wix-velo/external-db-google-sheets') +const { testResources: airtable } = require('@wix-velo/external-db-airtable') +const { testResources: dynamo } = require('@wix-velo/external-db-dynamodb') +const { testResources: bigquery } = require('@wix-velo/external-db-bigquery') + const ci = require('./ci_utils') const shutdownEnv = async(testEngine) => { @@ -46,6 +48,10 @@ const shutdownEnv = async(testEngine) => { case 'mongo': await mongo.shutdownEnv() break + + case 'bigquery': + await bigquery.shutdownEnv() + break } } diff --git a/apps/velo-external-db/test/resources/e2e_resources.ts b/apps/velo-external-db/test/resources/e2e_resources.ts index 913d63f9a..70b33cb6e 100644 --- a/apps/velo-external-db/test/resources/e2e_resources.ts +++ b/apps/velo-external-db/test/resources/e2e_resources.ts @@ -1,21 +1,21 @@ -import { Uninitialized, sleep } from '@wix-velo/test-commons' -import { suiteDef } from './test_suite_definition' -import { authInit } from '../drivers/auth_test_support' -import { waitUntil } from 'async-wait-until' +import { createApp } from 'velo-external-db' -import * as postgres from'./engines/postgres_resources' -import * as mysql from'./engines/mysql_resources' -import * as spanner from'./engines/spanner_resources' -import * as firestore from'./engines/firestore_resources' -import * as mssql from'./engines/mssql_resources' -import * as mongo from'./engines/mongo_resources' -import * as airtable from'./engines/airtable_resources' -import * as dynamo from'./engines/dynamodb_resources' -import * as bigquery from'./engines/bigquery_resources' +import { testResources as postgres } from '@wix-velo/external-db-postgres' +import { testResources as mysql } from '@wix-velo/external-db-mysql' +import { testResources as spanner } from '@wix-velo/external-db-spanner' +import { testResources as firestore } from '@wix-velo/external-db-firestore' +import { testResources as mssql } from '@wix-velo/external-db-mssql' +import { testResources as mongo } from '@wix-velo/external-db-mongo' +import { testResources as airtable } from '@wix-velo/external-db-airtable' +import { testResources as googleSheet } from '@wix-velo/external-db-google-sheets' +import { testResources as dynamo } from '@wix-velo/external-db-dynamodb' +import { testResources as bigquery } from '@wix-velo/external-db-bigquery' + +import { E2EResources } from '@wix-velo/external-db-testkit' +import { Uninitialized } from '@wix-velo/test-commons' +import { ExternalDbRouter } from '@wix-velo/velo-external-db-core' import { Server } from 'http' import { ConnectionCleanUp, ISchemaProvider } from '@wix-velo/velo-external-db-types' -import { ExternalDbRouter } from '@wix-velo/velo-external-db-core' -// const googleSheet = require('./engines/google_sheets_resources') interface App { server: Server; @@ -30,7 +30,7 @@ interface App { type Internals = () => App -export const env:{ +export let env:{ app: App, externalDbRouter: ExternalDbRouter, internals: Internals @@ -40,61 +40,27 @@ export const env:{ externalDbRouter: Uninitialized } -export const initApp = async() => { - process.env.CLOUD_VENDOR = 'azure' - - if (env.app) { - await env.app.reload() - } else { - authInit() - env.internals = require('../../src/app').internals - - await waitUntil(() => env.internals().started) - } - env.app = env.internals() - env.externalDbRouter = env.app.externalDbRouter -} - -export const teardownApp = async() => { - await sleep(500) - await env.app.server.close() -} - -const dbInit = async impl => { - await impl.cleanup() - impl.setActive() -} - -export const dbTeardown = async() => { - await env.app.cleanup() -} - -const postgresTestEnvInit = async() => await dbInit(postgres) -const mysqlTestEnvInit = async() => await dbInit(mysql) -const spannerTestEnvInit = async() => await dbInit(spanner) -const firestoreTestEnvInit = async() => await dbInit(firestore) -const mssqlTestEnvInit = async() => await dbInit(mssql) -const mongoTestEnvInit = async() => await dbInit(mongo) -const airTableTestEnvInit = async() => await dbInit(airtable) -const dynamoTestEnvInit = async() => await dbInit(dynamo) -const bigqueryTestEnvInit = async() => await dbInit(bigquery) -// const googleSheetTestEnvInit = async() => await dbInit(googleSheet) - const testSuits = { - mysql: suiteDef('MySql', mysqlTestEnvInit, mysql.supportedOperations), - postgres: suiteDef('Postgres', postgresTestEnvInit, postgres.supportedOperations), - spanner: suiteDef('Spanner', spannerTestEnvInit, spanner.supportedOperations), - firestore: suiteDef('Firestore', firestoreTestEnvInit, firestore.supportedOperations), - mssql: suiteDef('Sql Server', mssqlTestEnvInit, mssql.supportedOperations), - mongo: suiteDef('Mongo', mongoTestEnvInit, mongo.supportedOperations), - airtable: suiteDef('Airtable', airTableTestEnvInit, airtable.supportedOperations), - dynamodb: suiteDef('DynamoDb', dynamoTestEnvInit, dynamo.supportedOperations), - bigquery: suiteDef('BigQuery', bigqueryTestEnvInit, bigquery.supportedOperations), - // 'google-sheet': suiteDef('Google-sheet', googleSheetTestEnvInit, googleSheet.supportedOperations), + mysql: new E2EResources(mysql, createApp), + postgres: new E2EResources(postgres, createApp), + spanner: new E2EResources(spanner, createApp), + firestore: new E2EResources(firestore, createApp), + mssql: new E2EResources(mssql, createApp), + mongo: new E2EResources(mongo, createApp), + 'google-sheet': new E2EResources(googleSheet, createApp), + airtable: new E2EResources(airtable, createApp), + dynamodb: new E2EResources(dynamo, createApp), + bigquery: new E2EResources(bigquery, createApp), } -const testedSuit = () => testSuits[process.env.TEST_ENGINE] + +export const testedSuit = () => testSuits[process.env.TEST_ENGINE] export const supportedOperations = testedSuit().supportedOperations -export const setupDb = () => testedSuit().setup() +export const setupDb = () => testedSuit().setUpDb() export const currentDbImplementationName = () => testedSuit().name +export const initApp = async () => { + env = await testedSuit().initApp() +} +export const teardownApp = async () => testedSuit().teardownApp() +export const dbTeardown = async () => testedSuit().dbTeardown() diff --git a/apps/velo-external-db/test/resources/engines/docker_support.ts b/apps/velo-external-db/test/resources/engines/docker_support.ts deleted file mode 100644 index 08a010f65..000000000 --- a/apps/velo-external-db/test/resources/engines/docker_support.ts +++ /dev/null @@ -1,9 +0,0 @@ -import * as compose from 'docker-compose' - -export const runImage = async(image: string, showLogs?: boolean) => { - await compose.upOne(image, { cwd: __dirname, log: true, commandOptions: [['--force-recreate', '--remove-orphans']] }) - if (showLogs) { - await compose.logs(image, { cwd: __dirname, log: true }) - } -} -export const stopImage = async(image: string) => await compose.stopOne(image, { cwd: __dirname, log: true }) diff --git a/apps/velo-external-db/test/resources/engines/firestore_resources.ts b/apps/velo-external-db/test/resources/engines/firestore_resources.ts deleted file mode 100644 index a6a9613ac..000000000 --- a/apps/velo-external-db/test/resources/engines/firestore_resources.ts +++ /dev/null @@ -1,39 +0,0 @@ -import { init } from '@wix-velo/external-db-firestore' -import { runImage, stopImage } from './docker_support' -export { supportedOperations } from '@wix-velo/external-db-firestore' - -const setEmulatorOn = () => process.env.FIRESTORE_EMULATOR_HOST = 'localhost:8082' - -export const connection = () => { - const { connection, schemaProvider, cleanup } = init({ projectId: 'test-project' }) - - return { pool: connection, schemaProvider, cleanup } -} - -export const cleanup = async() => { - setEmulatorOn() - const { schemaProvider, cleanup } = init({ projectId: 'test-project' }) - const res = await schemaProvider.list() - const tables = res.map(t => t.id) - - for (const t of tables) { - await schemaProvider.drop(t) - } - - await cleanup() -} - -export const initEnv = async() => { - - await runImage('firestore') -} - -export const setActive = () => { - setEmulatorOn() - process.env.TYPE = 'firestore' - process.env.PROJECT_ID = 'test-project' -} - -export const shutdownEnv = async() => { - await stopImage('firestore') -} diff --git a/apps/velo-external-db/test/resources/provider_resources.ts b/apps/velo-external-db/test/resources/provider_resources.ts index e71492826..09ab98997 100644 --- a/apps/velo-external-db/test/resources/provider_resources.ts +++ b/apps/velo-external-db/test/resources/provider_resources.ts @@ -2,31 +2,22 @@ import { Uninitialized } from '@wix-velo/test-commons' import { suiteDef } from './test_suite_definition' import * as mysql from '@wix-velo/external-db-mysql' -import * as mysqlTestEnv from './engines/mysql_resources' import * as spanner from '@wix-velo/external-db-spanner' -import * as spannerTestEnv from './engines/spanner_resources' import * as postgres from '@wix-velo/external-db-postgres' -import * as postgresTestEnv from './engines/postgres_resources' import * as firestore from '@wix-velo/external-db-firestore' -import * as firestoreTestEnv from './engines/firestore_resources' import * as mssql from '@wix-velo/external-db-mssql' -import * as mssqlTestEnv from './engines/mssql_resources' import * as mongo from '@wix-velo/external-db-mongo' -import * as mongoTestEnv from './engines/mongo_resources' import * as airtable from '@wix-velo/external-db-airtable' -import * as airtableEnv from './engines/airtable_resources' import * as dynamo from '@wix-velo/external-db-dynamodb' -import * as dynamoTestEnv from './engines/dynamodb_resources' import * as bigquery from '@wix-velo/external-db-bigquery' -import * as bigqueryTestEnv from './engines/bigquery_resources' import { AnyFixMe, ConnectionCleanUp, IDataProvider, ISchemaProvider } from '@wix-velo/velo-external-db-types' // const googleSheet = require('@wix-velo/external-db-google-sheets') @@ -44,14 +35,15 @@ export const env: { driver: Uninitialized, } -const dbInit = async(testEnv: any, impl: any) => { - await testEnv.cleanup() +const dbInit = async(impl: any) => { + const testResources = impl.testResources + await testResources.cleanup() - const { pool, cleanup } = await testEnv.connection() + const { pool, cleanup } = await testResources.connection() const driver = impl.driver() env.dataProvider = new impl.DataProvider(pool, driver.filterParser) - env.schemaProvider = new impl.SchemaProvider(pool, testEnv.schemaProviderTestVariables?.() ) + env.schemaProvider = new impl.SchemaProvider(pool, testResources.schemaProviderTestVariables?.() ) env.driver = driver env.cleanup = cleanup } @@ -63,15 +55,15 @@ export const dbTeardown = async() => { env.driver = Uninitialized } -const postgresTestEnvInit = async() => await dbInit(postgresTestEnv, postgres) -const mysqlTestEnvInit = async() => await dbInit(mysqlTestEnv, mysql) -const spannerTestEnvInit = async() => await dbInit(spannerTestEnv, spanner) -const firestoreTestEnvInit = async() => await dbInit(firestoreTestEnv, firestore) -const mssqlTestEnvInit = async() => await dbInit(mssqlTestEnv, mssql) -const mongoTestEnvInit = async() => await dbInit(mongoTestEnv, mongo) -const airTableTestEnvInit = async() => await dbInit(airtableEnv, airtable) -const dynamoTestEnvInit = async() => await dbInit(dynamoTestEnv, dynamo) -const bigqueryTestEnvInit = async() => await dbInit(bigqueryTestEnv, bigquery) +const postgresTestEnvInit = async() => await dbInit(postgres) +const mysqlTestEnvInit = async() => await dbInit(mysql) +const spannerTestEnvInit = async() => await dbInit(spanner) +const firestoreTestEnvInit = async() => await dbInit(firestore) +const mssqlTestEnvInit = async() => await dbInit(mssql) +const mongoTestEnvInit = async() => await dbInit(mongo) +const airTableTestEnvInit = async() => await dbInit(airtable) +const dynamoTestEnvInit = async() => await dbInit(dynamo) +const bigqueryTestEnvInit = async() => await dbInit(bigquery) // const googleSheetTestEnvInit = async() => await dbInit(googleSheetTestEnv, googleSheet) const testSuits = { diff --git a/jest.config.ts b/jest.config.ts new file mode 100644 index 000000000..010e8c5f2 --- /dev/null +++ b/jest.config.ts @@ -0,0 +1,5 @@ +import { getJestProjects } from '@nrwl/jest'; + +export default { +projects: getJestProjects() +}; \ No newline at end of file diff --git a/libs/external-db-airtable/src/index.ts b/libs/external-db-airtable/src/index.ts index 8005c51a9..359d43345 100644 --- a/libs/external-db-airtable/src/index.ts +++ b/libs/external-db-airtable/src/index.ts @@ -6,7 +6,7 @@ export { default as init } from './connection_provider' export { default as DatabaseOperations } from './airtable_operations' export { supportedOperations } from './supported_operations' export { app as mockServer } from '../tests/drivers/mock_air_table' - +export * as testResources from '../tests/e2e-testkit/airtable_resources' import init from './connection_provider' import { AirtableConfigValidator } from './airtable_config_validator' import { DbConnector } from '@wix-velo/velo-external-db-commons' diff --git a/apps/velo-external-db/test/resources/engines/airtable_resources.ts b/libs/external-db-airtable/tests/e2e-testkit/airtable_resources.ts similarity index 71% rename from apps/velo-external-db/test/resources/engines/airtable_resources.ts rename to libs/external-db-airtable/tests/e2e-testkit/airtable_resources.ts index 4215d4384..d9490fead 100644 --- a/apps/velo-external-db/test/resources/engines/airtable_resources.ts +++ b/libs/external-db-airtable/tests/e2e-testkit/airtable_resources.ts @@ -1,5 +1,6 @@ -import { init, mockServer } from '@wix-velo/external-db-airtable' -export { supportedOperations } from '@wix-velo/external-db-airtable' +import init from '../../src/connection_provider' +import { app as mockServer } from '../drivers/mock_air_table' +export { supportedOperations } from '../../src/supported_operations' import { Server } from 'http' let _server: Server @@ -29,11 +30,11 @@ export const shutdownEnv = async() => { } export const setActive = () => { - process.env.AIRTABLE_API_KEY = 'key123' - process.env.META_API_KEY = 'meta123' - process.env.TYPE = 'airtable' - process.env.BASE_ID = 'app123' - process.env.BASE_URL = 'http://localhost:9000' + process.env['AIRTABLE_API_KEY'] = 'key123' + process.env['META_API_KEY'] = 'meta123' + process.env['TYPE'] = 'airtable' + process.env['BASE_ID'] = 'app123' + process.env['BASE_URL'] = 'http://localhost:9000' } export const schemaProviderTestVariables = () => ( @@ -45,3 +46,5 @@ export const schemaProviderTestVariables = () => ( ) const connectionConfig = () => ({ apiPrivateKey: 'key123', baseId: 'app123', metaApiKey: 'meta123', baseUrl: `http://localhost:${PORT}` }) + +export const name = 'airtable' \ No newline at end of file diff --git a/libs/external-db-bigquery/src/index.js b/libs/external-db-bigquery/src/index.js index e181c682f..fecbd7087 100644 --- a/libs/external-db-bigquery/src/index.js +++ b/libs/external-db-bigquery/src/index.js @@ -7,7 +7,7 @@ const SchemaColumnTranslator = require('./sql_schema_translator') const init = require('./connection_provider') const DatabaseOperations = require ('./bigquery_operations') const { supportedOperations } = require('./supported_operations') - +const testResources = require ('../tests/e2e-testkit/bigquery_resources') const driver = () => require('../tests/drivers/sql_filter_transformer_test_support') const opsDriver = () => require('../tests/drivers/db_operations_test_support') @@ -24,4 +24,4 @@ const bigqueryFactory = async(config, options) => { return { connector, connection, providers, cleanup } } -module.exports = { SchemaProvider, DataProvider, FilterParser, SchemaColumnTranslator, driver, opsDriver, init, DatabaseOperations, supportedOperations, BigQueryConnector, bigqueryFactory } +module.exports = { SchemaProvider, DataProvider, FilterParser, SchemaColumnTranslator, driver, opsDriver, init, DatabaseOperations, supportedOperations, BigQueryConnector, bigqueryFactory, testResources } diff --git a/apps/velo-external-db/test/resources/engines/bigquery_resources.ts b/libs/external-db-bigquery/tests/e2e-testkit/bigquery_resources.ts similarity index 65% rename from apps/velo-external-db/test/resources/engines/bigquery_resources.ts rename to libs/external-db-bigquery/tests/e2e-testkit/bigquery_resources.ts index 914ba68ec..784dd32e7 100644 --- a/apps/velo-external-db/test/resources/engines/bigquery_resources.ts +++ b/libs/external-db-bigquery/tests/e2e-testkit/bigquery_resources.ts @@ -1,5 +1,5 @@ -import { init } from '@wix-velo/external-db-bigquery' -export { supportedOperations } from '@wix-velo/external-db-bigquery' +const init = require ('../../src/connection_provider') +export { supportedOperations } from '../../src/supported_operations' const databaseId = 'testDB' const projectId = 'corvid-managed-cfe9809c' @@ -12,19 +12,21 @@ export const connection = () => { export const cleanup = async() => { const { schemaProvider } = init({ databaseId, projectId }) const tables = await schemaProvider.list() - await Promise.all(tables.map(t => t.id).map( t => schemaProvider.drop(t) )) + await Promise.all(tables.map((t: { id: any }) => t.id).map( (t: any) => schemaProvider.drop(t) )) } // eslint-disable-next-line @typescript-eslint/no-empty-function export const initEnv = async() => {} export const setActive = () => { - process.env.TYPE = 'bigquery' - process.env.PROJECT_ID = projectId - process.env.DATABASE_ID = databaseId + process.env['TYPE'] = 'bigquery' + process.env['PROJECT_ID'] = projectId + process.env['DATABASE_ID'] = databaseId } // eslint-disable-next-line @typescript-eslint/no-empty-function export const shutdownEnv = async() => {} export const schemaProviderTestVariables = () => ({ projectId, databaseId }) + +export const name = 'bigquery' diff --git a/libs/external-db-deletme/.babelrc b/libs/external-db-deletme/.babelrc new file mode 100644 index 000000000..cf7ddd99c --- /dev/null +++ b/libs/external-db-deletme/.babelrc @@ -0,0 +1,3 @@ +{ + "presets": [["@nrwl/web/babel", { "useBuiltIns": "usage" }]] +} diff --git a/libs/external-db-deletme/.eslintrc.json b/libs/external-db-deletme/.eslintrc.json new file mode 100644 index 000000000..53132676a --- /dev/null +++ b/libs/external-db-deletme/.eslintrc.json @@ -0,0 +1,33 @@ +{ + "extends": [ + "../../.eslintrc.json" + ], + "ignorePatterns": [ + "!**/*" + ], + "overrides": [ + { + "files": [ + "*.ts", + "*.tsx", + "*.js", + "*.jsx" + ], + "rules": {} + }, + { + "files": [ + "*.ts", + "*.tsx" + ], + "rules": {} + }, + { + "files": [ + "*.js", + "*.jsx" + ], + "rules": {} + } + ] +} diff --git a/libs/external-db-deletme/README.md b/libs/external-db-deletme/README.md new file mode 100644 index 000000000..4eb213232 --- /dev/null +++ b/libs/external-db-deletme/README.md @@ -0,0 +1,14 @@ +# external-db-deletme + +This library was generated with [Nx](https://nx.dev). + + +## Running unit tests + +Run `nx test external-db-deletme` to execute the unit tests via [Jest](https://jestjs.io). + + +## Running lint + +Run `nx lint external-db-deletme` to execute the lint via [ESLint](https://eslint.org/). + diff --git a/libs/external-db-deletme/jest.config.ts b/libs/external-db-deletme/jest.config.ts new file mode 100644 index 000000000..8e45d18af --- /dev/null +++ b/libs/external-db-deletme/jest.config.ts @@ -0,0 +1,17 @@ +/* eslint-disable */ +export default { + displayName: 'external-db-deletme', + preset: '../../jest.preset.js', + globals: { + 'ts-jest': { + tsconfig: '/tsconfig.spec.json', + } + }, + testEnvironment: 'node', + transform: { + '^.+\\.[tj]sx?$': 'ts-jest' + }, + testTimeout: 30000, + moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx'], + coverageDirectory: '../../coverage/libs/external-db-deletme' +}; diff --git a/libs/external-db-deletme/project.json b/libs/external-db-deletme/project.json new file mode 100644 index 000000000..e999cd549 --- /dev/null +++ b/libs/external-db-deletme/project.json @@ -0,0 +1,29 @@ +{ + "$schema": "../../node_modules/nx/schemas/project-schema.json", + "sourceRoot": "libs/external-db-deletme/src", + "projectType": "library", + "targets": { + "lint": { + "executor": "@nrwl/linter:eslint", + "outputs": [ + "{options.outputFile}" + ], + "options": { + "lintFilePatterns": [ + "libs/external-db-deletme/**/*.ts" + ] + } + }, + "test": { + "executor": "@nrwl/jest:jest", + "outputs": [ + "coverage/libs/external-db-deletme" + ], + "options": { + "jestConfig": "libs/external-db-deletme/jest.config.ts", + "passWithNoTests": true + } + } + }, + "tags": [] +} diff --git a/libs/external-db-deletme/src/app.js b/libs/external-db-deletme/src/app.js new file mode 100644 index 000000000..22881eb09 --- /dev/null +++ b/libs/external-db-deletme/src/app.js @@ -0,0 +1,35 @@ +const express = require('express') +const { ExternalDbRouter } = require('@wix-velo/velo-external-db-core') +const { mySqlFactory } = require('@wix-velo/external-db-mysql') + +const main = async() => { + const { connector: engineConnector, cleanup } = await mySqlFactory({ + user: 'test-user', + host: 'localhost', + password: 'password', + db: 'test-db' + }) + + const externalDbRouter = new ExternalDbRouter({ + connector: engineConnector, + config: { + secretKey: 'secretKey' + }, + hooks: { + schemaHooks: { + beforeCreate: (payload, _requestContext, _serviceContext) => { + return { ...payload, collectionName: payload.collectionName + '-hook' } + } + } + } + }) + + const app = express() + app.use(externalDbRouter.router) + + const server = app.listen(8080, () => console.log('Listening on port 8080')) + + return { server, externalDbRouter, cleanup } +} + +module.exports = { main } \ No newline at end of file diff --git a/libs/external-db-deletme/src/index.ts b/libs/external-db-deletme/src/index.ts new file mode 100644 index 000000000..524d2701e --- /dev/null +++ b/libs/external-db-deletme/src/index.ts @@ -0,0 +1,3 @@ +export * from './lib/external-db-deletme'; + + diff --git a/libs/external-db-deletme/src/lib/external-db-deletme.spec.ts b/libs/external-db-deletme/src/lib/external-db-deletme.spec.ts new file mode 100644 index 000000000..d8bbda9ff --- /dev/null +++ b/libs/external-db-deletme/src/lib/external-db-deletme.spec.ts @@ -0,0 +1,42 @@ +import { externalDbDeletme } from './external-db-deletme'; +import * as mysql from '@wix-velo/external-db-mysql' +import { authOwner, E2EResources } from '@wix-velo/external-db-testkit' +import { main } from '../app' +const axios = require('axios').create({ + baseURL: 'http://localhost:8080' +}) + +let env: E2EResources +describe('externalDbDeletme', () => { + beforeAll(async () => { + env = new E2EResources(mysql.testResources, main) + await env.globalSetUp() + }) + + test('list', async () => { + await expect(axios.post('/schemas/list', {}, authOwner)).resolves.toEqual(expect.objectContaining({ + data: { + schemas: [] + } + })) + }) + + test('should add -hook suffix to collection', async () => { + await axios.post('/schemas/create', { collectionName: 'collection' }, authOwner) + + await expect(axios.post('/schemas/find', { schemaIds: ['collection-hook'] }, authOwner)).resolves.toEqual(expect.objectContaining({ + data: + { + schemas: expect.arrayContaining([ + expect.objectContaining({ + id: 'collection-hook' + }) + ]) + } + })) + }) + + afterAll(async () => { + await env.globalTeardown() + }) +}) diff --git a/libs/external-db-deletme/src/lib/external-db-deletme.ts b/libs/external-db-deletme/src/lib/external-db-deletme.ts new file mode 100644 index 000000000..da79c1bbf --- /dev/null +++ b/libs/external-db-deletme/src/lib/external-db-deletme.ts @@ -0,0 +1,4 @@ +export function externalDbDeletme(): string { + return 'external-db-deletme'; +} + diff --git a/libs/external-db-deletme/src/support.js b/libs/external-db-deletme/src/support.js new file mode 100644 index 000000000..d55af6336 --- /dev/null +++ b/libs/external-db-deletme/src/support.js @@ -0,0 +1,42 @@ + +const axios = require('axios').create({ + baseURL: 'http://localhost:8080', +}) + +const secretKey = 'secretKey' + +const authInit = () => { + process.env.SECRET_KEY = secretKey +} + +const appendSecretKeyToRequest = dataRaw => { + const data = JSON.parse( dataRaw ) + return JSON.stringify({ ...data, ...{ requestContext: { settings: { secretKey: secretKey } } } }) +} + +const appendRoleToRequest = role => dataRaw => { + const data = JSON.parse( dataRaw ) + return JSON.stringify({ ...data, ...{ requestContext: { ...data.requestContext, role: role } } }) +} + +const authAdmin = { transformRequest: axios.defaults + .transformRequest + .concat( appendSecretKeyToRequest, appendRoleToRequest('BACKEND_CODE') ) } + +const authOwner = { transformRequest: axios.defaults + .transformRequest + .concat( appendSecretKeyToRequest, appendRoleToRequest('OWNER' ) ) } + +const authVisitor = { transformRequest: axios.defaults + .transformRequest + .concat( appendSecretKeyToRequest, appendRoleToRequest('VISITOR' ) ) } + +const authOwnerWithoutSecretKey = { transformRequest: axios.defaults + .transformRequest + .concat( appendRoleToRequest('OWNER' ) ) } + +const errorResponseWith = (status, message) => ({ response: { data: { message: expect.stringContaining(message) }, status } }) + + +module.exports = { authInit, authAdmin, authOwner, authVisitor, authOwnerWithoutSecretKey, errorResponseWith } + diff --git a/libs/external-db-deletme/tsconfig.json b/libs/external-db-deletme/tsconfig.json new file mode 100644 index 000000000..62ebbd946 --- /dev/null +++ b/libs/external-db-deletme/tsconfig.json @@ -0,0 +1,13 @@ +{ + "extends": "../../tsconfig.base.json", + "files": [], + "include": [], + "references": [ + { + "path": "./tsconfig.lib.json" + }, + { + "path": "./tsconfig.spec.json" + } + ] +} diff --git a/libs/external-db-deletme/tsconfig.lib.json b/libs/external-db-deletme/tsconfig.lib.json new file mode 100644 index 000000000..0e2a172ab --- /dev/null +++ b/libs/external-db-deletme/tsconfig.lib.json @@ -0,0 +1,11 @@ +{ + "extends": "./tsconfig.json", + "compilerOptions": { + "module": "commonjs", + "outDir": "../../dist/out-tsc", + "declaration": true, + "types": ["node"] + }, + "exclude": ["jest.config.ts", "**/*.spec.ts", "**/*.test.ts"], + "include": ["**/*.ts"] +} diff --git a/libs/external-db-deletme/tsconfig.spec.json b/libs/external-db-deletme/tsconfig.spec.json new file mode 100644 index 000000000..ff08addd6 --- /dev/null +++ b/libs/external-db-deletme/tsconfig.spec.json @@ -0,0 +1,20 @@ +{ + "extends": "./tsconfig.json", + "compilerOptions": { + "outDir": "../../dist/out-tsc", + "module": "commonjs", + "types": ["jest", "node"] + }, + "include": [ + "jest.config.ts", + "**/*.test.ts", + "**/*.spec.ts", + "**/*.test.tsx", + "**/*.spec.tsx", + "**/*.test.js", + "**/*.spec.js", + "**/*.test.jsx", + "**/*.spec.jsx", + "**/*.d.ts" + ] +} diff --git a/libs/external-db-dynamodb/src/index.ts b/libs/external-db-dynamodb/src/index.ts index 4fea73c45..2ec241661 100644 --- a/libs/external-db-dynamodb/src/index.ts +++ b/libs/external-db-dynamodb/src/index.ts @@ -4,6 +4,7 @@ export { default as FilterParser } from './sql_filter_transformer' export { default as DatabaseOperations } from './dynamo_operations' export { default as init } from './connection_provider' export { supportedOperations } from './supported_operations' +export * as testResources from '../tests/e2e-testkit/dynamodb_resources' import init from './connection_provider' import { DbConnector } from '@wix-velo/velo-external-db-commons' diff --git a/libs/external-db-dynamodb/tests/e2e-testkit/docker-compose.yaml b/libs/external-db-dynamodb/tests/e2e-testkit/docker-compose.yaml new file mode 100644 index 000000000..4076d6f0b --- /dev/null +++ b/libs/external-db-dynamodb/tests/e2e-testkit/docker-compose.yaml @@ -0,0 +1,11 @@ +version: "3.9" +services: + dynamodb: + image: amazon/dynamodb-local:latest + restart: always + ports: + - 8000:8000 + environment: + AWS_REGION: us-west-2 + AWS_ACCESS_KEY_ID: TEST_ACCESS_KEY_ID + AWS_SECRET_ACCESS_KEY: TEST_SECRET_ACCESS_KEY \ No newline at end of file diff --git a/apps/velo-external-db/test/resources/engines/dynamodb_resources.ts b/libs/external-db-dynamodb/tests/e2e-testkit/dynamodb_resources.ts similarity index 59% rename from apps/velo-external-db/test/resources/engines/dynamodb_resources.ts rename to libs/external-db-dynamodb/tests/e2e-testkit/dynamodb_resources.ts index fe67d4ac5..98808c7ec 100644 --- a/apps/velo-external-db/test/resources/engines/dynamodb_resources.ts +++ b/libs/external-db-dynamodb/tests/e2e-testkit/dynamodb_resources.ts @@ -1,6 +1,6 @@ -import { init } from '@wix-velo/external-db-dynamodb' -import { runImage, stopImage } from './docker_support' -export { supportedOperations } from '@wix-velo/external-db-dynamodb' +import init from '../../src/connection_provider' +export { supportedOperations } from '../../src/supported_operations' +import * as compose from 'docker-compose' export const connection = async() => { const { connection, schemaProvider, cleanup } = init(connectionConfig(), accessOptions()) @@ -16,19 +16,19 @@ export const cleanup = async() => { } export const initEnv = async() => { - await runImage('dynamodb') + await compose.upOne('dynamodb', { cwd: __dirname, log: true, commandOptions: [['--force-recreate', '--remove-orphans']] }) } export const shutdownEnv = async() => { - await stopImage('dynamodb') + await compose.stopOne('dynamodb', { cwd: __dirname, log: true }) } export const setActive = () => { - process.env.TYPE = 'dynamodb' - process.env.REGION = 'us-west-2' - process.env.AWS_SECRET_ACCESS_KEY = 'TEST_SECRET_ACCESS_KEY' - process.env.AWS_ACCESS_KEY_ID = 'TEST_ACCESS_KEY_ID' - process.env.ENDPOINT_URL = 'http://localhost:8000' + process.env['TYPE'] = 'dynamodb' + process.env['REGION'] = 'us-west-2' + process.env['AWS_SECRET_ACCESS_KEY'] = 'TEST_SECRET_ACCESS_KEY' + process.env['AWS_ACCESS_KEY_ID'] = 'TEST_ACCESS_KEY_ID' + process.env['ENDPOINT_URL'] = 'http://localhost:8000' } const connectionConfig = () => ({ endpoint: 'http://localhost:8000', @@ -37,3 +37,5 @@ const connectionConfig = () => ({ endpoint: 'http://localhost:8000', const accessOptions = () => ({ credentials: { accessKeyId: 'TEST_ACCESS_KEY_ID', secretAccessKey: 'TEST_SECRET_ACCESS_KEY' } }) + +export const name = 'dynamodb' \ No newline at end of file diff --git a/libs/external-db-firestore/src/index.js b/libs/external-db-firestore/src/index.js index c14bf8837..d226f6a15 100644 --- a/libs/external-db-firestore/src/index.js +++ b/libs/external-db-firestore/src/index.js @@ -4,6 +4,8 @@ const ConfigValidator = require('./firestore_config_validator') const init = require('./connection_provider') const { supportedOperations } = require('./supported_operations') const { DbConnector } = require('@wix-velo/velo-external-db-commons') +const testResources = require ('../tests/e2e-testkit/firestore_resources') + const driver = () => require('../tests/drivers/sql_filter_transformer_test_support') const opsDriver = () => require('../tests/drivers/db_operations_test_support') @@ -21,4 +23,4 @@ const firestoreFactory = async(config, options) => { } -module.exports = { SchemaProvider, init, driver, opsDriver, DataProvider, supportedOperations, FirestoreConnector, firestoreFactory } +module.exports = { SchemaProvider, init, driver, opsDriver, DataProvider, supportedOperations, FirestoreConnector, firestoreFactory, testResources } diff --git a/libs/external-db-firestore/tests/e2e-testkit/docker-compose.yaml b/libs/external-db-firestore/tests/e2e-testkit/docker-compose.yaml new file mode 100644 index 000000000..408f8a581 --- /dev/null +++ b/libs/external-db-firestore/tests/e2e-testkit/docker-compose.yaml @@ -0,0 +1,16 @@ +version: "3.9" +services: + firestore: + image: groovex/firestore-emulator:latest + ports: + - 8082:8082 + build: + args: + GCLOUD_SDK_VERSION: 360.0.0 + context: . + networks: + - backend + +networks: + backend: + driver: bridge \ No newline at end of file diff --git a/libs/external-db-firestore/tests/e2e-testkit/firestore_resources.ts b/libs/external-db-firestore/tests/e2e-testkit/firestore_resources.ts new file mode 100644 index 000000000..67e1cae5a --- /dev/null +++ b/libs/external-db-firestore/tests/e2e-testkit/firestore_resources.ts @@ -0,0 +1,42 @@ +export { supportedOperations } from '../../src/supported_operations' +const init = require('../../src/connection_provider') + +import * as compose from 'docker-compose' + +const setEmulatorOn = () => process.env['FIRESTORE_EMULATOR_HOST'] = 'localhost:8082' + +export const connection = () => { + const { connection, schemaProvider, cleanup } = init({ projectId: 'test-project' }) + + return { pool: connection, schemaProvider, cleanup } +} + +export const cleanup = async() => { + setEmulatorOn() + const { schemaProvider, cleanup } = init({ projectId: 'test-project' }) + const res = await schemaProvider.list() + const tables = res.map((t: { id: any }) => t.id) + + for (const t of tables) { + await schemaProvider.drop(t) + } + + await cleanup() +} + +export const initEnv = async() => { + await compose.upOne('firestore', { cwd: __dirname, log: true, commandOptions: [['--force-recreate', '--remove-orphans']] }) + +} + +export const setActive = () => { + setEmulatorOn() + process.env['TYPE'] = 'firestore' + process.env['PROJECT_ID'] = 'test-project' +} + +export const shutdownEnv = async() => { + await compose.stopOne('firestore', { cwd: __dirname, log: true }) +} + +export const name = 'firestore' diff --git a/libs/external-db-google-sheets/src/index.js b/libs/external-db-google-sheets/src/index.js index bb6261c40..83426576e 100644 --- a/libs/external-db-google-sheets/src/index.js +++ b/libs/external-db-google-sheets/src/index.js @@ -5,6 +5,7 @@ const ConfigValidator = require('./google_sheet_config_validator') const init = require('./connection_provider') const { app: mockServer, cleanupSheets } = require('../tests/mock_google_sheets_api') const { supportedOperations } = require('./supported_operations') +const testResources = require ('../tests/e2e-testkit/google_sheets_resources') const driver = () => require('../tests/sql_filter_transformer_test_support.js') @@ -23,4 +24,4 @@ const googleSheetFactory = async(config, options) => { -module.exports = { SchemaProvider, DataProvider, init, mockServer, driver, cleanupSheets, supportedOperations, GoogleSheetConnector, googleSheetFactory } \ No newline at end of file +module.exports = { SchemaProvider, DataProvider, init, mockServer, driver, cleanupSheets, supportedOperations, GoogleSheetConnector, googleSheetFactory, testResources } \ No newline at end of file diff --git a/apps/velo-external-db/test/resources/engines/google_sheets_resources.ts b/libs/external-db-google-sheets/tests/e2e-testkit/google_sheets_resources.ts similarity index 51% rename from apps/velo-external-db/test/resources/engines/google_sheets_resources.ts rename to libs/external-db-google-sheets/tests/e2e-testkit/google_sheets_resources.ts index c87998b20..ad330de6b 100644 --- a/apps/velo-external-db/test/resources/engines/google_sheets_resources.ts +++ b/libs/external-db-google-sheets/tests/e2e-testkit/google_sheets_resources.ts @@ -1,6 +1,7 @@ -import { init, mockServer } from '@wix-velo/external-db-google-sheets' +const init = require('../../src/connection_provider') +const { app: mockServer} = require ('../mock_google_sheets_api') import { Server } from 'http' -export { supportedOperations } from '@wix-velo/external-db-google-sheets' +export { supportedOperations } from '../../src/supported_operations' let _server: Server, port = 1502 @@ -13,7 +14,7 @@ export const cleanup = async() => { // todo: add cleanup logic } -export const initEnv = async() => { +export const initEnv = async() => { _server = mockServer.listen(port) } @@ -22,8 +23,10 @@ export const shutdownEnv = async() => { } export const setActive = () => { - process.env.TYPE = 'google-sheet' - process.env.CLIENT_EMAIL = 'client-email' - process.env.SHEET_ID = 'test-sheet-id' - process.env.API_PRIVATE_KEY = 'test-api-key' + process.env['TYPE'] = 'google-sheet' + process.env['CLIENT_EMAIL'] = 'client-email' + process.env['SHEET_ID'] = 'test-sheet-id' + process.env['API_PRIVATE_KEY'] = 'test-api-key' } + +export const name = 'google-sheets' diff --git a/libs/external-db-mongo/src/index.ts b/libs/external-db-mongo/src/index.ts index e410e3c6d..860e54ac4 100644 --- a/libs/external-db-mongo/src/index.ts +++ b/libs/external-db-mongo/src/index.ts @@ -4,6 +4,7 @@ export { default as FilterParser } from './sql_filter_transformer' export { default as DatabaseOperations } from './mongo_operations' export { default as init } from './connection_provider' export { supportedOperations } from './supported_operations' +export * as testResources from '../tests/e2e-testkit/mongo_resources' import init from './connection_provider' import { MongoConfigValidator } from './mongo_config_validator' diff --git a/libs/external-db-mongo/tests/e2e-testkit/docker-compose.yaml b/libs/external-db-mongo/tests/e2e-testkit/docker-compose.yaml new file mode 100644 index 000000000..36abdc0d4 --- /dev/null +++ b/libs/external-db-mongo/tests/e2e-testkit/docker-compose.yaml @@ -0,0 +1,17 @@ +version: "3.9" +services: + mongo: + image: mongo:5 + environment: + MONGO_INITDB_DATABASE: testdb + MONGO_INITDB_ROOT_USERNAME: root + MONGO_INITDB_ROOT_PASSWORD: pass + restart: always + ports: + - 27017:27017 + volumes: + - ./init-mongo.js:/docker-entrypoint-initdb.d/init-mongo.js:ro + +networks: + backend: + driver: bridge diff --git a/libs/external-db-mongo/tests/e2e-testkit/init-mongo.js b/libs/external-db-mongo/tests/e2e-testkit/init-mongo.js new file mode 100644 index 000000000..6d327c235 --- /dev/null +++ b/libs/external-db-mongo/tests/e2e-testkit/init-mongo.js @@ -0,0 +1,13 @@ +/* eslint-disable no-undef */ +db.createUser( + { + user: 'root', + pwd: 'pass', + roles: [ + { + role: 'dbOwner', + db: 'testdb' + } + ] + } +) diff --git a/apps/velo-external-db/test/resources/engines/mongo_resources.ts b/libs/external-db-mongo/tests/e2e-testkit/mongo_resources.ts similarity index 68% rename from apps/velo-external-db/test/resources/engines/mongo_resources.ts rename to libs/external-db-mongo/tests/e2e-testkit/mongo_resources.ts index e8493b2b9..62722d74c 100644 --- a/apps/velo-external-db/test/resources/engines/mongo_resources.ts +++ b/libs/external-db-mongo/tests/e2e-testkit/mongo_resources.ts @@ -1,5 +1,5 @@ import { init } from '@wix-velo/external-db-mongo' -import { runImage, stopImage } from './docker_support' +import * as compose from 'docker-compose' export { supportedOperations } from '@wix-velo/external-db-mongo' export const connection = async() => { @@ -18,14 +18,16 @@ export const cleanup = async() => { } export const initEnv = async() => { - await runImage('mongo') + await compose.upOne('mongo', { cwd: __dirname, log: true, commandOptions: [['--force-recreate', '--remove-orphans']] }) } export const shutdownEnv = async() => { - await stopImage('mongo') + await compose.stopOne('mongo', { cwd: __dirname, log: true }) } export const setActive = () => { - process.env.TYPE = 'mongo' - process.env.URI = 'mongodb://root:pass@localhost/testdb' + process.env['TYPE'] = 'mongo' + process.env['URI'] = 'mongodb://root:pass@localhost/testdb' } + +export const name = 'mongo' diff --git a/libs/external-db-mssql/src/index.ts b/libs/external-db-mssql/src/index.ts index be438a761..68acfb589 100644 --- a/libs/external-db-mssql/src/index.ts +++ b/libs/external-db-mssql/src/index.ts @@ -5,6 +5,8 @@ export { default as SchemaColumnTranslator} from './sql_schema_translator' export { default as DatabaseOperations } from './mssql_operations' export { default as init } from './connection_provider' export { supportedOperations } from './supported_operations' +export * as testResources from '../tests/e2e-testkit/mssql_resources' + import init from './connection_provider' import { DbConnector } from '@wix-velo/velo-external-db-commons' diff --git a/libs/external-db-mssql/tests/e2e-testkit/docker-compose.yaml b/libs/external-db-mssql/tests/e2e-testkit/docker-compose.yaml new file mode 100644 index 000000000..753773db3 --- /dev/null +++ b/libs/external-db-mssql/tests/e2e-testkit/docker-compose.yaml @@ -0,0 +1,22 @@ +version: "3.9" +services: + mssql: + image: mcr.microsoft.com/mssql/server:2019-latest + ports: + - 1433:1433 + environment: + SA_PASSWORD: "t9D4:EHfU6Xgccs-" + ACCEPT_EULA: "Y" + networks: + - backend + command: + - /bin/bash + - -c + - | + /opt/mssql/bin/sqlservr + curl -s https://raw.githubusercontent.com/vishnubob/wait-for-it/master/wait-for-it.sh | bash /dev/stdin localhost:1433 + + +networks: + backend: + driver: bridge \ No newline at end of file diff --git a/apps/velo-external-db/test/resources/engines/mssql_resources.ts b/libs/external-db-mssql/tests/e2e-testkit/mssql_resources.ts similarity index 66% rename from apps/velo-external-db/test/resources/engines/mssql_resources.ts rename to libs/external-db-mssql/tests/e2e-testkit/mssql_resources.ts index 6fad08d86..0e3db2941 100644 --- a/apps/velo-external-db/test/resources/engines/mssql_resources.ts +++ b/libs/external-db-mssql/tests/e2e-testkit/mssql_resources.ts @@ -1,6 +1,6 @@ import { init } from '@wix-velo/external-db-mssql' -import { runImage, stopImage } from './docker_support' export { supportedOperations } from '@wix-velo/external-db-mssql' +import * as compose from 'docker-compose' const testEnvConfig = { host: 'localhost', @@ -34,18 +34,20 @@ export const cleanup = async() => { } export const initEnv = async() => { - await runImage('mssql') + await compose.upOne('mssql', { cwd: __dirname, log: true, commandOptions: [['--force-recreate', '--remove-orphans']] }) } export const shutdownEnv = async() => { - await stopImage('mssql') + await compose.stopOne('mssql', { cwd: __dirname, log: true }) } export const setActive = () => { - process.env.TYPE = 'mssql' - process.env.HOST = 'localhost' - process.env.USER = 'sa' - process.env.PASSWORD = 't9D4:EHfU6Xgccs-' - process.env.DB = 'tempdb' - process.env.UNSECURED_ENV = 'true' + process.env['TYPE'] = 'mssql' + process.env['HOST'] = 'localhost' + process.env['USER'] = 'sa' + process.env['PASSWORD'] = 't9D4:EHfU6Xgccs-' + process.env['DB'] = 'tempdb' + process.env['UNSECURED_ENV'] = 'true' } + +export const name = 'mssql' diff --git a/libs/external-db-mysql/src/index.ts b/libs/external-db-mysql/src/index.ts index 41e6504c8..9d7ab87b0 100644 --- a/libs/external-db-mysql/src/index.ts +++ b/libs/external-db-mysql/src/index.ts @@ -5,6 +5,7 @@ export { default as SchemaColumnTranslator } from './sql_schema_translator' export { default as init } from './connection_provider' export { default as DatabaseOperations } from './mysql_operations' export { supportedOperations } from './supported_operations' +export * as testResources from '../tests/e2e-testkit/mysql_resources' import { MySqlConfigValidator } from './mysql_config_validator' import { DatabaseFactoryResponse, DbConnector } from '@wix-velo/velo-external-db-commons' diff --git a/libs/external-db-mysql/tests/e2e-testkit/docker-compose.yaml b/libs/external-db-mysql/tests/e2e-testkit/docker-compose.yaml new file mode 100644 index 000000000..558361968 --- /dev/null +++ b/libs/external-db-mysql/tests/e2e-testkit/docker-compose.yaml @@ -0,0 +1,19 @@ +version: "3.9" +services: + mysql: + image: mysql:8 + command: --default-authentication-plugin=mysql_native_password + restart: always + environment: + MYSQL_USER: test-user + MYSQL_PASSWORD: password + MYSQL_DATABASE: test-db + MYSQL_ALLOW_EMPTY_PASSWORD: "yes" + ports: + - 3306:3306 + networks: + - backend + +networks: + backend: + driver: bridge \ No newline at end of file diff --git a/apps/velo-external-db/test/resources/engines/mysql_resources.ts b/libs/external-db-mysql/tests/e2e-testkit/mysql_resources.ts similarity index 64% rename from apps/velo-external-db/test/resources/engines/mysql_resources.ts rename to libs/external-db-mysql/tests/e2e-testkit/mysql_resources.ts index d5e9b6d1b..37b9854b3 100644 --- a/apps/velo-external-db/test/resources/engines/mysql_resources.ts +++ b/libs/external-db-mysql/tests/e2e-testkit/mysql_resources.ts @@ -1,7 +1,8 @@ import { init } from '@wix-velo/external-db-mysql' -import { runImage, stopImage } from './docker_support' import { waitUntil } from 'async-wait-until' export { supportedOperations } from '@wix-velo/external-db-mysql' +import * as compose from 'docker-compose' + export const connection = () => { const { connection, schemaProvider, cleanup } = init({ host: 'localhost', user: 'test-user', password: 'password', db: 'test-db' }, { connectionLimit: 1, queueLimit: 0 }) @@ -17,18 +18,20 @@ export const cleanup = async() => { await cleanup() } -export const initEnv = async() => { - await runImage('mysql') +export const initEnv = async () => { + await compose.upOne('mysql', { cwd: __dirname, log: true, commandOptions: [['--force-recreate', '--remove-orphans']] }) } -export const shutdownEnv = async() => { - await stopImage('mysql') +export const shutdownEnv = async () => { + await compose.stopOne('mysql', { cwd: __dirname, log: true }) } export const setActive = () => { - process.env.TYPE = 'mysql' - process.env.HOST = 'localhost' - process.env.USER = 'test-user' - process.env.PASSWORD = 'password' - process.env.DB = 'test-db' + process.env['TYPE'] = 'mysql' + process.env['HOST'] = 'localhost' + process.env['USER'] = 'test-user' + process.env['PASSWORD'] = 'password' + process.env['DB'] = 'test-db' } + +export const name = 'mysql' diff --git a/libs/external-db-postgres/src/index.ts b/libs/external-db-postgres/src/index.ts index 2cb23ff84..9a461b195 100644 --- a/libs/external-db-postgres/src/index.ts +++ b/libs/external-db-postgres/src/index.ts @@ -11,6 +11,7 @@ export { supportedOperations } from './supported_operations' export { default as init } from './connection_provider' export const driver = () => require('../tests/drivers/sql_filter_transformer_test_support') export const opsDriver = () => require('../tests/drivers/db_operations_test_support') +export * as testResources from '../tests/e2e-testkit/postgres_resources' export class PostgresConnector extends DbConnector { constructor() { diff --git a/libs/external-db-postgres/tests/e2e-testkit/docker-compose.yaml b/libs/external-db-postgres/tests/e2e-testkit/docker-compose.yaml new file mode 100644 index 000000000..c4afecb61 --- /dev/null +++ b/libs/external-db-postgres/tests/e2e-testkit/docker-compose.yaml @@ -0,0 +1,15 @@ +version: "3.9" +services: + postgres: + image: postgres:latest + restart: always + ports: + - 5432:5432 + environment: + POSTGRES_USER: test-user + POSTGRES_PASSWORD: password + POSTGRES_DB: test-db + +networks: + backend: + driver: bridge \ No newline at end of file diff --git a/apps/velo-external-db/test/resources/engines/postgres_resources.ts b/libs/external-db-postgres/tests/e2e-testkit/postgres_resources.ts similarity index 64% rename from apps/velo-external-db/test/resources/engines/postgres_resources.ts rename to libs/external-db-postgres/tests/e2e-testkit/postgres_resources.ts index 94dd9d6db..9f47ef74b 100644 --- a/apps/velo-external-db/test/resources/engines/postgres_resources.ts +++ b/libs/external-db-postgres/tests/e2e-testkit/postgres_resources.ts @@ -1,6 +1,6 @@ import { init } from '@wix-velo/external-db-postgres' -import { runImage, stopImage } from './docker_support' export { supportedOperations } from '@wix-velo/external-db-postgres' +import * as compose from 'docker-compose' export const connection = () => { const { connection, schemaProvider, cleanup } = init({ host: 'localhost', user: 'test-user', password: 'password', db: 'test-db' }, { max: 1 }) @@ -16,17 +16,19 @@ export const cleanup = async() => { } export const initEnv = async() => { - await runImage('postgres') + await compose.upOne('postgres', { cwd: __dirname, log: true, commandOptions: [['--force-recreate', '--remove-orphans']] }) } export const setActive = () => { - process.env.TYPE = 'postgres' - process.env.HOST = 'localhost' - process.env.USER = 'test-user' - process.env.PASSWORD = 'password' - process.env.DB = 'test-db' + process.env['TYPE'] = 'postgres' + process.env['HOST'] = 'localhost' + process.env['USER'] = 'test-user' + process.env['PASSWORD'] = 'password' + process.env['DB'] = 'test-db' } export const shutdownEnv = async() => { - await stopImage('postgres') + await compose.stopOne('postgres', { cwd: __dirname, log: true }) } + +export const name = 'postgres' diff --git a/libs/external-db-spanner/src/index.ts b/libs/external-db-spanner/src/index.ts index ad8fc6e31..2e6b6b0d7 100644 --- a/libs/external-db-spanner/src/index.ts +++ b/libs/external-db-spanner/src/index.ts @@ -5,6 +5,7 @@ export {default as SchemaColumnTranslator} from './sql_schema_translator' export { default as init } from './connection_provider' export { default as DatabaseOperations } from './spanner_operations' export { supportedOperations } from './supported_operations' +export * as testResources from '../tests/e2e-testkit/spanner_resources' import { ConfigValidator } from './spanner_config_validator' import { DbConnector } from '@wix-velo/velo-external-db-commons' diff --git a/libs/external-db-spanner/tests/e2e-testkit/docker-compose.yaml b/libs/external-db-spanner/tests/e2e-testkit/docker-compose.yaml new file mode 100644 index 000000000..c7e847942 --- /dev/null +++ b/libs/external-db-spanner/tests/e2e-testkit/docker-compose.yaml @@ -0,0 +1,17 @@ +version: "3.9" +services: + spanner: + image: roryq/spanner-emulator:latest + ports: + - 9010:9010 + - 9020:9020 + environment: + SPANNER_DATABASE_ID: test-database + SPANNER_INSTANCE_ID: test-instance + SPANNER_PROJECT_ID: test-project + networks: + - backend + +networks: + backend: + driver: bridge \ No newline at end of file diff --git a/apps/velo-external-db/test/resources/engines/spanner_resources.ts b/libs/external-db-spanner/tests/e2e-testkit/spanner_resources.ts similarity index 62% rename from apps/velo-external-db/test/resources/engines/spanner_resources.ts rename to libs/external-db-spanner/tests/e2e-testkit/spanner_resources.ts index 0bb30b7e2..c262c04d4 100644 --- a/apps/velo-external-db/test/resources/engines/spanner_resources.ts +++ b/libs/external-db-spanner/tests/e2e-testkit/spanner_resources.ts @@ -1,8 +1,8 @@ import { init } from '@wix-velo/external-db-spanner' -import { runImage, stopImage } from './docker_support' export { supportedOperations } from '@wix-velo/external-db-spanner' +import * as compose from 'docker-compose' -const setEmulatorOn = () => process.env.SPANNER_EMULATOR_HOST = 'localhost:9010' +const setEmulatorOn = () => process.env['SPANNER_EMULATOR_HOST'] = 'localhost:9010' export const connection = () => { const { connection, schemaProvider, cleanup } = init({ projectId: 'test-project', instanceId: 'test-instance', databaseId: 'test-database' }) @@ -23,17 +23,19 @@ export const cleanup = async() => { } export const initEnv = async() => { - await runImage('spanner') + await compose.upOne('spanner', { cwd: __dirname, log: true, commandOptions: [['--force-recreate', '--remove-orphans']] }) } export const setActive = () => { setEmulatorOn() - process.env.TYPE = 'spanner' - process.env.PROJECT_ID = 'test-project' - process.env.INSTANCE_ID = 'test-instance' - process.env.DATABASE_ID = 'test-database' + process.env['TYPE'] = 'spanner' + process.env['PROJECT_ID'] = 'test-project' + process.env['INSTANCE_ID'] = 'test-instance' + process.env['DATABASE_ID'] = 'test-database' } export const shutdownEnv = async() => { - await stopImage('spanner') + await compose.stopOne('spanner', { cwd: __dirname, log: true }) } + +export const name = 'spanner' diff --git a/libs/external-db-testkit/.eslintrc.json b/libs/external-db-testkit/.eslintrc.json new file mode 100644 index 000000000..9d9c0db55 --- /dev/null +++ b/libs/external-db-testkit/.eslintrc.json @@ -0,0 +1,18 @@ +{ + "extends": ["../../.eslintrc.json"], + "ignorePatterns": ["!**/*"], + "overrides": [ + { + "files": ["*.ts", "*.tsx", "*.js", "*.jsx"], + "rules": {} + }, + { + "files": ["*.ts", "*.tsx"], + "rules": {} + }, + { + "files": ["*.js", "*.jsx"], + "rules": {} + } + ] +} diff --git a/libs/external-db-testkit/README.md b/libs/external-db-testkit/README.md new file mode 100644 index 000000000..e69de29bb diff --git a/libs/external-db-testkit/jest.config.ts b/libs/external-db-testkit/jest.config.ts new file mode 100644 index 000000000..5ac068f98 --- /dev/null +++ b/libs/external-db-testkit/jest.config.ts @@ -0,0 +1,17 @@ +export default { + displayName: 'external-db-testkit', + clearMocks: true, + verbose: true, + preset: '../../jest.preset.js', + globals: { + 'ts-jest': { + tsconfig: '/tsconfig.spec.json', + }, + }, + transform: { + '^.+\\.[tj]s$': 'ts-jest', + }, + moduleFileExtensions: ['ts', 'js', 'html'], + coverageDirectory: '../../coverage/libs/external-db-testkit', + testEnvironment: 'node' +} \ No newline at end of file diff --git a/libs/external-db-testkit/package.json b/libs/external-db-testkit/package.json new file mode 100644 index 000000000..a55d3f34e --- /dev/null +++ b/libs/external-db-testkit/package.json @@ -0,0 +1,5 @@ +{ + "name": "@wix-velo/external-db-testkit", + "version": "1.0.0", + "type": "commonjs" +} \ No newline at end of file diff --git a/libs/external-db-testkit/project.json b/libs/external-db-testkit/project.json new file mode 100644 index 000000000..2695fa211 --- /dev/null +++ b/libs/external-db-testkit/project.json @@ -0,0 +1,48 @@ +{ + "$schema": "../../node_modules/nx/schemas/project-schema.json", + "sourceRoot": "libs/external-db-testkit/src", + "projectType": "library", + "targets": { + "build": { + "executor": "@nrwl/js:tsc", + "outputs": [ "{options.outputPath}" ], + "options": { + "outputPath": "dist/libs/external-db-testkit", + "main": "libs/external-db-testkit/src/index.ts", + "tsConfig": "libs/external-db-testkit/tsconfig.lib.json", + "assets": [ "libs/external-db-testkit/*.md" ], + "buildableProjectDepsInPackageJsonType": "dependencies" + } + }, + "publish": { + "executor": "@nrwl/workspace:run-commands", + "options": { + "command": "node tools/scripts/publish.mjs @wix-velo/external-db-testkit {args.ver} {args.tag}" + }, + "dependsOn": [ + { + "projects": "self", + "target": "build" + } + ] + }, + "lint": { + "executor": "@nrwl/linter:eslint", + "outputs": [ + "{options.outputFile}" + ], + "options": { + "lintFilePatterns": [ "libs/external-db-testkit/**/*.ts" ] + } + }, + "test": { + "executor": "@nrwl/jest:jest", + "outputs": [ "coverage/libs/external-db-testkit" ], + "options": { + "jestConfig": "libs/external-db-testkit/jest.config.ts", + "passWithNoTests": true + } + } + }, + "tags": [] +} diff --git a/libs/external-db-testkit/src/index.ts b/libs/external-db-testkit/src/index.ts new file mode 100644 index 000000000..c03495c8d --- /dev/null +++ b/libs/external-db-testkit/src/index.ts @@ -0,0 +1,2 @@ +export { default as E2EResources } from "./lib/e2e_resources"; +export * from './lib/auth_test_support' \ No newline at end of file diff --git a/apps/velo-external-db/test/drivers/auth_test_support.ts b/libs/external-db-testkit/src/lib/auth_test_support.ts similarity index 94% rename from apps/velo-external-db/test/drivers/auth_test_support.ts rename to libs/external-db-testkit/src/lib/auth_test_support.ts index 3ae4d7a96..095e7c5a3 100644 --- a/apps/velo-external-db/test/drivers/auth_test_support.ts +++ b/libs/external-db-testkit/src/lib/auth_test_support.ts @@ -1,13 +1,14 @@ import * as Chance from 'chance' + const chance = Chance() const axios = require('axios').create({ baseURL: 'http://localhost:8080', }) -const secretKey = chance.word() +const secretKey = 'secretKey' export const authInit = () => { - process.env.SECRET_KEY = secretKey + process.env['SECRET_KEY'] = secretKey } const appendSecretKeyToRequest = (dataRaw: string) => { @@ -36,4 +37,5 @@ export const authOwnerWithoutSecretKey = { transformRequest: axios.defaults .transformRequest .concat( appendRoleToRequest('OWNER' ) ) } +//@ts-ignore export const errorResponseWith = (status: any, message: string) => ({ response: { data: { message: expect.stringContaining(message) }, status } }) diff --git a/libs/external-db-testkit/src/lib/docker_support.js b/libs/external-db-testkit/src/lib/docker_support.js new file mode 100644 index 000000000..33610c345 --- /dev/null +++ b/libs/external-db-testkit/src/lib/docker_support.js @@ -0,0 +1,11 @@ +const compose = require('docker-compose') + +const runImage = async(image, showLogs) => { + await compose.upOne(image, { cwd: __dirname, log: true, commandOptions: [['--force-recreate', '--remove-orphans']] }) + if (showLogs) { + await compose.logs(image, { cwd: __dirname, log: true }) + } +} +const stopImage = async(image) => await compose.stopOne(image, { cwd: __dirname, log: true }) + +module.exports = { runImage, stopImage } \ No newline at end of file diff --git a/libs/external-db-testkit/src/lib/e2e_resources.ts b/libs/external-db-testkit/src/lib/e2e_resources.ts new file mode 100644 index 000000000..e4e4a9e19 --- /dev/null +++ b/libs/external-db-testkit/src/lib/e2e_resources.ts @@ -0,0 +1,77 @@ +import { sleep } from '@wix-velo/test-commons' +import { ConnectionCleanUp, IImplementationResources, SchemaOperations } from '@wix-velo/velo-external-db-types'; +import { ExternalDbRouter } from '@wix-velo/velo-external-db-core' +import { Server } from 'http'; +import { authInit } from './auth_test_support'; + +type InitFunc = () => Promise<{ server: Server, externalDbRouter: ExternalDbRouter, cleanup: ConnectionCleanUp, [x: string]: any }> + +export default class E2EResources { + implementation: IImplementationResources; + initFunc: InitFunc; + env!: { + server: Server, externalDbRouter: ExternalDbRouter, cleanup: ConnectionCleanUp, [x: string]: any + }; + externalDbRouter!: ExternalDbRouter; + currentDbImplementationName: string; + supportedOperations: SchemaOperations[] + constructor(implementation: IImplementationResources, initFunc: InitFunc) { + this.implementation = implementation; + this.initFunc = initFunc; + this.currentDbImplementationName = this.implementation.name; + this.supportedOperations = this.implementation.supportedOperations; + } + + async globalSetUp() { + await this.initEnv() + await this.setUpDb() + await this.initApp() + } + + async globalTeardown() { + await this.dbTeardown() + await this.shutdownEnv() + await this.teardownApp() + } + async initEnv() { + await this.implementation.initEnv() + await sleep(5000) + } + + async setUpDb() { + await this.implementation.cleanup() + this.implementation.setActive() + } + + async shutdownEnv() { + await this.implementation.shutdownEnv() + } + + async initApp() { + process.env['CLOUD_VENDOR'] = 'azure' + if (this.env) { + console.log('closing server...') + await this.env.server.close() + } + else{ + authInit() + } + this.env = await this.initFunc() + this.env.externalDbRouter = this.env.externalDbRouter + return this.env + } + + async dbTeardown() { + await this.env.cleanup() + } + + async teardownApp() { + await sleep(500) + this.env.server.close() + } + + async dbInit() { + await this.implementation.cleanup() + this.implementation.setActive() + } +} diff --git a/libs/external-db-testkit/tsconfig.json b/libs/external-db-testkit/tsconfig.json new file mode 100644 index 000000000..9d7f15bc8 --- /dev/null +++ b/libs/external-db-testkit/tsconfig.json @@ -0,0 +1,23 @@ +{ + "extends": "../../tsconfig.base.json", + "compilerOptions": { + "module": "commonjs", + "allowJs": true, + "forceConsistentCasingInFileNames": true, + "strict": true, + "noImplicitOverride": true, + "noPropertyAccessFromIndexSignature": true, + "noImplicitReturns": true, + "noFallthroughCasesInSwitch": true + }, + "files": [], + "include": [], + "references": [ + { + "path": "./tsconfig.lib.json" + }, + { + "path": "./tsconfig.spec.json" + } + ] +} diff --git a/libs/external-db-testkit/tsconfig.lib.json b/libs/external-db-testkit/tsconfig.lib.json new file mode 100644 index 000000000..80e0aebae --- /dev/null +++ b/libs/external-db-testkit/tsconfig.lib.json @@ -0,0 +1,15 @@ +{ + "extends": "./tsconfig.json", + "compilerOptions": { + "outDir": "../../dist/out-tsc", + "declaration": true, + "types": [] + }, + "include": ["**/*.ts", "**/*.js"], + "exclude": [ + "jest.config.ts", + "**/*.spec.ts", + "**/*.test.ts", + "**/*.test.js" + ] +} diff --git a/libs/external-db-testkit/tsconfig.spec.json b/libs/external-db-testkit/tsconfig.spec.json new file mode 100644 index 000000000..546f12877 --- /dev/null +++ b/libs/external-db-testkit/tsconfig.spec.json @@ -0,0 +1,9 @@ +{ + "extends": "./tsconfig.json", + "compilerOptions": { + "outDir": "../../dist/out-tsc", + "module": "commonjs", + "types": ["jest", "node"] + }, + "include": ["jest.config.ts", "**/*.test.ts", "**/*.spec.ts", "**/*.d.ts"] +} diff --git a/libs/test-commons/src/index.ts b/libs/test-commons/src/index.ts index 20290e545..f223424ff 100644 --- a/libs/test-commons/src/index.ts +++ b/libs/test-commons/src/index.ts @@ -1,2 +1,2 @@ export * from './libs/test-commons' -export * as gen from './libs/gen' +export * as gen from './libs/gen' \ No newline at end of file diff --git a/libs/velo-external-db-types/src/index.ts b/libs/velo-external-db-types/src/index.ts index a57851a55..bac846cf1 100644 --- a/libs/velo-external-db-types/src/index.ts +++ b/libs/velo-external-db-types/src/index.ts @@ -241,3 +241,13 @@ export interface CollectionPermissions { export interface RoleConfig { collectionPermissions: CollectionPermissions[] } + +export interface IImplementationResources { + initEnv(): Promise + shutdownEnv(): Promise + setActive(): Promise | void + cleanup(): Promise | void + supportedOperations: SchemaOperations[] + name: string + // connection(): Promise | T +} \ No newline at end of file diff --git a/nx.json b/nx.json index 94a716414..f75b3d712 100644 --- a/nx.json +++ b/nx.json @@ -4,7 +4,13 @@ "default": { "runner": "nx/tasks-runners/default", "options": { - "cacheableOperations": [ "build", "test", "lint", "package", "prepare" ] + "cacheableOperations": [ + "build", + "test", + "lint", + "package", + "prepare" + ] } } }, diff --git a/tsconfig.base.json b/tsconfig.base.json index 40a1bc6c3..1b5ea37fa 100644 --- a/tsconfig.base.json +++ b/tsconfig.base.json @@ -10,30 +10,76 @@ "importHelpers": true, "target": "es2015", "module": "esnext", - "lib": [ "es2017", "dom" ], + "lib": [ + "es2017", + "dom" + ], "skipLibCheck": true, "skipDefaultLibCheck": true, "baseUrl": ".", "resolveJsonModule": true, "paths": { - "@wix-velo/external-db-config": [ "libs/external-db-config/src/index.ts" ], - "@wix-velo/external-db-mysql": [ "libs/external-db-mysql/src/index.ts" ], - "@wix-velo/external-db-postgres": [ "libs/external-db-postgres/src/index.ts" ], - "@wix-velo/external-db-mssql": [ "libs/external-db-mssql/src/index.ts" ], - "@wix-velo/external-db-spanner": [ "libs/external-db-spanner/src/index.ts" ], - "@wix-velo/external-db-mongo": [ "libs/external-db-mongo/src/index.ts" ], - "@wix-velo/external-db-firestore": [ "libs/external-db-firestore/src/index.js" ], - "@wix-velo/external-db-dynamodb": [ "libs/external-db-dynamodb/src/index.ts" ], - "@wix-velo/external-db-google-sheets": [ "libs/external-db-google-sheets/src/index.js" ], - "@wix-velo/external-db-airtable": [ "libs/external-db-airtable/src/index.ts" ], - "@wix-velo/external-db-bigquery": [ "libs/external-db-bigquery/src/index.js" ], - "@wix-velo/external-db-security": [ "libs/external-db-security/src/index.ts" ], - "@wix-velo/test-commons": [ "libs/test-commons/src/index.ts" ], - "@wix-velo/velo-external-db-commons": [ "libs/velo-external-db-commons/src/index.ts" ], - "@wix-velo/velo-external-db-core": [ "libs/velo-external-db-core/src/index.ts" ], - "@wix-velo/velo-external-db-types": ["libs/velo-external-db-types/src/index.ts"], - "velo-external-db": [ "apps/velo-external-db/src/app.ts" ] + "@/external-db-deletme": [ + "libs/external-db-deletme/src/index.ts" + ], + "@wix-velo/external-db-airtable": [ + "libs/external-db-airtable/src/index.ts" + ], + "@wix-velo/external-db-bigquery": [ + "libs/external-db-bigquery/src/index.js" + ], + "@wix-velo/external-db-config": [ + "libs/external-db-config/src/index.ts" + ], + "@wix-velo/external-db-dynamodb": [ + "libs/external-db-dynamodb/src/index.ts" + ], + "@wix-velo/external-db-firestore": [ + "libs/external-db-firestore/src/index.js" + ], + "@wix-velo/external-db-google-sheets": [ + "libs/external-db-google-sheets/src/index.js" + ], + "@wix-velo/external-db-mongo": [ + "libs/external-db-mongo/src/index.ts" + ], + "@wix-velo/external-db-mssql": [ + "libs/external-db-mssql/src/index.ts" + ], + "@wix-velo/external-db-mysql": [ + "libs/external-db-mysql/src/index.ts" + ], + "@wix-velo/external-db-postgres": [ + "libs/external-db-postgres/src/index.ts" + ], + "@wix-velo/external-db-security": [ + "libs/external-db-security/src/index.ts" + ], + "@wix-velo/external-db-spanner": [ + "libs/external-db-spanner/src/index.ts" + ], + "@wix-velo/external-db-testkit": [ + "libs/external-db-testkit/src/index.ts" + ], + "@wix-velo/test-commons": [ + "libs/test-commons/src/index.ts" + ], + "@wix-velo/velo-external-db-commons": [ + "libs/velo-external-db-commons/src/index.ts" + ], + "@wix-velo/velo-external-db-core": [ + "libs/velo-external-db-core/src/index.ts" + ], + "@wix-velo/velo-external-db-types": [ + "libs/velo-external-db-types/src/index.ts" + ], + "velo-external-db": [ + "apps/velo-external-db/src/app.ts" + ] } }, - "exclude": [ "node_modules", "tmp" ] + "exclude": [ + "node_modules", + "tmp" + ] } diff --git a/workspace.json b/workspace.json index 0f85fcc7a..b7c574ba3 100644 --- a/workspace.json +++ b/workspace.json @@ -1,22 +1,24 @@ { "version": 2, "projects": { - "@wix-velo/external-db-config": "libs/external-db-config", - "@wix-velo/external-db-postgres": "libs/external-db-postgres", - "@wix-velo/external-db-mysql": "libs/external-db-mysql", - "@wix-velo/external-db-mssql": "libs/external-db-mssql", - "@wix-velo/external-db-spanner": "libs/external-db-spanner", - "@wix-velo/external-db-mongo": "libs/external-db-mongo", - "@wix-velo/external-db-firestore": "libs/external-db-firestore", "@wix-velo/external-db-airtable": "libs/external-db-airtable", "@wix-velo/external-db-bigquery": "libs/external-db-bigquery", + "@wix-velo/external-db-config": "libs/external-db-config", "@wix-velo/external-db-dynamodb": "libs/external-db-dynamodb", + "@wix-velo/external-db-firestore": "libs/external-db-firestore", "@wix-velo/external-db-google-sheets": "libs/external-db-google-sheets", + "@wix-velo/external-db-mongo": "libs/external-db-mongo", + "@wix-velo/external-db-mssql": "libs/external-db-mssql", + "@wix-velo/external-db-mysql": "libs/external-db-mysql", + "@wix-velo/external-db-postgres": "libs/external-db-postgres", "@wix-velo/external-db-security": "libs/external-db-security", + "@wix-velo/external-db-spanner": "libs/external-db-spanner", + "@wix-velo/external-db-testkit": "libs/external-db-testkit", "@wix-velo/test-commons": "libs/test-commons", "@wix-velo/velo-external-db-commons": "libs/velo-external-db-commons", "@wix-velo/velo-external-db-core": "libs/velo-external-db-core", "@wix-velo/velo-external-db-types": "libs/velo-external-db-types", + "external-db-deletme": "libs/external-db-deletme", "velo-external-db": "apps/velo-external-db" } }