Skip to content

Commit

Permalink
Merge pull request #57 from timotheecour/pr_fixes
Browse files Browse the repository at this point in the history
several fixes
  • Loading branch information
SSPkrolik authored Nov 19, 2018

Verified

This commit was signed with the committer’s verified signature. The key has expired.
addaleax Anna Henningsen
2 parents 96c54cf + 3ada6cd commit 777cb55
Showing 8 changed files with 38 additions and 16 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -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"
20 changes: 18 additions & 2 deletions nimongo.nimble
Original file line number Diff line number Diff line change
@@ -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 <rostislav.dzinko@gmail.com>"

# 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()
2 changes: 0 additions & 2 deletions nimongo/bson.nim
Original file line number Diff line number Diff line change
@@ -9,8 +9,6 @@ import strutils
import times
import tables

import timeit

# ------------- type: BsonKind -------------------#

type BsonKind* = char
20 changes: 12 additions & 8 deletions nimongo/mongo.nim
Original file line number Diff line number Diff line change
@@ -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,15 +438,13 @@ 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())

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())
4 changes: 3 additions & 1 deletion nimongo/bsontest.nim → tests/bsontest.nim
Original file line number Diff line number Diff line change
@@ -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)
6 changes: 4 additions & 2 deletions nimongo/mongotest.nim → tests/mongotest.nim
Original file line number Diff line number Diff line change
@@ -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
File renamed without changes.
File renamed without changes.

0 comments on commit 777cb55

Please sign in to comment.