Skip to content

Update to latest mongodb driver and add tests #7

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Oct 5, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,18 @@ jobs:
runs-on: ${{ matrix.os }}
strategy:
matrix:
os: [ubuntu-latest, windows-latest, macOS-latest]
os: [ubuntu-latest]
rust: [stable]
mongodb-version: [4.2]

steps:
- uses: actions/checkout@master


- name: Start MongoDB
uses: supercharge/mongodb-github-action@1.3.0
with:
mongodb-version: ${{ matrix.mongodb-version }}

- name: Install ${{ matrix.rust }}
uses: actions-rs/toolchain@v1
with:
Expand Down
5 changes: 3 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,12 @@ authors = [
[features]

[dependencies]
# mongodb = { version = "1.0.0", default-features = false, features = ["async-std-runtime"] }
mongodb = { git = "https://github.com/saghm/mongo-rust-driver", default-features = false, branch = "RUST-452", features = ["async-std-runtime"] }
mongodb = { version = "1.1.1", default-features = false, features = ["async-std-runtime"] }
async-session = "2.0.0"
async-trait = "0.1.36"
serde_json = "1.0.56"

[dev-dependencies]
async-std = { version = "1.6.2", features = ["attributes"] }
rand = {version = "0.7.3"}
lazy_static = "1"
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,24 @@
```sh
$ cargo add async-mongodb-session
```
## Test

The tests rely on an running instance of mongodb either on your local machine or remote.
The quickest way to get an instance up and running locally is using the following docker command:

```
$ docker run -d -p 27017:27017 -v ~/data:/data/db mongo:4.2
```

The tests can then be executed with
```
$ cargo test
```

The default settings for the mongodb instance is set to 127.0.0.1:27017 but that can be over ridden by setting the HOST and PORT environment variables.
```
$ HOST=mymongo.com PORT=1234 cargo test
```

## Safety
This crate uses ``#![deny(unsafe_code)]`` to ensure everything is implemented in
Expand Down
33 changes: 32 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@
//! # Examples
//!
//! ```
//! // tbi
//! use async_mongodb_session::*;
//! use async_session::{Session, SessionStore};
//!
//! # fn main() -> async_session::Result { async_std::task::block_on(async {
//! let store = MongodbSessionStore::connect("mongodb://127.0.0.1:27017", "db_name", "collection");
//! # Ok(()) }) }
//! ```

#![forbid(unsafe_code, future_incompatible, rust_2018_idioms)]
Expand All @@ -26,12 +31,38 @@ pub struct MongodbSessionStore {

impl MongodbSessionStore {
/// Create a new instance of `MongodbSessionStore`.
/// ```rust
/// # fn main() -> async_session::Result { async_std::task::block_on(async {
/// # use async_mongodb_session::MongodbSessionStore;
/// let store =
/// MongodbSessionStore::connect("mongodb://127.0.0.1:27017", "db_name", "collection")
/// .await?;
/// # Ok(()) }) }
/// ```
pub async fn connect(uri: &str, db: &str, coll_name: &str) -> mongodb::error::Result<Self> {
let client = Client::with_uri_str(uri).await?;
Ok(Self::from_client(client, db, coll_name))
}

/// Create a new instance of `MongodbSessionStore` from an open client.
/// ```rust
/// use mongodb::{options::ClientOptions, Client};
///
/// # fn main() -> async_session::Result { async_std::task::block_on(async {
/// # use async_mongodb_session::MongodbSessionStore;
/// let client_options = match ClientOptions::parse("mongodb://127.0.0.1:27017").await {
/// Ok(c) => c,
/// Err(e) => panic!("Client Options Failed: {}", e),
/// };

/// let client = match Client::with_options(client_options) {
/// Ok(c) => c,
/// Err(e) => panic!("Client Creation Failed: {}", e),
/// };

/// let store = MongodbSessionStore::from_client(client, "db_name", "collection");
/// # Ok(()) }) }
/// ```
pub fn from_client(client: Client, db: &str, coll_name: &str) -> Self {
Self {
client,
Expand Down
65 changes: 65 additions & 0 deletions tests/test.rs
Original file line number Diff line number Diff line change
@@ -1 +1,66 @@
#[cfg(test)]
mod tests {
use async_mongodb_session::*;
use async_session::{Session, SessionStore};
use lazy_static::lazy_static;
use mongodb::{options::ClientOptions, Client};
use rand::Rng;
use std::env;

lazy_static! {
static ref HOST: String = env::var("HOST").unwrap_or_else(|_| "127.0.0.1".to_string());
static ref PORT: String = env::var("PORT").unwrap_or_else(|_| "27017".to_string());
static ref CONNECTION_STRING: String =
format!("mongodb://{}:{}/", HOST.as_str(), PORT.as_str());
}

#[test]
fn test_from_client() -> async_session::Result {
async_std::task::block_on(async {
let client_options = match ClientOptions::parse(&CONNECTION_STRING).await {
Ok(c) => c,
Err(e) => panic!("Client Options Failed: {}", e),
};

let client = match Client::with_options(client_options) {
Ok(c) => c,
Err(e) => panic!("Client Creation Failed: {}", e),
};

let store = MongodbSessionStore::from_client(client, "db_name", "collection");
let mut rng = rand::thread_rng();
let n2: u16 = rng.gen();
let key = format!("key-{}", n2);
let value = format!("value-{}", n2);
let mut session = Session::new();
session.insert(&key, &value)?;

let cookie_value = store.store_session(session).await?.unwrap();
let session = store.load_session(cookie_value).await?.unwrap();
assert_eq!(&session.get::<String>(&key).unwrap(), &value);

Ok(())
})
}

#[test]
fn test_connect() -> async_session::Result {
async_std::task::block_on(async {
let store =
MongodbSessionStore::connect(&CONNECTION_STRING, "db_name", "collection").await?;

let mut rng = rand::thread_rng();
let n2: u16 = rng.gen();
let key = format!("key-{}", n2);
let value = format!("value-{}", n2);
let mut session = Session::new();
session.insert(&key, &value)?;

let cookie_value = store.store_session(session).await?.unwrap();
let session = store.load_session(cookie_value).await?.unwrap();
assert_eq!(&session.get::<String>(&key).unwrap(), &value);

Ok(())
})
}
}