Skip to content

Commit

Permalink
feat: add new generateGroups command (#2133)
Browse files Browse the repository at this point in the history
  • Loading branch information
MartinGbz authored Jul 27, 2023
1 parent 92ca22e commit 9797fb0
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/topics/group-generator/group-generator.api.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ describe("test groups generator api", () => {
it("Should get example-generator", async () => {
const response = await request(api.server).get(`/group-generators`);
expect(response.statusCode).toBe(200);
expect(response.body.items).toHaveLength(5);
expect(response.body.items).toHaveLength(6);
expect(response.body.items[0].name).toBe("test-generator");
expect(response.body.items[0].generationFrequency).toBe(GenerationFrequency.Once);
});
Expand Down
51 changes: 44 additions & 7 deletions src/topics/group-generator/group-generator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import {
groupToUpdateMetadata,
groupToUpdateMetadata2,
groupsToUpdateMetadataGenerators,
testGroup2,
} from "./test-group-generator";
import { MemoryGroupGeneratorStore } from "infrastructure/group-generator-store";
import { MemoryGroupSnapshotStore } from "infrastructure/group-snapshot/group-snapshot-memory";
Expand Down Expand Up @@ -230,7 +231,32 @@ describe("test group generator", () => {
expect(context.timestamp).toEqual(1);
});

test("Should generate a group with the generator", async () => {
test("Should generate 2 group with generators", async () => {
await service.generateGroups("test-generator,test-generator-2", {
timestamp: 1,
});
const groups = await groupStore.all();
expect(Object.keys(groups)).toHaveLength(2);
expect(groups[testGroup.name]).toBeSameGroup(testGroup);
expect(groups[testGroup2.name]).toBeSameGroup(testGroup2);
const generatorGroups = await groupGeneratorStore.search({
generatorName: "test-generator",
});
expect(generatorGroups).toHaveLength(1);
expect(generatorGroups[0].timestamp).toEqual(1);
expect(generatorGroups[0].name).toEqual("test-generator");
expect(generatorGroups[0].generationFrequency).toEqual(GenerationFrequency.Once);

const generatorGroups2 = await groupGeneratorStore.search({
generatorName: "test-generator-2",
});
expect(generatorGroups2).toHaveLength(1);
expect(generatorGroups2[0].timestamp).toEqual(1);
expect(generatorGroups2[0].name).toEqual("test-generator-2");
expect(generatorGroups2[0].generationFrequency).toEqual(GenerationFrequency.Once);
});

test("Should generate a group with a generator", async () => {
await service.generateGroups("test-generator", {
timestamp: 1,
});
Expand Down Expand Up @@ -415,11 +441,12 @@ describe("test group generator", () => {
});
const allGroups = await groupStore.all();
const groups = Object.values(allGroups);
expect(groups).toHaveLength(4);
expect(groups).toHaveLength(5);
expect(groups[0]).toBeSameGroup(testGroup);
expect(groups[1]).toBeSameGroup(dependentGroup);
expect(groups[2]).toBeSameGroup(testGroupWithDisplayName);
expect(groups[3]).toBeSameGroup(dependentGroupTwo);
expect(groups[2]).toBeSameGroup(testGroup2);
expect(groups[3]).toBeSameGroup(testGroupWithDisplayName);
expect(groups[4]).toBeSameGroup(dependentGroupTwo);
});

it("should generate only once if the first generation only option is enabled", async () => {
Expand All @@ -444,8 +471,9 @@ describe("test group generator", () => {
timestamp: 1,
});
const groups = await groupStore.all();
expect(Object.keys(groups)).toHaveLength(1);
expect(Object.keys(groups)).toHaveLength(2);
expect(Object.values(groups)[0]).toBeSameGroup(testGroup);
expect(Object.values(groups)[1]).toBeSameGroup(testGroup2);
});

it("should generate only the groups with Once frequency with additional data", async () => {
Expand All @@ -458,13 +486,22 @@ describe("test group generator", () => {
},
});
const groups = await groupStore.all();
expect(Object.keys(groups)).toHaveLength(1);
expect(Object.keys(groups)).toHaveLength(2);

const data = await Object.values(groups)[0].data();
expect(data["0x0000000000000000000000000000000000000030"]).toBe("1");
expect(data["0x0000000000000000000000000000000000000031"]).toBe("2");
expect(data["0x411c16b4688093c81db91e192aeb5945dca6b785"]).toBe("1");
expect(data["0x45647ff5380d7da60e9018d1d29d529664839789"]).toBe("1");

const data2 = await Object.values(groups)[1].data();
expect(data2["0x0000000000000000000000000000000000000030"]).toBe("1");
expect(data2["0x0000000000000000000000000000000000000031"]).toBe("2");
expect(data2["0xd8da6bf26964af9d7eed9e03e53415d37aa96045"]).toBe("1");
expect(data2["0x45647ff5380d7da60e9018d1d29d529664839789"]).toBe("4");
});

it("should generate only two groups with frequency Daily and respect dependencies", async () => {
it("should generate only three groups with frequency Daily and respect dependencies", async () => {
await service.generateAllGroups({
frequency: "daily",
timestamp: 1,
Expand Down
22 changes: 22 additions & 0 deletions src/topics/group-generator/group-generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,28 @@ export class GroupGeneratorService {
}

public async generateGroups(
generatorNames: string,
{
timestamp,
additionalData,
lastGenerationTimeInterval,
firstGenerationOnly,
}: GenerateGroupOptions
) {
const generatorNamesArray = generatorNames.split(",");
return Promise.all(
generatorNamesArray.map((generatorName) =>
this.generateGroup(generatorName, {
timestamp,
additionalData,
lastGenerationTimeInterval,
firstGenerationOnly,
})
)
).then((groups) => groups.flat());
}

public async generateGroup(
generatorName: string,
{
timestamp,
Expand Down
27 changes: 27 additions & 0 deletions src/topics/group-generator/test-group-generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,21 @@ export const testGroup: GroupWithData = {
tags: [Tags.Vote, Tags.Mainnet],
};

export const testGroup2: GroupWithData = {
name: "test-group-2",
timestamp: 1,
description: "test-description-2",
specs: "test-specs-2",
data: {
"0xd8da6bf26964af9d7eed9e03e53415d37aa96045": 1,
"0x45647ff5380d7da60e9018d1d29d529664839789": 4,
"0xfd247ff5380d7da60e9018d1d29d529664839af2": 2,
},
accountSources: [AccountSource.ETHEREUM],
valueType: ValueType.Info,
tags: [Tags.Vote, Tags.Mainnet],
};

export const testGroupWithDisplayName: GroupWithData = {
name: "test-group-display-name",
displayName: "Name displayed",
Expand Down Expand Up @@ -86,6 +101,17 @@ export const testGroupGenerator: GroupGenerator = {
): Promise<GroupWithData[]> => [testGroup],
};

export const testGroupGenerator2: GroupGenerator = {
generationFrequency: GenerationFrequency.Once,

generate: async (
// eslint-disable-next-line @typescript-eslint/no-unused-vars
context: GenerationContext,
// eslint-disable-next-line @typescript-eslint/no-unused-vars
groupStore: GroupStore
): Promise<GroupWithData[]> => [testGroup2],
};

export const testGroupWithDisplayNameGenerator: GroupGenerator = {
generationFrequency: GenerationFrequency.Daily,

Expand Down Expand Up @@ -134,6 +160,7 @@ export const groupWithIssueGenerator: GroupGenerator = {

export const groupGenerators: GroupGeneratorsLibrary = {
"test-generator": testGroupGenerator,
"test-generator-2": testGroupGenerator2,
"test-generator-with-display-name": testGroupWithDisplayNameGenerator,
"dependent-generator": dependentGroupGenerator,
"dependent-generator-two": dependentTwoGroupGenerator,
Expand Down

0 comments on commit 9797fb0

Please sign in to comment.