Skip to content

Commit

Permalink
feat: add more unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
fengjiachun committed Dec 20, 2023
1 parent 0361c06 commit 3f7f7ce
Show file tree
Hide file tree
Showing 5 changed files with 336 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -179,5 +179,9 @@ public Database.RowInsertRequest into() {
.setRows(rows) //
.build();
}

public List<RowData.Row> rows() {
return rows;
}
}
}
47 changes: 47 additions & 0 deletions ingester-protocol/src/test/java/io/greptime/models/ErrTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* Copyright 2023 Greptime Team
*
* 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.greptime.models;

import io.greptime.common.Endpoint;
import org.junit.Assert;
import org.junit.Test;

/**
* @author jiachun.fjc
*/
public class ErrTest {

@Test
public void testWriteErr() {
Err err = Err.writeErr(300, new IllegalStateException("test_err"), Endpoint.of("127.0.0.1", 8081), null);

Assert.assertFalse(err.mapToResult().isOk());
Assert.assertEquals("test_err", err.getError().getMessage());
Assert.assertEquals("127.0.0.1:8081", err.getErrTo().toString());
Assert.assertNull(err.getRowsFailed());
}

@Test
public void testQueryErr() {
IllegalStateException error = new IllegalStateException("test_err");
Err err = Err.queryErr(300, error, Endpoint.of("127.0.0.1", 8081), "select * from test");

Assert.assertFalse(err.mapToResult().isOk());
Assert.assertEquals("test_err", err.getError().getMessage());
Assert.assertEquals("127.0.0.1:8081", err.getErrTo().toString());
Assert.assertEquals(err.getFailedQl(), "select * from test");
}
}
120 changes: 120 additions & 0 deletions ingester-protocol/src/test/java/io/greptime/models/ResultTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
/*
* Copyright 2023 Greptime Team
*
* 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.greptime.models;

import org.junit.Assert;
import org.junit.Test;

/**
* @author jiachun.fjc
*/
public class ResultTest {

@Test
public void testMap() {
final Result<WriteOk, Err> r1 = Result.ok(WriteOk.ok(2, 0, null));
final Result<Integer, Err> r2 = r1.map(WriteOk::getSuccess);
Assert.assertEquals(2, r2.getOk().intValue());

final Result<WriteOk, Err> r5 = Result.err(Err.writeErr(400, null, null, null));
final Result<Integer, Err> r6 = r5.map(WriteOk::getSuccess);
Assert.assertFalse(r6.isOk());
}

@Test
public void testMapOr() {
final Result<WriteOk, Err> r1 = Result.ok(WriteOk.ok(2, 0, null));
final Integer r2 = r1.mapOr(-1, WriteOk::getSuccess);
Assert.assertEquals(2, r2.intValue());

final Result<WriteOk, Err> r3 = Result.err(Err.writeErr(400, null, null, null));
final Integer r4 = r3.mapOr(-1, WriteOk::getSuccess);
Assert.assertEquals(-1, r4.intValue());
}

@Test
public void testMapOrElse() {
final Result<WriteOk, Err> r1 = Result.ok(WriteOk.ok(2, 0, null));
final Integer r2 = r1.mapOrElse(err -> -1, WriteOk::getSuccess);
Assert.assertEquals(2, r2.intValue());

final Result<WriteOk, Err> r3 = Result.err(Err.writeErr(400, null, null, null));
final Integer r4 = r3.mapOrElse(err -> -1, WriteOk::getSuccess);
Assert.assertEquals(-1, r4.intValue());
}

@Test
public void testMapErr() {
final Result<WriteOk, Err> r1 = Result.ok(WriteOk.ok(2, 0, null));
final Result<WriteOk, Throwable> r2 = r1.mapErr(Err::getError);
Assert.assertEquals(2, r2.getOk().getSuccess());

IllegalStateException error = new IllegalStateException("error test");
final Result<WriteOk, Err> r3 = Result.err(Err.writeErr(400, error, null, null));
final Result<WriteOk, Throwable> r4 = r3.mapErr(Err::getError);
Assert.assertEquals("error test", r4.getErr().getMessage());
}

@Test
public void testAndThen() {
final Result<WriteOk, Err> r1 = Result.ok(WriteOk.ok(2, 0, null));
final Result<WriteOk, Err> r2 = r1.andThen(writeOk -> {
WriteOk newOne = WriteOk.ok(writeOk.getSuccess() + 1, 0, null);
return newOne.mapToResult();
});
Assert.assertEquals(3, r2.getOk().getSuccess());

final Result<WriteOk, Err> r3 = Result.err(Err.writeErr(400, null, null, null));
final Result<WriteOk, Err> r4 = r3.andThen(writeOk -> {
WriteOk newOne = WriteOk.ok(writeOk.getSuccess() + 1, 0, null);
return newOne.mapToResult();
});
Assert.assertFalse(r4.isOk());
}

@Test
public void testOrElse() {
final Result<WriteOk, Err> r1 = Result.ok(WriteOk.ok(2, 0, null));
final Result<WriteOk, String> r2 = r1.orElse(err -> Result.ok(WriteOk.ok(0, 0, null)));
Assert.assertEquals(2, r2.getOk().getSuccess());

final Result<WriteOk, Err> r3 = Result.err(Err.writeErr(400, null, null, null));
final Result<WriteOk, String> r4 = r3.orElse(err -> Result.ok(WriteOk.ok(0, 0, null)));
Assert.assertEquals(0, r4.getOk().getSuccess());
}

@Test
public void testUnwrapOr() {
final Result<WriteOk, Err> r1 = Result.ok(WriteOk.ok(2, 0, null));
final WriteOk r2 = r1.unwrapOr(WriteOk.emptyOk());
Assert.assertEquals(2, r2.getSuccess());

final Result<WriteOk, Err> r3 = Result.err(Err.writeErr(400, null, null, null));
final WriteOk r4 = r3.unwrapOr(WriteOk.emptyOk());
Assert.assertEquals(0, r4.getSuccess());
}

@Test
public void testUnwrapOrElse() {
final Result<WriteOk, Err> r1 = Result.ok(WriteOk.ok(2, 0, null));
final WriteOk r2 = r1.unwrapOrElse(err -> WriteOk.emptyOk());
Assert.assertEquals(2, r2.getSuccess());

final Result<WriteOk, Err> r3 = Result.err(Err.writeErr(400, null, null, null));
final WriteOk r4 = r3.unwrapOrElse(err -> WriteOk.emptyOk());
Assert.assertEquals(0, r4.getSuccess());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
* Copyright 2023 Greptime Team
*
* 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.greptime.models;

import org.junit.Assert;
import org.junit.Test;

/**
* @author jiachun.fjc
*/
public class TableRowsTest {

@Test
public void testWriteRowsNonNull() {
TableSchema schema = TableSchema.newBuilder(TableName.with("", "test_table")) //
.columnNames("col1", "col2", "col3") //
.semanticTypes(SemanticType.Tag, SemanticType.Tag, SemanticType.Field) //
.dataTypes(DataType.String, DataType.String, DataType.Int32) //
.build();

TableRows.RowBasedTableRows rows = (TableRows.RowBasedTableRows) TableRows.newBuilder(schema).build();
rows.insert("1", "11", 111) //
.insert("2", "22", 222) //
.insert("3", "33", 333);

Assert.assertEquals(3, rows.rowCount());
Assert.assertEquals(111, rows.rows().get(0).getValues(2).getI32Value());
Assert.assertEquals(222, rows.rows().get(1).getValues(2).getI32Value());
Assert.assertEquals(333, rows.rows().get(2).getValues(2).getI32Value());
}

@Test
public void testWriteRowsSomeNull() {
TableSchema schema = TableSchema.newBuilder(TableName.with("", "test_table")) //
.columnNames("col1", "col2", "col3") //
.semanticTypes(SemanticType.Tag, SemanticType.Tag, SemanticType.Field) //
.dataTypes(DataType.String, DataType.String, DataType.Int32) //
.build();

TableRows.RowBasedTableRows rows = (TableRows.RowBasedTableRows) TableRows.newBuilder(schema).build();
rows.insert("1", "11", 111) //
.insert("2", null, 222) //
.insert("3", "33", null);

Assert.assertEquals(3, rows.rowCount());
Assert.assertEquals(111, rows.rows().get(0).getValues(2).getI32Value());
Assert.assertEquals(222, rows.rows().get(1).getValues(2).getI32Value());
Assert.assertFalse(rows.rows().get(2).getValues(2).hasI32Value());
Assert.assertFalse(rows.rows().get(1).getValues(1).hasStringValue());
}
}
101 changes: 101 additions & 0 deletions ingester-protocol/src/test/java/io/greptime/models/UtilTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
/*
* Copyright 2023 Greptime Team
*
* 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.greptime.models;

import io.greptime.v1.Common;
import org.junit.Assert;
import org.junit.Test;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.time.Instant;
import java.time.LocalDate;
import java.util.Calendar;
import java.util.Random;
import java.util.TimeZone;

/**
* @author jiachun.fjc
*/
public class UtilTest {

@Test
public void testGetLongValue() {
Assert.assertEquals(1L, Util.getLongValue(1));
Assert.assertEquals(1L, Util.getLongValue(1L));
Assert.assertEquals(1L, Util.getLongValue(1.0));
Assert.assertEquals(1L, Util.getLongValue(1.0f));
Assert.assertEquals(1L, Util.getLongValue(BigInteger.valueOf(1)));
Assert.assertEquals(1L, Util.getLongValue(BigDecimal.valueOf(1)));
}

@Test
public void testGetDateValue() {
Calendar cal = Calendar.getInstance();
TimeZone gmtTimeZone = TimeZone.getTimeZone("GMT");
cal.setTimeZone(gmtTimeZone);
cal.set(1970, Calendar.JANUARY, 2);
Assert.assertEquals(1, Util.getDateValue(cal.getTime()));
Assert.assertEquals(1, Util.getDateValue(Instant.ofEpochSecond(86400)));
Assert.assertEquals(1, Util.getDateValue(LocalDate.ofEpochDay(1)));
Assert.assertEquals(1, Util.getDateValue(1));
}

@Test
public void testGetDateTimeValue() {
Calendar cal = Calendar.getInstance();
TimeZone gmtTimeZone = TimeZone.getTimeZone("GMT");
cal.setTimeZone(gmtTimeZone);
cal.set(1970, Calendar.JANUARY, 2, 0, 0, 0);
Assert.assertEquals(86400, Util.getDateTimeValue(cal.getTime()));
Assert.assertEquals(86400, Util.getDateTimeValue(Instant.ofEpochSecond(86400)));
Assert.assertEquals(86400, Util.getDateTimeValue(86400));
}

@Test
public void testGetDecimal128Value() {
final int precision = 38;
final int scale = 9;

Common.DecimalTypeExtension decimalTypeExtension = Common.DecimalTypeExtension.newBuilder() //
.setPrecision(precision) //
.setScale(scale) //
.build();
Common.ColumnDataTypeExtension dataTypeExtension = Common.ColumnDataTypeExtension.newBuilder() //
.setDecimalType(decimalTypeExtension) //
.build();

for (int i = 0; i < 1000; i++) {
BigInteger bigInt = BigInteger.valueOf(new Random().nextLong()).shiftLeft(64);
bigInt = bigInt.add(BigInteger.valueOf(new Random().nextLong()));
BigDecimal value = new BigDecimal(bigInt, scale);
Common.Decimal128 result = Util.getDecimal128Value(dataTypeExtension, value);

long lo = result.getLo();
BigInteger loValue = BigInteger.valueOf(lo & Long.MAX_VALUE);
if (lo < 0) {
loValue = loValue.add(BigInteger.valueOf(1).shiftLeft(63));
}

BigInteger unscaledValue = BigInteger.valueOf(result.getHi());
unscaledValue = unscaledValue.shiftLeft(64);
unscaledValue = unscaledValue.add(loValue);

BigDecimal value2 = new BigDecimal(unscaledValue, scale);

Assert.assertEquals(value, value2);
}
}
}

0 comments on commit 3f7f7ce

Please sign in to comment.