-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixes #5.
- Loading branch information
Showing
9 changed files
with
179 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package com.exasol.common.avro | ||
|
||
import java.io.File | ||
|
||
import com.exasol.common.data.Row | ||
|
||
import org.apache.avro.SchemaBuilder | ||
import org.apache.avro.file.DataFileReader | ||
import org.apache.avro.file.DataFileWriter | ||
import org.apache.avro.generic._ | ||
import org.apache.avro.specific.SpecificDatumWriter | ||
import org.scalatest.BeforeAndAfterEach | ||
import org.scalatest.funsuite.AnyFunSuite | ||
|
||
class AvroRowIteratorTest extends AnyFunSuite with BeforeAndAfterEach { | ||
|
||
private[this] var record: GenericData.Record = _ | ||
private[this] val schema = SchemaBuilder | ||
.record("record") | ||
.fields() | ||
.requiredString("name") | ||
.endRecord() | ||
|
||
override final def beforeEach(): Unit = { | ||
record = new GenericData.Record(schema) | ||
record.put("name", "John") | ||
() | ||
} | ||
|
||
test("apply returns iterator") { | ||
val file = File.createTempFile("record", "avro") | ||
file.deleteOnExit() | ||
write(file, record) | ||
val reader = new DataFileReader(file, new GenericDatumReader[GenericRecord]()) | ||
val iterator = AvroRowIterator(reader) | ||
assert(iterator.hasNext === true) | ||
assert(iterator.next() === Row(Seq("John"))) | ||
assert(iterator.hasNext === false) | ||
val thrown = intercept[NoSuchElementException] { | ||
iterator.next() | ||
} | ||
assert(thrown.getMessage === "Avro reader called next on an empty iterator!") | ||
} | ||
|
||
private[this] def write[T <: GenericRecord](file: File, record: T): Unit = { | ||
val writer = new DataFileWriter[T](new SpecificDatumWriter[T]()) | ||
writer.create(record.getSchema, file) | ||
writer.append(record) | ||
writer.close() | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package com.exasol.common.data | ||
|
||
import org.scalatest.funsuite.AnyFunSuite | ||
import org.scalatest.matchers.should.Matchers | ||
|
||
class RowTest extends AnyFunSuite with Matchers { | ||
|
||
private[this] val values: Seq[Any] = Seq("value1", 1, 3.14, null) | ||
private[this] val row = Row(values) | ||
|
||
test("getValues returns row values") { | ||
assert(row.getValues() === values) | ||
} | ||
|
||
test("get value at index") { | ||
assert(row.get(0).isInstanceOf[String]) | ||
assert(row.get(0) === "value1") | ||
} | ||
|
||
test("getAs[T] value at index") { | ||
assert(row.getAs[String](0) === "value1") | ||
assert(row.getAs[Double](2) === 3.14) | ||
} | ||
|
||
test("isNuallAt returns true if null") { | ||
assert(row.isNullAt(3) === true) | ||
assert(row.isNullAt(1) === false) | ||
} | ||
|
||
test("equalality check") { | ||
val rowEqual = Row(Seq("value1", 1, 3.14, null)) | ||
val rowNotEqual = Row(Seq("value1", 2, 3.14, null)) | ||
row shouldEqual rowEqual | ||
row should not equal rowNotEqual | ||
} | ||
|
||
test("throws class cast exception") { | ||
val thrown = intercept[ClassCastException] { | ||
row.getAs[Int](0) | ||
} | ||
val expected = "class java.lang.String cannot be cast to class java.lang.Integer" | ||
assert(thrown.getMessage.contains(expected)) | ||
} | ||
|
||
test("throws index out of bounds") { | ||
val thrown = intercept[IndexOutOfBoundsException] { | ||
row.get(5) | ||
} | ||
assert(thrown.getMessage === "5") | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package com.exasol.common.json | ||
|
||
import org.scalatest.funsuite.AnyFunSuite | ||
|
||
class JsonMapperTest extends AnyFunSuite { | ||
|
||
test("parse to and from json to internal data type") { | ||
val jsonStr = """{"name":"John","age":30,"skills":["a","b","c"]}""" | ||
val map = JsonMapper.parseJson[Map[String, Any]](jsonStr) | ||
assert(map.get("skills") === Option(Seq("a", "b", "c"))) | ||
assert(JsonMapper.toJson(map) === jsonStr) | ||
} | ||
|
||
} |