From 214ffef353b173807e76cb3d0d5937d06b70a6e7 Mon Sep 17 00:00:00 2001 From: Johannes Loher Date: Wed, 19 Dec 2018 01:45:34 +0100 Subject: [PATCH] fix: do not generate a new id on update --- .../dataaccess/in_memory_todo_repository.d | 42 ++++++++++++++++++- source/d_webservice_example/model/todo.d | 8 ++++ 2 files changed, 49 insertions(+), 1 deletion(-) diff --git a/source/d_webservice_example/dataaccess/in_memory_todo_repository.d b/source/d_webservice_example/dataaccess/in_memory_todo_repository.d index fa13623..43ee63f 100644 --- a/source/d_webservice_example/dataaccess/in_memory_todo_repository.d +++ b/source/d_webservice_example/dataaccess/in_memory_todo_repository.d @@ -3,6 +3,11 @@ module d_webservice_example.dataaccess.in_memory_todo_repository; import aermicioi.aedi : component, qualifier; import d_webservice_example.business.todo_service : TodoRepository; +version (unittest) +{ + import fluent.asserts; +} + @component @qualifier!TodoRepository() class InMemoryTodoRepository : TodoRepository { import d_webservice_example.model.todo : Todo; @@ -15,11 +20,46 @@ private: public: override Todo save(Todo todo) { - todo.id = randomUUID.toString; + import std.range.primitives : empty; + + if (todo.id.empty) + todo.id = randomUUID.toString; todos[todo.id] = todo; return todo; } + unittest + { + // given + auto underTest = new InMemoryTodoRepository; + auto todo = Todo("some title", "some content", + UUID("00000000-0000-0000-0000-000000000001")); + + // when + auto savedTodo = underTest.save(todo); + + // then + savedTodo.title.should.equal(todo.title); + savedTodo.content.should.equal(todo.content); + savedTodo.uuid.should.equal(todo.uuid); + savedTodo.id.length.should.equal(36); + } + + unittest + { + // given + auto underTest = new InMemoryTodoRepository; + auto todo = Todo("some title", "some content", + UUID("00000000-0000-0000-0000-000000000001"), + "00000000-0000-0000-0000-000000000002"); + + // when + auto savedTodo = underTest.save(todo); + + // then + savedTodo.should.equal(todo); + } + override Todo[] findAll() { import std.array : array; diff --git a/source/d_webservice_example/model/todo.d b/source/d_webservice_example/model/todo.d index 4536e2a..e7e36d4 100644 --- a/source/d_webservice_example/model/todo.d +++ b/source/d_webservice_example/model/todo.d @@ -18,6 +18,14 @@ struct Todo this.uuid = uuid; } + this(string title, string content, UUID uuid, string id) nothrow pure @safe @nogc + { + this.title = title; + this.content = content; + this.uuid = uuid; + this.id = id; + } + string title; string content;