-
Notifications
You must be signed in to change notification settings - Fork 223
fix(typeorm): Warning log when multiple DataSources have the same name #2461
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
kevo-1
wants to merge
2
commits into
nestjs:master
Choose a base branch
from
kevo-1:fix/validate-datasource-names
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+169
−2
Open
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,143 @@ | ||
| import { TypeOrmCoreModule } from '../../lib/typeorm-core.module'; | ||
| import { Logger } from '@nestjs/common'; | ||
|
|
||
| describe('TypeOrmModule - validateDataSourceNames', () => { | ||
| let warnSpy: jest.SpyInstance; | ||
|
|
||
| beforeEach(() => { | ||
| warnSpy = jest | ||
| .spyOn((TypeOrmCoreModule as any).logger, 'warn') | ||
| .mockImplementation(); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| jest.restoreAllMocks(); | ||
| }); | ||
|
|
||
| it('should log a warning when duplicate data source names are provided', () => { | ||
| const options = [ | ||
| { name: 'default', type: 'sqlite', database: ':memory:' }, | ||
| { name: 'secondary', type: 'sqlite', database: ':memory:' }, | ||
| { name: 'secondary', type: 'sqlite', database: ':memory:' }, | ||
| ]; | ||
|
|
||
| (TypeOrmCoreModule as any)['validateDataSourceNames'](options); | ||
|
|
||
| expect(warnSpy).toHaveBeenCalledWith( | ||
| expect.stringContaining( | ||
| 'Duplicate DataSource names detected: [secondary]', | ||
| ), | ||
| ); | ||
| }); | ||
|
|
||
| it('should not log a warning when all data source names are unique', () => { | ||
| const options = [ | ||
| { name: 'default', type: 'sqlite', database: ':memory:' }, | ||
| { name: 'secondary', type: 'sqlite', database: ':memory:' }, | ||
| ]; | ||
|
|
||
| (TypeOrmCoreModule as any)['validateDataSourceNames'](options); | ||
|
|
||
| expect(warnSpy).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it('should treat missing names as "default" and log warning if repeated', () => { | ||
| const options = [ | ||
| { type: 'sqlite', database: ':memory:' }, | ||
| { type: 'sqlite', database: ':memory:' }, | ||
| ]; | ||
|
|
||
| (TypeOrmCoreModule as any)['validateDataSourceNames'](options); | ||
|
|
||
| expect(warnSpy).toHaveBeenCalledWith( | ||
| expect.stringContaining('Duplicate DataSource names detected: [default]'), | ||
| ); | ||
| }); | ||
|
|
||
| it('should not log warning for a single configuration object', () => { | ||
| const options = { name: 'default', type: 'sqlite', database: ':memory:' }; | ||
|
|
||
| (TypeOrmCoreModule as any)['validateDataSourceNames'](options); | ||
|
|
||
| expect(warnSpy).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it('should handle an empty array without throwing or logging', () => { | ||
| const options: any[] = []; | ||
|
|
||
| expect(() => { | ||
| (TypeOrmCoreModule as any)['validateDataSourceNames'](options); | ||
| }).not.toThrow(); | ||
|
|
||
| expect(warnSpy).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it('should handle mixed named and unnamed configurations correctly', () => { | ||
| const options = [ | ||
| { name: 'primary', type: 'sqlite', database: ':memory:' }, | ||
| { type: 'sqlite', database: ':memory:' }, | ||
| { type: 'sqlite', database: ':memory:' }, | ||
| ]; | ||
|
|
||
| (TypeOrmCoreModule as any)['validateDataSourceNames'](options); | ||
|
|
||
| expect(warnSpy).toHaveBeenCalledWith( | ||
| expect.stringContaining('Duplicate DataSource names detected: [default]'), | ||
| ); | ||
| }); | ||
|
|
||
| it('should log warning when multiple different duplicates exist', () => { | ||
| const options = [ | ||
| { name: 'alpha', type: 'sqlite', database: ':memory:' }, | ||
| { name: 'alpha', type: 'sqlite', database: ':memory:' }, | ||
| { name: 'beta', type: 'sqlite', database: ':memory:' }, | ||
| { name: 'beta', type: 'sqlite', database: ':memory:' }, | ||
| ]; | ||
|
|
||
| (TypeOrmCoreModule as any)['validateDataSourceNames'](options); | ||
|
|
||
| expect(warnSpy).toHaveBeenCalledWith( | ||
| expect.stringContaining( | ||
| 'Duplicate DataSource names detected: [alpha, beta]', | ||
| ), | ||
| ); | ||
| }); | ||
|
|
||
| it('should handle case-sensitive names as different (no warning)', () => { | ||
| const options = [ | ||
| { name: 'Main', type: 'sqlite', database: ':memory:' }, | ||
| { name: 'main', type: 'sqlite', database: ':memory:' }, | ||
| ]; | ||
|
|
||
| (TypeOrmCoreModule as any)['validateDataSourceNames'](options); | ||
|
|
||
| expect(warnSpy).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it('should not crash if one of the configs is null or undefined', () => { | ||
| const options = [ | ||
| { name: 'alpha', type: 'sqlite', database: ':memory:' }, | ||
| null as any, | ||
| undefined as any, | ||
| { name: 'beta', type: 'sqlite', database: ':memory:' }, | ||
| ]; | ||
|
|
||
| expect(() => { | ||
| (TypeOrmCoreModule as any)['validateDataSourceNames'](options); | ||
| }).not.toThrow(); | ||
| }); | ||
|
|
||
| it('should log warning if all names are missing (multiple "default")', () => { | ||
| const options = [ | ||
| { type: 'sqlite', database: ':memory:' }, | ||
| { type: 'sqlite', database: ':memory:' }, | ||
| { type: 'sqlite', database: ':memory:' }, | ||
| ]; | ||
|
|
||
| (TypeOrmCoreModule as any)['validateDataSourceNames'](options); | ||
|
|
||
| expect(warnSpy).toHaveBeenCalledWith( | ||
| expect.stringContaining('Duplicate DataSource names detected: [default]'), | ||
| ); | ||
| }); | ||
| }); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.