mirror of
https://github.com/typeorm/typeorm.git
synced 2025-12-08 21:26:23 +00:00
added test util scenario for spanner driver
This commit is contained in:
parent
e58c796071
commit
22d21b0a82
@ -5,13 +5,16 @@ import { EntityTarget } from "../common/EntityTarget"
|
||||
import { ObjectType } from "../common/ObjectType"
|
||||
import { EntityManager } from "../entity-manager/EntityManager"
|
||||
import { DefaultNamingStrategy } from "../naming-strategy/DefaultNamingStrategy"
|
||||
import { CannotExecuteNotConnectedError } from "../error/CannotExecuteNotConnectedError"
|
||||
import { CannotConnectAlreadyConnectedError } from "../error/CannotConnectAlreadyConnectedError"
|
||||
import {
|
||||
CannotConnectAlreadyConnectedError,
|
||||
CannotExecuteNotConnectedError,
|
||||
EntityMetadataNotFoundError,
|
||||
QueryRunnerProviderAlreadyReleasedError,
|
||||
} from "../error"
|
||||
import { TreeRepository } from "../repository/TreeRepository"
|
||||
import { NamingStrategyInterface } from "../naming-strategy/NamingStrategyInterface"
|
||||
import { EntityMetadata } from "../metadata/EntityMetadata"
|
||||
import { Logger } from "../logger/Logger"
|
||||
import { EntityMetadataNotFoundError } from "../error/EntityMetadataNotFoundError"
|
||||
import { MigrationInterface } from "../migration/MigrationInterface"
|
||||
import { MigrationExecutor } from "../migration/MigrationExecutor"
|
||||
import { Migration } from "../migration/Migration"
|
||||
@ -19,7 +22,6 @@ import { MongoRepository } from "../repository/MongoRepository"
|
||||
import { MongoEntityManager } from "../entity-manager/MongoEntityManager"
|
||||
import { EntityMetadataValidator } from "../metadata-builder/EntityMetadataValidator"
|
||||
import { DataSourceOptions } from "./DataSourceOptions"
|
||||
import { QueryRunnerProviderAlreadyReleasedError } from "../error/QueryRunnerProviderAlreadyReleasedError"
|
||||
import { EntityManagerFactory } from "../entity-manager/EntityManagerFactory"
|
||||
import { DriverFactory } from "../driver/DriverFactory"
|
||||
import { ConnectionMetadataBuilder } from "../connection/ConnectionMetadataBuilder"
|
||||
@ -33,7 +35,7 @@ import { RelationLoader } from "../query-builder/RelationLoader"
|
||||
import { ObjectUtils } from "../util/ObjectUtils"
|
||||
import { IsolationLevel } from "../driver/types/IsolationLevel"
|
||||
import { ReplicationMode } from "../driver/types/ReplicationMode"
|
||||
import { TypeORMError } from "../error/TypeORMError"
|
||||
import { TypeORMError } from "../error"
|
||||
import { RelationIdLoader } from "../query-builder/RelationIdLoader"
|
||||
import { DriverUtils } from "../driver/DriverUtils"
|
||||
import { InstanceChecker } from "../util/InstanceChecker"
|
||||
|
||||
@ -336,15 +336,15 @@ export class InsertQueryBuilder<Entity> extends QueryBuilder<Entity> {
|
||||
|
||||
/**
|
||||
* @deprecated
|
||||
*
|
||||
*
|
||||
* `.orUpdate({ columns: [ "is_updated" ] }).setParameter("is_updated", value)`
|
||||
*
|
||||
*
|
||||
* is now `.orUpdate(["is_updated"])`
|
||||
*
|
||||
*
|
||||
* `.orUpdate({ conflict_target: ['date'], overwrite: ['title'] })`
|
||||
*
|
||||
*
|
||||
* is now `.orUpdate(['title'], ['date'])`
|
||||
*
|
||||
*
|
||||
*/
|
||||
orUpdate(statement?: {
|
||||
columns?: string[]
|
||||
|
||||
@ -1,5 +1,8 @@
|
||||
import "../../utils/test-setup"
|
||||
import { setupTestingConnections } from "../../utils/test-utils"
|
||||
import {
|
||||
createDataSource,
|
||||
setupTestingConnections,
|
||||
} from "../../utils/test-utils"
|
||||
import { User } from "./entity/User"
|
||||
import { expect } from "chai"
|
||||
import { DataSource } from "../../../src"
|
||||
@ -37,7 +40,7 @@ describe("base entity", () => {
|
||||
// reset data source just to make sure inside DataSource it's really being set
|
||||
User.useDataSource(null)
|
||||
|
||||
const dataSource = new DataSource(dataSourceOptions[0])
|
||||
const dataSource = createDataSource(dataSourceOptions[0])
|
||||
await dataSource.initialize()
|
||||
await dataSource.synchronize(true)
|
||||
|
||||
|
||||
@ -1,7 +1,9 @@
|
||||
import { Entity } from "../../../../src/decorator/entity/Entity"
|
||||
import { PrimaryGeneratedColumn } from "../../../../src/decorator/columns/PrimaryGeneratedColumn"
|
||||
import { Column } from "../../../../src/decorator/columns/Column"
|
||||
import { BaseEntity } from "../../../../src"
|
||||
import {
|
||||
BaseEntity,
|
||||
Column,
|
||||
Entity,
|
||||
PrimaryGeneratedColumn,
|
||||
} from "../../../../src"
|
||||
|
||||
@Entity()
|
||||
export class User extends BaseEntity {
|
||||
|
||||
@ -1,12 +1,19 @@
|
||||
import { DataSource } from "../../src/data-source/DataSource"
|
||||
import { DataSourceOptions } from "../../src/data-source/DataSourceOptions"
|
||||
import { DatabaseType } from "../../src/driver/types/DatabaseType"
|
||||
import { EntitySchema } from "../../src/entity-schema/EntitySchema"
|
||||
import { createConnections } from "../../src/index"
|
||||
import { NamingStrategyInterface } from "../../src/naming-strategy/NamingStrategyInterface"
|
||||
import {
|
||||
DatabaseType,
|
||||
DataSource,
|
||||
DataSourceOptions,
|
||||
EntitySchema,
|
||||
EntitySubscriberInterface,
|
||||
getMetadataArgsStorage,
|
||||
InsertEvent,
|
||||
Logger,
|
||||
NamingStrategyInterface,
|
||||
} from "../../src"
|
||||
import { QueryResultCache } from "../../src/cache/QueryResultCache"
|
||||
import { Logger } from "../../src/logger/Logger"
|
||||
import path from "path"
|
||||
import { ObjectUtils } from "../../src/util/ObjectUtils"
|
||||
import { EntitySubscriberMetadataArgs } from "../../src/metadata-args/EntitySubscriberMetadataArgs"
|
||||
import { v4 as uuidv4 } from "uuid"
|
||||
|
||||
/**
|
||||
* Interface in which data is stored in ormconfig.json of the project.
|
||||
@ -57,7 +64,7 @@ export interface TestingOptions {
|
||||
/**
|
||||
* Subscribers needs to be included in the connection for the given test suite.
|
||||
*/
|
||||
subscribers?: string[] | Function[]
|
||||
subscribers?: (string | Function)[]
|
||||
|
||||
/**
|
||||
* Indicates if schema sync should be performed or not.
|
||||
@ -293,6 +300,65 @@ export function setupTestingConnections(
|
||||
})
|
||||
}
|
||||
|
||||
export function createDataSource(options: DataSourceOptions): DataSource {
|
||||
class GeneratedColumnReplacerSubscriber
|
||||
implements EntitySubscriberInterface
|
||||
{
|
||||
static globalIncrementValues: { [entityName: string]: number } = {}
|
||||
beforeInsert(event: InsertEvent<any>): Promise<any> | void {
|
||||
event.metadata.generatedColumns.map((column) => {
|
||||
if (column.generationStrategy === "increment") {
|
||||
if (
|
||||
!GeneratedColumnReplacerSubscriber
|
||||
.globalIncrementValues[event.metadata.tableName]
|
||||
) {
|
||||
GeneratedColumnReplacerSubscriber.globalIncrementValues[
|
||||
event.metadata.tableName
|
||||
] = 0
|
||||
}
|
||||
GeneratedColumnReplacerSubscriber.globalIncrementValues[
|
||||
event.metadata.tableName
|
||||
] += 1
|
||||
|
||||
column.setEntityValue(
|
||||
event.entity,
|
||||
GeneratedColumnReplacerSubscriber.globalIncrementValues[
|
||||
event.metadata.tableName
|
||||
],
|
||||
)
|
||||
} else if (column.generationStrategy === "uuid") {
|
||||
column.setEntityValue(event.entity, uuidv4())
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// todo: uncomment later
|
||||
if (options.type === ("spanner" as any)) {
|
||||
getMetadataArgsStorage().entitySubscribers.push({
|
||||
target: GeneratedColumnReplacerSubscriber,
|
||||
} as EntitySubscriberMetadataArgs)
|
||||
|
||||
if (Array.isArray(options.subscribers)) {
|
||||
options.subscribers.push(
|
||||
GeneratedColumnReplacerSubscriber as Function,
|
||||
)
|
||||
} else if (ObjectUtils.isObject(options.subscribers)) {
|
||||
options.subscribers["GeneratedColumnReplacer"] =
|
||||
GeneratedColumnReplacerSubscriber
|
||||
} else {
|
||||
options = {
|
||||
...options,
|
||||
subscribers: {
|
||||
GeneratedColumnReplacer: GeneratedColumnReplacerSubscriber,
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return new DataSource(options)
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a testing connections based on the configuration in the ormconfig.json
|
||||
* and given options that can override some of its configuration for the test-specific use case.
|
||||
@ -300,11 +366,16 @@ export function setupTestingConnections(
|
||||
export async function createTestingConnections(
|
||||
options?: TestingOptions,
|
||||
): Promise<DataSource[]> {
|
||||
const connections = await createConnections(
|
||||
setupTestingConnections(options),
|
||||
)
|
||||
const dataSourceOptions = setupTestingConnections(options)
|
||||
const dataSources: DataSource[] = []
|
||||
for (let options of dataSourceOptions) {
|
||||
const dataSource = createDataSource(options)
|
||||
await dataSource.initialize()
|
||||
dataSources.push(dataSource)
|
||||
}
|
||||
|
||||
await Promise.all(
|
||||
connections.map(async (connection) => {
|
||||
dataSources.map(async (connection) => {
|
||||
// create new databases
|
||||
const databases: string[] = []
|
||||
connection.entityMetadatas.forEach((metadata) => {
|
||||
@ -388,7 +459,7 @@ export async function createTestingConnections(
|
||||
}),
|
||||
)
|
||||
|
||||
return connections
|
||||
return dataSources
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user