Skip to content
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

fix(generic-records): support custom id property #964

Merged
Merged
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 @@ -22,11 +22,12 @@ export class GenericRecordsModule {
this.logger = agentConfig.logger
}

public async save({ content, tags }: SaveGenericRecordOption) {
public async save({ content, tags, id }: SaveGenericRecordOption) {
try {
const record = await this.genericRecordsService.save({
content: content,
tags: tags,
id,
content,
tags,
})
return record
} catch (error) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,11 @@ export class GenericRecordService {
this.genericRecordsRepository = genericRecordsRepository
}

public async save({ content, tags }: SaveGenericRecordOption) {
public async save({ content, tags, id }: SaveGenericRecordOption) {
const genericRecord = new GenericRecord({
content: content,
tags: tags,
id,
content,
tags,
})

try {
Expand Down
34 changes: 14 additions & 20 deletions packages/core/tests/generic-records.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@ describe('genericRecords', () => {
aliceAgent = new Agent(aliceConfig.config, aliceConfig.agentDependencies)
await aliceAgent.initialize()

//Save genericRecord message (Minimal)
// Save genericRecord message (Minimal)

const savedRecord1: GenericRecord = await aliceAgent.genericRecords.save({ content: barString })

//Save genericRecord message with tag
// Save genericRecord message with tag
const tags1 = { myTag: 'foobar1' }
const tags2 = { myTag: 'foobar2' }

Expand All @@ -41,12 +41,15 @@ describe('genericRecords', () => {

const savedRecord3: GenericRecord = await aliceAgent.genericRecords.save({ content: barString, tags: tags2 })
expect(savedRecord3).toBeDefined()

const record = await aliceAgent.genericRecords.save({ content: barString, tags: tags2, id: 'foo' })
expect(record.id).toBe('foo')
})

test('get generic-record records', async () => {
//Create genericRecord message
const savedRecords = await aliceAgent.genericRecords.getAll()
expect(savedRecords.length).toBe(3)
expect(savedRecords.length).toBe(4)
})

test('get generic-record specific record', async () => {
Expand All @@ -56,7 +59,7 @@ describe('genericRecords', () => {
expect(savedRecords1[0].content).toEqual({ foo: 42 })

const savedRecords2 = await aliceAgent.genericRecords.findAllByQuery({ myTag: 'foobar2' })
expect(savedRecords2?.length == 1).toBe(true)
expect(savedRecords2.length == 2).toBe(true)
expect(savedRecords2[0].content).toEqual({ foo: 'Some data saved' })
})

Expand All @@ -65,32 +68,27 @@ describe('genericRecords', () => {
const savedRecord1: GenericRecord = await aliceAgent.genericRecords.save({ content: barString, id: myId })
expect(savedRecord1).toBeDefined()

const retrievedRecord: GenericRecord | null = await aliceAgent.genericRecords.findById(savedRecord1.id)

if (retrievedRecord) {
expect(retrievedRecord.content).toEqual({ foo: 'Some data saved' })
} else {
throw Error('retrieved record not found')
}
const retrievedRecord = await aliceAgent.genericRecords.findById(savedRecord1.id)
expect(retrievedRecord?.content).toEqual({ foo: 'Some data saved' })
})

test('delete generic record', async () => {
const myId = '100'
const myId = '101'
const savedRecord1: GenericRecord = await aliceAgent.genericRecords.save({ content: barString, id: myId })
expect(savedRecord1).toBeDefined()

await aliceAgent.genericRecords.delete(savedRecord1)

const retrievedRecord: GenericRecord | null = await aliceAgent.genericRecords.findById(savedRecord1.id)
const retrievedRecord = await aliceAgent.genericRecords.findById(savedRecord1.id)
expect(retrievedRecord).toBeNull()
})

test('update generic record', async () => {
const myId = '100'
const myId = '102'
const savedRecord1: GenericRecord = await aliceAgent.genericRecords.save({ content: barString, id: myId })
expect(savedRecord1).toBeDefined()

let retrievedRecord: GenericRecord | null = await aliceAgent.genericRecords.findById(savedRecord1.id)
let retrievedRecord = await aliceAgent.genericRecords.findById(savedRecord1.id)
expect(retrievedRecord).toBeDefined()

const amendedFooString = { foo: 'Some even more cool data saved' }
Expand All @@ -101,10 +99,6 @@ describe('genericRecords', () => {
await aliceAgent.genericRecords.update(savedRecord1)

retrievedRecord = await aliceAgent.genericRecords.findById(savedRecord1.id)
if (retrievedRecord) {
expect(retrievedRecord.content).toEqual({ foo: 'Some even more cool data saved' })
} else {
throw Error('retrieved record not found in update test')
}
expect(retrievedRecord?.content).toEqual({ foo: 'Some even more cool data saved' })
})
})