Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,39 @@ as MyModule_b
module MockDb = {
@genType
let createMockDb = TestHelpers_MockDb.createMockDb

@genType
let getAccessedIds = TestHelpers_MockDb.getAccessedIds

@genType
let getAccessedIdsByEntityType = TestHelpers_MockDb.getAccessedIdsByEntityType

{{#each entities as | entity |}}
@genType
let getAccessed{{entity.name.capitalized}}Ids = TestHelpers_MockDb.getAccessed{{entity.name.capitalized}}Ids

{{/each}}

@genType
let getRequestedButNotFoundIdsByEntityType = TestHelpers_MockDb.getRequestedButNotFoundIdsByEntityType

{{#each entities as | entity |}}
@genType
let getRequestedButNotFound{{entity.name.capitalized}}Ids = TestHelpers_MockDb.getRequestedButNotFound{{entity.name.capitalized}}Ids

{{/each}}

@genType
let getFoundIdsByEntityType = TestHelpers_MockDb.getFoundIdsByEntityType

{{#each entities as | entity |}}
@genType
let getFound{{entity.name.capitalized}}Ids = TestHelpers_MockDb.getFound{{entity.name.capitalized}}Ids

{{/each}}

@genType
let clearAccessTracking = TestHelpers_MockDb.clearAccessTracking
}

@genType
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

/***** TAKE NOTE ******
This file module is a hack to get genType to work!

Expand Down Expand Up @@ -40,6 +39,16 @@ let deleteDictKey: (dict<'a>, string) => unit = %raw(`
}
`)

/**
Type to track which entities have been accessed via get operations
*/
@genType
type accessedEntityRecord = {
entityType: string,
entityId: string,
found: bool, // whether the entity was found in the store
}

/**
The mockDb type is simply an InMemoryStore internally. __dbInternal__ holds a reference
to an inMemoryStore and all the the accessor methods point to the reference of that inMemory
Expand All @@ -51,6 +60,7 @@ type inMemoryStore = InMemoryStore.t
@genType
type rec t = {
__dbInternal__: inMemoryStore,
__accessedIds__: ref<array<accessedEntityRecord>>,
entities: entities,
rawEvents: storeOperations<InMemoryStore.rawEventsKey, TablesStatic.RawEvents.t>,
eventSyncState: storeOperations<Types.chainId, TablesStatic.EventSyncState.t>,
Expand Down Expand Up @@ -119,12 +129,20 @@ let makeStoreOperatorEntity = (
~makeMockDb,
~getStore: InMemoryStore.t => InMemoryTable.Entity.t<'entity>,
~getKey: 'entity => Types.id,
~accessedIds: ref<array<accessedEntityRecord>>,
~entityType: string,
): storeOperations<Types.id, 'entity> => {
let {getUnsafe, values, set} = module(InMemoryTable.Entity)

// Track entity access when get is called
let get = id => {
let store = inMemoryStore->getStore
if store.table->InMemoryTable.hasByHash(id) {
let found = store.table->InMemoryTable.hasByHash(id)

// Record this access regardless of whether found
accessedIds := accessedIds.contents->Array.concat([{entityType, entityId: id, found}])

if found {
getUnsafe(store)(id)
} else {
None
Expand Down Expand Up @@ -213,6 +231,9 @@ instantiate a "MockDb". This is useful for cloning or making a MockDb
out of an existing inMemoryStore
*/
let rec makeWithInMemoryStore: InMemoryStore.t => t = (inMemoryStore: InMemoryStore.t) => {
// Create shared access tracking ref
let accessedIds = ref([])

let rawEvents = makeStoreOperatorMeta(
~inMemoryStore,
~makeMockDb=makeWithInMemoryStore,
Expand All @@ -237,6 +258,8 @@ let rec makeWithInMemoryStore: InMemoryStore.t => t = (inMemoryStore: InMemorySt
~makeMockDb=makeWithInMemoryStore,
~getKey=({chainId, contractAddress}) =>
TablesStatic.DynamicContractRegistry.makeId(~chainId, ~contractAddress),
~accessedIds,
~entityType="DynamicContractRegistry",
)

let entities = {
Expand All @@ -247,12 +270,14 @@ let rec makeWithInMemoryStore: InMemoryStore.t => t = (inMemoryStore: InMemorySt
~makeMockDb=makeWithInMemoryStore,
~getStore=db => db.entities->InMemoryStore.EntityTables.get(module(Entities.{{entity.name.capitalized}})),
~getKey=({id}) => id,
~accessedIds,
~entityType="{{entity.name.capitalized}}",
)
},
{{/each}}
}

{__dbInternal__: inMemoryStore, entities, rawEvents, eventSyncState, dynamicContractRegistry}
{__dbInternal__: inMemoryStore, __accessedIds__: accessedIds, entities, rawEvents, eventSyncState, dynamicContractRegistry}
}

/**
Expand All @@ -279,6 +304,88 @@ let cloneMockDb = (self: t) => {
clonedInternalDb->makeWithInMemoryStore
}

/**
Get all accessed entity records (entity type and ID pairs)
*/
@genType
let getAccessedIds = (self: t): array<accessedEntityRecord> => {
self.__accessedIds__.contents
}

/**
Get accessed entity IDs filtered by entity type
*/
@genType
let getAccessedIdsByEntityType = (self: t, entityType: string): array<string> => {
self.__accessedIds__.contents
->Array.keepMap(record =>
record.entityType == entityType ? Some(record.entityId) : None
)
}

{{#each entities as | entity |}}
/**
Get accessed {{entity.name.capitalized}} entity IDs
*/
@genType
let getAccessed{{entity.name.capitalized}}Ids = (self: t): array<string> => {
self->getAccessedIdsByEntityType("{{entity.name.capitalized}}")
}

{{/each}}

/**
Get entity IDs that were requested but not found, filtered by entity type
*/
@genType
let getRequestedButNotFoundIdsByEntityType = (self: t, entityType: string): array<string> => {
self.__accessedIds__.contents
->Array.keepMap(record =>
record.entityType == entityType && !record.found ? Some(record.entityId) : None
)
}

{{#each entities as | entity |}}
/**
Get {{entity.name.capitalized}} entity IDs that were requested but not found
*/
@genType
let getRequestedButNotFound{{entity.name.capitalized}}Ids = (self: t): array<string> => {
self->getRequestedButNotFoundIdsByEntityType("{{entity.name.capitalized}}")
}

{{/each}}

/**
Get entity IDs that were successfully found when accessed, filtered by entity type
*/
@genType
let getFoundIdsByEntityType = (self: t, entityType: string): array<string> => {
self.__accessedIds__.contents
->Array.keepMap(record =>
record.entityType == entityType && record.found ? Some(record.entityId) : None
)
}

{{#each entities as | entity |}}
/**
Get {{entity.name.capitalized}} entity IDs that were successfully found when accessed
*/
@genType
let getFound{{entity.name.capitalized}}Ids = (self: t): array<string> => {
self->getFoundIdsByEntityType("{{entity.name.capitalized}}")
}

{{/each}}

/**
Clear the access tracking (useful for testing scenarios where you want to reset tracking between operations)
*/
@genType
let clearAccessTracking = (self: t): unit => {
self.__accessedIds__ := []
}

let getEntityOperations = (mockDb: t, ~entityMod): entityStoreOperations<Entities.internalEntity> => {
let module(Entity: Entities.InternalEntity) = entityMod
mockDb.entities
Expand Down
Loading
Loading