Skip to content

Commit

Permalink
More coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
Willena committed Nov 10, 2024
1 parent c664e71 commit 3e7608d
Show file tree
Hide file tree
Showing 13 changed files with 148 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ private Call(Builder builder) {
name = builder.name;
args = builder.args;
ensureDefined("name", name);
ensureDefined("args", args);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1965,8 +1965,10 @@ private Predictors() {
* @return the call
*/
public static Call holtWinters(Call call, IntegerLiteral n, IntegerLiteral s) {
if (call.getArgs() != null
&& (call.getArgs().size() != 1 || !(call.getArgs().get(0) instanceof VarRef))) {
if (call == null
|| call.getArgs() == null
|| call.getArgs().size() != 1
|| !(call.getArgs().get(0) instanceof VarRef)) {
throw new IllegalArgumentException("First argument of function must be a field");
}
return new Call.Builder().function(HOLT_WINTERS_NAME).withArguments(call, n, s).build();
Expand All @@ -1981,8 +1983,10 @@ public static Call holtWinters(Call call, IntegerLiteral n, IntegerLiteral s) {
* @return the call
*/
public static Call holtWintersWithFit(Call call, IntegerLiteral n, IntegerLiteral s) {
if (call.getArgs() != null
&& (call.getArgs().size() != 1 || !(call.getArgs().get(0) instanceof VarRef))) {
if (call == null
|| call.getArgs() == null
|| call.getArgs().size() != 1
|| !(call.getArgs().get(0) instanceof VarRef)) {
throw new IllegalArgumentException("First argument of function must be a field");
}
return new Call.Builder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ public Builder replication(Integer retentionPolicyReplication) {
* @param retentionPolicyName the {@code retentionPolicyName} to set
* @return a reference to this Builder
*/
public Builder withRetentionPolicyName(String retentionPolicyName) {
public Builder retentionPolicyName(String retentionPolicyName) {
this.retentionPolicyName = retentionPolicyName;
return this;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,9 @@ public static SortField desc(String name) {
@Override
public String toString() {
var buf = new StringBuilder();
if (name != null && !name.isEmpty()) {
// TODO: not sure about that
// buf.append(name);
buf.append(quoteIdentifier(name));
buf.append(" ");
}
buf.append(quoteIdentifier(name));
buf.append(" ");

if (ascending) {
buf.append("ASC");
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,7 @@ public CreateDatabaseStatement visitCreate_database_stmt(

RetentionPolicy rp =
new RetentionPolicy.Builder()
.withRetentionPolicyName(
.retentionPolicyName(
Optional.ofNullable(ctx.retention_policy_name())
.map(e -> e.IDENTIFIER().getText())
.map(InfluxqlAstAdapter::trimIdentifierQuotes)
Expand Down Expand Up @@ -974,16 +974,6 @@ public SelectStatement visitSelect_stmt(InfluxqlParser.Select_stmtContext ctx) {
.build();
}

@Override
public Node visitAlias(InfluxqlParser.AliasContext ctx) {
return null;
}

@Override
public Node visitBack_ref(InfluxqlParser.Back_refContext ctx) {
return null;
}

@Override
public Dimension visitDimension(InfluxqlParser.DimensionContext ctx) {
return Dimension.of(visitExpression(ctx.expression()));
Expand Down
3 changes: 2 additions & 1 deletion src/test/java/io/github/willena/influxql/ast/QueryTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,8 @@ void parse_multiple_statements() throws URISyntaxException, IOException {
Map.entry(
"SHOW TAG VALUES EXACT CARDINALITY WITH KEY = \"myTagKey\"",
"SHOW TAG VALUES EXACT CARDINALITY WITH KEY = myTagKey"),
Map.entry("SHOW USERS", "SHOW USERS"));
Map.entry("SHOW USERS", "SHOW USERS"),
Map.entry("SHOW SHARDS", "SHOW SHARDS"));

/**
* Test statements from <a
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,5 +44,14 @@ void testCall() {
List.of(VarRef.of("uuu"), StringLiteral.of("ddd"))))
.build()
.toString());

assertEquals(
"mean(uuu, 'ddd')",
new Call.Builder()
.function("mean")
.withArguments(new StringJoiningList<>(List.of(VarRef.of("uuu"))))
.withArguments(StringLiteral.of("ddd"))
.build()
.toString());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,6 @@ void of() {
@Test
void ofType() {
assertEquals("tutu::string", VarRef.of("tutu", DataType.STRING).toString());
assertEquals("tutu", VarRef.of("tutu", DataType.UNKNOWN).toString());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import static org.junit.jupiter.api.Assertions.assertEquals;

import java.time.Duration;
import java.util.List;
import org.junit.jupiter.api.Test;

Expand All @@ -33,4 +34,22 @@ void listTest() {
List.of(StringLiteral.of("A"), StringLiteral.of("B")),
ListLiteral.of(List.of(StringLiteral.of("A"), StringLiteral.of("B"))).getValue());
}

@Test
void listOfDuration() {
assertEquals(
"(1d, 1h)", ListLiteral.of(Duration.ofDays(1), Duration.ofHours(1)).toString());
}

@Test
void listOfNumber() {
assertEquals("(1.0, 2.0)", ListLiteral.of(1, 2).toString());
}

@Test
void listOfLiteral() {
assertEquals(
"('A', 'B')",
ListLiteral.of(StringLiteral.of("A"), StringLiteral.of("B")).toString());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* InfluxQL Java package
* Copyright 2024 Guillaume VILLENA also known as "Willena" on GitHub
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.github.willena.influxql.ast.expr.literal;

import static org.junit.jupiter.api.Assertions.*;

import org.junit.jupiter.api.Test;

class NullLiteralTest {

@Test
void of() {
NullLiteral nullLiteral = NullLiteral.of();
assertEquals("null", nullLiteral.toString());
assertNull(nullLiteral.getValue());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,15 @@ void transformerAtan2() {
FunctionFactory.Selectors.sample(
VarRef.of("oo"), IntegerLiteral.of(1)))
.toString());

assertThrows(
IllegalArgumentException.class,
() ->
FunctionFactory.Transformations.atan2(
FunctionFactory.Selectors.first(VarRef.of("oo")),
FunctionFactory.Selectors.sample(
VarRef.of("oo"), IntegerLiteral.of(1)))
.toString());
}

@Test
Expand Down Expand Up @@ -473,6 +482,14 @@ void transformerDerivative() {
VarRef.of("oo"), IntegerLiteral.of(1)),
DurationLiteral.of(Duration.ofHours(1)))
.toString());

assertThrows(
IllegalArgumentException.class,
() ->
FunctionFactory.Transformations.derivative(
FunctionFactory.Selectors.sample(
VarRef.of("oo"), IntegerLiteral.of(1)))
.toString());
}

@Test
Expand Down Expand Up @@ -545,6 +562,13 @@ void transformerElapsed() {
VarRef.of("oo"), IntegerLiteral.of(1)),
DurationLiteral.of(Duration.ofHours(1)))
.toString());
assertThrows(
IllegalArgumentException.class,
() ->
FunctionFactory.Transformations.elapsed(
FunctionFactory.Selectors.sample(
VarRef.of("oo"), IntegerLiteral.of(1)))
.toString());
}

@Test
Expand Down Expand Up @@ -746,6 +770,13 @@ void transformerNonNegativeDerivative() {
VarRef.of("oo"), IntegerLiteral.of(1)),
DurationLiteral.of(Duration.ofHours(1)))
.toString());
assertThrows(
IllegalArgumentException.class,
() ->
FunctionFactory.Transformations.nonNegativeDerivative(
FunctionFactory.Selectors.sample(
VarRef.of("oo"), IntegerLiteral.of(1)))
.toString());
}

@Test
Expand Down Expand Up @@ -882,6 +913,28 @@ void predictorsHoltWinters() {
FunctionFactory.Aggregations.count(Wildcard.wildcard()),
IntegerLiteral.of(1),
IntegerLiteral.of(1)));
assertThrows(
IllegalArgumentException.class,
() ->
FunctionFactory.Predictors.holtWinters(
new Call.Builder().function("tutu").build(),
IntegerLiteral.of(1),
IntegerLiteral.of(1)));
assertThrows(
IllegalArgumentException.class,
() ->
FunctionFactory.Predictors.holtWinters(
null, IntegerLiteral.of(1), IntegerLiteral.of(1)));
assertThrows(
IllegalArgumentException.class,
() ->
FunctionFactory.Predictors.holtWinters(
new Call.Builder()
.function("tutu")
.withArguments(NumberLiteral.of(1), NumberLiteral.of(2))
.build(),
IntegerLiteral.of(1),
IntegerLiteral.of(1)));
assertEquals(
"HOLT_WINTERS(COUNT(rrr), 1, 1)",
FunctionFactory.Predictors.holtWinters(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

package io.github.willena.influxql.ast.extra;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.*;

import java.time.Duration;
import org.junit.jupiter.api.Test;
Expand All @@ -28,7 +28,7 @@ void retentionPolicy() {
RetentionPolicy rp =
new RetentionPolicy.Builder()
.duration(Duration.ofSeconds(1))
.withRetentionPolicyName("test")
.retentionPolicyName("test")
.shardDuration(Duration.ofHours(1))
.replication(10)
.build();
Expand All @@ -38,4 +38,13 @@ void retentionPolicy() {
assertEquals(Duration.ofHours(1), rp.getRetentionPolicyShardGroupDuration());
assertEquals(10, rp.getRetentionPolicyReplication());
}

@Test
void isEmpty() {
assertTrue(new RetentionPolicy.Builder().build().isEmpty());
assertFalse(new RetentionPolicy.Builder().retentionPolicyName("nn").build().isEmpty());
assertFalse(new RetentionPolicy.Builder().duration(Duration.ZERO).build().isEmpty());
assertFalse(new RetentionPolicy.Builder().shardDuration(Duration.ZERO).build().isEmpty());
assertFalse(new RetentionPolicy.Builder().replication(2).build().isEmpty());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,14 @@ class FieldTest {
@Test
void testField() {
assertEquals("fieldName", Field.field("fieldName").toString());
assertEquals("fieldName", Field.field("fieldName").toString());
assertEquals(
"fieldName",
new Field.Builder()
.withExpr(VarRef.of("fieldName"))
.withAlias("")
.build()
.toString());
assertEquals("fieldName::integer", Field.field("fieldName", DataType.INTEGER).toString());
}

Expand Down

0 comments on commit 3e7608d

Please sign in to comment.