Skip to content

Commit

Permalink
Merge pull request #1158 from XYOracleNetwork/feature/image-thumbnail…
Browse files Browse the repository at this point in the history
…-diviner-test

Iimage thumbnail diviner tests
  • Loading branch information
JoelBCarter authored Oct 5, 2023
2 parents d7b6ae8 + 2334805 commit 2334d85
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export class MemoryPayloadDiviner<TParams extends PayloadDivinerParams = Payload
}
const parsedLimit = limit || all.length
const parsedOffset = offset || 0
return parsedOffset
return offset !== undefined
? all.slice(parsedOffset, parsedLimit)
: (async () => {
const allPairs = await Promise.all(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ export type ImageThumbnailDivinerQuerySchema = `${ImageThumbnailDivinerSchema}.q
export const ImageThumbnailDivinerQuerySchema: ImageThumbnailDivinerQuerySchema = `${ImageThumbnailDivinerSchema}.query`

export type ImageThumbnailDivinerQuery = Pick<PayloadDivinerPredicate, 'limit' | 'offset' | 'order'> &
Payload<{ status?: boolean; url: string }, ImageThumbnailDivinerQuerySchema>
Payload<{ status?: number; success?: boolean; url: string }, ImageThumbnailDivinerQuerySchema>

export const isImageThumbnailDivinerQuery = isPayloadOfSchemaType<ImageThumbnailDivinerQuery>(ImageThumbnailDivinerQuerySchema)
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ export type ImageThumbnailResultIndexSchema = typeof ImageThumbnailResultIndexSc

export interface ImageThumbnailResultInfo {
sources: string[]
status: boolean
status?: number
success: boolean
timestamp: number
url: string
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,6 @@ describeIf(process.env.INFURA_PROJECT_ID)('getNftCollectionCount', () => {
it.each(cases)('gets NFTs owned by the address', async (address, chainId, expected) => {
const result = await getNftCollectionCount(address, chainId, privateKey)
expect(result).toBeNumber()
expect(result).toEqual(expected)
expect(result).toBeGreaterThanOrEqual(expected)
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
ImageThumbnailDivinerParams,
ImageThumbnailResult,
ImageThumbnailResultIndexSchema,
ImageThumbnailResultInfo,
ImageThumbnailSchema,
isImageThumbnail,
isImageThumbnailDivinerQuery,
Expand All @@ -37,7 +38,7 @@ type ConfigStore = Extract<keyof ImageThumbnailDivinerConfig, ConfigStoreKey>
/**
* The fields that will need to be indexed on in the underlying store
*/
type QueryableImageThumbnailResultProperties = Extract<keyof ImageThumbnailResult, 'url' | 'timestamp' | 'status'>
type QueryableImageThumbnailResultProperties = Extract<keyof ImageThumbnailResultInfo, 'status' | 'success' | 'timestamp' | 'url'>

/**
* The query that will be used to retrieve the results from the underlying store
Expand Down Expand Up @@ -129,11 +130,11 @@ export class ImageThumbnailDiviner<TParams extends ImageThumbnailDivinerParams =
([boundWitnessHash, thumbnailHash, thumbnailPayload, timestampHash, timestampPayload]) => {
const { sourceUrl: url } = thumbnailPayload
const { timestamp } = timestampPayload
const status = thumbnailPayload.http?.status ? true : false
const status = thumbnailPayload.http?.status
const success = thumbnailPayload.http?.status ? true : false
const sources = [boundWitnessHash, thumbnailHash, timestampHash]
const result = new PayloadBuilder<ImageThumbnailResult>({ schema: ImageThumbnailResultIndexSchema })
.fields({ sources, status, timestamp, url })
.build()
const fields = status ? { sources, status, success, timestamp, url } : { sources, success, timestamp, url }
const result = new PayloadBuilder<ImageThumbnailResult>({ schema: ImageThumbnailResultIndexSchema }).fields(fields).build()
return result
},
)
Expand Down Expand Up @@ -168,14 +169,17 @@ export class ImageThumbnailDiviner<TParams extends ImageThumbnailDivinerParams =
const results = (
await Promise.all(
urls.map(async (payload) => {
const { limit: payloadLimit, offset: payloadOffset, order: payloadOrder, status: payloadStatus, url } = payload
const { limit: payloadLimit, offset: payloadOffset, order: payloadOrder, status: payloadStatus, success: payloadSuccess, url } = payload
const limit = payloadLimit ?? 1
const order = payloadOrder ?? 'desc'
const offset = payloadOffset ?? 0
const status = payloadStatus ?? true
const query = new PayloadBuilder<ImageThumbnailResultQuery>({ schema: PayloadDivinerQuerySchema })
.fields({ limit, offset, order, status, url })
.build()
const success = payloadSuccess ?? true
const fields: Partial<ImageThumbnailResultQuery> = { limit, offset, order, success, url }
// Default to filtering on 200 status code if success was not supplied
if (payloadSuccess === undefined) fields.status = payloadStatus ?? 200
// If success is true and status was supplied, use it
if (success === true && payloadStatus !== undefined) fields.status = payloadStatus
const query = new PayloadBuilder<ImageThumbnailResultQuery>({ schema: PayloadDivinerQuerySchema }).fields(fields).build()
return await diviner.divine([query])
}),
)
Expand Down

Large diffs are not rendered by default.

0 comments on commit 2334d85

Please sign in to comment.