-
Notifications
You must be signed in to change notification settings - Fork 31
Refactor getFirst to not depend on our PgAirtableDb abstraction #2620
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
Merged
Merged
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
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
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,5 @@ | ||
| APP_NAME=db-tests | ||
| PG_URL=postgresql://fake:fake@localhost:5432/fake | ||
| AIRTABLE_PERSONAL_ACCESS_TOKEN=FAKE_TOKEN | ||
| ALERTS_SLACK_CHANNEL_ID=FAKE_CHANNEL | ||
| ALERTS_SLACK_BOT_TOKEN=FAKE_TOKEN |
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 |
|---|---|---|
| @@ -1,12 +1,212 @@ | ||
| import { | ||
| describe, | ||
| expect, | ||
| test, | ||
| beforeAll, beforeEach, describe, expect, test, | ||
| } from 'vitest'; | ||
| import { | ||
| PgAirtableDb, | ||
| createTestDbClients, | ||
| exerciseResponseTable, | ||
| getFirstFromPg, | ||
| personTable, | ||
| pushTestSchema, | ||
| resetTestDb, | ||
| type TestPgAirtableDb, | ||
| userTable, | ||
| } from '../index'; | ||
|
|
||
| let db: PgAirtableDb; | ||
| let testDb: TestPgAirtableDb; | ||
|
|
||
| beforeAll(async () => { | ||
| const { pgClient, airtableClient } = createTestDbClients(); | ||
| db = new PgAirtableDb({ | ||
| pgConnString: 'unused', | ||
| airtableApiKey: 'unused', | ||
| pgClient, | ||
| airtableClient, | ||
| }); | ||
| testDb = db as unknown as TestPgAirtableDb; | ||
| await pushTestSchema(db); | ||
| }); | ||
|
|
||
| beforeEach(async () => resetTestDb(db)); | ||
|
|
||
| describe('db.getFirst', () => { | ||
| describe('with autoNumberId table', () => { | ||
| test('returns null when no record matches', async () => { | ||
| const result = await db.getFirst(userTable, { filter: { email: 'nonexistent@example.com' } }); | ||
| expect(result).toBeNull(); | ||
| }); | ||
|
|
||
| test('returns the matching record', async () => { | ||
| await testDb.insert(userTable, { id: 'u1', email: 'a@x', name: 'Alice' }); | ||
| const result = await db.getFirst(userTable, { filter: { email: 'a@x' } }); | ||
| expect(result?.email).toBe('a@x'); | ||
| expect(result?.name).toBe('Alice'); | ||
| }); | ||
|
|
||
| test('defaults to autoNumberId DESC (newest first) with no sortBy', async () => { | ||
| await testDb.insert(userTable, { | ||
| id: 'u1', email: 'shared@x', name: 'First', autoNumberId: 1, | ||
| }); | ||
| await testDb.insert(userTable, { | ||
| id: 'u2', email: 'shared@x', name: 'Second', autoNumberId: 2, | ||
| }); | ||
| await testDb.insert(userTable, { | ||
| id: 'u3', email: 'shared@x', name: 'Third', autoNumberId: 3, | ||
| }); | ||
|
|
||
| const result = await db.getFirst(userTable, { filter: { email: 'shared@x' } }); | ||
| expect(result?.name).toBe('Third'); | ||
| }); | ||
|
|
||
| test('explicit sortBy as string defaults to ASC', async () => { | ||
| await testDb.insert(userTable, { id: 'u1', email: 'b@x', name: 'B' }); | ||
| await testDb.insert(userTable, { id: 'u2', email: 'a@x', name: 'A' }); | ||
| await testDb.insert(userTable, { id: 'u3', email: 'c@x', name: 'C' }); | ||
|
|
||
| const result = await db.getFirst(userTable, { sortBy: 'email' }); | ||
| expect(result?.email).toBe('a@x'); | ||
| }); | ||
|
|
||
| test('explicit sortBy: autoNumberId as string defaults to DESC (special case)', async () => { | ||
| await testDb.insert(userTable, { | ||
| id: 'u1', email: 'a@x', name: 'A', autoNumberId: 1, | ||
| }); | ||
| await testDb.insert(userTable, { | ||
| id: 'u2', email: 'a@x', name: 'B', autoNumberId: 2, | ||
| }); | ||
|
|
||
| const result = await db.getFirst(userTable, { sortBy: 'autoNumberId' }); | ||
| expect(result?.autoNumberId).toBe(2); | ||
| }); | ||
|
|
||
| test('explicit sortBy as object respects direction', async () => { | ||
| await testDb.insert(userTable, { id: 'u1', email: 'b@x', name: 'B' }); | ||
| await testDb.insert(userTable, { id: 'u2', email: 'a@x', name: 'A' }); | ||
|
|
||
| const result = await db.getFirst(userTable, { sortBy: { field: 'email', direction: 'desc' } }); | ||
| expect(result?.email).toBe('b@x'); | ||
| }); | ||
| }); | ||
|
|
||
| describe('with non-autoNumberId table', () => { | ||
| test('throws when sortBy is not provided', async () => { | ||
| // The type system enforces sortBy at compile time; runtime check is the safety net. | ||
| // eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
| const call = db.getFirst(personTable, { filter: { email: 'a@x' } } as any); | ||
| await expect(call).rejects.toThrow(/autoNumberId for default sorting/); | ||
| }); | ||
|
|
||
| test('works with explicit sortBy', async () => { | ||
| await testDb.insert(personTable, { id: 'p1', email: 'b@x' }); | ||
| await testDb.insert(personTable, { id: 'p2', email: 'a@x' }); | ||
|
|
||
| const result = await db.getFirst(personTable, { sortBy: 'email' }); | ||
| expect(result?.email).toBe('a@x'); | ||
| }); | ||
| }); | ||
|
|
||
| describe('filter handling', () => { | ||
| test('AND filter combines conditions', async () => { | ||
| await testDb.insert(userTable, { id: 'u1', email: 'a@x', name: 'Alice' }); | ||
| await testDb.insert(userTable, { id: 'u2', email: 'a@x', name: 'Bob' }); | ||
|
|
||
| const result = await db.getFirst(userTable, { | ||
| filter: { AND: [{ email: 'a@x' }, { name: 'Bob' }] }, | ||
| }); | ||
| expect(result?.name).toBe('Bob'); | ||
| }); | ||
|
|
||
| test('OR filter matches either branch', async () => { | ||
| await testDb.insert(userTable, { id: 'u1', email: 'a@x', name: 'Alice' }); | ||
|
|
||
| const result = await db.getFirst(userTable, { | ||
| filter: { OR: [{ email: 'a@x' }, { email: 'nonexistent@x' }] }, | ||
| }); | ||
| expect(result?.email).toBe('a@x'); | ||
| }); | ||
|
|
||
| test('comparison operator >', async () => { | ||
| await testDb.insert(userTable, { | ||
| id: 'u1', email: 'a@x', name: 'A', autoNumberId: 1, | ||
| }); | ||
| await testDb.insert(userTable, { | ||
| id: 'u2', email: 'a@x', name: 'B', autoNumberId: 5, | ||
| }); | ||
| await testDb.insert(userTable, { | ||
| id: 'u3', email: 'a@x', name: 'C', autoNumberId: 10, | ||
| }); | ||
|
|
||
| // Default sort is autoNumberId DESC, so we get the highest match. | ||
| const result = await db.getFirst(userTable, { filter: { autoNumberId: { '>': 3 } } }); | ||
| expect(result?.autoNumberId).toBe(10); | ||
| }); | ||
|
|
||
| test('comparison operator !=', async () => { | ||
| await testDb.insert(userTable, { | ||
| id: 'u1', email: 'a@x', name: 'A', autoNumberId: 1, | ||
| }); | ||
| await testDb.insert(userTable, { | ||
| id: 'u2', email: 'b@x', name: 'B', autoNumberId: 2, | ||
| }); | ||
|
|
||
| const result = await db.getFirst(userTable, { filter: { email: { '!=': 'a@x' } } }); | ||
| expect(result?.email).toBe('b@x'); | ||
| }); | ||
|
|
||
| test('empty AND defensively returns no rows (matches no records)', async () => { | ||
| await testDb.insert(userTable, { id: 'u1', email: 'a@x', name: 'A' }); | ||
| const result = await db.getFirst(userTable, { filter: { AND: [] } }); | ||
| expect(result).toBeNull(); | ||
| }); | ||
|
|
||
| test('empty OR returns no rows', async () => { | ||
| await testDb.insert(userTable, { id: 'u1', email: 'a@x', name: 'A' }); | ||
| const result = await db.getFirst(userTable, { filter: { OR: [] } }); | ||
| expect(result).toBeNull(); | ||
| }); | ||
|
|
||
| test('throws on unknown field in filter', async () => { | ||
| // eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
| const call = db.getFirst(userTable, { filter: { nonExistent: 'x' } as any }); | ||
| await expect(call).rejects.toThrow(/Unknown field/); | ||
| }); | ||
| }); | ||
|
|
||
| describe('no-options invocation', () => { | ||
| test('with autoNumberId table, no options at all returns newest row', async () => { | ||
| await testDb.insert(exerciseResponseTable, { | ||
| id: 'r1', email: 'a@x', exerciseId: 'e1', response: 'first', autoNumberId: 1, | ||
| }); | ||
| await testDb.insert(exerciseResponseTable, { | ||
| id: 'r2', email: 'a@x', exerciseId: 'e2', response: 'second', autoNumberId: 2, | ||
| }); | ||
|
|
||
| const result = await db.getFirst(exerciseResponseTable); | ||
| expect(result?.id).toBe('r2'); | ||
| }); | ||
| }); | ||
| }); | ||
|
|
||
| describe('getFirstFromPg (direct)', () => { | ||
| test('returns the matching record with a raw pgClient and PgTable', async () => { | ||
| await testDb.insert(userTable, { id: 'u1', email: 'direct@x', name: 'Direct' }); | ||
|
|
||
| const result = await getFirstFromPg(db.pg, userTable.pg, { | ||
| filter: { email: 'direct@x' }, | ||
| }); | ||
| expect(result?.id).toBe('u1'); | ||
| expect(result?.name).toBe('Direct'); | ||
| }); | ||
|
|
||
| test('returns null when no record matches', async () => { | ||
| const result = await getFirstFromPg(db.pg, userTable.pg, { | ||
| filter: { email: 'nobody@x' }, | ||
| }); | ||
| expect(result).toBeNull(); | ||
| }); | ||
|
|
||
| // TODO | ||
| describe('dummy test', () => { | ||
| test('should pass', () => { | ||
| expect(true).toBe(true); | ||
| test('throws when table has no autoNumberId and no sortBy is provided', async () => { | ||
| await expect(getFirstFromPg(db.pg, personTable.pg)).rejects.toThrow(/autoNumberId for default sorting/); | ||
| }); | ||
| }); | ||
Oops, something went wrong.
Oops, something went wrong.
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.