-
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.
feat: add provider data conversion from document to entity and test i…
…n-memory gateway
- Loading branch information
Showing
12 changed files
with
165 additions
and
31 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
4 changes: 2 additions & 2 deletions
4
src/providers/todo-http.gateway.ts → src/providers/todo-http/todo-http.gateway.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
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,51 @@ | ||
import { Todo } from "../../modules/todos/entities/todo"; | ||
|
||
import TodoDocument from "./todo-document"; | ||
|
||
const notDefined: any = undefined; | ||
const description = "description"; | ||
const todo = new Todo("id", "description", new Date()) | ||
|
||
describe("[TodoDocument] Test Cases", () => { | ||
|
||
it("should create a document successfully", () => { | ||
const document = new TodoDocument("description", new Date().toISOString()) | ||
expect(document).toBeDefined() | ||
}) | ||
|
||
it("should create a document from todo successfully", () => { | ||
const document = TodoDocument.fromTodo(todo) | ||
expect(document.description).toEqual(description) | ||
expect(document.due).toEqual(todo.due.toISOString()) | ||
}) | ||
|
||
it("should convert a document to todo successfully", () => { | ||
const document = TodoDocument.fromTodo(todo) | ||
expect(document.toEntity("id")).toEqual(todo) | ||
}) | ||
|
||
it("should throw en error because of an undefined Todo", () => { | ||
try { | ||
const _ = TodoDocument.fromTodo(notDefined) | ||
} catch (error) { | ||
expect(error.message).toEqual("ValidationError: No todo provided!") | ||
} | ||
}) | ||
|
||
it("should throw en error because of a missing Description", () => { | ||
try { | ||
const _ = new TodoDocument(notDefined, new Date().toISOString()) | ||
} catch (error) { | ||
expect(error.message).toEqual("ValidationError: Description not provided!") | ||
} | ||
}) | ||
|
||
it("should throw en error because of a missing Due date", () => { | ||
try { | ||
const _ = new TodoDocument("description", notDefined) | ||
} catch (error) { | ||
expect(error.message).toEqual("ValidationError: Due date not provided!") | ||
} | ||
}) | ||
|
||
}) |
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,18 @@ | ||
import { Todo } from "../../modules/todos/entities/todo"; | ||
|
||
export default class TodoDocument { | ||
|
||
public static fromTodo(todo: Todo): TodoDocument { | ||
if (!todo) throw new Error("ValidationError: No todo provided!") | ||
return new TodoDocument(todo.description, todo.due.toISOString()); | ||
} | ||
|
||
constructor(public readonly description: string, public readonly due: string) { | ||
if (!description) throw new Error("ValidationError: Description not provided!") | ||
if (!due) throw new Error("ValidationError: Due date not provided!") | ||
} | ||
|
||
public toEntity(id: string): Todo { | ||
return new Todo(id, this.description, new Date(this.due)); | ||
} | ||
} |
75 changes: 75 additions & 0 deletions
75
src/providers/todo-in-memory/todo-in-memory.gateway.spec.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,75 @@ | ||
import { Todo } from "../../modules/todos/entities/todo"; | ||
import TodoDocument from "./todo-document"; | ||
import InMemoryTodoGateway from "./todo-in-memory.gateway"; | ||
|
||
describe("[InMemoryTodoGateway] Initialization", () => { | ||
it("should create an in memory todo gateway without any todos", async () => { | ||
const inMemTodoGW = new InMemoryTodoGateway() | ||
const todos = await inMemTodoGW.find({ limit: 10, skip: 0 }) | ||
expect(todos.length).toEqual(0) | ||
expect.assertions(1) | ||
}) | ||
}) | ||
|
||
describe("[InMemoryTodoGateway] Test Cases", () => { | ||
|
||
let inMemTodoGW: InMemoryTodoGateway | ||
|
||
beforeEach(() => { | ||
const seed = new Map<string, TodoDocument>([ | ||
["1", new TodoDocument("first", new Date().toISOString())], | ||
["2", new TodoDocument("second", new Date().toISOString())], | ||
["3", new TodoDocument("third", new Date().toISOString())], | ||
]) | ||
|
||
inMemTodoGW = new InMemoryTodoGateway(seed); | ||
}) | ||
|
||
it("should return all items in map as todos", async () => { | ||
const todos = await inMemTodoGW.find({ limit: 10, skip: 0 }) | ||
expect(todos.length).toEqual(3) | ||
expect.assertions(1) | ||
}) | ||
|
||
it("should return second todo", async () => { | ||
expect.assertions(2) | ||
const todos = await inMemTodoGW.find({ limit: 1, skip: 1 }) | ||
expect(todos.length).toEqual(1) | ||
expect(todos[0].id).toEqual("2") | ||
}) | ||
|
||
it("should add a new todo and return it", async () => { | ||
const todo = await inMemTodoGW.save(new Todo("4", "forth", new Date())); | ||
const todos = await inMemTodoGW.find({ limit: 1, skip: 3 }) | ||
expect(todos.length).toEqual(1) | ||
expect(todos[0]).toEqual(todo) | ||
expect.assertions(2) | ||
}) | ||
|
||
it("should throw an error because the todo already exist", async () => { | ||
try { | ||
const _ = await inMemTodoGW.save(new Todo("1", "description", new Date())); | ||
} catch (error) { | ||
expect(error.message).toEqual("Todo already exists!"); | ||
expect.assertions(1) | ||
} | ||
}) | ||
|
||
it("should remove a todo and return its value", async () => { | ||
const todo = await inMemTodoGW.delete("3"); | ||
const todos = await inMemTodoGW.find({ limit: 10, skip: 0 }) | ||
expect(todos).not.toContain(todo) | ||
expect(todos.length).toEqual(2) | ||
expect.assertions(2) | ||
}) | ||
|
||
it("should throw an error because the todo does not exist", async () => { | ||
try { | ||
const _ = await inMemTodoGW.delete("4"); | ||
} catch (error) { | ||
expect(error.message).toEqual("Todo does not exist!"); | ||
expect.assertions(1) | ||
} | ||
}) | ||
|
||
}) |
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,9 +1,9 @@ | ||
import UUDIGenerator from "../modules/shared/uuid-generator"; | ||
import UUDIGenerator from "../../modules/shared/uuid-generator"; | ||
|
||
import { v4 } from "uuid"; | ||
|
||
export default class V4UuidGenerator implements UUDIGenerator { | ||
public generate(): string { | ||
return v4() | ||
} | ||
} | ||
} |