Skip to content
Permalink
Browse files

Unit tests of a SQL repository

  • Loading branch information...
mchmielarz mchmielarz
mchmielarz authored and mchmielarz committed Oct 28, 2016
1 parent 7581487 commit b95e15d7bc83476b7b4bce195dac2c1c2b6ed1a4
@@ -0,0 +1,34 @@
package pl.devthoughts.todos.assertj;

import org.assertj.core.api.AbstractAssert;
import pl.devthoughts.todos.domain.TodoItem;

import java.util.Date;
import java.util.Objects;

public class TodoItemAssert extends AbstractAssert<TodoItemAssert, TodoItem> {

public TodoItemAssert(TodoItem actual) {
super(actual, TodoItemAssert.class);
}

public static TodoItemAssert assertThat(TodoItem actual) {
return new TodoItemAssert(actual);
}

public TodoItemAssert hasName(String name) {
isNotNull();
if (!Objects.equals(actual.getName(), name)) {
failWithMessage("Expected item's name to be <%s> but was <%s>", name, actual.getName());
}
return this;
}

public TodoItemAssert hasDueDate(Date dueDate) {
isNotNull();
if (!Objects.equals(actual.getDueDate(), dueDate)) {
failWithMessage("Expected item's dueDate to be <%s> but was <%s>", dueDate, actual.getDueDate());
}
return this;
}
}
@@ -0,0 +1,52 @@
package pl.devthoughts.todos.repository;

import com.google.common.collect.ImmutableMap;
import javaslang.control.Option;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import pl.devthoughts.todos.domain.TodoItem;
import pl.devthoughts.todos.domain.TodoItemId;
import play.db.Database;
import play.db.Databases;
import play.db.evolutions.Evolutions;

import java.util.Date;

import static org.assertj.core.api.Assertions.assertThat;
import static pl.devthoughts.todos.assertj.TodoItemAssert.assertThat;

public class TodoItemSqlRepositoryTest {

private TodoItemSqlRepository repository;
private Database db;

@Before
public void setup() {
db = Databases.inMemory("todos", "jdbc:h2:mem:todos;MODE=PostgreSQL;DATABASE_TO_UPPER=false", ImmutableMap.of());
Evolutions.applyEvolutions(db);
repository = new TodoItemSqlRepository(db);
}

@After
public void cleanup() {
Evolutions.cleanupEvolutions(db);
db.shutdown();
}

@Test
public void should_save_single_todo_item() {
final Date dueDate = new Date();
final String name = "Do something";

final Option<TodoItemId> itemId = repository.saveItem(new TodoItem(name, dueDate));
assertThat(itemId.isDefined()).isEqualTo(true);

final Option<TodoItem> item = repository.findItem(itemId.get());
assertThat(item.isDefined());
assertThat(item.get())
.hasName(name)
.hasDueDate(dueDate);
}

}

0 comments on commit b95e15d

Please sign in to comment.
You can’t perform that action at this time.