You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@hudi.apache.org by GitBox <gi...@apache.org> on 2020/07/29 07:25:39 UTC

[GitHub] [hudi] xushiyan commented on a change in pull request #1884: [HUDI-995] Use Transformations, Assertions and SchemaTestUtil

xushiyan commented on a change in pull request #1884:
URL: https://github.com/apache/hudi/pull/1884#discussion_r462017090



##########
File path: hudi-spark/src/test/java/org/apache/hudi/testutils/DataSourceTestUtils.java
##########
@@ -1,71 +0,0 @@
-/*
- * 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 org.apache.hudi.testutils;
-
-import org.apache.hudi.common.model.HoodieKey;
-import org.apache.hudi.common.model.HoodieRecord;
-import org.apache.hudi.common.model.HoodieRecordPayload;
-import org.apache.hudi.common.testutils.RawTripTestPayload;
-import org.apache.hudi.common.util.Option;
-import org.apache.hudi.table.UserDefinedBulkInsertPartitioner;
-
-import org.apache.spark.api.java.JavaRDD;
-
-import java.io.IOException;
-import java.util.List;
-import java.util.stream.Collectors;
-
-/**
- * Test utils for data source tests.
- */
-public class DataSourceTestUtils {

Review comment:
       all methods moved to `Transformations.java`

##########
File path: hudi-common/src/test/java/org/apache/hudi/common/testutils/Transformations.java
##########
@@ -0,0 +1,100 @@
+/*
+ * 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 org.apache.hudi.common.testutils;
+
+import org.apache.hudi.common.model.HoodieKey;
+import org.apache.hudi.common.model.HoodieRecord;
+import org.apache.hudi.common.util.Option;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Set;
+import java.util.stream.Collectors;
+
+/**
+ * Common transformations in test cases.
+ */
+public final class Transformations {
+
+  public static <T> List<T> flatten(Iterator<List<T>> iteratorOfLists) {
+    List<T> flattened = new ArrayList<>();
+    iteratorOfLists.forEachRemaining(flattened::addAll);
+    return flattened;
+  }
+
+  public static <T> Iterator<T> flattenAsIterator(Iterator<List<T>> iteratorOfLists) {
+    return flatten(iteratorOfLists).iterator();
+  }
+
+  public static Set<String> recordsToRecordKeySet(List<HoodieRecord> records) {
+    return records.stream().map(HoodieRecord::getRecordKey).collect(Collectors.toSet());
+  }
+
+  public static List<HoodieKey> recordsToHoodieKeys(List<HoodieRecord> records) {
+    return records.stream().map(HoodieRecord::getKey).collect(Collectors.toList());
+  }
+
+  public static List<String> hoodieKeysToStrings(List<HoodieKey> keys) {
+    return keys.stream()
+        .map(hr -> "{\"_row_key\":\"" + hr.getRecordKey() + "\",\"partition\":\"" + hr.getPartitionPath() + "\"}")
+        .collect(Collectors.toList());
+  }
+
+  public static List<String> recordsToStrings(List<HoodieRecord> records) {
+    return records.stream().map(Transformations::recordToString).filter(Option::isPresent).map(Option::get)
+        .collect(Collectors.toList());
+  }
+
+  public static Option<String> recordToString(HoodieRecord record) {
+    try {
+      String str = ((RawTripTestPayload) record.getData()).getJsonData();
+      str = "{" + str.substring(str.indexOf("\"timestamp\":"));
+      // Remove the last } bracket
+      str = str.substring(0, str.length() - 1);
+      return Option.of(str + ", \"partition\": \"" + record.getPartitionPath() + "\"}");
+    } catch (IOException e) {
+      return Option.empty();
+    }
+  }
+
+  /**
+   * Pseudorandom: select even indices first, then select odd ones.
+   */
+  public static <T> List<T> randomSelect(List<T> items, int n) {

Review comment:
       IMO not needing real randomness for caller's scenario

##########
File path: hudi-common/src/test/java/org/apache/hudi/common/table/timeline/TestHoodieActiveTimeline.java
##########
@@ -94,17 +94,19 @@ public void testLoadingInstantsFromFiles() throws IOException {
     timeline = timeline.reload();
 
     assertEquals(5, timeline.countInstants(), "Total instants should be 5");
-    HoodieTestUtils.assertStreamEquals("Check the instants stream",
+    assertStreamEquals(

Review comment:
       moving message arg to the last, align with junit API style




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org