Skip to content

Commit

Permalink
refactor: add filters to some collections (asyncapi#592)
Browse files Browse the repository at this point in the history
  • Loading branch information
smoya authored and magicmatatjahu committed Oct 3, 2022
1 parent 0c70963 commit 3656738
Show file tree
Hide file tree
Showing 14 changed files with 243 additions and 32 deletions.
5 changes: 4 additions & 1 deletion src/models/channels.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import type { Collection } from "./collection";
import type { ChannelInterface } from "./channel";

export interface ChannelsInterface extends Collection<ChannelInterface> {}
export interface ChannelsInterface extends Collection<ChannelInterface> {
filterBySend(): ChannelInterface[];
filterByReceive(): ChannelInterface[];
}
4 changes: 4 additions & 0 deletions src/models/collection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,8 @@ export abstract class Collection<T extends BaseModel | Collection<any>> extends
isEmpty(): boolean {
return this.collections.length === 0;
}

filterBy(filter: (item: T) => boolean): T[] {
return this.collections.filter(filter);
}
}
5 changes: 4 additions & 1 deletion src/models/messages.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import type { Collection } from "./collection";
import type { MessageInterface } from "./message";

export interface MessagesInterface extends Collection<MessageInterface> {}
export interface MessagesInterface extends Collection<MessageInterface> {
filterBySend(): MessageInterface[];
filterByReceive(): MessageInterface[];
}
5 changes: 4 additions & 1 deletion src/models/operations.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import type { Collection } from "./collection";
import type { OperationInterface } from "./operation";

export interface OperationsInterface extends Collection<OperationInterface> {}
export interface OperationsInterface extends Collection<OperationInterface> {
filterBySend(): OperationInterface[];
filterByReceive(): OperationInterface[];
}
5 changes: 4 additions & 1 deletion src/models/servers.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import type { Collection } from "./collection";
import type { ServerInterface } from "./server";

export interface ServersInterface extends Collection<ServerInterface> {}
export interface ServersInterface extends Collection<ServerInterface> {
filterBySend(): ServerInterface[];
filterByReceive(): ServerInterface[];
}
8 changes: 8 additions & 0 deletions src/models/v2/channels.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,12 @@ export class Channels extends Collection<ChannelInterface> implements ChannelsIn
override has(id: string): boolean {
return this.collections.some(channel => channel.id() === id);
}

filterBySend(): ChannelInterface[] {
return this.filterBy(channel => channel.operations().filterBySend().length > 0);
}

filterByReceive(): ChannelInterface[] {
return this.filterBy(channel => channel.operations().filterByReceive().length > 0);
}
}
8 changes: 8 additions & 0 deletions src/models/v2/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,12 @@ export class Messages extends Collection<MessageInterface> implements MessagesIn
override has(id: string): boolean {
return this.collections.some(message => message.id() === id);
}

filterBySend(): MessageInterface[] {
return this.filterBy(message => message.operations().filterBySend().length > 0);
}

filterByReceive(): MessageInterface[] {
return this.filterBy(message => message.operations().filterByReceive().length > 0);
}
}
8 changes: 8 additions & 0 deletions src/models/v2/operations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,12 @@ export class Operations extends Collection<OperationInterface> implements Operat
override has(id: string): boolean {
return this.collections.some(operation => operation.id() === id);
}

filterBySend(): OperationInterface[] {
return this.filterBy(operation => operation.isSend());
}

filterByReceive(): OperationInterface[] {
return this.filterBy(operation => operation.isReceive());
}
}
8 changes: 8 additions & 0 deletions src/models/v2/servers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,12 @@ export class Servers extends Collection<ServerInterface> implements ServersInter
override has(id: string): boolean {
return this.collections.some(server => server.id() === id);
}

filterBySend(): ServerInterface[] {
return this.filterBy(server => server.operations().filterBySend().length > 0);
}

filterByReceive(): ServerInterface[] {
return this.filterBy(server => server.operations().filterByReceive().length > 0);
}
}
24 changes: 24 additions & 0 deletions test/models/collection.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,4 +76,28 @@ describe('Collection model', function() {
expect(d.get('name1')).toEqual(undefined);
});
});

describe('.filterBy()', function() {
it('should return array of ItemModel if filter function matches', function() {
const doc = { name: 'name' };
const item = new ItemModel(doc);
const d = new Model([item]);

const filter = function (_: ItemModel): boolean {
return true;
}
expect(d.filterBy(filter)).toEqual([item]);
});

it('should return empty array of ItemModel if filter function does not match', function() {
const doc = { name: 'name' };
const item = new ItemModel(doc);
const d = new Model([item]);

const filter = function (_: ItemModel): boolean {
return false;
}
expect(d.filterBy(filter)).toEqual([]);
});
});
});
30 changes: 30 additions & 0 deletions test/models/v2/channels.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ const channel = {
publish: {},
};
const channelItem = new Channel(channel, { asyncapi: {} as any, pointer: '', id: 'channel', address: '' });
const channelItems = [
new Channel({ publish: {operationId: 'test1'} }, { asyncapi: {} as any, pointer: '', id: 'channel1', address: '' }),
new Channel({ publish: {operationId: 'test2'} }, { asyncapi: {} as any, pointer: '', id: 'channel2', address: '' }),
new Channel({ subscribe: {operationId: 'test3'} }, { asyncapi: {} as any, pointer: '', id: 'channel3', address: '' }),
new Channel({ subscribe: {operationId: 'test4'} }, { asyncapi: {} as any, pointer: '', id: 'channel4', address: '' }),
];

describe('Channels model', function () {
describe('.isEmpty()', function () {
Expand Down Expand Up @@ -42,4 +48,28 @@ describe('Channels model', function () {
expect(servers.has('anotherName')).toEqual(false);
})
})

describe('.filterBySend()', function () {
it('should return all channels with subscribe operation', function () {
const operations = new Channels(channelItems);
expect(operations.filterBySend()).toEqual([channelItems[2], channelItems[3]]);
})

it('should return empty if there are no channels with operations with subscribe action', function () {
const operations = new Channels([channelItems[0], channelItems[1]]);
expect(operations.filterBySend()).toEqual([]);
})
})

describe('.filterByReceive()', function () {
it('should return all channels with publish operation', function () {
const operations = new Channels(channelItems);
expect(operations.filterByReceive()).toEqual([channelItems[0], channelItems[1]]);
})

it('should return empty if there are no channels with operations with publish action', function () {
const operations = new Channels([channelItems[2], channelItems[3]]);
expect(operations.filterByReceive()).toEqual([]);
})
})
})
65 changes: 49 additions & 16 deletions test/models/v2/messages.spec.ts
Original file line number Diff line number Diff line change
@@ -1,45 +1,78 @@
import { Messages } from '../../../src/models/v2/messages';
import { Message } from '../../../src/models/v2/message';

const message = {
messageId: 'test',
};
const messageItem = new Message(message, { asyncapi: {} as any, pointer: '', id: 'test' });
const messages = {
test1: { messageId: 'test1' },
test2: { messageId: 'test2' },
test3: { messageId: 'test3' },
test4: { messageId: 'test4' },
}

const messageItems = [
new Message(messages.test1, { asyncapi: { parsed: { channels: { test1: { publish: { operationId: "test1", message: messages.test1 } } } } } as any, pointer: '', id: 'test1' }),
new Message(messages.test2, { asyncapi: { parsed: { channels: { test2: { publish: { operationId: "test2", message: messages.test2 } } } } } as any, pointer: '', id: 'test2' }),
new Message(messages.test3, { asyncapi: { parsed: { channels: { test3: { subscribe: { operationId: "test3", message: messages.test3 } } } } } as any, pointer: '', id: 'test3' }),
new Message(messages.test4, { asyncapi: { parsed: { channels: { test4: { subscribe: { operationId: "test4", message: messages.test4 } } } } } as any, pointer: '', id: 'test4' }),
];

describe('Messages model', function () {
describe('.isEmpty()', function () {
it('should return true if collection is empty', function () {
const servers = new Messages([]);
expect(servers.isEmpty()).toEqual(true);
const messages = new Messages([]);
expect(messages.isEmpty()).toEqual(true);
});

it('should return false if collection is not empty', function () {
const servers = new Messages([messageItem]);
expect(servers.isEmpty()).toEqual(false);
const messages = new Messages([messageItems[0]]);
expect(messages.isEmpty()).toEqual(false);
});
});

describe('.get(id)', function () {
it('should return a specific Message if it is present', function () {
const servers = new Messages([messageItem]);
expect(servers.get('test')).toBeTruthy();
const messages = new Messages([messageItems[0]]);
expect(messages.get('test1')).toBeTruthy();
});

it('should return undefined if specific Message is missing', function () {
const servers = new Messages([]);
expect(servers.get('test')).toBeUndefined();
const messages = new Messages([]);
expect(messages.get('test1')).toBeUndefined();
});
});

describe('.has(id)', function () {
it('should return true if the said id is available', function () {
const servers = new Messages([messageItem]);
expect(servers.has('test')).toEqual(true);
const messages = new Messages([messageItems[0]]);
expect(messages.has('test1')).toEqual(true);
})

it('should return false if the Message id is missing', function () {
const servers = new Messages([messageItem]);
expect(servers.has('anotherId')).toEqual(false);
const messages = new Messages([messageItems[0]]);
expect(messages.has('anotherId')).toEqual(false);
})
})

describe('.filterBySend()', function () {
it('should return all messages in channels with subscribe operation', function () {
const messages = new Messages(messageItems);
expect(messages.filterBySend()).toEqual([messageItems[2], messageItems[3]]);
})

it('should return empty if there are no messages in channels with operations with subscribe action', function () {
const messages = new Messages([messageItems[0], messageItems[1]]);
expect(messages.filterBySend()).toEqual([]);
})
})

describe('.filterByReceive()', function () {
it('should return all messages in channels with publish operation', function () {
const messages = new Messages(messageItems);
expect(messages.filterByReceive()).toEqual([messageItems[0], messageItems[1]]);
})

it('should return empty if there are no messages in channels with operations with publish action', function () {
const messages = new Messages([messageItems[2], messageItems[3]]);
expect(messages.filterByReceive()).toEqual([]);
})
})
})
54 changes: 42 additions & 12 deletions test/models/v2/operations.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,41 +5,71 @@ const operation = {
operationId: 'test',
};
const operationItem = new Operation(operation, { asyncapi: {} as any, pointer: '', id: 'test', action: 'publish' });
const operationItems = [
new Operation({operationId: 'test1'}, { asyncapi: {} as any, pointer: '', id: 'test1', action: 'publish' }),
new Operation({operationId: 'test2'}, { asyncapi: {} as any, pointer: '', id: 'test2', action: 'publish' }),
new Operation({operationId: 'test3'}, { asyncapi: {} as any, pointer: '', id: 'test3', action: 'subscribe' }),
new Operation({operationId: 'test4'}, { asyncapi: {} as any, pointer: '', id: 'test4', action: 'subscribe' }),
];

describe('Operations model', function () {
describe('.isEmpty()', function () {
it('should return true if collection is empty', function () {
const servers = new Operations([]);
expect(servers.isEmpty()).toEqual(true);
const operations = new Operations([]);
expect(operations.isEmpty()).toEqual(true);
});

it('should return false if collection is not empty', function () {
const servers = new Operations([operationItem]);
expect(servers.isEmpty()).toEqual(false);
const operations = new Operations([operationItem]);
expect(operations.isEmpty()).toEqual(false);
});
});

describe('.get(id)', function () {
it('should return a specific Operation if it is present', function () {
const servers = new Operations([operationItem]);
expect(servers.get('test')).toBeTruthy();
const operations = new Operations([operationItem]);
expect(operations.get('test')).toBeTruthy();
});

it('should return undefined if specific Operation is missing', function () {
const servers = new Operations([]);
expect(servers.get('test')).toBeUndefined();
const operations = new Operations([]);
expect(operations.get('test')).toBeUndefined();
});
});

describe('.has(id)', function () {
it('should return true if the said id is available', function () {
const servers = new Operations([operationItem]);
expect(servers.has('test')).toEqual(true);
const operations = new Operations([operationItem]);
expect(operations.has('test')).toEqual(true);
})

it('should return false if the Operation id is missing', function () {
const servers = new Operations([operationItem]);
expect(servers.has('anotherId')).toEqual(false);
const operations = new Operations([operationItem]);
expect(operations.has('anotherId')).toEqual(false);
})
})

describe('.filterBySend()', function () {
it('should return all operations with subscribe action', function () {
const operations = new Operations(operationItems);
expect(operations.filterBySend()).toEqual([operationItems[2], operationItems[3]]);
})

it('should return empty if there are no operations with subscribe action', function () {
const operations = new Operations([operationItems[0], operationItems[1]]);
expect(operations.filterBySend()).toEqual([]);
})
})

describe('.filterByReceive()', function () {
it('should return all operations with publish action', function () {
const operations = new Operations(operationItems);
expect(operations.filterByReceive()).toEqual([operationItems[0], operationItems[1]]);
})

it('should return empty if there are no operations with publish action', function () {
const operations = new Operations([operationItems[2], operationItems[3]]);
expect(operations.filterByReceive()).toEqual([]);
})
})
})
Loading

0 comments on commit 3656738

Please sign in to comment.