-
Notifications
You must be signed in to change notification settings - Fork 2.5k
test: add coverage for resolveSqlToOneRelationStorage fallback branches #30292
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
EmaToplek
wants to merge
1
commit into
prisma:main
Choose a base branch
from
EmaToplek:test/contract-relation-storage-coverage
base: main
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.
Open
Changes from all commits
Commits
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
143 changes: 143 additions & 0 deletions
143
packages/2-sql/1-core/contract/test/relation-storage.test.ts
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 type { ContractToOneRelation } from '@internal/contract/types'; | ||
| import { UNBOUND_NAMESPACE_ID } from '@internal/framework-components/ir'; | ||
| import { describe, expect, it } from 'vitest'; | ||
| import { col, fk, table } from '../src/factories'; | ||
| import { SqlStorage } from '../src/ir/sql-storage'; | ||
| import { resolveSqlToOneRelationStorage } from '../src/relation-storage'; | ||
| import { createTestSqlNamespace } from './test-support'; | ||
|
|
||
| function makeRelation(overrides: { | ||
| cardinality: '1:1' | 'N:1'; | ||
| localFields: readonly string[]; | ||
| }): ContractToOneRelation { | ||
| return { | ||
| to: {} as never, | ||
| cardinality: overrides.cardinality, | ||
| nullable: false, | ||
| on: { localFields: overrides.localFields, targetFields: ['id'] }, | ||
| }; | ||
| } | ||
|
|
||
| describe('resolveSqlToOneRelationStorage', () => { | ||
| it('resolves a mapped, non-nullable column for an existing field', () => { | ||
| const usersTable = table({ | ||
| id: col('int4', 'pg/int4@1'), | ||
| author_id: col('int4', 'pg/int4@1'), | ||
| }); | ||
| const storage = new SqlStorage({ | ||
| storageHash: 'test' as never, | ||
| namespaces: { | ||
| [UNBOUND_NAMESPACE_ID]: createTestSqlNamespace({ | ||
| id: UNBOUND_NAMESPACE_ID, | ||
| entries: { table: { posts: usersTable } }, | ||
| }), | ||
| }, | ||
| }); | ||
| const modelStorage = { | ||
| table: 'posts', | ||
| namespaceId: UNBOUND_NAMESPACE_ID, | ||
| fields: { authorId: { column: 'author_id' } }, | ||
| }; | ||
| const relation = makeRelation({ cardinality: 'N:1', localFields: ['authorId'] }); | ||
|
|
||
| const result = resolveSqlToOneRelationStorage({ storage } as never, modelStorage, relation); | ||
|
|
||
| expect(result.columns).toEqual([{ name: 'author_id', nullable: false }]); | ||
| expect(result.ownsForeignKey).toBe(true); | ||
| }); | ||
|
|
||
| it('falls back to the field name and treats the column as nullable when the field has no column mapping', () => { | ||
| const usersTable = table({ id: col('int4', 'pg/int4@1') }); | ||
| const storage = new SqlStorage({ | ||
| storageHash: 'test' as never, | ||
| namespaces: { | ||
| [UNBOUND_NAMESPACE_ID]: createTestSqlNamespace({ | ||
| id: UNBOUND_NAMESPACE_ID, | ||
| entries: { table: { posts: usersTable } }, | ||
| }), | ||
| }, | ||
| }); | ||
| const modelStorage = { | ||
| table: 'posts', | ||
| namespaceId: UNBOUND_NAMESPACE_ID, | ||
| fields: {}, | ||
| }; | ||
| const relation = makeRelation({ cardinality: 'N:1', localFields: ['missingField'] }); | ||
|
|
||
| const result = resolveSqlToOneRelationStorage({ storage } as never, modelStorage, relation); | ||
|
|
||
| expect(result.columns).toEqual([{ name: 'missingField', nullable: true }]); | ||
| }); | ||
|
|
||
| it('does not own the foreign key for a 1:1 relation whose table declares no matching foreign key', () => { | ||
| const usersTable = table({ id: col('int4', 'pg/int4@1') }); | ||
| const storage = new SqlStorage({ | ||
| storageHash: 'test' as never, | ||
| namespaces: { | ||
| [UNBOUND_NAMESPACE_ID]: createTestSqlNamespace({ | ||
| id: UNBOUND_NAMESPACE_ID, | ||
| entries: { table: { profiles: usersTable } }, | ||
| }), | ||
| }, | ||
| }); | ||
| const modelStorage = { | ||
| table: 'profiles', | ||
| namespaceId: UNBOUND_NAMESPACE_ID, | ||
| fields: { id: { column: 'id' } }, | ||
| }; | ||
| const relation = makeRelation({ cardinality: '1:1', localFields: ['id'] }); | ||
|
|
||
| const result = resolveSqlToOneRelationStorage({ storage } as never, modelStorage, relation); | ||
|
|
||
| expect(result.ownsForeignKey).toBe(false); | ||
| }); | ||
|
|
||
| it('owns the foreign key for a 1:1 relation whose table declares a matching foreign key', () => { | ||
| const foreignKey = fk('profiles', ['user_id'], 'users', ['id']); | ||
| const profilesTable = table( | ||
| { id: col('int4', 'pg/int4@1'), user_id: col('int4', 'pg/int4@1') }, | ||
| { fks: [foreignKey] }, | ||
| ); | ||
| const storage = new SqlStorage({ | ||
| storageHash: 'test' as never, | ||
| namespaces: { | ||
| [UNBOUND_NAMESPACE_ID]: createTestSqlNamespace({ | ||
| id: UNBOUND_NAMESPACE_ID, | ||
| entries: { table: { profiles: profilesTable } }, | ||
| }), | ||
| }, | ||
| }); | ||
| const modelStorage = { | ||
| table: 'profiles', | ||
| namespaceId: UNBOUND_NAMESPACE_ID, | ||
| fields: { userId: { column: 'user_id' } }, | ||
| }; | ||
| const relation = makeRelation({ cardinality: '1:1', localFields: ['userId'] }); | ||
|
|
||
| const result = resolveSqlToOneRelationStorage({ storage } as never, modelStorage, relation); | ||
|
|
||
| expect(result.ownsForeignKey).toBe(true); | ||
| }); | ||
|
|
||
| it('does not own the foreign key when the referenced table is entirely missing', () => { | ||
| const storage = new SqlStorage({ | ||
| storageHash: 'test' as never, | ||
| namespaces: { | ||
| [UNBOUND_NAMESPACE_ID]: createTestSqlNamespace({ | ||
| id: UNBOUND_NAMESPACE_ID, | ||
| entries: { table: {} }, | ||
| }), | ||
| }, | ||
| }); | ||
| const modelStorage = { | ||
| table: 'missing_table', | ||
| namespaceId: UNBOUND_NAMESPACE_ID, | ||
| fields: {}, | ||
| }; | ||
| const relation = makeRelation({ cardinality: '1:1', localFields: ['x'] }); | ||
|
|
||
| const result = resolveSqlToOneRelationStorage({ storage } as never, modelStorage, relation); | ||
|
|
||
| expect(result.ownsForeignKey).toBe(false); | ||
| }); | ||
| }); | ||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win
Add a mapped-but-missing-column case.
This case only tests a missing field mapping. It does not test a field with a configured column that is absent from
table.columns.Add a case with
fields: { authorId: { column: 'author_id' } }and noauthor_idtable column. Assert{ name: 'author_id', nullable: true }. This detects regressions that replace the configured column name with the field name.🤖 Prompt for AI Agents