Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Query with any was failing, change to IN instead
Browse files Browse the repository at this point in the history
kevinszuchet committed Jan 24, 2025
1 parent 74de1cb commit 6fdec9b
Showing 4 changed files with 29 additions and 38 deletions.
39 changes: 16 additions & 23 deletions src/adapters/db.ts
Original file line number Diff line number Diff line change
@@ -57,22 +57,6 @@ export function createDBComponent(components: Pick<AppComponents, 'pg' | 'logs'>
return baseQuery
}

function filterActiveFriendshipsFromAddresses(userAddress: string, userAddresses: string[]) {
return SQL`
SELECT DISTINCT
CASE
WHEN address_requester = ${userAddress} THEN address_requested
ELSE address_requester
END as address
FROM friendships
WHERE (
(address_requester = ${userAddress} AND address_requested = ANY(${userAddresses}))
OR
(address_requested = ${userAddress} AND address_requester = ANY(${userAddresses}))
)
AND is_active = true`
}

return {
async getFriends(userAddress, { onlyActive = true, pagination = { limit: FRIENDSHIPS_PER_PAGE, offset: 0 } } = {}) {
const { limit, offset } = pagination
@@ -307,14 +291,23 @@ export function createDBComponent(components: Pick<AppComponents, 'pg' | 'logs'>

return results.rows
},
streamOnlineFriends(userAddress: string, onlinePeers: string[]) {
const query: SQLStatement = filterActiveFriendshipsFromAddresses(userAddress, onlinePeers)
return pg.streamQuery<Friend>(query)
},
async getOnlineFriends(userAddress: string, potentialFriends: string[]) {
if (potentialFriends.length === 0) return []
async getOnlineFriends(userAddress: string, onlinePotentialFriends: string[]) {
if (onlinePotentialFriends.length === 0) return []

const query: SQLStatement = SQL`
SELECT DISTINCT
CASE
WHEN address_requester = ${userAddress} THEN address_requested
ELSE address_requester
END as address
FROM friendships
WHERE (
(address_requester = ${userAddress} AND address_requested IN (${onlinePotentialFriends}))
OR
(address_requested = ${userAddress} AND address_requester IN (${onlinePotentialFriends}))
)
AND is_active = true`

const query: SQLStatement = filterActiveFriendshipsFromAddresses(userAddress, potentialFriends)
const results = await pg.query<Friend>(query)
return results.rows
},
1 change: 0 additions & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
@@ -99,7 +99,6 @@ export interface IDatabaseComponent {
): Promise<string>
getReceivedFriendshipRequests(userAddress: string, pagination?: Pagination): Promise<FriendshipRequest[]>
getSentFriendshipRequests(userAddress: string, pagination?: Pagination): Promise<FriendshipRequest[]>
streamOnlineFriends(userAddress: string, onlinePeers: string[]): AsyncGenerator<Friend>
getOnlineFriends(userAddress: string, potentialFriends: string[]): Promise<Friend[]>
executeTx<T>(cb: (client: PoolClient) => Promise<T>): Promise<T>
}
1 change: 0 additions & 1 deletion test/mocks/components/db.ts
Original file line number Diff line number Diff line change
@@ -5,7 +5,6 @@ export const mockDb: jest.Mocked<IDatabaseComponent> = {
updateFriendshipStatus: jest.fn(),
getFriends: jest.fn(),
getFriendsCount: jest.fn(),
streamOnlineFriends: jest.fn(),
getOnlineFriends: jest.fn(),
getMutualFriends: jest.fn(),
getMutualFriendsCount: jest.fn(),
26 changes: 13 additions & 13 deletions test/unit/adapters/db.spec.ts
Original file line number Diff line number Diff line change
@@ -334,32 +334,32 @@ describe('db', () => {
rows: [{ address: '0x456' }, { address: '0x789' }],
rowCount: 2
}
const userAddress = '0x123'
mockPg.query.mockResolvedValueOnce(mockResult)

const potentialFriends = ['0x456', '0x789', '0x999']
const result = await dbComponent.getOnlineFriends('0x123', potentialFriends)
await dbComponent.getOnlineFriends('0x123', potentialFriends)

const queryExpectations = [
{ text: 'address_requester =', values: ['0x123'] },
{ text: 'AND address_requested = ANY(' },
{ text: 'address_requested =', values: ['0x123'] },
{ text: 'address_requester = ANY(' }
{ text: 'address_requester =' },
{ text: 'AND address_requested IN' },
{ text: 'address_requested =' },
{ text: 'address_requester IN' }
]

queryExpectations.forEach(({ text, values }) => {
queryExpectations.forEach(({ text }) => {
expect(mockPg.query).toHaveBeenCalledWith(
expect.objectContaining({
text: expect.stringContaining(text)
})
)
if (values) {
expect(mockPg.query).toHaveBeenCalledWith(
expect.objectContaining({
values: expect.arrayContaining(values)
})
)
}
})

expect(mockPg.query).toHaveBeenCalledWith(
expect.objectContaining({
values: expect.arrayContaining([userAddress, userAddress, potentialFriends, userAddress, potentialFriends])
})
)
})
})

0 comments on commit 6fdec9b

Please sign in to comment.