-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c24b4bf
commit 2c30d57
Showing
2 changed files
with
212 additions
and
0 deletions.
There are no files selected for viewing
104 changes: 104 additions & 0 deletions
104
parquet/src/test/scala/magnolify/parquet/AvroParquetSuite2.scala
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,104 @@ | ||
/* | ||
* Copyright 2022 Spotify AB | ||
* | ||
* 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 magnolify.parquet | ||
|
||
import cats.* | ||
import magnolify.avro.AvroType | ||
import magnolify.cats.auto.* | ||
import magnolify.scalacheck.auto.* | ||
import magnolify.test.* | ||
import org.apache.avro.generic.GenericRecord | ||
import org.apache.hadoop.conf.Configuration | ||
import org.apache.parquet.avro.{ | ||
AvroParquetReader, | ||
AvroParquetWriter, | ||
AvroReadSupport, | ||
GenericDataSupplier | ||
} | ||
import org.scalacheck.* | ||
|
||
import scala.reflect.ClassTag | ||
|
||
case class WithList2(b: List[Boolean]) | ||
|
||
class AvroParquetSuite2 extends MagnolifySuite { | ||
|
||
private def test[T: Arbitrary: ClassTag]()(implicit | ||
at: AvroType[T], | ||
tpe: ParquetType[T], | ||
eq: Eq[T] | ||
): Unit = test[T](className[T]) | ||
|
||
private def test[T: Arbitrary]( | ||
name: String | ||
)(implicit | ||
at: AvroType[T], | ||
tpe: ParquetType[T], | ||
eq: Eq[T] | ||
): Unit = { | ||
// Ensure serializable even after evaluation of `schema` and `avroSchema` | ||
tpe.schema: Unit | ||
tpe.avroSchema: Unit | ||
val pt = ensureSerializable(tpe) | ||
|
||
property(s"$name.avro2parquet") { | ||
Prop.forAll { (t: T) => | ||
val r = at(t) | ||
|
||
val out = new TestOutputFile | ||
val writer = AvroParquetWriter.builder[GenericRecord](out).withSchema(at.schema).build() | ||
writer.write(r) | ||
writer.close() | ||
|
||
val in = new TestInputFile(out.getBytes) | ||
val reader = pt.readBuilder(in).build() | ||
val copy = reader.read() | ||
val next = reader.read() | ||
reader.close() | ||
Prop.all(eq.eqv(t, copy), next == null) | ||
} | ||
} | ||
|
||
property(s"$name.parquet2avro") { | ||
Prop.forAll { (t: T) => | ||
val out = new TestOutputFile | ||
val writer = pt.writeBuilder(out).build() | ||
writer.write(t) | ||
writer.close() | ||
|
||
val in = new TestInputFile(out.getBytes) | ||
val conf = new Configuration() | ||
AvroReadSupport.setAvroDataSupplier(conf, classOf[GenericDataSupplier]) | ||
// read with AvroType schema instead of parquet writer one | ||
AvroReadSupport.setAvroReadSchema(conf, at.schema) | ||
try { | ||
val reader = AvroParquetReader.builder[GenericRecord](in).withConf(conf).build() | ||
|
||
reader.read() | ||
reader.read() | ||
reader.close() | ||
// Prop.all(eq.eqv(t, at(copy)), next == null) | ||
} catch { | ||
case _: Exception => | ||
// println(e) | ||
} | ||
} | ||
} | ||
} | ||
|
||
test[WithList2]() | ||
} |
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