Skip to content

Commit

Permalink
Make test work again, mention filtering out extra fields
Browse files Browse the repository at this point in the history
  • Loading branch information
corneliusroemer committed Nov 19, 2024
1 parent 448b0eb commit 1d90b9e
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@ class ProcessedDataPostprocessor(
.let { processedMetadataPostprocessor.stripNullValuesFromMetadata(it) }

fun retrieveFromStoredValue(storedValue: ProcessedData<CompressedSequence>, organism: Organism) = storedValue
.let { processedMetadataPostprocessor.addMissingMetadataAsNull(it, organism) }
.let { processedMetadataPostprocessor.filterOutExtraFieldsAndAddNulls(it, organism) }
.let { compressionService.decompressSequencesInProcessedData(it, organism) }
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,12 @@ import org.loculus.backend.config.BackendConfig
import org.springframework.stereotype.Service

@Service
class ProcessedMetadataPostprocessor(
private val backendConfig: BackendConfig,
) {
class ProcessedMetadataPostprocessor(private val backendConfig: BackendConfig) {
fun <SequenceType> stripNullValuesFromMetadata(processedData: ProcessedData<SequenceType>) =
processedData.copy(metadata = processedData.metadata.filterNot { (_, value) -> value.isNull })

fun <SequenceType> addMissingMetadataAsNull(processedData: ProcessedData<SequenceType>, organism: Organism) =
/** Filter out any extra fields that are not in the current schema and add nulls for any missing fields. */
fun <SequenceType> filterOutExtraFieldsAndAddNulls(processedData: ProcessedData<SequenceType>, organism: Organism) =
processedData.copy(
metadata = backendConfig
.getInstanceConfig(organism)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,10 @@
package org.loculus.backend.service

import com.fasterxml.jackson.databind.node.NullNode
import com.fasterxml.jackson.databind.node.TextNode
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Assertions.assertFalse
import org.junit.jupiter.api.Assertions.assertTrue
import org.junit.jupiter.api.Test
import org.loculus.backend.SpringBootTestWithoutDatabase
import org.loculus.backend.api.Organism
import org.loculus.backend.api.OriginalData
import org.loculus.backend.api.ProcessedData
import org.loculus.backend.config.BackendConfig
import org.loculus.backend.service.submission.CompressionService
import org.springframework.beans.factory.annotation.Autowired
Expand All @@ -36,46 +31,4 @@ class CompressionServiceTest(

assertEquals(testData, decompressed)
}

@Test
fun `Metadata handling in compression and decompression of ProcessedData`() {
val organism = Organism(backendConfig.organisms.keys.first())
val configuredFields = backendConfig.getInstanceConfig(organism).schema.metadata.map { it.name }
require(configuredFields.size >= 2) { "Test requires at least 2 configured metadata fields" }

val configuredPresent = configuredFields[0]
val configuredNull = configuredFields[1]
val unconfiguredPresent = "unconfigured_present"
val unconfiguredNull = "unconfigured_null"

val testData = ProcessedData<String>(
metadata = mapOf(
configuredPresent to TextNode("value1"),
configuredNull to NullNode.instance,
unconfiguredPresent to TextNode("value2"),
unconfiguredNull to NullNode.instance,
),
unalignedNucleotideSequences = emptyMap(),
alignedNucleotideSequences = emptyMap(),
nucleotideInsertions = emptyMap(),
alignedAminoAcidSequences = emptyMap(),
aminoAcidInsertions = emptyMap(),
)

val compressed = compressor.compressProcessedData(testData, organism)
val decompressed = compressor.decompressProcessedData(compressed, organism)

// Check compression behavior
assertFalse(compressed.metadata.containsKey(configuredNull))
assertFalse(compressed.metadata.containsKey(unconfiguredNull))
assertTrue(compressed.metadata.containsKey(configuredPresent))
assertTrue(compressed.metadata.containsKey(unconfiguredPresent))
assertEquals(compressed.metadata[configuredPresent], testData.metadata[configuredPresent])

// Check decompression behavior
assertEquals(decompressed.metadata[configuredPresent], testData.metadata[configuredPresent])
assertEquals(decompressed.metadata[configuredNull], testData.metadata[configuredNull])
assertFalse(decompressed.metadata.containsKey(unconfiguredPresent))
assertFalse(decompressed.metadata.containsKey(unconfiguredNull))
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package org.loculus.backend.service

import com.fasterxml.jackson.databind.node.NullNode
import com.fasterxml.jackson.databind.node.TextNode
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Assertions.assertFalse
import org.junit.jupiter.api.Assertions.assertTrue
import org.junit.jupiter.api.Test
import org.loculus.backend.SpringBootTestWithoutDatabase
import org.loculus.backend.api.Organism
import org.loculus.backend.api.ProcessedData
import org.loculus.backend.config.BackendConfig
import org.loculus.backend.service.submission.ProcessedMetadataPostprocessor
import org.springframework.beans.factory.annotation.Autowired

@SpringBootTestWithoutDatabase
class ProcessedMetadataPostprocessorTest(
@Autowired private val processedMetadataPostprocessor: ProcessedMetadataPostprocessor,
@Autowired private val backendConfig: BackendConfig,
) {

@Test
fun `Processed Metadata Postprocessor correctly round trips metadata`() {
val organism = Organism(backendConfig.organisms.keys.first())
val configuredFields = backendConfig.getInstanceConfig(organism).schema.metadata.map { it.name }
require(configuredFields.size >= 2) { "Test requires at least 2 configured metadata fields" }

val configuredPresent = configuredFields[0]
val configuredNull = configuredFields[1]
val unconfiguredPresent = "unconfigured_present"
val unconfiguredNull = "unconfigured_null"

val testData = ProcessedData<String>(
metadata = mapOf(
configuredPresent to TextNode("value1"),
configuredNull to NullNode.instance,
unconfiguredPresent to TextNode("value2"),
unconfiguredNull to NullNode.instance,
),
unalignedNucleotideSequences = emptyMap(),
alignedNucleotideSequences = emptyMap(),
nucleotideInsertions = emptyMap(),
alignedAminoAcidSequences = emptyMap(),
aminoAcidInsertions = emptyMap(),
)

// "Compression" is only used in the sense that we are removing null values
val compressed = processedMetadataPostprocessor.stripNullValuesFromMetadata(testData)
val decompressed = processedMetadataPostprocessor.filterOutExtraFieldsAndAddNulls(compressed, organism)

// Check compression behavior
assertFalse(compressed.metadata.containsKey(configuredNull))
assertFalse(compressed.metadata.containsKey(unconfiguredNull))
assertTrue(compressed.metadata.containsKey(configuredPresent))
assertTrue(compressed.metadata.containsKey(unconfiguredPresent))
assertEquals(compressed.metadata[configuredPresent], testData.metadata[configuredPresent])

// Check decompression behavior
assertEquals(decompressed.metadata[configuredPresent], testData.metadata[configuredPresent])
assertEquals(decompressed.metadata[configuredNull], testData.metadata[configuredNull])
assertFalse(decompressed.metadata.containsKey(unconfiguredPresent))
assertFalse(decompressed.metadata.containsKey(unconfiguredNull))
}
}

0 comments on commit 1d90b9e

Please sign in to comment.