-
Notifications
You must be signed in to change notification settings - Fork 153
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(relayer): add database integration
- [x] Add mongodb integration - [x] Add message repository - [x] Add message batch repository - [x] Add message batch service - [x] Add message and message batch schemas - [x] Integrate message repository into message service - [x] Integrate message batch service into message service
- Loading branch information
Showing
27 changed files
with
1,120 additions
and
73 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 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 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,20 @@ | ||
import type { MongooseModuleOptions } from "@nestjs/mongoose"; | ||
|
||
/** | ||
* Get test mongoose module options | ||
* | ||
* @param options mongoose module options | ||
* @returns mongoose module options for testing | ||
*/ | ||
export const getTestMongooseModuleOptions = async ( | ||
options: MongooseModuleOptions = {}, | ||
): Promise<MongooseModuleOptions> => { | ||
// eslint-disable-next-line import/no-extraneous-dependencies | ||
const { MongoMemoryServer } = await import("mongodb-memory-server"); | ||
const mongod = await MongoMemoryServer.create(); | ||
|
||
return { | ||
uri: mongod.getUri(), | ||
...options, | ||
}; | ||
}; |
77 changes: 77 additions & 0 deletions
77
apps/relayer/ts/message/__tests__/message.repository.test.ts
This file contains 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,77 @@ | ||
import { ZeroAddress } from "ethers"; | ||
import { Keypair } from "maci-domainobjs"; | ||
import { Model } from "mongoose"; | ||
|
||
import { MessageRepository } from "../message.repository"; | ||
import { Message } from "../message.schema"; | ||
|
||
import { defaultSaveMessagesArgs } from "./utils"; | ||
|
||
describe("MessageRepository", () => { | ||
const defaultMessages: Message[] = [ | ||
{ | ||
publicKey: new Keypair().pubKey.serialize(), | ||
data: ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"], | ||
maciContractAddress: ZeroAddress, | ||
poll: 0, | ||
}, | ||
]; | ||
|
||
const mockMessageModel = { | ||
find: jest | ||
.fn() | ||
.mockReturnValue({ limit: jest.fn().mockReturnValue({ exec: jest.fn().mockResolvedValue(defaultMessages) }) }), | ||
insertMany: jest.fn().mockResolvedValue(defaultMessages), | ||
} as unknown as Model<Message>; | ||
|
||
beforeEach(() => { | ||
mockMessageModel.find = jest | ||
.fn() | ||
.mockReturnValue({ limit: jest.fn().mockReturnValue({ exec: jest.fn().mockResolvedValue(defaultMessages) }) }); | ||
mockMessageModel.insertMany = jest.fn().mockResolvedValue(defaultMessages); | ||
}); | ||
|
||
afterEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
test("should create messages properly", async () => { | ||
const repository = new MessageRepository(mockMessageModel); | ||
|
||
const result = await repository.create(defaultSaveMessagesArgs); | ||
|
||
expect(result).toStrictEqual(defaultMessages); | ||
}); | ||
|
||
test("should throw an error if creation is failed", async () => { | ||
const error = new Error("error"); | ||
|
||
(mockMessageModel.insertMany as jest.Mock).mockRejectedValue(error); | ||
|
||
const repository = new MessageRepository(mockMessageModel); | ||
|
||
await expect(repository.create(defaultSaveMessagesArgs)).rejects.toThrow(error); | ||
}); | ||
|
||
test("should find messages properly", async () => { | ||
const repository = new MessageRepository(mockMessageModel); | ||
|
||
const result = await repository.find({}); | ||
|
||
expect(result).toStrictEqual(defaultMessages); | ||
}); | ||
|
||
test("should throw an error if find is failed", async () => { | ||
const error = new Error("error"); | ||
|
||
(mockMessageModel.find as jest.Mock).mockReturnValue({ | ||
limit: jest.fn().mockReturnValue({ | ||
exec: jest.fn().mockRejectedValue(error), | ||
}), | ||
}); | ||
|
||
const repository = new MessageRepository(mockMessageModel); | ||
|
||
await expect(repository.find({})).rejects.toThrow(error); | ||
}); | ||
}); |
This file contains 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,21 +1,64 @@ | ||
import type { MessageBatchService } from "../../messageBatch/messageBatch.service"; | ||
import type { MessageRepository } from "../message.repository"; | ||
|
||
import { MessageService } from "../message.service"; | ||
|
||
import { defaultSaveMessagesArgs } from "./utils"; | ||
import { defaultMessages, defaultSaveMessagesArgs } from "./utils"; | ||
|
||
describe("MessageService", () => { | ||
const mockMessageBatchService = { | ||
saveMessageBatches: jest.fn().mockImplementation((args) => Promise.resolve(args)), | ||
} as unknown as MessageBatchService; | ||
|
||
const mockRepository = { | ||
create: jest.fn().mockResolvedValue(defaultMessages), | ||
find: jest.fn().mockResolvedValue(defaultMessages), | ||
} as unknown as MessageRepository; | ||
|
||
beforeEach(() => { | ||
mockMessageBatchService.saveMessageBatches = jest.fn().mockImplementation((args) => Promise.resolve(args)); | ||
|
||
mockRepository.create = jest.fn().mockResolvedValue(defaultMessages); | ||
mockRepository.find = jest.fn().mockResolvedValue(defaultMessages); | ||
}); | ||
|
||
afterEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
test("should save messages properly", async () => { | ||
const service = new MessageService(); | ||
const service = new MessageService(mockMessageBatchService, mockRepository); | ||
|
||
const result = await service.saveMessages(defaultSaveMessagesArgs); | ||
|
||
expect(result).toBe(true); | ||
expect(result).toStrictEqual(defaultMessages); | ||
}); | ||
|
||
test("should throw an error if can't save messages", async () => { | ||
const error = new Error("error"); | ||
|
||
(mockRepository.create as jest.Mock).mockRejectedValue(error); | ||
|
||
const service = new MessageService(mockMessageBatchService, mockRepository); | ||
|
||
await expect(service.saveMessages(defaultSaveMessagesArgs)).rejects.toThrow(error); | ||
}); | ||
|
||
test("should publish messages properly", async () => { | ||
const service = new MessageService(); | ||
const service = new MessageService(mockMessageBatchService, mockRepository); | ||
|
||
const result = await service.publishMessages(defaultSaveMessagesArgs); | ||
const result = await service.publishMessages(); | ||
|
||
expect(result).toStrictEqual({ hash: "", ipfsHash: "" }); | ||
}); | ||
|
||
test("should throw an error if can't save message batch", async () => { | ||
const error = new Error("error"); | ||
|
||
(mockMessageBatchService.saveMessageBatches as jest.Mock).mockRejectedValue(error); | ||
|
||
const service = new MessageService(mockMessageBatchService, mockRepository); | ||
|
||
await expect(service.publishMessages()).rejects.toThrow(error); | ||
}); | ||
}); |
This file contains 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,15 +1,19 @@ | ||
import { ZeroAddress } from "ethers"; | ||
import { Keypair } from "maci-domainobjs"; | ||
|
||
import { defaultMessageBatches } from "../../messageBatch/__tests__/utils"; | ||
import { PublishMessagesDto } from "../dto"; | ||
|
||
const keypair = new Keypair(); | ||
|
||
export const defaultSaveMessagesArgs = { | ||
maciContractAddress: ZeroAddress, | ||
poll: 0, | ||
messages: [ | ||
{ | ||
data: ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"], | ||
publicKey: keypair.pubKey.serialize(), | ||
}, | ||
], | ||
}; | ||
export const defaultMessages = defaultMessageBatches[0].messages; | ||
|
||
export const defaultSaveMessagesArgs = new PublishMessagesDto(); | ||
defaultSaveMessagesArgs.maciContractAddress = ZeroAddress; | ||
defaultSaveMessagesArgs.poll = 0; | ||
defaultSaveMessagesArgs.messages = [ | ||
{ | ||
data: ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"], | ||
publicKey: keypair.pubKey.serialize(), | ||
}, | ||
]; |
This file contains 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 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,10 +1,16 @@ | ||
import { Module } from "@nestjs/common"; | ||
import { MongooseModule } from "@nestjs/mongoose"; | ||
|
||
import { MessageBatchModule } from "../messageBatch/messageBatch.module"; | ||
|
||
import { MessageController } from "./message.controller"; | ||
import { MessageRepository } from "./message.repository"; | ||
import { Message, MessageSchema } from "./message.schema"; | ||
import { MessageService } from "./message.service"; | ||
|
||
@Module({ | ||
imports: [MongooseModule.forFeature([{ name: Message.name, schema: MessageSchema }]), MessageBatchModule], | ||
controllers: [MessageController], | ||
providers: [MessageService], | ||
providers: [MessageService, MessageRepository], | ||
}) | ||
export class MessageModule {} |
Oops, something went wrong.