Skip to content

Commit

Permalink
Add unit tests to verify functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
rainbowdashlabs committed Dec 4, 2024
1 parent e2103a4 commit 182cb2f
Show file tree
Hide file tree
Showing 8 changed files with 93 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
* A call is a subelement of a {@link Calls}. It represents a single query call of any kind.
*/
public final class CallImpl implements Call {
private final List<BaseParameter> tokens = new ArrayList<>();
private final List<BaseParameter> parameter = new ArrayList<>();
private int index = 1;

public CallImpl() {
Expand All @@ -56,12 +56,12 @@ private int nextIndex() {
}

private Call addToken(String token, ThrowingBiConsumer<PreparedStatement, Integer, SQLException> apply) {
tokens.add(new TokenParameter(token, apply));
parameter.add(new TokenParameter(token, apply));
return this;
}

private Call addToken(ThrowingBiConsumer<PreparedStatement, Integer, SQLException> apply) {
tokens.add(new IndexParameter(nextIndex(), apply));
parameter.add(new IndexParameter(nextIndex(), apply));
return this;
}

Expand Down Expand Up @@ -351,12 +351,15 @@ public <T> Call bind(T value, Adapter<T> adapter) {
}

public void apply(TokenizedQuery query, PreparedStatement stmt) throws SQLException {
int count = 0;
for (var token : tokens) {
token.apply(query, stmt);
if (token instanceof IndexParameter) count++;
int indexCount = 0;
var tokens = query.getNamedTokens();
for (var param : parameter) {
param.apply(query, stmt);
if (param instanceof IndexParameter) indexCount++;
if (param instanceof TokenParameter token) tokens.remove(token.token());
}
Check.assertIndexFilled(count, query);
Check.assertIndexFilled(indexCount, query);
Check.missingToken(tokens, query);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import org.jetbrains.annotations.Nullable;

import java.sql.ResultSet;
import java.util.Set;

public final class Check {
public static void assertQueryResult(@Nullable Object object) {
Expand All @@ -23,14 +24,17 @@ public static void assertQueryResult(@Nullable Object object) {
}

public static void assertIndexRange(int index, TokenizedQuery query) {
if (query.indexSize() >= index) {
throw new IllegalQueryParameterException("No parameter with index %s exists in query \"%s\". Only %s parameters are defined".formatted(index, query.sql(), query.indexSize()));
}
if (query.indexSize() >= index) return;
throw new IllegalQueryParameterException("No parameter with index %s exists in query \"%s\". Only %s parameter(s) is/are defined".formatted(index, query.sql(), query.indexSize()));
}

public static void assertIndexFilled(int count, TokenizedQuery query) {
if (query.indexSize() != count) {
throw new IllegalQueryParameterException("Missing index parameter. %s were given, but %s were set in query: \"%s\"".formatted(count, query.indexSize(), query.sql()));
}
if (query.indexSize() == count) return;
throw new IllegalQueryParameterException("Missing index parameter. %s were given, but %s were set in query: \"%s\"".formatted(count, query.indexSize(), query.sql()));
}

public static void missingToken(Set<String> tokens, TokenizedQuery query) {
if (tokens.isEmpty()) return;
throw new IllegalQueryParameterException("The parameters %s are not bound in query: %s".formatted(String.join(", ", tokens), query.sql()));
}
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
/*
* SPDX-License-Identifier: LGPL-3.0-or-later
*
* Copyright (C) RainbowDashLabs and Contributor
*/

package de.chojo.sadu.queries.exception;

public class IllegalQueryParameterException extends RuntimeQueryException{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
/*
* SPDX-License-Identifier: LGPL-3.0-or-later
*
* Copyright (C) RainbowDashLabs and Contributor
*/

package de.chojo.sadu.queries.exception;

import de.chojo.sadu.queries.query.ParsedQueryImpl;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
/*
* SPDX-License-Identifier: LGPL-3.0-or-later
*
* Copyright (C) RainbowDashLabs and Contributor
*/

package de.chojo.sadu.queries.exception;

public class RuntimeQueryException extends RuntimeException{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,8 @@ public void apply(TokenizedQuery query, PreparedStatement stmt) throws SQLExcept
apply.accept(stmt, index);
}
}

public String token() {
return token;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,12 @@
package de.chojo.sadu.queries.query;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.regex.Pattern;

public class TokenizedQuery {
Expand Down Expand Up @@ -47,14 +50,18 @@ public static TokenizedQuery create(String sql) {
}

public List<Integer> getNamedTokenIndex(String token) {
return namedToken.get(token);
return namedToken.getOrDefault(token, Collections.emptyList());
}

public Set<String> getNamedTokens() {
return new HashSet<>(namedToken.keySet());
}

public int getIndexTokenIndex(int index) {
return indexToken.get(index);
}

public int indexSize(){
public int indexSize() {
return indexToken.size();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import de.chojo.sadu.queries.api.configuration.QueryConfiguration;
import de.chojo.sadu.queries.configuration.QueryConfigurationBuilder;
import de.chojo.sadu.queries.examples.dao.User;
import de.chojo.sadu.queries.exception.IllegalQueryParameterException;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
Expand Down Expand Up @@ -54,4 +55,44 @@ public void failOnInvalidToken() {
.all();
});
}

// Retrieve all matching users directly
@Test
public void failOnTooFewParameters() {
Assertions.assertThrows(IllegalQueryParameterException.class, () -> {
List<User> users = query.query("SELECT * FROM users WHERE id = ?")
.single(call())
.map(User.map())
.all();
});
}

@Test
public void failOnTooManyParameters() {
Assertions.assertThrows(IllegalQueryParameterException.class, () -> {
List<User> users = query.query("SELECT * FROM users WHERE id = ?")
.single(call().bind(1).bind(1))
.map(User.map())
.all();
});
}

@Test
public void failOnMissingNamedToken() {
Assertions.assertThrows(IllegalQueryParameterException.class, () -> {
List<User> users = query.query("SELECT * FROM users WHERE id = ? AND name = :name")
.single(call().bind(1))
.map(User.map())
.all();
});
}
@Test
public void failOnUnknownNamedToken() {
Assertions.assertThrows(IllegalQueryParameterException.class, () -> {
List<User> users = query.query("SELECT * FROM users WHERE id = ?")
.single(call().bind(1).bind("name", "name"))
.map(User.map())
.all();
});
}
}

0 comments on commit 182cb2f

Please sign in to comment.