Skip to content

Commit

Permalink
Add method to compare DocResult from mongo and polypheny
Browse files Browse the repository at this point in the history
  • Loading branch information
murermader committed Aug 27, 2024
1 parent 0f203b1 commit 5acba0b
Showing 1 changed file with 43 additions and 2 deletions.
45 changes: 43 additions & 2 deletions dbms/src/test/java/org/polypheny/db/mql/MqlGeoFunctionsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@

package org.polypheny.db.mql;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
Expand All @@ -29,6 +32,8 @@
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Map;
import java.util.Objects;

import static org.junit.jupiter.api.Assertions.assertEquals;

Expand Down Expand Up @@ -74,8 +79,11 @@ public void beforeEach() {

@Test
public void docGeoIntersectsTest() {
// TODO: compare the results directly, instead of only validating the length.
ArrayList<DocResult> results = new ArrayList<>();

for (String ns : namespaces) {
// TODO: We are using MongoDB twice...
initDatabase(ns);
String insertDocuments = """
db.%s.insertMany([
{
Expand Down Expand Up @@ -111,7 +119,40 @@ public void docGeoIntersectsTest() {
})
""".formatted( ns );
result = execute( geoIntersects, ns );
assertEquals( result.data.length, 2 );
results.add( result );
}

compareResults(results.get(0), results.get(1));
}

public static void compareResults(DocResult mongoResult, DocResult result){
assertEquals( mongoResult.data.length, result.data.length );

ObjectMapper objectMapper = new ObjectMapper();
for ( int i = 0; i < result.data.length; i++ ) {
String document = result.data[i];
String mongoDocument = mongoResult.data[i];

Map<String, Object> documentMap;
Map<String, Object> mongoDocumentMap;
try {
documentMap = objectMapper.readValue(document, new TypeReference<Map<String, Object>>() {});
mongoDocumentMap = objectMapper.readValue(mongoDocument, new TypeReference<Map<String, Object>>() {});
} catch ( JsonProcessingException e ) {
throw new RuntimeException( e );
}
assertEquals( mongoDocumentMap.keySet(), documentMap.keySet() );

for (Map.Entry<String, Object> entry : documentMap.entrySet()) {
String key = entry.getKey();
if( Objects.equals( key, "_id" ) ){
// Do not compare the _id, as this will be different.
continue;
}
Object value = entry.getValue();
Object mongoValue = entry.getValue();
assertEquals( mongoValue, value );
}
}
}

Expand Down

0 comments on commit 5acba0b

Please sign in to comment.