Compare commits

...

33 Commits

Author SHA1 Message Date
Thijs Cadier f69ea92007 Bump version 1 year ago
Thijs Cadier 1194acf7ff Only enable zstd if package found 1 year ago
Thijs Cadier e7a200e47e Bump version 1 year ago
Thijs Cadier 81b452d429 Disable snappy if the lib can't be found 1 year ago
thijsc d176ca1cf6 Bump version 2 years ago
Thijs Cadier 8da4e45e01
Merge pull request #73 from appsignal/mongoc_1_22
Bump mongoc driver to 1.22.0
2 years ago
thijsc 74d1f73261 Bump mongoc driver to 1.22.0 2 years ago
Thijs Cadier 7fcdeed53c Bump version 3 years ago
Thijs Cadier e3bb485807
Merge pull request #67 from appsignal/bsconc_memory_leak_fix
Fix memory leak in Bscon::new
3 years ago
Thijs Cadier fa2dd0dbd9 Fix memory leak in Bscon::new
See https://github.com/appsignal/mongo-rust-driver/issues/66 for
details. Thanks to @austinjones for figuring this out.
3 years ago
Thijs Cadier 6b298e17a2 Hard version lock on sys dependency 3 years ago
Thijs Cadier 13ab54f62e Bump version 3 years ago
Thijs Cadier f83786cdac
Merge pull request #65 from appsignal/snappy
Tweak linking in build
3 years ago
Thijs Cadier d67b74f8c6 Tweak linking in build 3 years ago
Thijs Cadier a231e250cc Bump version 3 years ago
Thijs Cadier f2180f4263 Remove travis from readme and cargo config 3 years ago
Thijs Cadier c9af4825ed Always print cargo link in sys build 3 years ago
Thijs Cadier f581295984
Merge pull request #64 from appsignal/mongoc_1_17_5
Bump libmongoc to 1.17.5
3 years ago
Thijs Cadier 719a8f52bc Bump libmongoc to 1.17.5 3 years ago
Thijs Cadier 903d387dd5 Bump version 3 years ago
Thijs Cadier c24310b49d Use static libbson in probe in build 3 years ago
Thijs Cadier 219bfb839a Remove two cmake options 4 years ago
Thijs Cadier 7c09166ebd Bump version 4 years ago
Thijs Cadier a38d9bc26e
Merge pull request #61 from appsignal/static
Move back to static linking
4 years ago
Thijs Cadier 62930df7fe Move back to static linking 4 years ago
Thijs Cadier ce51abf864 Bump version 4 years ago
Thijs Cadier 8b32365c05
Merge pull request #59 from appsignal/mongoc_1_17
Upgrade to libmongoc 1.17
4 years ago
Thijs Cadier 7a2baf438b Use latest mongodb in tests 4 years ago
Thijs Cadier 8e9751271c Upgrade mongoc to 1.17 4 years ago
Thijs Cadier 9c0b0df99c
Merge pull request #60 from appsignal/setup-semaphore
Semaphore
4 years ago
Thijs Cadier 25ce6da768 Switch CI to Semaphore 4 years ago
Thijs Cadier 58cab34895
Merge pull request #58 from thijsc/bson_11
Bump bson dependency to 1.2
4 years ago
Thijs Cadier e5f89793f6
Merge pull request #57 from thijsc/modern_rust
Modern rust
4 years ago

1
.gitignore vendored

@ -1,6 +1,5 @@
target target
Cargo.lock Cargo.lock
mongoc-sys/mongo-c-driver*
ssl_env_vars ssl_env_vars
.idea/ .idea/
.DS_Store .DS_Store

@ -0,0 +1,21 @@
version: '3.7'
services:
mongodb:
image: mongo:latest
environment:
- MONGO_DATA_DIR=/data/db
ports:
- "27017:27017"
rust:
image: rust:latest
container_name: "rust"
depends_on:
- mongodb
environment:
RUST_TEST_THREADS: "1"
RUST_BACKTRACE: "full"
SKIP_SSL_CONNECTION_TESTS: "true"
MONGODB_CONNECTION_STRING: "mongodb://mongodb:27017"
working_dir: /project
command: tail -f /dev/null

@ -0,0 +1,44 @@
version: v1.0
name: Mongo Rust Driver tests
agent:
machine:
type: e1-standard-8
os_image: ubuntu1804
# Cancel all running and queued workflows before this one
auto_cancel:
running:
# Ignore main AND develop branch as we want it to build all workflows
when: "branch != 'main' AND branch != 'develop'"
global_job_config:
env_vars:
- name: "CONTAINER_NAME"
value: rust
- name: "COMPOSE_OPTIONS"
value: "-f .semaphore/docker-compose.yml"
prologue:
commands:
- checkout
blocks:
- name: Build & Test
dependencies: []
task:
prologue:
commands:
# Create containers but don't start them yet. We first want to copy the caches onto it
- docker-compose $COMPOSE_OPTIONS up --no-start
# Copy project to main test container
- docker cp . $CONTAINER_NAME:/project
# Then start the containers so we can run the test on it
- docker-compose $COMPOSE_OPTIONS up -d --no-recreate
# Install dependencies
- docker-compose $COMPOSE_OPTIONS exec $CONTAINER_NAME apt-get update
- docker-compose $COMPOSE_OPTIONS exec $CONTAINER_NAME apt-get install -y cmake libssl-dev libsasl2-dev libicu-dev
jobs:
- name: Test
commands:
- docker-compose $COMPOSE_OPTIONS exec $CONTAINER_NAME cargo test

@ -1,9 +0,0 @@
language: rust
rust:
- stable
- beta
- nightly
services:
- mongodb
env:
- SKIP_SSL_CONNECTION_TESTS=true SKIP_EXTENDED_BULK_OPERATION_TESTS=true

@ -1,3 +1,36 @@
# 0.14.2
* Disable zstd if package is not found
# 0.14.1
* Disable snappy if package is not found
# 0.14.0
* Bump mongoc to 1.22.0
# 0.13.6
* Memory leak fix in Bscon::new by (by austinjones)
# 0.13.5
* Hard version lock on sys dependency
# 0.13.4
* Tweak linking options, use snappy if available
# 0.13.3
* Upgrade libmongoc to 1.17.4
* Another static linking fix
# 0.13.2
* Fix probe of libbson so static linking actually works
# 0.13.1
* Statically link libmongoc
# 0.13.0
* Bump bson dependency to 1.2
* Upgrade libmongoc to 1.17.4
* Remove Windows support
# 0.12.1 # 0.12.1
* Add support for commands that return batches (by MiesJansen) * Add support for commands that return batches (by MiesJansen)

@ -1,6 +1,6 @@
[package] [package]
name = "mongo_driver" name = "mongo_driver"
version = "0.12.1" version = "0.14.2"
authors = ["Thijs Cadier <thijs@appsignal.com>"] authors = ["Thijs Cadier <thijs@appsignal.com>"]
description = "Mongo Rust driver built on top of the Mongo C driver" description = "Mongo Rust driver built on top of the Mongo C driver"
readme = "README.md" readme = "README.md"
@ -12,7 +12,6 @@ license = "MIT/Apache-2.0"
edition = "2018" edition = "2018"
[badges] [badges]
travis-ci = { repository = "thijsc/mongo-rust-driver" }
is-it-maintained-issue-resolution = { repository = "thijsc/mongo-rust-driver" } is-it-maintained-issue-resolution = { repository = "thijsc/mongo-rust-driver" }
is-it-maintained-open-issues = { repository = "thijsc/mongo-rust-driver" } is-it-maintained-open-issues = { repository = "thijsc/mongo-rust-driver" }
@ -25,7 +24,7 @@ serde_derive = "1.0"
[dependencies.mongoc-sys] [dependencies.mongoc-sys]
path = "mongoc-sys" path = "mongoc-sys"
version = "1.8.2-1" version = "=1.22.0-2"
[dev-dependencies] [dev-dependencies]
chrono = "^0.4" chrono = "^0.4"

@ -1,6 +1,5 @@
# Mongo Rust Driver # Mongo Rust Driver
[![Build Status](https://travis-ci.org/thijsc/mongo-rust-driver.svg)](https://travis-ci.org/thijsc/mongo-rust-driver)
[![Crate](http://meritbadge.herokuapp.com/mongo_driver)](https://crates.io/crates/mongo_driver) [![Crate](http://meritbadge.herokuapp.com/mongo_driver)](https://crates.io/crates/mongo_driver)
Mongo Rust driver built on top of the [Mongo C driver](https://github.com/mongodb/mongo-c-driver). Mongo Rust driver built on top of the [Mongo C driver](https://github.com/mongodb/mongo-c-driver).
@ -21,10 +20,11 @@ The driver currently only builds on Unix, tested on Mac Os X and Linux so far. I
If you have any trouble installing the crate (linking openssl can be If you have any trouble installing the crate (linking openssl can be
tricky) please check out the [installation instructions for the C driver](http://mongoc.org/libmongoc/current/installing.html). tricky) please check out the [installation instructions for the C driver](http://mongoc.org/libmongoc/current/installing.html).
To build on Mac install OpenSSL 1.1: To build on Mac install OpenSSL 1.1 and cmake:
``` ```
brew install openssl@1.1 brew install openssl@1.1
brew install cmake
``` ```
Export these env vars the before you make a clean build: Export these env vars the before you make a clean build:

@ -1,6 +1,6 @@
[package] [package]
name = "mongoc-sys" name = "mongoc-sys"
version = "1.8.2-1" version = "1.22.0-2"
description = "Sys package with installer and bindings for mongoc" description = "Sys package with installer and bindings for mongoc"
authors = ["Thijs Cadier <thijs@appsignal.com>"] authors = ["Thijs Cadier <thijs@appsignal.com>"]
build = "build.rs" build = "build.rs"
@ -8,12 +8,10 @@ repository = "https://github.com/thijsc/mongo-rust-driver"
links = "mongoc" links = "mongoc"
license = "MIT/Apache-2.0" license = "MIT/Apache-2.0"
categories = ["external-ffi-bindings"] categories = ["external-ffi-bindings"]
exclude = ["mongo-c-driver-*"]
[dependencies] [dependencies]
libc = "0.2" libc = "^0.2"
openssl-sys = "0.9" openssl-sys = "^0.9"
[build-dependencies] [build-dependencies]
pkg-config = "0.3" pkg-config = "^0.3"
vcpkg = "0.2.6"

@ -1,142 +1,103 @@
extern crate pkg_config; extern crate pkg_config;
#[cfg(target_env = "msvc")]
extern crate vcpkg;
use std::env; use std::env;
use std::path::Path;
use std::process::Command;
#[cfg(not(target_env = "msvc"))] fn main() {
fn lin(mongoc_version: &str) { let mongoc_version = env!("CARGO_PKG_VERSION")
use std::path::Path; .split('-')
use std::process::Command; .next()
.expect("Crate version is not valid");
if pkg_config::Config::new()
.atleast_version(mongoc_version)
.statik(true)
.probe("libmongoc-1.0")
.is_err()
{
let out_dir_var = env::var("OUT_DIR").expect("No out dir");
let out_dir = format!("{}/{}", out_dir_var, mongoc_version);
let driver_src_path = format!("mongo-c-driver-{}", mongoc_version);
let libmongoc_path = Path::new(&out_dir).join("lib/libmongoc-1.0.a");
if !libmongoc_path.exists() {
// Download and extract driver archive
let url = format!(
"https://github.com/mongodb/mongo-c-driver/releases/download/{}/mongo-c-driver-{}.tar.gz",
mongoc_version,
mongoc_version
);
assert!(
Command::new("curl").arg("-O") // Save to disk
.arg("-L") // Follow redirects
.arg(url)
.status()
.expect("Could not run curl")
.success()
);
let archive_name = format!("mongo-c-driver-{}.tar.gz", mongoc_version);
assert!(
Command::new("tar")
.arg("xzf")
.arg(&archive_name)
.status()
.expect("Could not run tar")
.success()
);
// Configure and install
let mut command = Command::new("sh");
command.arg("configure");
command.arg("--enable-ssl=openssl");
command.arg("--enable-sasl=no");
command.arg("--enable-static=yes");
command.arg("--enable-shared=no");
command.arg("--enable-shm-counters=no");
command.arg("--with-libbson=bundled");
command.arg("--with-pic=yes");
command.arg("--with-snappy=no");
command.arg("--with-zlib=no");
command.arg(format!("--prefix={}", &out_dir));
command.current_dir(&driver_src_path);
// Enable debug symbols if configured for this profile
if env::var("DEBUG") == Ok("true".to_string()) {
command.arg("--enable-debug-symbols=yes");
}
// Use target that Cargo sets
if let Ok(target) = env::var("TARGET") {
command.arg(format!("--build={}", target));
}
assert!(command.status().expect("Could not run configure").success()); let out_dir_var = env::var("OUT_DIR").expect("No out dir");
assert!( let out_dir = Path::new(&out_dir_var);
Command::new("make") let driver_src_path = out_dir.join(format!("mongo-c-driver-{}", mongoc_version));
.current_dir(&driver_src_path)
.env("CFLAGS", "-DMONGOC_TRACE") let libmongoc_path = out_dir.join("usr/local/lib/libmongoc-static-1.0.a");
.status() if !libmongoc_path.exists() {
.expect("Could not run make") // Download and extract driver archive
.success() let url = format!(
); "https://github.com/mongodb/mongo-c-driver/releases/download/{}/mongo-c-driver-{}.tar.gz",
assert!( mongoc_version,
Command::new("make") mongoc_version
.arg("install") );
.current_dir(&driver_src_path) assert!(
.status() Command::new("curl").arg("-O") // Save to disk
.expect("Could not run make install") .current_dir(out_dir)
.success() .arg("-L") // Follow redirects
); .arg(url)
.status()
.expect("Could not run curl")
.success()
);
let archive_name = format!("mongo-c-driver-{}.tar.gz", mongoc_version);
assert!(
Command::new("tar")
.current_dir(out_dir)
.arg("xzf")
.arg(&archive_name)
.status()
.expect("Could not run tar")
.success()
);
// Set up cmake command
let mut cmake = Command::new("cmake");
cmake.current_dir(&driver_src_path);
let pkg = pkg_config::Config::new();
pkg.probe("zlib").expect("Cannot find zlib");
#[cfg(target_os = "linux")] pkg.probe("icu-i18n").expect("Cannot find icu");
match pkg.probe("snappy") {
Ok(_) => {
cmake.arg("-DENABLE_SNAPPY=ON");
},
Err(e) => {
println!("Snappy not found: {}", e);
cmake.arg("-DENABLE_SNAPPY=OFF");
} }
// Output to Cargo
println!("cargo:rustc-link-search=native={}/lib", &out_dir);
println!("cargo:rustc-link-lib=static=bson-1.0");
println!("cargo:rustc-link-lib=static=mongoc-1.0");
} }
} match pkg.probe("zstd") {
Ok(_) => {
#[cfg(target_env = "msvc")] cmake.arg("-DENABLE_ZSTD=ON");
fn win(_mongoc_version: &str) { },
use vcpkg; Err(e) => {
println!("Zstd not found: {}", e);
let mongo_lib = "mongoc-1.0"; cmake.arg("-DENABLE_ZSTD=OFF");
let bson_lib = "bson-1.0";
if vcpkg::Config::new()
.emit_includes(true)
.probe("mongo-c-driver")
.is_ok()
{
// Output to Cargo
println!("cargo:rustc-link-lib={}", bson_lib);
println!("cargo:rustc-link-lib={}", mongo_lib);
} else {
if let Ok(bson_dir_lib) = env::var("MONGO_LIB") {
if let Ok(mongo_dir_lib) = env::var("BSON_LIB") {
println!("cargo:rustc-link-search=native={}", bson_dir_lib);
println!("cargo:rustc-link-lib=dylib={}", bson_lib);
println!("cargo:rustc-link-search=native={}", mongo_dir_lib);
println!("cargo:rustc-link-lib=dylib={}", mongo_lib);
} else {
panic!("please define BSON_LIB to {}.lib, \n for example set BSON_LIB=C:\\vcpkg\\packages\\libbson_x64-windows\\lib", bson_lib);
} }
} else {
panic!("please define MONGO_LIB to {}.lib, \n for example set MONGO_LIB=C:\\vcpkg\\packages\\mongo-c-driver_x64-windows\\lib", mongo_lib);
} }
}
}
fn main() { cmake.arg("-DENABLE_AUTOMATIC_INIT_AND_CLEANUP=OFF");
let mongoc_version = env!("CARGO_PKG_VERSION") cmake.arg("-DENABLE_SSL=OPENSSL");
.split('-') cmake.arg("-DENABLE_SASL=OFF");
.next() cmake.arg("-DENABLE_STATIC=ON");
.expect("Crate version is not valid"); cmake.arg("-DENABLE_BSON=ON");
cmake.arg("-DENABLE_ENABLE_EXAMPLES=OFF");
cmake.arg("-DENABLE_TESTS=OFF");
cmake.arg("-DENABLE_SHM_COUNTERS=OFF");
cmake.arg("-DWITH_PIC=ON");
// Run in current dir
cmake.arg(".");
// Run cmake command
assert!(cmake.status().expect("Could not run cmake").success());
// Set up make install command
let mut make = Command::new("make");
make.current_dir(&driver_src_path);
make.arg(format!("DESTDIR={}", out_dir.to_string_lossy()));
make.arg("install");
// Run make command
assert!(make.status().expect("Could not run make install").success());
}
#[cfg(target_env = "msvc")] // Output to Cargo
win(mongoc_version); println!("cargo:rustc-link-search=native={}/usr/local/lib", &out_dir.to_string_lossy());
#[cfg(not(target_env = "msvc"))] println!("cargo:rustc-link-lib=static=bson-static-1.0");
lin(mongoc_version); println!("cargo:rustc-link-lib=static=mongoc-static-1.0");
println!("cargo:rustc-link-lib=resolv");
} }

@ -254,3 +254,11 @@ pub mod bindings {
pub const MONGOC_ERROR_WRITE_CONCERN_ERROR: ::libc::c_uint = 64; pub const MONGOC_ERROR_WRITE_CONCERN_ERROR: ::libc::c_uint = 64;
pub const MONGOC_ERROR_DUPLICATE_KEY: ::libc::c_uint = 11000; pub const MONGOC_ERROR_DUPLICATE_KEY: ::libc::c_uint = 11000;
} }
#[cfg(test)]
mod tests {
#[test]
fn run() {
// Just a quick test to make seeing if everything links right easy
}
}

@ -10,17 +10,28 @@ use bson;
use super::Result; use super::Result;
pub struct Bsonc { pub struct Bsonc {
inner: *mut bindings::bson_t inner: *mut bindings::bson_t,
destroy_inner_on_drop: bool
} }
impl Bsonc { impl Bsonc {
pub fn new() -> Bsonc { pub fn new() -> Bsonc {
Bsonc::from_ptr(unsafe { bindings::bson_new() }) let inner: *const bindings::bson_t = unsafe { bindings::bson_new() };
assert!(!inner.is_null());
Bsonc {
inner: inner as *mut bindings::bson_t,
destroy_inner_on_drop: true,
}
} }
/// Create a bsonc from a raw pointer. Does not run cleanup
/// logic on drop in this case.
pub fn from_ptr(inner: *const bindings::bson_t) -> Bsonc { pub fn from_ptr(inner: *const bindings::bson_t) -> Bsonc {
assert!(!inner.is_null()); assert!(!inner.is_null());
Bsonc { inner: inner as *mut bindings::bson_t } Bsonc {
inner: inner as *mut bindings::bson_t,
destroy_inner_on_drop: false
}
} }
pub fn from_document(document: &bson::Document) -> Result<Bsonc> { pub fn from_document(document: &bson::Document) -> Result<Bsonc> {
@ -39,7 +50,10 @@ impl Bsonc {
// See: http://mongoc.org/libbson/current/bson_new_from_data.html // See: http://mongoc.org/libbson/current/bson_new_from_data.html
assert!(!inner.is_null()); assert!(!inner.is_null());
Ok(Bsonc{ inner: inner }) Ok(Bsonc{
inner: inner,
destroy_inner_on_drop: true
})
} }
/// Decode a bson from the C side to a document /// Decode a bson from the C side to a document
@ -92,14 +106,23 @@ impl fmt::Debug for Bsonc {
impl Drop for Bsonc { impl Drop for Bsonc {
fn drop(&mut self) { fn drop(&mut self) {
unsafe { if self.destroy_inner_on_drop {
bindings::bson_destroy(self.inner); unsafe {
bindings::bson_destroy(self.inner);
}
} }
} }
} }
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
#[test]
fn test_bsonc_new() {
for _ in 0..100 {
let _ = super::Bsonc::new();
}
}
#[test] #[test]
fn test_bsonc_from_and_as_document() { fn test_bsonc_from_and_as_document() {
let document = doc! { "key": "value" }; let document = doc! { "key": "value" };

@ -45,8 +45,7 @@ pub struct Database<'a> {
} }
impl<'a> Database<'a> { impl<'a> Database<'a> {
#[doc(ignore)] pub(crate) fn new(
pub fn new(
created_by: CreatedBy<'a>, created_by: CreatedBy<'a>,
inner: *mut bindings::mongoc_database_t inner: *mut bindings::mongoc_database_t
) -> Database<'a> { ) -> Database<'a> {

@ -9,7 +9,7 @@
//! //!
//! # Example //! # Example
//! //!
//! ``` //! ```no_run
//! use std::sync::Arc; //! use std::sync::Arc;
//! use mongo_driver::client::{ClientPool,Uri}; //! use mongo_driver::client::{ClientPool,Uri};
//! //!

@ -2,6 +2,8 @@ extern crate bson;
extern crate chrono; extern crate chrono;
extern crate mongo_driver; extern crate mongo_driver;
mod helpers;
use chrono::prelude::*; use chrono::prelude::*;
use mongo_driver::client::{ClientPool,Uri}; use mongo_driver::client::{ClientPool,Uri};
@ -16,7 +18,7 @@ use bson::spec::BinarySubtype;
#[test] #[test]
fn test_bson_encode_decode() { fn test_bson_encode_decode() {
let uri = Uri::new("mongodb://localhost:27017/").unwrap(); let uri = Uri::new(helpers::mongodb_test_connection_string()).unwrap();
let pool = ClientPool::new(uri, None); let pool = ClientPool::new(uri, None);
let client = pool.pop(); let client = pool.pop();
let mut collection = client.get_collection("rust_driver_test", "bson"); let mut collection = client.get_collection("rust_driver_test", "bson");
@ -38,7 +40,7 @@ fn test_bson_encode_decode() {
bytes: vec![0, 1, 2, 3, 4] bytes: vec![0, 1, 2, 3, 4]
}) })
}; };
assert!(collection.insert(&document, None).is_ok()); collection.insert(&document, None).expect("Could not insert");
let found_document = collection.find(&doc!{}, None).unwrap().next().unwrap().unwrap(); let found_document = collection.find(&doc!{}, None).unwrap().next().unwrap().unwrap();

@ -1,6 +1,8 @@
extern crate bson; extern crate bson;
extern crate mongo_driver; extern crate mongo_driver;
mod helpers;
use std::env; use std::env;
use bson::doc; use bson::doc;
@ -8,7 +10,7 @@ use mongo_driver::client::{ClientPool,Uri};
#[test] #[test]
fn test_execute_error() { fn test_execute_error() {
let uri = Uri::new("mongodb://localhost:27017/").unwrap(); let uri = Uri::new(helpers::mongodb_test_connection_string()).unwrap();
let pool = ClientPool::new(uri, None); let pool = ClientPool::new(uri, None);
let client = pool.pop(); let client = pool.pop();
let mut collection = client.get_collection("rust_driver_test", "bulk_operation_error"); let mut collection = client.get_collection("rust_driver_test", "bulk_operation_error");
@ -25,7 +27,7 @@ fn test_execute_error() {
#[test] #[test]
fn test_basics() { fn test_basics() {
let uri = Uri::new("mongodb://localhost:27017/").unwrap(); let uri = Uri::new(helpers::mongodb_test_connection_string()).unwrap();
let pool = ClientPool::new(uri, None); let pool = ClientPool::new(uri, None);
let client = pool.pop(); let client = pool.pop();
let mut collection = client.get_collection("rust_driver_test", "bulk_operation_basics"); let mut collection = client.get_collection("rust_driver_test", "bulk_operation_basics");
@ -35,7 +37,7 @@ fn test_basics() {
let document = doc! {"key_1": "Value 1"}; let document = doc! {"key_1": "Value 1"};
bulk_operation.insert(&document).expect("Could not insert"); bulk_operation.insert(&document).expect("Could not insert");
assert!(bulk_operation.execute().is_ok()); bulk_operation.execute().expect("Could not execute bulk operation");
let first_document = collection.find(&doc!{}, None).unwrap().next().unwrap().unwrap(); let first_document = collection.find(&doc!{}, None).unwrap().next().unwrap().unwrap();
assert_eq!( assert_eq!(
@ -46,7 +48,7 @@ fn test_basics() {
#[test] #[test]
fn test_utf8() { fn test_utf8() {
let uri = Uri::new("mongodb://localhost:27017/").unwrap(); let uri = Uri::new(helpers::mongodb_test_connection_string()).unwrap();
let pool = ClientPool::new(uri, None); let pool = ClientPool::new(uri, None);
let client = pool.pop(); let client = pool.pop();
let mut collection = client.get_collection("rust_driver_test", "bulk_operation_utf8"); let mut collection = client.get_collection("rust_driver_test", "bulk_operation_utf8");
@ -56,7 +58,7 @@ fn test_utf8() {
let document = doc! {"key_1": "kācaṃ śaknomyattum; nopahinasti mām."}; let document = doc! {"key_1": "kācaṃ śaknomyattum; nopahinasti mām."};
bulk_operation.insert(&document).expect("Could not insert"); bulk_operation.insert(&document).expect("Could not insert");
assert!(bulk_operation.execute().is_ok()); bulk_operation.execute().expect("Could not execute bulk operation");
let first_document = collection.find(&doc!{}, None).unwrap().next().unwrap().unwrap(); let first_document = collection.find(&doc!{}, None).unwrap().next().unwrap().unwrap();
assert_eq!( assert_eq!(
@ -71,7 +73,7 @@ fn test_insert_remove_replace_update_extended() {
return return
} }
let uri = Uri::new("mongodb://localhost:27017/").unwrap(); let uri = Uri::new(helpers::mongodb_test_connection_string()).unwrap();
let pool = ClientPool::new(uri, None); let pool = ClientPool::new(uri, None);
let client = pool.pop(); let client = pool.pop();
let mut collection = client.get_collection("rust_driver_test", "bulk_operation_extended"); let mut collection = client.get_collection("rust_driver_test", "bulk_operation_extended");
@ -89,11 +91,10 @@ fn test_insert_remove_replace_update_extended() {
bulk_operation.insert(&document).unwrap(); bulk_operation.insert(&document).unwrap();
} }
let result = bulk_operation.execute(); let result = bulk_operation.execute().expect("Could not execute bulk operation");
assert!(result.is_ok());
assert_eq!( assert_eq!(
result.ok().unwrap().get("nInserted").unwrap(), result.get("nInserted").unwrap(),
&bson::Bson::Int32(5) &bson::Bson::Int32(5)
); );
assert_eq!(5, collection.count(&doc!{}, None).unwrap()); assert_eq!(5, collection.count(&doc!{}, None).unwrap());
@ -114,12 +115,10 @@ fn test_insert_remove_replace_update_extended() {
false false
).unwrap(); ).unwrap();
let result = bulk_operation.execute(); let result = bulk_operation.execute().expect("Could not execute bulk operation");
println!("{:?}", result);
assert!(result.is_ok());
assert_eq!( assert_eq!(
result.ok().unwrap().get("nModified").unwrap(), result.get("nModified").unwrap(),
&bson::Bson::Int32(1) &bson::Bson::Int32(1)
); );
@ -141,12 +140,10 @@ fn test_insert_remove_replace_update_extended() {
false false
).unwrap(); ).unwrap();
let result = bulk_operation.execute(); let result = bulk_operation.execute().expect("Could not execute bulk operation");
println!("{:?}", result);
assert!(result.is_ok());
assert_eq!( assert_eq!(
result.ok().unwrap().get("nModified").unwrap(), result.get("nModified").unwrap(),
&bson::Bson::Int32(4) &bson::Bson::Int32(4)
); );
@ -171,11 +168,10 @@ fn test_insert_remove_replace_update_extended() {
false false
).unwrap(); ).unwrap();
let result = bulk_operation.execute(); let result = bulk_operation.execute().expect("Could not execute bulk operation");
assert!(result.is_ok());
assert_eq!( assert_eq!(
result.ok().unwrap().get("nModified").unwrap(), result.get("nModified").unwrap(),
&bson::Bson::Int32(1) &bson::Bson::Int32(1)
); );
@ -193,11 +189,10 @@ fn test_insert_remove_replace_update_extended() {
let bulk_operation = collection.create_bulk_operation(None); let bulk_operation = collection.create_bulk_operation(None);
bulk_operation.remove_one(&query).unwrap(); bulk_operation.remove_one(&query).unwrap();
let result = bulk_operation.execute(); let result = bulk_operation.execute().expect("Could not execute bulk operation");
assert!(result.is_ok());
assert_eq!( assert_eq!(
result.ok().unwrap().get("nRemoved").unwrap(), result.get("nRemoved").unwrap(),
&bson::Bson::Int32(1) &bson::Bson::Int32(1)
); );
assert_eq!(4, collection.count(&query, None).unwrap()); assert_eq!(4, collection.count(&query, None).unwrap());
@ -208,11 +203,10 @@ fn test_insert_remove_replace_update_extended() {
let bulk_operation = collection.create_bulk_operation(None); let bulk_operation = collection.create_bulk_operation(None);
bulk_operation.remove(&query).unwrap(); bulk_operation.remove(&query).unwrap();
let result = bulk_operation.execute(); let result = bulk_operation.execute().expect("Could not execute bulk operation");
assert!(result.is_ok());
assert_eq!( assert_eq!(
result.ok().unwrap().get("nRemoved").unwrap(), result.get("nRemoved").unwrap(),
&bson::Bson::Int32(4) &bson::Bson::Int32(4)
); );
assert_eq!(0, collection.count(&query, None).unwrap()); assert_eq!(0, collection.count(&query, None).unwrap());

@ -1,6 +1,8 @@
extern crate bson; extern crate bson;
extern crate mongo_driver; extern crate mongo_driver;
mod helpers;
use std::env; use std::env;
use std::path::PathBuf; use std::path::PathBuf;
use std::sync::Arc; use std::sync::Arc;
@ -11,7 +13,7 @@ use mongo_driver::client::{ClientPool,SslOptions,Uri};
#[test] #[test]
fn test_new_pool_pop_client_and_borrow_collection() { fn test_new_pool_pop_client_and_borrow_collection() {
let uri = Uri::new("mongodb://localhost:27017/").unwrap(); let uri = Uri::new(helpers::mongodb_test_connection_string()).unwrap();
let pool = ClientPool::new(uri.clone(), None); let pool = ClientPool::new(uri.clone(), None);
assert_eq!(pool.get_uri(), &uri); assert_eq!(pool.get_uri(), &uri);
@ -28,7 +30,7 @@ fn test_new_pool_pop_client_and_borrow_collection() {
#[test] #[test]
fn test_new_pool_pop_client_and_take_collection() { fn test_new_pool_pop_client_and_take_collection() {
let uri = Uri::new("mongodb://localhost:27017/").unwrap(); let uri = Uri::new(helpers::mongodb_test_connection_string()).unwrap();
let pool = ClientPool::new(uri.clone(), None); let pool = ClientPool::new(uri.clone(), None);
assert_eq!(pool.get_uri(), &uri); assert_eq!(pool.get_uri(), &uri);
@ -40,7 +42,7 @@ fn test_new_pool_pop_client_and_take_collection() {
#[test] #[test]
fn test_new_pool_pop_client_and_take_database_and_collection() { fn test_new_pool_pop_client_and_take_database_and_collection() {
let uri = Uri::new("mongodb://localhost:27017/").unwrap(); let uri = Uri::new(helpers::mongodb_test_connection_string()).unwrap();
let pool = ClientPool::new(uri.clone(), None); let pool = ClientPool::new(uri.clone(), None);
assert_eq!(pool.get_uri(), &uri); assert_eq!(pool.get_uri(), &uri);
@ -54,7 +56,7 @@ fn test_new_pool_pop_client_and_take_database_and_collection() {
#[test] #[test]
fn test_new_pool_and_pop_client_in_threads() { fn test_new_pool_and_pop_client_in_threads() {
let uri = Uri::new("mongodb://localhost:27017/").unwrap(); let uri = Uri::new(helpers::mongodb_test_connection_string()).unwrap();
let pool = Arc::new(ClientPool::new(uri, None)); let pool = Arc::new(ClientPool::new(uri, None));
let pool1 = pool.clone(); let pool1 = pool.clone();
@ -75,7 +77,7 @@ fn test_new_pool_and_pop_client_in_threads() {
#[test] #[test]
fn test_get_server_status() { fn test_get_server_status() {
let uri = Uri::new("mongodb://localhost:27017/").unwrap(); let uri = Uri::new(helpers::mongodb_test_connection_string()).unwrap();
let pool = ClientPool::new(uri, None); let pool = ClientPool::new(uri, None);
let client = pool.pop(); let client = pool.pop();
@ -87,7 +89,7 @@ fn test_get_server_status() {
#[test] #[test]
fn test_read_command_with_opts() { fn test_read_command_with_opts() {
let uri = Uri::new("mongodb://localhost:27017/").unwrap(); let uri = Uri::new(helpers::mongodb_test_connection_string()).unwrap();
let pool = ClientPool::new(uri, None); let pool = ClientPool::new(uri, None);
let client = pool.pop(); let client = pool.pop();
let db_name = "rust_driver_test"; let db_name = "rust_driver_test";
@ -110,7 +112,7 @@ fn test_read_command_with_opts() {
#[test] #[test]
fn test_new_pool_with_ssl_options() { fn test_new_pool_with_ssl_options() {
let uri = Uri::new("mongodb://localhost:27017/").unwrap(); let uri = Uri::new(helpers::mongodb_test_connection_string()).unwrap();
let ssl_options = SslOptions::new( let ssl_options = SslOptions::new(
Some(PathBuf::from("./README.md")), Some(PathBuf::from("./README.md")),
Some("password".to_string()), Some("password".to_string()),
@ -118,9 +120,8 @@ fn test_new_pool_with_ssl_options() {
Some(PathBuf::from("./README.md")), Some(PathBuf::from("./README.md")),
Some(PathBuf::from("./README.md")), Some(PathBuf::from("./README.md")),
false false
); ).expect("Ssl options not correct");
assert!(ssl_options.is_ok()); ClientPool::new(uri, Some(ssl_options));
ClientPool::new(uri, Some(ssl_options.unwrap()));
} }
#[test] #[test]

@ -1,6 +1,8 @@
extern crate bson; extern crate bson;
extern crate mongo_driver; extern crate mongo_driver;
mod helpers;
use bson::doc; use bson::doc;
use mongo_driver::CommandAndFindOptions; use mongo_driver::CommandAndFindOptions;
@ -10,14 +12,14 @@ use mongo_driver::flags;
#[test] #[test]
fn test_aggregate() { fn test_aggregate() {
let uri = Uri::new("mongodb://localhost:27017/").unwrap(); let uri = Uri::new(helpers::mongodb_test_connection_string()).unwrap();
let pool = ClientPool::new(uri, None); let pool = ClientPool::new(uri, None);
let client = pool.pop(); let client = pool.pop();
let mut collection = client.get_collection("rust_driver_test", "aggregate"); let mut collection = client.get_collection("rust_driver_test", "aggregate");
collection.drop().unwrap_or(()); collection.drop().unwrap_or(());
for _ in 0..5 { for _ in 0..5 {
assert!(collection.insert(&doc!{"key": 1}, None).is_ok()); collection.insert(&doc!{"key": 1}, None).expect("Could not insert");
} }
let pipeline = doc!{ let pipeline = doc!{
@ -36,10 +38,9 @@ fn test_aggregate() {
assert_eq!(Ok(5), total.get_i32("total")); assert_eq!(Ok(5), total.get_i32("total"));
} }
#[cfg_attr(target_os = "windows", ignore)]
#[test] #[test]
fn test_command() { fn test_command() {
let uri = Uri::new("mongodb://localhost:27017/").unwrap(); let uri = Uri::new(helpers::mongodb_test_connection_string()).unwrap();
let pool = ClientPool::new(uri, None); let pool = ClientPool::new(uri, None);
let client = pool.pop(); let client = pool.pop();
let collection = client.get_collection("rust_driver_test", "items"); let collection = client.get_collection("rust_driver_test", "items");
@ -52,7 +53,7 @@ fn test_command() {
#[test] #[test]
fn test_command_simple() { fn test_command_simple() {
let uri = Uri::new("mongodb://localhost:27017/").unwrap(); let uri = Uri::new(helpers::mongodb_test_connection_string()).unwrap();
let pool = ClientPool::new(uri, None); let pool = ClientPool::new(uri, None);
let client = pool.pop(); let client = pool.pop();
let collection = client.get_collection("rust_driver_test", "items"); let collection = client.get_collection("rust_driver_test", "items");
@ -65,7 +66,7 @@ fn test_command_simple() {
#[test] #[test]
fn test_mutation_and_finding() { fn test_mutation_and_finding() {
let uri = Uri::new("mongodb://localhost:27017/").unwrap(); let uri = Uri::new(helpers::mongodb_test_connection_string()).unwrap();
let pool = ClientPool::new(uri, None); let pool = ClientPool::new(uri, None);
let client = pool.pop(); let client = pool.pop();
let _ = client.get_collection("rust_driver_test".to_string(), "items"); let _ = client.get_collection("rust_driver_test".to_string(), "items");
@ -94,7 +95,7 @@ fn test_mutation_and_finding() {
let second_document = doc! { let second_document = doc! {
"key_1": "Value 3" "key_1": "Value 3"
}; };
assert!(collection.insert(&second_document, None).is_ok()); collection.insert(&second_document, None).expect("Could not insert");
let query = doc!{}; let query = doc!{};
@ -119,7 +120,7 @@ fn test_mutation_and_finding() {
// Update the second document // Update the second document
let update = doc!{"$set": {"key_1": "Value 4"}}; let update = doc!{"$set": {"key_1": "Value 4"}};
assert!(collection.update(&second_document, &update, None).is_ok()); collection.update(&second_document, &update, None).expect("Could not update");
// Reload and check value // Reload and check value
let query_after_update = doc! { let query_after_update = doc! {
@ -133,7 +134,7 @@ fn test_mutation_and_finding() {
// Save the second document // Save the second document
found_document.insert("key_1".to_string(), bson::Bson::String("Value 5".to_string())); found_document.insert("key_1".to_string(), bson::Bson::String("Value 5".to_string()));
assert!(collection.save(&found_document, None).is_ok()); collection.save(&found_document, None).expect("Could not save");
// Reload and check value // Reload and check value
let found_document = collection.find(&found_document, None).unwrap().next().unwrap().unwrap(); let found_document = collection.find(&found_document, None).unwrap().next().unwrap().unwrap();
@ -143,7 +144,7 @@ fn test_mutation_and_finding() {
); );
// Remove one // Remove one
assert!(collection.remove(&found_document, None).is_ok()); collection.remove(&found_document, None).expect("Could not remove");
// Count again // Count again
assert_eq!(1, collection.count(&query, None).unwrap()); assert_eq!(1, collection.count(&query, None).unwrap());
@ -186,7 +187,7 @@ fn test_mutation_and_finding() {
#[test] #[test]
fn test_find_and_modify() { fn test_find_and_modify() {
let uri = Uri::new("mongodb://localhost:27017/").unwrap(); let uri = Uri::new(helpers::mongodb_test_connection_string()).unwrap();
let pool = ClientPool::new(uri, None); let pool = ClientPool::new(uri, None);
let client = pool.pop(); let client = pool.pop();
let mut collection = client.get_collection("rust_driver_test", "find_and_modify"); let mut collection = client.get_collection("rust_driver_test", "find_and_modify");
@ -199,12 +200,11 @@ fn test_find_and_modify() {
let update = doc! { let update = doc! {
"$set": {"content": 1i32} "$set": {"content": 1i32}
}; };
let result = collection.find_and_modify( collection.find_and_modify(
&query, &query,
FindAndModifyOperation::Upsert(&update), FindAndModifyOperation::Upsert(&update),
None None
); ).expect("Could not find and modify");
assert!(result.is_ok());
assert_eq!(1, collection.count(&query, None).unwrap()); assert_eq!(1, collection.count(&query, None).unwrap());
let found_document = collection.find(&query, None).unwrap().next().unwrap().unwrap(); let found_document = collection.find(&query, None).unwrap().next().unwrap().unwrap();
assert_eq!(found_document.get_i32("content"), Ok(1)); assert_eq!(found_document.get_i32("content"), Ok(1));
@ -213,23 +213,21 @@ fn test_find_and_modify() {
let update2 = doc! { let update2 = doc! {
"$set": {"content": 2i32} "$set": {"content": 2i32}
}; };
let result = collection.find_and_modify( collection.find_and_modify(
&query, &query,
FindAndModifyOperation::Update(&update2), FindAndModifyOperation::Update(&update2),
None None
); ).expect("Could not find and modify");
assert!(result.is_ok());
assert_eq!(1, collection.count(&query, None).unwrap()); assert_eq!(1, collection.count(&query, None).unwrap());
let found_document = collection.find(&query, None).unwrap().next().unwrap().unwrap(); let found_document = collection.find(&query, None).unwrap().next().unwrap().unwrap();
assert_eq!(found_document.get_i32("content"), Ok(2)); assert_eq!(found_document.get_i32("content"), Ok(2));
// Remove it // Remove it
let result = collection.find_and_modify( collection.find_and_modify(
&query, &query,
FindAndModifyOperation::Remove, FindAndModifyOperation::Remove,
None None
); ).expect("Could not find and modify");
assert!(result.is_ok());
assert_eq!(0, collection.count(&query, None).unwrap()); assert_eq!(0, collection.count(&query, None).unwrap());
} }

@ -1,6 +1,8 @@
extern crate bson; extern crate bson;
extern crate mongo_driver; extern crate mongo_driver;
mod helpers;
use std::sync::Arc; use std::sync::Arc;
use std::thread; use std::thread;
use std::time::Duration; use std::time::Duration;
@ -12,7 +14,7 @@ use mongo_driver::Result;
#[test] #[test]
fn test_cursor() { fn test_cursor() {
let uri = Uri::new("mongodb://localhost:27017/").unwrap(); let uri = Uri::new(helpers::mongodb_test_connection_string()).unwrap();
let pool = ClientPool::new(uri, None); let pool = ClientPool::new(uri, None);
let client = pool.pop(); let client = pool.pop();
let mut collection = client.get_collection("rust_driver_test", "cursor_items"); let mut collection = client.get_collection("rust_driver_test", "cursor_items");
@ -21,7 +23,7 @@ fn test_cursor() {
collection.drop().unwrap_or(()); collection.drop().unwrap_or(());
for _ in 0..10 { for _ in 0..10 {
assert!(collection.insert(&document, None).is_ok()); collection.insert(&document, None).expect("Could not insert");
} }
let query = doc! {}; let query = doc! {};
@ -37,7 +39,7 @@ fn test_cursor() {
fn test_tailing_cursor() { fn test_tailing_cursor() {
// See: http://mongoc.org/libmongoc/current/cursors.html#tailable // See: http://mongoc.org/libmongoc/current/cursors.html#tailable
let uri = Uri::new("mongodb://localhost:27017/").unwrap(); let uri = Uri::new(helpers::mongodb_test_connection_string()).unwrap();
let pool = Arc::new(ClientPool::new(uri, None)); let pool = Arc::new(ClientPool::new(uri, None));
let client = pool.pop(); let client = pool.pop();
let database = client.get_database("rust_test"); let database = client.get_database("rust_test");
@ -68,7 +70,7 @@ fn test_tailing_cursor() {
let cursor = collection.tail(doc!{}, None, None); let cursor = collection.tail(doc!{}, None, None);
let mut counter = 0usize; let mut counter = 0usize;
for result in cursor.into_iter() { for result in cursor.into_iter() {
assert!(result.is_ok()); result.expect("Error tailing");
counter += 1; counter += 1;
if counter == 25 { if counter == 25 {
break; break;
@ -91,10 +93,9 @@ fn test_tailing_cursor() {
assert_eq!(25, guard.join().expect("Thread failed")); assert_eq!(25, guard.join().expect("Thread failed"));
} }
#[cfg_attr(target_os = "windows", ignore)]
#[test] #[test]
fn test_batch_cursor() { fn test_batch_cursor() {
let uri = Uri::new("mongodb://localhost:27017/").unwrap(); let uri = Uri::new(helpers::mongodb_test_connection_string()).unwrap();
let pool = Arc::new(ClientPool::new(uri, None)); let pool = Arc::new(ClientPool::new(uri, None));
let client = pool.pop(); let client = pool.pop();
let database = client.get_database("rust_test"); let database = client.get_database("rust_test");
@ -115,11 +116,10 @@ fn test_batch_cursor() {
bulk_operation.insert(&doc!{"key": i}).unwrap(); bulk_operation.insert(&doc!{"key": i}).unwrap();
} }
let result = bulk_operation.execute(); let result = bulk_operation.execute().expect("Could not execute bulk operation");
assert!(result.is_ok());
assert_eq!( assert_eq!(
result.ok().unwrap().get("nInserted").unwrap(), // why is this an i32? result.get("nInserted").unwrap(), // why is this an i32?
&bson::Bson::Int32(NUM_TO_TEST) &bson::Bson::Int32(NUM_TO_TEST)
); );
assert_eq!(NUM_TO_TEST as i64, collection.count(&doc!{}, None).unwrap()); assert_eq!(NUM_TO_TEST as i64, collection.count(&doc!{}, None).unwrap());

@ -1,14 +1,15 @@
extern crate bson; extern crate bson;
extern crate mongo_driver; extern crate mongo_driver;
mod helpers;
use bson::doc; use bson::doc;
use mongo_driver::client::{ClientPool,Uri}; use mongo_driver::client::{ClientPool,Uri};
#[cfg_attr(target_os = "windows", ignore)]
#[test] #[test]
fn test_command() { fn test_command() {
let uri = Uri::new("mongodb://localhost:27017/").unwrap(); let uri = Uri::new(helpers::mongodb_test_connection_string()).unwrap();
let pool = ClientPool::new(uri, None); let pool = ClientPool::new(uri, None);
let client = pool.pop(); let client = pool.pop();
let database = client.get_database("rust_test"); let database = client.get_database("rust_test");
@ -21,7 +22,7 @@ fn test_command() {
#[test] #[test]
fn test_command_simple() { fn test_command_simple() {
let uri = Uri::new("mongodb://localhost:27017/").unwrap(); let uri = Uri::new(helpers::mongodb_test_connection_string()).unwrap();
let pool = ClientPool::new(uri, None); let pool = ClientPool::new(uri, None);
let client = pool.pop(); let client = pool.pop();
let database = client.get_database("rust_test"); let database = client.get_database("rust_test");
@ -34,7 +35,7 @@ fn test_command_simple() {
#[test] #[test]
fn test_get_collection_and_name() { fn test_get_collection_and_name() {
let uri = Uri::new("mongodb://localhost:27017/").unwrap(); let uri = Uri::new(helpers::mongodb_test_connection_string()).unwrap();
let pool = ClientPool::new(uri, None); let pool = ClientPool::new(uri, None);
let client = pool.pop(); let client = pool.pop();
let database = client.get_database("rust_test"); let database = client.get_database("rust_test");
@ -47,7 +48,7 @@ fn test_get_collection_and_name() {
#[test] #[test]
fn test_create_collection() { fn test_create_collection() {
let uri = Uri::new("mongodb://localhost:27017/").unwrap(); let uri = Uri::new(helpers::mongodb_test_connection_string()).unwrap();
let pool = ClientPool::new(uri, None); let pool = ClientPool::new(uri, None);
let client = pool.pop(); let client = pool.pop();
let database = client.get_database("rust_test"); let database = client.get_database("rust_test");
@ -63,7 +64,7 @@ fn test_create_collection() {
#[test] #[test]
fn test_has_collection() { fn test_has_collection() {
let uri = Uri::new("mongodb://localhost:27017/").unwrap(); let uri = Uri::new(helpers::mongodb_test_connection_string()).unwrap();
let pool = ClientPool::new(uri, None); let pool = ClientPool::new(uri, None);
let client = pool.pop(); let client = pool.pop();
let database = client.get_database("rust_test"); let database = client.get_database("rust_test");

@ -0,0 +1,8 @@
use std::env;
pub fn mongodb_test_connection_string() -> &'static str {
match env::var("MONGODB_CONNECTION_STRING") {
Ok(value) => Box::leak(value.into_boxed_str()),
Err(_) => "mongodb://localhost:27017",
}
}
Loading…
Cancel
Save