From 6b63b777cfd27a9d7f15ba7636f77523529ffef4 Mon Sep 17 00:00:00 2001 From: andrewlalis Date: Sat, 20 Jul 2024 18:35:17 -0400 Subject: [PATCH] Added tests! --- .../impl/FileSystemSavedQueryRepository.java | 1 - .../perfin/data/TimestampRangeTest.java | 21 +++++ .../FileSystemSavedQueryRepositoryTest.java | 84 +++++++++++++++++++ 3 files changed, 105 insertions(+), 1 deletion(-) create mode 100644 src/test/java/com/andrewlalis/perfin/data/TimestampRangeTest.java create mode 100644 src/test/java/com/andrewlalis/perfin/data/impl/FileSystemSavedQueryRepositoryTest.java diff --git a/src/main/java/com/andrewlalis/perfin/data/impl/FileSystemSavedQueryRepository.java b/src/main/java/com/andrewlalis/perfin/data/impl/FileSystemSavedQueryRepository.java index 2490d44..3d3a399 100644 --- a/src/main/java/com/andrewlalis/perfin/data/impl/FileSystemSavedQueryRepository.java +++ b/src/main/java/com/andrewlalis/perfin/data/impl/FileSystemSavedQueryRepository.java @@ -5,7 +5,6 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.io.IOException; -import java.io.UncheckedIOException; import java.nio.file.Files; import java.nio.file.Path; import java.util.Collections; diff --git a/src/test/java/com/andrewlalis/perfin/data/TimestampRangeTest.java b/src/test/java/com/andrewlalis/perfin/data/TimestampRangeTest.java new file mode 100644 index 0000000..debf70f --- /dev/null +++ b/src/test/java/com/andrewlalis/perfin/data/TimestampRangeTest.java @@ -0,0 +1,21 @@ +package com.andrewlalis.perfin.data; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +public class TimestampRangeTest { + @Test + public void testLastNDays() { + TimestampRange r = TimestampRange.lastNDays(1); + assertEquals(r.start(), r.end().minusDays(1)); + r = TimestampRange.lastNDays(42); + assertEquals(r.start(), r.end().minusDays(42)); + } + + @Test + public void testLastNMonths() { + TimestampRange r = TimestampRange.lastNMonths(1); + assertEquals(r.start(), r.end().minusMonths(1)); + } +} diff --git a/src/test/java/com/andrewlalis/perfin/data/impl/FileSystemSavedQueryRepositoryTest.java b/src/test/java/com/andrewlalis/perfin/data/impl/FileSystemSavedQueryRepositoryTest.java new file mode 100644 index 0000000..0fad8e4 --- /dev/null +++ b/src/test/java/com/andrewlalis/perfin/data/impl/FileSystemSavedQueryRepositoryTest.java @@ -0,0 +1,84 @@ +package com.andrewlalis.perfin.data.impl; + +import com.andrewlalis.perfin.data.util.FileUtil; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.List; + +import static org.junit.jupiter.api.Assertions.*; + +/** + * Tests the {@link FileSystemSavedQueryRepository}, which uses a specific + * directory under the Perfin profile's content directory for storing SQL + * queries. + */ +public class FileSystemSavedQueryRepositoryTest { + private static final Path CONTENT_DIR = Path.of("target", "FileSystemSavedQueryRepositoryTest_content-dir"); + + @BeforeEach + public void setupTestEnvironment() { + try { + if (Files.exists(CONTENT_DIR)) { + FileUtil.deleteDirRecursive(CONTENT_DIR); + } + + Files.createDirectories(CONTENT_DIR); + Path queriesDir = CONTENT_DIR.resolve("saved-queries"); + Files.createDirectory(queriesDir); + Files.writeString(queriesDir.resolve("sample-1.sql"), """ + SELECT * FROM transaction ORDER BY timestamp DESC; + """); + Files.writeString(queriesDir.resolve("sample-2.sql"), """ + SELECT COUNT(id) + FROM transaction + WHERE amount > 1000 + """); + Files.writeString(queriesDir.resolve("sample-3.sql"), """ + SELECT 'Test str'; + """); + Files.writeString(queriesDir.resolve("not-a-query.txt"), "not an SQL query!"); + } catch (IOException e) { + throw new RuntimeException(e); + } + } + + @Test + public void testGetSavedQueries() { + var repo = new FileSystemSavedQueryRepository(CONTENT_DIR); + var queries = repo.getSavedQueries(); + assertEquals(List.of("sample-1", "sample-2", "sample-3"), queries); + } + + @Test + public void testGetSavedQueryContent() { + var repo = new FileSystemSavedQueryRepository(CONTENT_DIR); + String content = repo.getSavedQueryContent("sample-1"); + assertEquals("SELECT * FROM transaction ORDER BY timestamp DESC;\n", content); + + assertNull(repo.getSavedQueryContent("non-existent-query")); + } + + @Test + public void testCreateSavedQuery() { + var repo = new FileSystemSavedQueryRepository(CONTENT_DIR); + String name = "sample-4"; + String content = "SELECT AVG(amount) FROM transaction;"; + repo.createSavedQuery(name, content); + assertTrue(repo.getSavedQueries().contains(name)); + assertEquals(content, repo.getSavedQueryContent(name)); + } + + @Test + public void testDeleteSavedQuery() { + var repo = new FileSystemSavedQueryRepository(CONTENT_DIR); + assertTrue(repo.getSavedQueries().contains("sample-1")); + repo.deleteSavedQuery("sample-1"); + assertFalse(repo.getSavedQueries().contains("sample-1")); + repo.deleteSavedQuery("sample-1"); + assertFalse(repo.getSavedQueries().contains("sample-1")); + } +}