Skip to content

Commit

Permalink
Test UserRepository
Browse files Browse the repository at this point in the history
  • Loading branch information
alexsuter committed Dec 21, 2024
1 parent eae49de commit b024c4b
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 5 deletions.
16 changes: 12 additions & 4 deletions src/main/java/io/ivyteam/devops/db/Database.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,15 @@
public class Database {

private static final String VERSION = "14";
private static final Path PATH = Path.of("data", "devopsV" + VERSION + ".db");
private final Path path;

public Database() {
this(Path.of("data", "devopsV" + VERSION + ".db"));
}

public Database(Path path) {
this.path = path;
}

public Connection connection() {
if (!exists()) {
Expand All @@ -25,9 +33,9 @@ public Connection connection() {

private Connection dbConnection() {
try {
Files.createDirectories(PATH.getParent());
Files.createDirectories(path.getParent());
Class.forName("org.sqlite.JDBC");
var connection = DriverManager.getConnection("jdbc:sqlite:" + PATH);
var connection = DriverManager.getConnection("jdbc:sqlite:" + path);
try (var stmt = connection.createStatement()) {
stmt.execute("PRAGMA foreign_keys = ON");
}
Expand All @@ -39,7 +47,7 @@ private Connection dbConnection() {
}

private boolean exists() {
return Files.exists(PATH);
return Files.exists(path);
}

private void create() {
Expand Down
7 changes: 6 additions & 1 deletion src/main/java/io/ivyteam/devops/user/UserRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ public class UserRepository {
@Autowired
private Database db;

public UserRepository(Database db) {
this.db = db;
}

public List<User> all() {
try (var connection = db.connection()) {
try (var stmt = connection.prepareStatement("SELECT * FROM user ORDER BY name")) {
Expand All @@ -29,8 +33,9 @@ public List<User> all() {

public void create(User user) {
try (var connection = db.connection()) {
try (var stmt = connection.prepareStatement("INSERT INTO user (name) VALUES (?)")) {
try (var stmt = connection.prepareStatement("INSERT INTO user (name, avatarUrl) VALUES (?, ?)")) {
stmt.setString(1, user.name());
stmt.setString(2, user.avatarUrl());
stmt.execute();
}
} catch (SQLException ex) {
Expand Down
57 changes: 57 additions & 0 deletions src/test/java/io/ivyteam/devops/user/TestUserRepository.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package io.ivyteam.devops.user;

import static org.assertj.core.api.Assertions.assertThat;

import java.nio.file.Path;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;

import io.ivyteam.devops.db.Database;

class TestUserRepository {

private static final User ALEX = new User("alex", "https://example.com/alex.png");
private static final User LOUIS = new User("louis", "https://example.com/louis.png");

@TempDir
Path tempDir;

UserRepository users;

@BeforeEach
void beforeEach() {
var db = new Database(tempDir.resolve("test.db"));
users = new UserRepository(db);
}

@Test
void create() {
users.create(ALEX);
assertThat(users.all()).containsExactly(ALEX);
}

@Test
void exists() {
users.create(ALEX);
assertThat(users.exists(ALEX)).isTrue();
}

@Test
void doesNotExists() {
assertThat(users.exists(LOUIS)).isFalse();
}

@Test
void all_ordered() {
users.create(LOUIS);
users.create(ALEX);
assertThat(users.all()).containsExactly(ALEX, LOUIS);
}

@Test
void all_empty() {
assertThat(users.all()).isEmpty();
}
}

0 comments on commit b024c4b

Please sign in to comment.