This repository has been archived by the owner on Aug 18, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7 from yasaichi-sandbox/impl-effective
"Effective" implementation using Effect
- Loading branch information
Showing
15 changed files
with
273 additions
and
15 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,12 @@ | ||
import { Controller, Get, Param } from '@nestjs/common'; | ||
import { EffectiveUsersService } from './effective-users.service.ts'; | ||
|
||
@Controller('effective/users') | ||
export class EffectiveUsersController { | ||
constructor(private readonly effectiveUsersService: EffectiveUsersService) {} | ||
|
||
@Get(':id') | ||
findOne(@Param('id') id: number) { | ||
return this.effectiveUsersService.findOneWithLatestPosts(id); | ||
} | ||
} |
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,9 @@ | ||
import { Module } from '@nestjs/common'; | ||
import { EffectiveUsersController } from './effective-users.controller.ts'; | ||
import { EffectiveUsersService } from './effective-users.service.ts'; | ||
|
||
@Module({ | ||
controllers: [EffectiveUsersController], | ||
providers: [EffectiveUsersService], | ||
}) | ||
export class EffectiveUsersModule {} |
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,86 @@ | ||
import { FakeApiKiotaModule, type Post, type User } from '@app/fake-api-kiota'; | ||
import { Test, TestingModule } from '@nestjs/testing'; | ||
import { beforeEach, describe, it } from '@std/testing/bdd'; | ||
import { Effect } from 'effect'; | ||
import fetchMock from 'fetch-mock'; | ||
import createHttpError from 'http-errors'; | ||
import assert from 'node:assert'; | ||
import { EffectiveUsersService } from './effective-users.service.ts'; | ||
|
||
describe(EffectiveUsersService.name, () => { | ||
let service: EffectiveUsersService; | ||
|
||
describe(EffectiveUsersService.prototype.findOneWithLatestPosts.name, () => { | ||
const userId = 42; | ||
const user: User = { | ||
id: userId, | ||
name: 'Foo Bar', | ||
username: 'foobar', | ||
email: '[email protected]', | ||
}; | ||
const posts: Post[] = [{ | ||
id: 1, | ||
userId, | ||
title: 'post 1 title', | ||
body: 'post 1 body', | ||
}]; | ||
|
||
describe('when the user is not found', () => { | ||
beforeEach(async () => { | ||
const module: TestingModule = await Test.createTestingModule({ | ||
imports: [ | ||
FakeApiKiotaModule.register({ | ||
customFetch: fetchMock | ||
.sandbox() | ||
.get(`path:/users/${userId}`, { body: {}, status: 404 }) | ||
.getOnce('path:/posts', posts, { query: { userId, limit: 5 } }), | ||
}), | ||
], | ||
providers: [EffectiveUsersService], | ||
}).compile(); | ||
|
||
service = module.get(EffectiveUsersService); | ||
}); | ||
|
||
it('should throw an error equivalent to HTTP 404 error', () => { | ||
assert.rejects( | ||
Effect.runPromise( | ||
service.findOneWithLatestPosts(userId).pipe( | ||
Effect.withConcurrency('unbounded'), | ||
), | ||
), | ||
createHttpError(404), | ||
); | ||
}); | ||
}); | ||
|
||
describe('when the user is found', () => { | ||
beforeEach(async () => { | ||
const module: TestingModule = await Test.createTestingModule({ | ||
imports: [ | ||
FakeApiKiotaModule.register({ | ||
customFetch: fetchMock | ||
.sandbox() | ||
.get(`path:/users/${userId}`, user) | ||
.getOnce('path:/posts', posts, { query: { userId, limit: 5 } }), | ||
}), | ||
], | ||
providers: [EffectiveUsersService], | ||
}).compile(); | ||
|
||
service = module.get(EffectiveUsersService); | ||
}); | ||
|
||
it('should return a specified user with the latest posts', async () => { | ||
assert.deepStrictEqual( | ||
await Effect.runPromise(service.findOneWithLatestPosts(userId)), | ||
{ | ||
id: user.id, | ||
username: user.username, | ||
latestPosts: [{ id: posts[0].id, title: posts[0].title }], | ||
}, | ||
); | ||
}); | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.