Compare commits

..

No commits in common. 'main' and 'bson_11' have entirely different histories.

1
.gitignore vendored

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

@ -1,21 +0,0 @@
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

@ -1,44 +0,0 @@
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

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

@ -1,36 +1,3 @@
# 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
* Add support for commands that return batches (by MiesJansen)

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

@ -1,5 +1,6 @@
# 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)
Mongo Rust driver built on top of the [Mongo C driver](https://github.com/mongodb/mongo-c-driver).
@ -20,11 +21,10 @@ 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
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 and cmake:
To build on Mac install OpenSSL 1.1:
```
brew install openssl@1.1
brew install cmake
```
Export these env vars the before you make a clean build:

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

@ -1,20 +1,25 @@
extern crate pkg_config;
#[cfg(target_env = "msvc")]
extern crate vcpkg;
use std::env;
use std::path::Path;
use std::process::Command;
fn main() {
let mongoc_version = env!("CARGO_PKG_VERSION")
.split('-')
.next()
.expect("Crate version is not valid");
#[cfg(not(target_env = "msvc"))]
fn lin(mongoc_version: &str) {
use std::path::Path;
use std::process::Command;
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 = Path::new(&out_dir_var);
let driver_src_path = out_dir.join(format!("mongo-c-driver-{}", mongoc_version));
let out_dir = format!("{}/{}", out_dir_var, mongoc_version);
let driver_src_path = format!("mongo-c-driver-{}", mongoc_version);
let libmongoc_path = out_dir.join("usr/local/lib/libmongoc-static-1.0.a");
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!(
@ -24,7 +29,6 @@ fn main() {
);
assert!(
Command::new("curl").arg("-O") // Save to disk
.current_dir(out_dir)
.arg("-L") // Follow redirects
.arg(url)
.status()
@ -35,7 +39,6 @@ fn main() {
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()
@ -43,61 +46,97 @@ fn main() {
.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");
}
// 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");
}
match pkg.probe("zstd") {
Ok(_) => {
cmake.arg("-DENABLE_ZSTD=ON");
},
Err(e) => {
println!("Zstd not found: {}", e);
cmake.arg("-DENABLE_ZSTD=OFF");
// 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());
assert!(
Command::new("make")
.current_dir(&driver_src_path)
.env("CFLAGS", "-DMONGOC_TRACE")
.status()
.expect("Could not run make")
.success()
);
assert!(
Command::new("make")
.arg("install")
.current_dir(&driver_src_path)
.status()
.expect("Could not run make install")
.success()
);
}
cmake.arg("-DENABLE_AUTOMATIC_INIT_AND_CLEANUP=OFF");
cmake.arg("-DENABLE_SSL=OPENSSL");
cmake.arg("-DENABLE_SASL=OFF");
cmake.arg("-DENABLE_STATIC=ON");
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());
// 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");
}
}
#[cfg(target_env = "msvc")]
fn win(_mongoc_version: &str) {
use vcpkg;
let mongo_lib = "mongoc-1.0";
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-search=native={}/usr/local/lib", &out_dir.to_string_lossy());
println!("cargo:rustc-link-lib=static=bson-static-1.0");
println!("cargo:rustc-link-lib=static=mongoc-static-1.0");
println!("cargo:rustc-link-lib=resolv");
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() {
let mongoc_version = env!("CARGO_PKG_VERSION")
.split('-')
.next()
.expect("Crate version is not valid");
#[cfg(target_env = "msvc")]
win(mongoc_version);
#[cfg(not(target_env = "msvc"))]
lin(mongoc_version);
}

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

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

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

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

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

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

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

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

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

@ -1,8 +0,0 @@
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