Skip to content

Commit

Permalink
fix(test): split up plain container and service module helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
ssube committed Jun 1, 2019
1 parent 8bbdd28 commit 8a45015
Show file tree
Hide file tree
Showing 19 changed files with 87 additions and 57 deletions.
10 changes: 5 additions & 5 deletions test/TestBaseService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { BaseService, BaseServiceData, BaseServiceOptions } from 'src/BaseServic
import { ServiceEvent } from 'src/Service';

import { describeAsync, itAsync } from 'test/helpers/async';
import { createContainer, createService } from 'test/helpers/container';
import { createService, createServiceContainer } from 'test/helpers/container';

class StubService extends BaseService<BaseServiceData> {
constructor(options: BaseServiceOptions<BaseServiceData>) {
Expand All @@ -18,7 +18,7 @@ class StubService extends BaseService<BaseServiceData> {

describeAsync('base service', async () => {
itAsync('should throw on missing name', async () => {
const { container } = await createContainer();
const { container } = await createServiceContainer();
return expect(createService(container, StubService, {
data: {
filters: [],
Expand All @@ -32,7 +32,7 @@ describeAsync('base service', async () => {
});

itAsync('should log notifications', async () => {
const { container } = await createContainer();
const { container } = await createServiceContainer();
const svc = await createService(container, StubService, {
data: {
filters: [],
Expand All @@ -47,7 +47,7 @@ describeAsync('base service', async () => {
});

itAsync('should get ephemeral ID as UUID', async () => {
const { container } = await createContainer();
const { container } = await createServiceContainer();
const svc = await createService(container, StubService, {
data: {
filters: [],
Expand All @@ -62,7 +62,7 @@ describeAsync('base service', async () => {
});

itAsync('should get stable ID as kind:name pair', async () => {
const { container } = await createContainer();
const { container } = await createServiceContainer();
const svc = await createService(container, StubService, {
data: {
filters: [],
Expand Down
4 changes: 2 additions & 2 deletions test/controller/TestCompletionController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ import { expect } from 'chai';
import { CompletionController } from 'src/controller/CompletionController';

import { describeAsync, itAsync } from 'test/helpers/async';
import { createContainer, createService } from 'test/helpers/container';
import { createService, createServiceContainer } from 'test/helpers/container';

describeAsync('completion controller', async () => {
itAsync('should exist', async () => {
const { container } = await createContainer();
const { container } = await createServiceContainer();

const controller = await createService(container, CompletionController, {
data: {
Expand Down
22 changes: 19 additions & 3 deletions test/controller/TestEchoController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,24 @@ import { User } from 'src/entity/auth/User';
import { Command, CommandVerb } from 'src/entity/Command';
import { Context } from 'src/entity/Context';
import { Message } from 'src/entity/Message';
import { Listener } from 'src/listener';
import { ServiceModule } from 'src/module/ServiceModule';
import { TransformModule } from 'src/module/TransformModule';
import { Transform } from 'src/transform';

import { describeAsync, itAsync } from 'test/helpers/async';
import { createContainer, createService } from 'test/helpers/container';
import { createService, createServiceContainer } from 'test/helpers/container';

describeAsync('echo controller', async () => {
itAsync('should exist', async () => {
const { container } = await createContainer();
const { container } = await createServiceContainer();

const controller = await createService(container, EchoController, {
data: {
defaultTarget: {
kind: 'test-listener',
name: 'test-listener',
},
filters: [],
strict: true,
transforms: [],
Expand All @@ -38,7 +43,12 @@ describeAsync('echo controller', async () => {
const modules = [new ServiceModule({
timeout: 100,
}), new TransformModule()];
const { container, module } = await createContainer(...modules);
const { container, module, services } = await createServiceContainer(...modules);

services.addService(ineeda<Listener>({
kind: 'test-listener',
name: 'test-listener',
}));

const msg = 'hello world';
module.bind('test-transform').toInstance(ineeda<Transform>({
Expand All @@ -52,6 +62,10 @@ describeAsync('echo controller', async () => {
sendMessage,
}),
data: {
defaultTarget: {
kind: 'test-listener',
name: 'test-listener',
},
filters: [],
strict: true,
transforms: [{
Expand All @@ -75,6 +89,8 @@ describeAsync('echo controller', async () => {
const cmd = new Command({
context: ineeda<Context>({
checkGrants: () => true,
name: 'test-user',
uid: 'test-user',
user: ineeda<User>(),
}),
data: {},
Expand Down
4 changes: 2 additions & 2 deletions test/controller/TestRandomController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ import { expect } from 'chai';
import { RandomController } from 'src/controller/RandomController';

import { describeAsync, itAsync } from 'test/helpers/async';
import { createContainer, createService } from 'test/helpers/container';
import { createService, createServiceContainer } from 'test/helpers/container';

describeAsync('random controller', async () => {
itAsync('should exist', async () => {
const { container } = await createContainer();
const { container } = await createServiceContainer();

const controller = await createService(container, RandomController, {
data: {
Expand Down
4 changes: 2 additions & 2 deletions test/controller/TestTimeController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ import { expect } from 'chai';
import { TimeController } from 'src/controller/TimeController';

import { describeAsync, itAsync } from 'test/helpers/async';
import { createContainer, createService } from 'test/helpers/container';
import { createService, createServiceContainer } from 'test/helpers/container';

describeAsync('time controller', async () => {
itAsync('should exist', async () => {
const { container } = await createContainer();
const { container } = await createServiceContainer();

const controller = await createService(container, TimeController, {
data: {
Expand Down
4 changes: 2 additions & 2 deletions test/controller/TestWeatherController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@ import { Transform } from 'src/transform';
import { RequestFactory } from 'src/utils/Request';

import { describeAsync, itAsync } from 'test/helpers/async';
import { createContainer, createService } from 'test/helpers/container';
import { createService, createServiceContainer } from 'test/helpers/container';

describeAsync('weather controller', async () => {
itAsync('should send a message', async () => {
const modules = [new ServiceModule({
timeout: 100,
}), new TransformModule()];
const { container, module } = await createContainer(...modules);
const { container, module } = await createServiceContainer(...modules);

const data = { test: 'test' };
module.bind(INJECT_REQUEST).toInstance(ineeda<RequestFactory>({
Expand Down
4 changes: 2 additions & 2 deletions test/filter/TestCommandFilter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ import { FilterBehavior } from 'src/filter';
import { CommandFilter, CommandFilterData } from 'src/filter/CommandFilter';

import { describeAsync, itAsync } from 'test/helpers/async';
import { createContainer, createService } from 'test/helpers/container';
import { createService, createServiceContainer } from 'test/helpers/container';

const TEST_FILTER_KIND = 'user-filter';
const TEST_FILTER_NAME = 'test-filter';

async function createFilter(data: CommandFilterData) {
const { container } = await createContainer();
const { container } = await createServiceContainer();
const filter = await createService(container, CommandFilter, {
data,
metadata: {
Expand Down
4 changes: 2 additions & 2 deletions test/filter/TestMessageFilter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ import { MessageFilter, MessageFilterData } from 'src/filter/MessageFilter';
import { TYPE_TEXT } from 'src/utils/Mime';

import { describeAsync, itAsync } from 'test/helpers/async';
import { createContainer, createService } from 'test/helpers/container';
import { createService, createServiceContainer } from 'test/helpers/container';

const TEST_FILTER_KIND = 'user-filter';
const TEST_FILTER_NAME = 'test-filter';

async function createFilter(data: MessageFilterData) {
const { container } = await createContainer();
const { container } = await createServiceContainer();
const filter = await createService(container, MessageFilter, {
data,
metadata: {
Expand Down
4 changes: 2 additions & 2 deletions test/filter/TestSourceFilter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ import { SourceFilter, SourceFilterData } from 'src/filter/SourceFilter';
import { TYPE_TEXT } from 'src/utils/Mime';

import { describeAsync, itAsync } from 'test/helpers/async';
import { createContainer, createService } from 'test/helpers/container';
import { createService, createServiceContainer } from 'test/helpers/container';

const TEST_FILTER_KIND = 'user-filter';
const TEST_FILTER_NAME = 'test-filter';

async function createFilter(data: SourceFilterData) {
const { container } = await createContainer();
const { container } = await createServiceContainer();
const filter = await createService(container, SourceFilter, {
data,
metadata: {
Expand Down
4 changes: 2 additions & 2 deletions test/filter/TestUserFilter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ import { UserFilter, UserFilterData } from 'src/filter/UserFilter';
import { ChecklistMode } from 'src/utils/Checklist';

import { describeAsync, itAsync } from 'test/helpers/async';
import { createContainer, createService } from 'test/helpers/container';
import { createService, createServiceContainer } from 'test/helpers/container';

const TEST_FILTER_KIND = 'user-filter';
const TEST_FILTER_NAME = 'test-filter';

async function createUserFilter(data: UserFilterData) {
const { container } = await createContainer();
const { container } = await createServiceContainer();
const filter = await createService(container, UserFilter, {
data,
metadata: {
Expand Down
32 changes: 23 additions & 9 deletions test/helpers/container.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,25 +34,41 @@ export class TestModule extends Module {
}
}

/**
* Create a DI container for tests.
*/
export async function createContainer(...modules: Array<Module>): Promise<{ container: Container, module: Module }> {
const module = new TestModule();
const container = Container.from(module, ...modules);
await container.configure();
return { container, module };
}

/**
* Create a DI container for tests with a stub service module that will create, but not get, services.
*/
export async function createServiceContainer(...modules: Array<Module>): Promise<{
container: Container,
module: Module,
services: ServiceModule,
}> {
const services = new ServiceModule({
timeout: 100,
});
const { container, module } = await createContainer(...modules, services);
module.bind(INJECT_SERVICES).toInstance(services);
return { container, module, services };
}

export async function createService<
TService,
TData extends BotServiceData,
TOptions extends BotServiceOptions<TData> = BotServiceOptions<TData>,
>(
container: Container,
type: Constructor<TService, TOptions>,
options: Partial<TOptions>,
>(
container: Container,
type: Constructor<TService, TOptions>,
options: Partial<TOptions>,
): Promise<TService> {
const services = ineeda<ServiceModule>({
createService: (def: ServiceDefinition<TData>) => container.create(def.metadata.kind, def),
});
const schema = new Schema(); // tests use the real schema :D
const locale = await container.create(Locale, {
data: {
Expand All @@ -64,7 +80,6 @@ export async function createService<
},
[INJECT_LOGGER]: ConsoleLogger.global,
[INJECT_SCHEMA]: schema,
[INJECT_SERVICES]: services,
});

const fullOptions = {
Expand All @@ -77,7 +92,6 @@ export async function createService<
[INJECT_LOGGER]: ConsoleLogger.global,
[INJECT_METRICS]: new Registry(),
[INJECT_SCHEMA]: schema,
[INJECT_SERVICES]: services,
[INJECT_STORAGE]: ineeda<Connection>({
getRepository<T>(ctor: T) {
return ineeda<Repository<T>>();
Expand Down
10 changes: 5 additions & 5 deletions test/parser/TestArgsParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { Storage } from 'src/storage';
import { TYPE_JPEG, TYPE_TEXT } from 'src/utils/Mime';

import { describeAsync, itAsync } from 'test/helpers/async';
import { createContainer, createService } from 'test/helpers/container';
import { createService, createServiceContainer } from 'test/helpers/container';

const TEST_CONFIG = {
args: {
Expand Down Expand Up @@ -51,7 +51,7 @@ const TEST_STORAGE = ineeda<Storage>({

describeAsync('args parser', async () => {
itAsync('should parse a message body', async () => {
const { container } = await createContainer();
const { container } = await createServiceContainer();
const svc = await createService(container, ArgsParser, {
data: TEST_CONFIG,
metadata: TEST_METADATA,
Expand All @@ -71,7 +71,7 @@ describeAsync('args parser', async () => {
});

itAsync('should prompt to complete missing fields', async () => {
const { container } = await createContainer();
const { container } = await createServiceContainer();
const svc = await createService(container, ArgsParser, {
[INJECT_STORAGE]: TEST_STORAGE,
data: TEST_CONFIG,
Expand Down Expand Up @@ -100,7 +100,7 @@ describeAsync('args parser', async () => {
});

itAsync('should complete a fragment', async () => {
const { container } = await createContainer();
const { container } = await createServiceContainer();
const svc = await createService(container, ArgsParser, {
[INJECT_STORAGE]: TEST_STORAGE,
data: TEST_CONFIG,
Expand Down Expand Up @@ -134,7 +134,7 @@ describeAsync('args parser', async () => {
});

itAsync('should reject messages with other types', async () => {
const { container } = await createContainer();
const { container } = await createServiceContainer();
const svc = await createService(container, ArgsParser, {
[INJECT_STORAGE]: TEST_STORAGE,
data: TEST_CONFIG,
Expand Down
6 changes: 3 additions & 3 deletions test/parser/TestEchoParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { Storage } from 'src/storage';
import { TYPE_JPEG, TYPE_TEXT } from 'src/utils/Mime';

import { describeAsync, itAsync } from 'test/helpers/async';
import { createContainer, createService } from 'test/helpers/container';
import { createService, createServiceContainer } from 'test/helpers/container';

const TEST_CONFIG = {
dataMapper: {
Expand Down Expand Up @@ -51,7 +51,7 @@ const TEST_STORAGE = ineeda<Storage>({

describeAsync('echo parser', async () => {
itAsync('should parse the message body as-is', async () => {
const { container } = await createContainer();
const { container } = await createServiceContainer();
const svc = await createService(container, EchoParser, {
[INJECT_STORAGE]: TEST_STORAGE,
data: TEST_CONFIG,
Expand Down Expand Up @@ -79,7 +79,7 @@ describeAsync('echo parser', async () => {
});

itAsync('should reject messages with other types', async () => {
const { container } = await createContainer();
const { container } = await createServiceContainer();
const svc = await createService(container, EchoParser, {
[INJECT_STORAGE]: TEST_STORAGE,
data: TEST_CONFIG,
Expand Down
6 changes: 3 additions & 3 deletions test/parser/TestRegexParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { Storage } from 'src/storage';
import { TYPE_JPEG, TYPE_TEXT } from 'src/utils/Mime';

import { describeAsync, itAsync } from 'test/helpers/async';
import { createContainer, createService } from 'test/helpers/container';
import { createService, createServiceContainer } from 'test/helpers/container';

const TEST_CONFIG = {
dataMapper: {
Expand Down Expand Up @@ -47,7 +47,7 @@ const TEST_STORAGE = ineeda<Storage>({

describeAsync('regex parser', async () => {
itAsync('should split the message body into groups', async () => {
const { container } = await createContainer();
const { container } = await createServiceContainer();
const svc = await createService(container, RegexParser, {
[INJECT_STORAGE]: TEST_STORAGE,
data: TEST_CONFIG,
Expand Down Expand Up @@ -79,7 +79,7 @@ describeAsync('regex parser', async () => {
});

itAsync('should reject messages with other types', async () => {
const { container } = await createContainer();
const { container } = await createServiceContainer();
const svc = await createService(container, RegexParser, {
[INJECT_STORAGE]: TEST_STORAGE,
data: TEST_CONFIG,
Expand Down
Loading

0 comments on commit 8a45015

Please sign in to comment.