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 337924e
Showing 1 changed file with 85 additions and 39 deletions.
124 changes: 85 additions & 39 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 All @@ -42,12 +47,12 @@ public class MqlGeoFunctionsTest extends MqlTestTemplate {
final static String mongoAdapterName = "mongo";
final static ArrayList<String> namespaces = new ArrayList<>();


@BeforeAll
public static void init() {
createMongoDbAdapter();
namespaces.add(namespace);
namespaces.add(namespaceMongo);
log.info( "Created Mongo adapter successfully." );
namespaces.add( namespace );
namespaces.add( namespaceMongo );
}


Expand All @@ -67,54 +72,95 @@ public static void createMongoDbAdapter() {
@BeforeEach
public void beforeEach() {
// Clear both collections before each test
for (String ns : namespaces) {
execute("db.%s.deleteMany({})".formatted( ns ), ns);
for ( String ns : namespaces ) {
execute( "db.%s.deleteMany({})".formatted( ns ), ns );
}
}


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

for ( String ns : namespaces ) {
// TODO: We are using MongoDB twice...
initDatabase( ns );
String insertDocuments = """
db.%s.insertMany([
{
name: "Legacy [0,0]",
num: 1,
legacy: [0,0]
},
{
name: "Legacy [1,1]",
num: 2,
legacy: [1,1]
},
{
name: "Legacy [2,2]",
num: 3,
legacy: [2,2]
}
])
""".formatted( ns );
execute( insertDocuments, ns );
db.%s.insertMany([
{
name: "Legacy [0,0]",
num: 1,
legacy: [0,0]
},
{
name: "Legacy [1,1]",
num: 2,
legacy: [1,1]
},
{
name: "Legacy [2,2]",
num: 3,
legacy: [2,2]
}
])
""".formatted( ns );
execute( insertDocuments, ns );

DocResult result;
String geoIntersects = """
db.%s.find({
legacy: {
$geoIntersects: {
$geometry: {
type: "Polygon",
coordinates: [[ [0,0], [0,1], [1,1], [1,0], [0,0] ]]
}
}
}
})
""".formatted( ns );
result = execute( geoIntersects, ns );
assertEquals( result.data.length, 2 );
db.%s.find({
legacy: {
$geoIntersects: {
$geometry: {
type: "Polygon",
coordinates: [[ [0,0], [0,1], [1,1], [1,0], [0,0] ]]
}
}
}
})
""".formatted( ns );
result = execute( geoIntersects, ns );
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 );
}
}
}


@Test
public void docGeoWithinTest() {
String insertDocuments = """
Expand Down

0 comments on commit 337924e

Please sign in to comment.