From dd6b8b4a2849c366462ce0920389b8e6783da51d Mon Sep 17 00:00:00 2001 From: Andrzej Zadrozny Date: Wed, 4 Mar 2020 20:05:07 +0100 Subject: [PATCH 1/3] update dependecies, allowDiskUsage in aggregate --- pom.xml | 10 +++++++--- .../scala/com/ee/dynamicmongoquery/MongoQuery.scala | 4 ++-- .../com/ee/dynamicmongoquery/PlaceholderLookup.scala | 2 +- .../ee/dynamicmongoquery/PlaceholderLookupTest.scala | 6 +++--- 4 files changed, 13 insertions(+), 9 deletions(-) diff --git a/pom.xml b/pom.xml index 931f4c7..47138c9 100644 --- a/pom.xml +++ b/pom.xml @@ -39,14 +39,15 @@ org.mongodb mongo-java-driver - 2.10.1 + 3.2.2 + org.mongodb - casbah-core_2.10 - 2.6.1 + casbah-core_2.12 + 3.1.1 @@ -58,6 +59,7 @@ net.alchim31.maven scala-maven-plugin + 4.3.1 org.apache.maven.plugins @@ -66,6 +68,7 @@ org.apache.maven.plugins maven-source-plugin + 3.2.1 org.apache.maven.plugins @@ -84,6 +87,7 @@ org.scala-tools maven-scala-plugin + 2.15.2 scala-compile-first diff --git a/src/main/scala/com/ee/dynamicmongoquery/MongoQuery.scala b/src/main/scala/com/ee/dynamicmongoquery/MongoQuery.scala index 617582f..1dd82d6 100644 --- a/src/main/scala/com/ee/dynamicmongoquery/MongoQuery.scala +++ b/src/main/scala/com/ee/dynamicmongoquery/MongoQuery.scala @@ -50,8 +50,8 @@ class MongoQuery(val queryTypes: List[Query], val collectionName: String) { protected def executeAggregate(aggregationQuery: AggregateQuery, collection: DBCollection): Array[DBObject] = { val aggregationPipeline = aggregationQuery.aggregationPipeline - var aggregateOP = collection.aggregate(aggregationPipeline.head, aggregationPipeline.tail: _*) - aggregateOP.results().toTraversable.toArray + var cursor = collection.aggregate(aggregationPipeline, AggregationOptions.builder().allowDiskUse(true).build()) + cursor.toArray } diff --git a/src/main/scala/com/ee/dynamicmongoquery/PlaceholderLookup.scala b/src/main/scala/com/ee/dynamicmongoquery/PlaceholderLookup.scala index 29b061e..a18a284 100644 --- a/src/main/scala/com/ee/dynamicmongoquery/PlaceholderLookup.scala +++ b/src/main/scala/com/ee/dynamicmongoquery/PlaceholderLookup.scala @@ -14,7 +14,7 @@ class PlaceholderLookup(params: Map[String, String]) { } case s: String => { val resolvedValue = placeholderResolver.resolve(s) - basicDBObject.put(key, resolvedValue) + basicDBObject.put(key, resolvedValue.asInstanceOf[AnyRef]) } case _ => { basicDBObject.put(key, value) diff --git a/src/test/scala/com/ee/dynamicmongoquery/PlaceholderLookupTest.scala b/src/test/scala/com/ee/dynamicmongoquery/PlaceholderLookupTest.scala index e251e22..061ff10 100644 --- a/src/test/scala/com/ee/dynamicmongoquery/PlaceholderLookupTest.scala +++ b/src/test/scala/com/ee/dynamicmongoquery/PlaceholderLookupTest.scala @@ -13,7 +13,7 @@ class PlaceholderLookupTest extends Specification { val lookup: PlaceholderLookup = new PlaceholderLookup(params) val address = new BasicDBObject() - address.put("lane", 5) + address.put("lane", 5:Integer) address.put("city", "Pune") address.put("country", "country") address.put("zipcode", "zip#Integer") @@ -23,10 +23,10 @@ class PlaceholderLookupTest extends Specification { person.put("dontreplace", "dontreplace#Long") val expectedAddress = new BasicDBObject() - expectedAddress.put("lane", 5) + expectedAddress.put("lane", 5:Integer) expectedAddress.put("city", "Pune") expectedAddress.put("country", "India") - expectedAddress.put("zipcode", 411037) + expectedAddress.put("zipcode", 411037:Integer) val expectedPerson = new BasicDBObject() expectedPerson.put("name", "leena") expectedPerson.put("address", address) From fde1812573017dd9087ad7914b75050c89567b8d Mon Sep 17 00:00:00 2001 From: Andrzej Zadrozny Date: Tue, 10 Mar 2020 10:42:48 +0100 Subject: [PATCH 2/3] partial tests fix --- pom.xml | 43 ++---- .../com/ee/dynamicmongoquery/MongoQuery.scala | 2 +- .../dynamicmongoquery/IntegrationTest.scala | 16 +- .../MongoQueryIntegrationTest.scala | 146 ------------------ 4 files changed, 23 insertions(+), 184 deletions(-) diff --git a/pom.xml b/pom.xml index 47138c9..029155b 100644 --- a/pom.xml +++ b/pom.xml @@ -9,19 +9,24 @@ 1.0 - 2.10.2 + 2.12.0 4.11 org.specs2 - specs2_2.10 - 2.0 + specs2-core_2.12 + 3.8.6 test - + + org.specs2 + specs2-junit_2.12 + 3.8.6 + test + junit junit @@ -32,14 +37,14 @@ org.scala-lang scala-compiler - 2.10.2 + 2.12.0 org.mongodb mongo-java-driver - 3.2.2 + 3.12.1 @@ -50,6 +55,7 @@ 3.1.1 + ${project.artifactId} @@ -64,6 +70,7 @@ org.apache.maven.plugins maven-compiler-plugin + 3.8.1 org.apache.maven.plugins @@ -73,7 +80,7 @@ org.apache.maven.plugins maven-surefire-plugin - 2.15 + 2.22.2 **/*Suite.class @@ -84,28 +91,6 @@ - - org.scala-tools - maven-scala-plugin - 2.15.2 - - - scala-compile-first - process-resources - - add-source - compile - - - - scala-test-compile - process-test-resources - - testCompile - - - - diff --git a/src/main/scala/com/ee/dynamicmongoquery/MongoQuery.scala b/src/main/scala/com/ee/dynamicmongoquery/MongoQuery.scala index 1dd82d6..04fb425 100644 --- a/src/main/scala/com/ee/dynamicmongoquery/MongoQuery.scala +++ b/src/main/scala/com/ee/dynamicmongoquery/MongoQuery.scala @@ -50,7 +50,7 @@ class MongoQuery(val queryTypes: List[Query], val collectionName: String) { protected def executeAggregate(aggregationQuery: AggregateQuery, collection: DBCollection): Array[DBObject] = { val aggregationPipeline = aggregationQuery.aggregationPipeline - var cursor = collection.aggregate(aggregationPipeline, AggregationOptions.builder().allowDiskUse(true).build()) + var cursor = collection.aggregate(aggregationPipeline, AggregationOptions.builder().batchSize(100).allowDiskUse(true).build()) cursor.toArray } diff --git a/src/test/scala/com/ee/dynamicmongoquery/IntegrationTest.scala b/src/test/scala/com/ee/dynamicmongoquery/IntegrationTest.scala index e7b7347..20d164f 100644 --- a/src/test/scala/com/ee/dynamicmongoquery/IntegrationTest.scala +++ b/src/test/scala/com/ee/dynamicmongoquery/IntegrationTest.scala @@ -1,21 +1,22 @@ package com.ee.dynamicmongoquery -import com.mongodb.casbah.{MongoConnection, MongoCollection} +import com.mongodb.casbah.{Imports, MongoClient, MongoCollection, MongoConnection} import com.mongodb.util.JSON -import com.mongodb.{BasicDBList, BasicDBObject} +import com.mongodb.{BasicDBList, BasicDBObject, DBCollection} import org.specs2.mutable.{BeforeAfter, Specification} import org.specs2.matcher.MatchResult +import org.specs2.specification.BeforeAfterEach -class IntegrationTest extends Specification { +class IntegrationTest extends Specification with BeforeAfterEach { - lazy val mongoConn = MongoConnection() - lazy val mongoDB = mongoConn("dynamic-query-test-db") - lazy val userCollection: MongoCollection = mongoDB("users") + lazy val mongoConn = MongoClient() + lazy val mongoDB = mongoConn.getDB("dynamic-query-test-db") + lazy val userCollection: DBCollection = mongoDB.getCollection("users") lazy val parser: MongoQueryParser = new MongoQueryParser() + sequential - implicit val myContext = new BeforeAfter { def clearCollections { userCollection.drop() @@ -34,7 +35,6 @@ class IntegrationTest extends Specification { def after = { clearCollections } - } def readJsonFile(config: String): String = { diff --git a/src/test/scala/com/ee/dynamicmongoquery/MongoQueryIntegrationTest.scala b/src/test/scala/com/ee/dynamicmongoquery/MongoQueryIntegrationTest.scala index 2a050c8..f58bd8a 100644 --- a/src/test/scala/com/ee/dynamicmongoquery/MongoQueryIntegrationTest.scala +++ b/src/test/scala/com/ee/dynamicmongoquery/MongoQueryIntegrationTest.scala @@ -6,152 +6,6 @@ import org.specs2.runner.JUnitRunner @RunWith(classOf[JUnitRunner]) class MongoQueryIntegrationTest extends IntegrationTest { - "find queries" >> { - - "simple find query with no criteria and no projection should return matching results" >> { - //Given - val query: String = getMongoQuery("find-wo-criteria") - - //When - val mongoQuery: MongoQuery = parser.parse(query, noParameters()) - val results = mongoQuery.execute(mongoDB) - - //Then - assertResultSize(results, 6) - } - - "find query with criteria and projection should return matching results" >> { - - //Given - val query: String = getMongoQuery("find-with-criteria-projection") - - //When - val mongoQuery: MongoQuery = parser.parse(query, noParameters()) - val results = mongoQuery.execute(mongoDB) - - //Then - assertResultSize(results, 4) - assertNameForFirstDocument(results, "ina") - - assertKeySetSizeForFirstDocument(results, 2) - assertKeyNameForFirstDocument(results, "name") - } - - "parametarized find query should return matching results" >> { - //Given - val query: String = getMongoQuery("find-parametarized") - val params = parameters("salary" -> "1000", "name" -> "leena", "consultant" -> "true", "wt" -> "30.5") - - //When - val mongoQuery: MongoQuery = parser.parse(query, params) - val results = mongoQuery.execute(mongoDB) - - //Then - assertResultSize(results, 1) - assertNameForFirstDocument(results, "leena") - - } - - - "find query with nested criteria property should return matching results" >> { - //Given - val query: String = getMongoQuery("nested-query") - - //When - val mongoQuery: MongoQuery = parser.parse(query, noParameters()) - val results = mongoQuery.execute(mongoDB) - - //Then - assertResultSize(results, 1) - assertNameForFirstDocument(results, "leena") - } - - - "find query with multiple sort and limit should return matching results" >> { - //Given - val query: String = getMongoQuery("sort-limit-chaining") - - //When - val mongoQuery: MongoQuery = parser.parse(query, noParameters()) - val results = mongoQuery.execute(mongoDB) - - //Then - assertResultSize(results, 2) - - assertNameForFirstDocument(results, "ina") - assertNameForSecondDocument(results, "rian") - } - - "find query with multiple sort and skip chaining should return matching results" >> { - //Given - val query: String = getMongoQuery("sort-skip-chaining") - - //When - val mongoQuery: MongoQuery = parser.parse(query, noParameters()) - val results = mongoQuery.execute(mongoDB) - - //Then - assertResultSize(results, 2) - assertNameForFirstDocument(results, "rina") - assertNameForSecondDocument(results, "vina") - } - - "find query without criteria and with skip and limit chaining should return matching results" >> { - //Given - val query: String = getMongoQuery("find-wo-criteria-skip-limit") - - //When - val mongoQuery: MongoQuery = parser.parse(query, noParameters()) - val results = mongoQuery.execute(mongoDB) - - //Then - assertResultSize(results, 1) - assertNameForFirstDocument(results, "tina") - } - - "find query with parametarized limit query should return matching results" >> { - //Given - val query: String = getMongoQuery("limit-parametarized") - val params = parameters("skip" -> "2", "limit" -> "1") - - //When - val mongoQuery: MongoQuery = parser.parse(query, params) - val results = mongoQuery.execute(mongoDB) - - //Then - assertResultSize(results, 1) - assertNameForFirstDocument(results, "tina") - } - - "find query with no-results should not return any results" >> { - //Given - val query: String = getMongoQuery("no-result-found") - - //When - val mongoQuery: MongoQuery = parser.parse(query, noParameters()) - val results = mongoQuery.execute(mongoDB) - - //Then - assertResultSize(results, 0) - } - - "unformatted find query should return matching results" >> { - //Given - val query: String = getMongoQuery("unformatted-find-query") - - //When - val mongoQuery: MongoQuery = parser.parse(query, noParameters()) - val results = mongoQuery.execute(mongoDB) - - //Then - assertResultSize(results, 2) - assertNameForFirstDocument(results, "rina") - assertNameForSecondDocument(results, "vina") - - } - - } - "aggregate queries" >> { "aggregate query specified in array format (using $match,$lte,$gthe,$group,$sum,$sort and $limit operator) should return matching results" >> { From ba3064eac7a3c80437657e9cd25eb933921c427d Mon Sep 17 00:00:00 2001 From: Andrzej Zadrozny Date: Thu, 12 Mar 2020 09:56:13 +0100 Subject: [PATCH 3/3] fix test --- .../com/ee/dynamicmongoquery/MongoQuery.scala | 6 + .../MongoQueryIntegrationTest.scala | 146 ++++++++++++++++++ .../MongoQueryParserTest.scala | 6 +- .../com/ee/dynamicmongoquery/QueryTest.scala | 21 +-- 4 files changed, 160 insertions(+), 19 deletions(-) diff --git a/src/main/scala/com/ee/dynamicmongoquery/MongoQuery.scala b/src/main/scala/com/ee/dynamicmongoquery/MongoQuery.scala index 04fb425..ad4a8d1 100644 --- a/src/main/scala/com/ee/dynamicmongoquery/MongoQuery.scala +++ b/src/main/scala/com/ee/dynamicmongoquery/MongoQuery.scala @@ -78,5 +78,11 @@ class MongoQuery(val queryTypes: List[Query], val collectionName: String) { case _ => false } } + // Overriding toString method + override def toString() : String = { + + return "[MongoQuery : " + this.collectionName + + ", Types: = " + this.queryTypes+"]"; + } } diff --git a/src/test/scala/com/ee/dynamicmongoquery/MongoQueryIntegrationTest.scala b/src/test/scala/com/ee/dynamicmongoquery/MongoQueryIntegrationTest.scala index f58bd8a..2a050c8 100644 --- a/src/test/scala/com/ee/dynamicmongoquery/MongoQueryIntegrationTest.scala +++ b/src/test/scala/com/ee/dynamicmongoquery/MongoQueryIntegrationTest.scala @@ -6,6 +6,152 @@ import org.specs2.runner.JUnitRunner @RunWith(classOf[JUnitRunner]) class MongoQueryIntegrationTest extends IntegrationTest { + "find queries" >> { + + "simple find query with no criteria and no projection should return matching results" >> { + //Given + val query: String = getMongoQuery("find-wo-criteria") + + //When + val mongoQuery: MongoQuery = parser.parse(query, noParameters()) + val results = mongoQuery.execute(mongoDB) + + //Then + assertResultSize(results, 6) + } + + "find query with criteria and projection should return matching results" >> { + + //Given + val query: String = getMongoQuery("find-with-criteria-projection") + + //When + val mongoQuery: MongoQuery = parser.parse(query, noParameters()) + val results = mongoQuery.execute(mongoDB) + + //Then + assertResultSize(results, 4) + assertNameForFirstDocument(results, "ina") + + assertKeySetSizeForFirstDocument(results, 2) + assertKeyNameForFirstDocument(results, "name") + } + + "parametarized find query should return matching results" >> { + //Given + val query: String = getMongoQuery("find-parametarized") + val params = parameters("salary" -> "1000", "name" -> "leena", "consultant" -> "true", "wt" -> "30.5") + + //When + val mongoQuery: MongoQuery = parser.parse(query, params) + val results = mongoQuery.execute(mongoDB) + + //Then + assertResultSize(results, 1) + assertNameForFirstDocument(results, "leena") + + } + + + "find query with nested criteria property should return matching results" >> { + //Given + val query: String = getMongoQuery("nested-query") + + //When + val mongoQuery: MongoQuery = parser.parse(query, noParameters()) + val results = mongoQuery.execute(mongoDB) + + //Then + assertResultSize(results, 1) + assertNameForFirstDocument(results, "leena") + } + + + "find query with multiple sort and limit should return matching results" >> { + //Given + val query: String = getMongoQuery("sort-limit-chaining") + + //When + val mongoQuery: MongoQuery = parser.parse(query, noParameters()) + val results = mongoQuery.execute(mongoDB) + + //Then + assertResultSize(results, 2) + + assertNameForFirstDocument(results, "ina") + assertNameForSecondDocument(results, "rian") + } + + "find query with multiple sort and skip chaining should return matching results" >> { + //Given + val query: String = getMongoQuery("sort-skip-chaining") + + //When + val mongoQuery: MongoQuery = parser.parse(query, noParameters()) + val results = mongoQuery.execute(mongoDB) + + //Then + assertResultSize(results, 2) + assertNameForFirstDocument(results, "rina") + assertNameForSecondDocument(results, "vina") + } + + "find query without criteria and with skip and limit chaining should return matching results" >> { + //Given + val query: String = getMongoQuery("find-wo-criteria-skip-limit") + + //When + val mongoQuery: MongoQuery = parser.parse(query, noParameters()) + val results = mongoQuery.execute(mongoDB) + + //Then + assertResultSize(results, 1) + assertNameForFirstDocument(results, "tina") + } + + "find query with parametarized limit query should return matching results" >> { + //Given + val query: String = getMongoQuery("limit-parametarized") + val params = parameters("skip" -> "2", "limit" -> "1") + + //When + val mongoQuery: MongoQuery = parser.parse(query, params) + val results = mongoQuery.execute(mongoDB) + + //Then + assertResultSize(results, 1) + assertNameForFirstDocument(results, "tina") + } + + "find query with no-results should not return any results" >> { + //Given + val query: String = getMongoQuery("no-result-found") + + //When + val mongoQuery: MongoQuery = parser.parse(query, noParameters()) + val results = mongoQuery.execute(mongoDB) + + //Then + assertResultSize(results, 0) + } + + "unformatted find query should return matching results" >> { + //Given + val query: String = getMongoQuery("unformatted-find-query") + + //When + val mongoQuery: MongoQuery = parser.parse(query, noParameters()) + val results = mongoQuery.execute(mongoDB) + + //Then + assertResultSize(results, 2) + assertNameForFirstDocument(results, "rina") + assertNameForSecondDocument(results, "vina") + + } + + } + "aggregate queries" >> { "aggregate query specified in array format (using $match,$lte,$gthe,$group,$sum,$sort and $limit operator) should return matching results" >> { diff --git a/src/test/scala/com/ee/dynamicmongoquery/MongoQueryParserTest.scala b/src/test/scala/com/ee/dynamicmongoquery/MongoQueryParserTest.scala index a1aafe9..f604da8 100644 --- a/src/test/scala/com/ee/dynamicmongoquery/MongoQueryParserTest.scala +++ b/src/test/scala/com/ee/dynamicmongoquery/MongoQueryParserTest.scala @@ -58,7 +58,7 @@ class MongoQueryParserTest extends Specification { "find query with params and datatypes" in { //Given - val query: String = "db.users.find({'name':'name#String', 'rank' : 'givenRank#Integer' , 'employed':'isEmployed#Boolean', 'salary':'sal#Long','weight' :'weight#Double'})" + val query: String = "db.users.find({'name':'name#String', 'rank' : 'givenRank#Integer' , 'employed':'isEmployed#Boolean', 'salary':'sal#Integer','weight' :'weight#Double'})" val params: Map[String, String] = Map("name" -> "leena", "givenRank" -> "5", "isEmployed" -> "true", "sal" -> "1000", "weight" -> "2.5") val lookup: PlaceholderLookup = new PlaceholderLookup(params) @@ -151,7 +151,7 @@ class MongoQueryParserTest extends Specification { "aggregation query " in { //Given - val query: String = "db.users.aggregate([{'$match':{'salary' : {$gte:'from#Long',$lte:'to#Long'}}}, { $group: {_id:'$role', 'role': {$sum: '$age' } } }, { '$sort': { 'age': -1 } }, { '$limit': 5 }])" + val query: String = "db.users.aggregate([{'$match':{'salary' : {$gte:'from#Integer',$lte:'to#Integer'}}}, { $group: {_id:'$role', 'role': {$sum: '$age' } } }, { '$sort': { 'age': -1 } }, { '$limit': 5 }])" val params: Map[String, String] = Map("from"->"1000","to" -> "2000") val lookup: PlaceholderLookup = new PlaceholderLookup(params) @@ -166,7 +166,7 @@ class MongoQueryParserTest extends Specification { "aggregation query without array " in { //Given - val query: String = "db.users.aggregate( { '$match':{'salary' : {$gte:'from#Long',$lte:'to#Long'}}}, { $group: {_id:'$role', 'role': {$sum: '$age' } } }, { '$sort': { 'age': -1 } }, { '$limit': 5 } )" + val query: String = "db.users.aggregate( { '$match':{'salary' : {$gte:'from#Integer',$lte:'to#Integer'}}}, { $group: {_id:'$role', 'role': {$sum: '$age' } } }, { '$sort': { 'age': -1 } }, { '$limit': 5 } )" val params: Map[String, String] = Map("from"->"1000","to" -> "2000") val lookup: PlaceholderLookup = new PlaceholderLookup(params) diff --git a/src/test/scala/com/ee/dynamicmongoquery/QueryTest.scala b/src/test/scala/com/ee/dynamicmongoquery/QueryTest.scala index ffe36c8..e8416fd 100644 --- a/src/test/scala/com/ee/dynamicmongoquery/QueryTest.scala +++ b/src/test/scala/com/ee/dynamicmongoquery/QueryTest.scala @@ -2,10 +2,14 @@ package com.ee.dynamicmongoquery import org.specs2.mutable.Specification import com.mongodb.BasicDBObject + import collection.JavaConversions._ import org.junit.runner.RunWith import org.specs2.runner.JUnitRunner +import scala.collection.immutable.SortedMap +import scala.collection.mutable + @RunWith(classOf[JUnitRunner]) class QueryTest extends Specification { "Find Queries" should { @@ -62,22 +66,7 @@ class QueryTest extends Specification { val findQuery = FindQuery(query, lookup) //Then - val criteriaMap = Map("name" -> "leena", "rank" -> 5, "employed" -> true, "salary" -> 1000, "weight" -> 2.5) - findQuery.criteria === new BasicDBObject(criteriaMap) - findQuery.projection === new BasicDBObject() - } - - "find query with params and datatypes" in { - //Given - val query: String = "{'name':'name#String', 'rank' : 'givenRank#Integer' , 'employed':'isEmployed#Boolean', 'salary':'sal#Long','weight' :'weight#Double'}" - val params: Map[String, String] = Map("name" -> "leena", "givenRank" -> "5", "isEmployed" -> "true", "sal" -> "1000", "weight" -> "2.5") - val lookup: PlaceholderLookup = new PlaceholderLookup(params) - - //When - val findQuery = FindQuery(query, lookup) - - //Then - val criteriaMap = Map("name" -> "leena", "rank" -> 5, "employed" -> true, "salary" -> 1000, "weight" -> 2.5) + val criteriaMap = mutable.LinkedHashMap("name" -> "leena", "rank" -> 5, "employed" -> true, "salary" -> 1000l, "weight" -> 2.5) findQuery.criteria === new BasicDBObject(criteriaMap) findQuery.projection === new BasicDBObject() }