From e855d0f666db9186f957ecabf6b2bfa3eefb97af Mon Sep 17 00:00:00 2001 From: Kent Yao Date: Thu, 22 Mar 2018 21:33:17 +0800 Subject: [PATCH] add ut TypeDescriptorSuite --- .../kyuubi/schema/TypeDescriptor.scala | 2 +- .../yaooqinn/kyuubi/utils/ReflectUtils.scala | 2 +- .../kyuubi/schema/TypeDescriptorSuite.scala | 52 +++++++++++++++++++ .../kyuubi/schema/TypeQualifiersSuite.scala | 8 +-- 4 files changed, 58 insertions(+), 6 deletions(-) create mode 100644 src/test/scala/yaooqinn/kyuubi/schema/TypeDescriptorSuite.scala diff --git a/src/main/scala/yaooqinn/kyuubi/schema/TypeDescriptor.scala b/src/main/scala/yaooqinn/kyuubi/schema/TypeDescriptor.scala index be4bb67d8..b9d9eac68 100644 --- a/src/main/scala/yaooqinn/kyuubi/schema/TypeDescriptor.scala +++ b/src/main/scala/yaooqinn/kyuubi/schema/TypeDescriptor.scala @@ -21,7 +21,7 @@ import org.apache.hive.service.cli.thrift.{TPrimitiveTypeEntry, TTypeDesc, TType import org.apache.spark.sql.types.{DataType, DecimalType} class TypeDescriptor(typ: DataType) { - private val typeQualifiers: Option[TypeQualifiers] = typ match { + private[this] val typeQualifiers: Option[TypeQualifiers] = typ match { case d: DecimalType => Some(TypeQualifiers.fromTypeInfo(d)) case _ => None } diff --git a/src/main/scala/yaooqinn/kyuubi/utils/ReflectUtils.scala b/src/main/scala/yaooqinn/kyuubi/utils/ReflectUtils.scala index 13b25f93f..eaaa71be9 100644 --- a/src/main/scala/yaooqinn/kyuubi/utils/ReflectUtils.scala +++ b/src/main/scala/yaooqinn/kyuubi/utils/ReflectUtils.scala @@ -149,7 +149,7 @@ object ReflectUtils extends Logging { } } - def getObject(o: Any, name: String): Any = { + def getFieldValue(o: Any, name: String): Any = { Try { val field = o.getClass.getDeclaredField(name) field.setAccessible(true) diff --git a/src/test/scala/yaooqinn/kyuubi/schema/TypeDescriptorSuite.scala b/src/test/scala/yaooqinn/kyuubi/schema/TypeDescriptorSuite.scala new file mode 100644 index 000000000..f5f95323c --- /dev/null +++ b/src/test/scala/yaooqinn/kyuubi/schema/TypeDescriptorSuite.scala @@ -0,0 +1,52 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 yaooqinn.kyuubi.schema + +import org.apache.hive.service.cli.thrift.{TCLIServiceConstants, TTypeId} +import org.apache.spark.SparkFunSuite +import org.apache.spark.sql.types.{ByteType, DecimalType, NullType} + +import yaooqinn.kyuubi.utils.ReflectUtils + +class TypeDescriptorSuite extends SparkFunSuite { + + test("TypeDescriptor basic tests") { + val typeDescriptor = new TypeDescriptor(new DecimalType(10, 9)) + val tTypeDesc = typeDescriptor.toTTypeDesc + assert(tTypeDesc.getTypesSize === 1) + assert( + tTypeDesc + .getTypes.get(0) + .getPrimitiveEntry + .getTypeQualifiers + .getQualifiers + .get(TCLIServiceConstants.PRECISION).getI32Value === 10) + + val typeDescriptor2 = new TypeDescriptor(ByteType) + val tTypeDesc2 = typeDescriptor2.toTTypeDesc + assert(tTypeDesc2.getTypesSize === 1) + assert(tTypeDesc2.getTypes.get(0).getPrimitiveEntry.getTypeQualifiers === null) + assert(tTypeDesc2.getTypes.get(0).getPrimitiveEntry.getType === TTypeId.TINYINT_TYPE) + + assert(ReflectUtils.getFieldValue(typeDescriptor, "typeQualifiers") + .asInstanceOf[Option[TypeDescriptor]].isDefined) + assert(ReflectUtils.getFieldValue(typeDescriptor2, "typeQualifiers") + .asInstanceOf[Option[TypeDescriptor]].isEmpty) + } + +} diff --git a/src/test/scala/yaooqinn/kyuubi/schema/TypeQualifiersSuite.scala b/src/test/scala/yaooqinn/kyuubi/schema/TypeQualifiersSuite.scala index 3c4ae84c7..b5fb70058 100644 --- a/src/test/scala/yaooqinn/kyuubi/schema/TypeQualifiersSuite.scala +++ b/src/test/scala/yaooqinn/kyuubi/schema/TypeQualifiersSuite.scala @@ -29,10 +29,10 @@ class TypeQualifiersSuite extends SparkFunSuite { val typeQualifiers1 = TypeQualifiers.fromTypeInfo(new DecimalType(10, 9)) val typeQualifiers2 = TypeQualifiers.fromTypeInfo(BooleanType) - assert(ReflectUtils.getObject(typeQualifiers1, "precision") === Some(10)) - assert(ReflectUtils.getObject(typeQualifiers1, "scale") === Some(9)) - assert(ReflectUtils.getObject(typeQualifiers2, "precision") === None) - assert(ReflectUtils.getObject(typeQualifiers2, "scale") === None) + assert(ReflectUtils.getFieldValue(typeQualifiers1, "precision") === Some(10)) + assert(ReflectUtils.getFieldValue(typeQualifiers1, "scale") === Some(9)) + assert(ReflectUtils.getFieldValue(typeQualifiers2, "precision") === None) + assert(ReflectUtils.getFieldValue(typeQualifiers2, "scale") === None) assert(typeQualifiers1.toTTypeQualifiers .getQualifiers.get(TCLIServiceConstants.PRECISION).getI32Value === 10)