|
| 1 | +package io.opentargets.etl.backend.evidence |
| 2 | + |
| 3 | + |
| 4 | + |
| 5 | +import io.opentargets.etl.backend.{Configuration, ETLSessionContext} |
| 6 | +import io.opentargets.etl.backend.Configuration.OTConfig |
| 7 | +import io.opentargets.etl.backend.spark.{IOResource, IoHelpers} |
| 8 | +import org.apache.spark.sql.{DataFrame, Row, SparkSession} |
| 9 | +import org.apache.spark.sql.functions._ |
| 10 | +import org.apache.spark.sql.types._ |
| 11 | +import org.scalatest.flatspec.AnyFlatSpec |
| 12 | +import org.scalatest.matchers.should.Matchers |
| 13 | + |
| 14 | +class EvidenceDatingTest extends AnyFlatSpec with Matchers { |
| 15 | + |
| 16 | + implicit val sparkSession: SparkSession = SparkSession |
| 17 | + .builder() |
| 18 | + .appName("EvidenceTest") |
| 19 | + .master("local[*]") |
| 20 | + .config("spark.sql.adaptive.enabled", "false") |
| 21 | + .config("spark.sql.adaptive.coalescePartitions.enabled", "false") |
| 22 | + .getOrCreate() |
| 23 | + |
| 24 | + // Mock ETLSessionContext for testing |
| 25 | + implicit val mockContext: ETLSessionContext = ETLSessionContext( |
| 26 | + configuration = null.asInstanceOf[OTConfig], // We'll mock this as needed for specific tests |
| 27 | + sparkSession = sparkSession |
| 28 | + ) |
| 29 | + |
| 30 | + // Shared test data available to all tests |
| 31 | + val evidenceSchema = StructType(Array( |
| 32 | + StructField("id", StringType, nullable = false), |
| 33 | + StructField("releaseDate", StringType, nullable = true), |
| 34 | + StructField("literature", ArrayType(StringType), nullable = true) |
| 35 | + )) |
| 36 | + |
| 37 | + val testEvidenceData = sparkSession.createDataFrame( |
| 38 | + sparkSession.sparkContext.parallelize( |
| 39 | + Seq( |
| 40 | + Row("e1", null, Array.empty[String]), // No dates, empty array instead of null |
| 41 | + Row("e2", "2021-02-03", Array.empty[String]), // Release date is given, empty array |
| 42 | + Row("e3", "2021-02-03", Array("123", "PMC456")), // Both release date and literature is given |
| 43 | + Row("e4", null, Array("123", "PMC456")), // Only literature is given |
| 44 | + Row("e5", null, Array("PMC456")) // Only literature but only one source. |
| 45 | + ) |
| 46 | + ), |
| 47 | + evidenceSchema |
| 48 | + ) |
| 49 | + |
| 50 | + val literatureMapSchema = StructType( |
| 51 | + Array( |
| 52 | + StructField("source", StringType, nullable = false), |
| 53 | + StructField("firstPublicationDate", StringType, nullable = true), |
| 54 | + StructField("pmid", StringType, nullable = true), |
| 55 | + StructField("id", StringType, nullable = true), |
| 56 | + StructField("pmcid", StringType, nullable = true) |
| 57 | + ) |
| 58 | + ) |
| 59 | + |
| 60 | + val testPublicationData = sparkSession.createDataFrame( |
| 61 | + sparkSession.sparkContext.parallelize( |
| 62 | + Seq( |
| 63 | + Row("MED", "2021-06-15", "123", "123", "PMC9936"), |
| 64 | + Row("MED", "2021-08-15", null, "PMC456", "PMC456"), |
| 65 | + Row("AGR", "2021-07-30", "AGR001", "AGR001", null) |
| 66 | + ) |
| 67 | + ), |
| 68 | + literatureMapSchema |
| 69 | + ) |
| 70 | + |
| 71 | + // Apply the function using shared test data |
| 72 | + val result = Evidence.resolvePublicationDates(testEvidenceData, testPublicationData) |
| 73 | + |
| 74 | + "resolvePublicationDates" should "return dataframe" in { |
| 75 | + |
| 76 | + // Compile-time type assertion |
| 77 | + implicitly[result.type <:< DataFrame] |
| 78 | + |
| 79 | + } |
| 80 | + |
| 81 | + it should "return all evidence" in { |
| 82 | + // Test that DataFrame is created successfully |
| 83 | + result.count() should be(5) |
| 84 | + |
| 85 | + // Should have all expected columns |
| 86 | + val expectedColumns = testEvidenceData.columns |
| 87 | + result.columns should contain allElementsOf(expectedColumns) |
| 88 | + |
| 89 | + } |
| 90 | + |
| 91 | + it should "have new `publicationDate` column with the right type" in { |
| 92 | + // Test for new column: |
| 93 | + result.columns should contain("publicationDate") |
| 94 | + |
| 95 | + // Test column schema: |
| 96 | + result.schema("publicationDate").dataType should be(StringType) |
| 97 | + result.schema("publicationDate").nullable should be(true) |
| 98 | + } |
| 99 | + |
| 100 | + it should "have new `evidenceDate` column with the right type" in { |
| 101 | + // Test for new column: |
| 102 | + result.columns should contain("evidenceDate") |
| 103 | + |
| 104 | + // Test column schema: |
| 105 | + result.schema("evidenceDate").dataType should be(StringType) |
| 106 | + result.schema("evidenceDate").nullable should be(true) |
| 107 | + } |
| 108 | + |
| 109 | + it should "correctly resolve publication dates for specific evidence" in { |
| 110 | + // Test specific evidence records |
| 111 | + |
| 112 | + // e1: No literature, no releaseDate - publicationDate should be null, evidenceDate should be null |
| 113 | + val evidence1 = result.filter(col("id") === "e1").collect().head |
| 114 | + evidence1.getString(evidence1.fieldIndex("publicationDate")) should be(null) |
| 115 | + evidence1.getString(evidence1.fieldIndex("evidenceDate")) should be(null) |
| 116 | + |
| 117 | + // e2: No literature, has releaseDate - publicationDate should be null, evidenceDate should be releaseDate |
| 118 | + val evidence2 = result.filter(col("id") === "e2").collect().head |
| 119 | + evidence2.getString(evidence2.fieldIndex("publicationDate")) should be(null) |
| 120 | + evidence2.getString(evidence2.fieldIndex("evidenceDate")) should be("2021-02-03") |
| 121 | + |
| 122 | + // e3: Has literature that can be resolved - publicationDate should be from literature, evidenceDate should prioritize publication date |
| 123 | + val evidence3 = result.filter(col("id") === "e3").collect().head |
| 124 | + evidence3.getString(evidence3.fieldIndex("publicationDate")) should be("2021-06-15") |
| 125 | + evidence3.getString(evidence3.fieldIndex("evidenceDate")) should be("2021-06-15") |
| 126 | + |
| 127 | + // e4: Has literature that can be resolved - publicationDate should be from literature, evidenceDate should prioritize publication date |
| 128 | + val evidence4 = result.filter(col("id") === "e4").collect().head |
| 129 | + evidence4.getString(evidence4.fieldIndex("publicationDate")) should be("2021-06-15") |
| 130 | + evidence4.getString(evidence4.fieldIndex("evidenceDate")) should be("2021-06-15") |
| 131 | + |
| 132 | + // e5: Has literature that can be resolved - publicationDate should be from literature, which is an older date |
| 133 | + val evidence5 = result.filter(col("id") === "e5").collect().head |
| 134 | + evidence5.getString(evidence5.fieldIndex("publicationDate")) should be("2021-08-15") |
| 135 | + evidence5.getString(evidence5.fieldIndex("evidenceDate")) should be("2021-08-15") |
| 136 | + |
| 137 | + } |
| 138 | +} |
0 commit comments