diff --git a/.travis.yml b/.travis.yml index 2a9139b..0d928ae 100644 --- a/.travis.yml +++ b/.travis.yml @@ -4,4 +4,4 @@ services: before_install: - docker pull yglukhov/nim-base script: - - docker run -v "$(pwd):/project" -w /project yglukhov/nim-base run "nimble install -y && nim c -r nimongo/bson && nim c -r nimongo/bsontest" + - docker run -v "$(pwd):/project" -w /project yglukhov/nim-base bash -c "nimble install -y && nimble test_ci" diff --git a/nimongo.nimble b/nimongo.nimble index a05cf78..e1c299e 100644 --- a/nimongo.nimble +++ b/nimongo.nimble @@ -1,8 +1,24 @@ # Package description = "Pure Nim driver for MongoDB with support of synchronous and asynchronous I/O modes" -version = "0.1" +version = "0.2" license = "MIT" author = "Rostyslav Dzinko " # Dependencies -requires "scram >= 0.1.2" +requires "scram >= 0.1.7" + +proc runTest(input: string) = + let cmd = "nim c -r " & input + echo "running: " & cmd + exec cmd + +proc testNoMongod() = + runTest "nimongo/bson.nim" + runTest "tests/bsontest.nim" + +task test, "tests": + testNoMongod() + runTest "tests/nimongotest.nim" + +task test_ci, "tests for CI": + testNoMongod() diff --git a/nimongo/bson.nim b/nimongo/bson.nim index 0e35031..a8e3788 100644 --- a/nimongo/bson.nim +++ b/nimongo/bson.nim @@ -9,8 +9,6 @@ import strutils import times import tables -import timeit - # ------------- type: BsonKind -------------------# type BsonKind* = char diff --git a/nimongo/mongo.nim b/nimongo/mongo.nim index 0fc80fd..afbba36 100644 --- a/nimongo/mongo.nim +++ b/nimongo/mongo.nim @@ -20,7 +20,6 @@ import uri import os import bson except `()` -import timeit import scram/client @@ -376,7 +375,6 @@ proc performFindAsync(f: Cursor[AsyncMongo], numberToReturn, numberToSkip: int32 proc all*(f: Cursor[Mongo]): seq[Bson] = ## Perform MongoDB query and return all matching documents - result = @[] for doc in f.performFind(f.nlimit, f.nskip): result.add(doc) @@ -415,6 +413,18 @@ iterator items*(f: Cursor): Bson = for doc in f.performFind(f.nlimit, f.nskip): yield doc +iterator itemsForceSync*(f: Cursor[AsyncMongo]): Bson = + var count = 0'i32 + var limit = f.nlimit + if limit == 0: # pending https://github.com/SSPkrolik/nimongo/issues/64 + limit = type(f.nlimit).high + while count < limit: + let docs = waitFor f.performFindAsync(1, f.nskip + count) + if docs.len == 0: + break + count.inc + yield docs[0] + proc isMaster*(sm: Mongo): bool = ## Perform query in order to check if connected Mongo instance is a master return sm["admin"]["$cmd"].makeQuery(%*{"isMaster": 1}).one()["ismaster"] @@ -428,7 +438,6 @@ proc isMaster*(am: AsyncMongo): Future[bool] {.async.} = proc listDatabases*(sm: Mongo): seq[string] = ## Return list of databases on the server let response = sm["admin"]["$cmd"].makeQuery(%*{"listDatabases": 1}).one() - result = @[] if response.isReplyOk: for db in response["databases"].items(): result.add(db["name"].toString()) @@ -436,7 +445,6 @@ proc listDatabases*(sm: Mongo): seq[string] = proc listDatabases*(am: AsyncMongo): Future[seq[string]] {.async.} = ## Return list of databases on the server via async client let response = await am["admin"]["$cmd"].makeQuery(%*{"listDatabases": 1}).one() - result = @[] if response.isReplyOk: for db in response["databases"].items(): result.add(db["name"].toString()) @@ -468,7 +476,6 @@ proc createCollection*(db: Database[AsyncMongo], name: string, capped: bool = fa proc listCollections*(db: Database[Mongo], filter: Bson = %*{}): seq[string] = ## List collections inside specified database let response = db["$cmd"].makeQuery(%*{"listCollections": 1'i32}).one() - result = @[] if response.isReplyOk: for col in response["cursor"]["firstBatch"]: result.add(col["name"]) @@ -478,7 +485,6 @@ proc listCollections*(db: Database[AsyncMongo], filter: Bson = %*{}): Future[seq let request = %*{"listCollections": 1'i32} response = await db["$cmd"].makeQuery(request).one() - result = @[] if response.isReplyOk: for col in response["cursor"]["firstBatch"]: result.add(col["name"]) @@ -573,7 +579,6 @@ proc unique*(f: Cursor[Mongo], key: string): seq[string] = } response = f.collection.db["$cmd"].makeQuery(request).one() - result = @[] if response.isReplyOk: for item in response["values"].items(): result.add(item.toString()) @@ -590,7 +595,6 @@ proc unique*(f: Cursor[AsyncMongo], key: string): Future[seq[string]] {.async.} } response = await f.collection.db["$cmd"].makeQuery(request).one() - result = @[] if response.isReplyOk: for item in response["values"].items(): result.add(item.toString()) diff --git a/nimongo/bsontest.nim b/tests/bsontest.nim similarity index 96% rename from nimongo/bsontest.nim rename to tests/bsontest.nim index 0436feb..311341a 100644 --- a/nimongo/bsontest.nim +++ b/tests/bsontest.nim @@ -1,7 +1,7 @@ ## Tests for bson.nim module import unittest -import bson +import nimongo/bson suite "BSON serializer/deserializer test suite": @@ -29,6 +29,8 @@ suite "BSON serializer/deserializer test suite": } check(doc["double"] == 5436.5436) check(doc["stringkey"] == "stringvalue") + check doc["stringkey"] is Bson + check(doc["stringkey"].string == "stringvalue") check(doc["document"]["double"] == 5436.5436) check(doc["document"]["key"] == "value") check(doc["array"][0] == 1'i64) diff --git a/nimongo/mongotest.nim b/tests/mongotest.nim similarity index 99% rename from nimongo/mongotest.nim rename to tests/mongotest.nim index 7edf984..b8c6b05 100644 --- a/nimongo/mongotest.nim +++ b/tests/mongotest.nim @@ -4,8 +4,10 @@ import strutils import times import unittest -import bson -import mongo +import nimongo/bson +import nimongo/mongo + +# TODO: unused import timeit const diff --git a/nimongo/nimongotest.nim b/tests/nimongotest.nim similarity index 100% rename from nimongo/nimongotest.nim rename to tests/nimongotest.nim diff --git a/nimongo/timeit.nim b/tests/timeit.nim similarity index 100% rename from nimongo/timeit.nim rename to tests/timeit.nim