aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorBryan Newbold <bnewbold@robocracy.org>2018-05-23 22:24:52 -0700
committerBryan Newbold <bnewbold@robocracy.org>2018-05-24 15:21:32 -0700
commit32e69d59150a2badfd72c62753414cbb1eb37c73 (patch)
treec8d355272f23abf20fbcdf6b583ad5740282d58e
parent402cf5ebbd87b4e4de2d5ba83a97e7f9b014dfc9 (diff)
downloadfatcat-32e69d59150a2badfd72c62753414cbb1eb37c73.tar.gz
fatcat-32e69d59150a2badfd72c62753414cbb1eb37c73.zip
basic test for editgroup accept
-rw-r--r--rust/src/lib.rs31
-rw-r--r--rust/tests/test_api_server.rs73
2 files changed, 88 insertions, 16 deletions
diff --git a/rust/src/lib.rs b/rust/src/lib.rs
index 4e1dc8ba..c12575a9 100644
--- a/rust/src/lib.rs
+++ b/rust/src/lib.rs
@@ -36,7 +36,7 @@ pub use errors::*;
pub use self::errors::*;
use diesel::pg::PgConnection;
use diesel::prelude::*;
-use diesel::r2d2::{ConnectionManager, ManageConnection};
+use diesel::r2d2::ConnectionManager;
use dotenv::dotenv;
use iron::middleware::AfterMiddleware;
use iron::{Request, Response};
@@ -59,25 +59,30 @@ pub fn establish_connection() -> PgConnection {
/// Instantiate a new API server with a pooled database connection
pub fn server() -> Result<api_server::Server> {
dotenv().ok();
- let database_url = if cfg!(test) {
- env::var("DATABASE_URL").expect("DATABASE_URL must be set")
- } else {
- env::var("TEST_DATABASE_URL").expect("TEST_DATABASE_URL must be set")
- };
+ let database_url = env::var("DATABASE_URL").expect("DATABASE_URL must be set");
let manager = ConnectionManager::<PgConnection>::new(database_url);
- if cfg!(test) {
- // run migrations; revert latest (dummy data); re-run latest
- let conn = manager.connect().unwrap();
- diesel_migrations::run_pending_migrations(&conn).unwrap();
- diesel_migrations::revert_latest_migration(&conn).unwrap();
- diesel_migrations::run_pending_migrations(&conn).unwrap();
- }
let pool = diesel::r2d2::Pool::builder()
.build(manager)
.expect("Failed to create database pool.");
Ok(api_server::Server { db_pool: pool })
}
+pub fn test_server() -> Result<api_server::Server> {
+
+ dotenv().ok();
+ let database_url = env::var("TEST_DATABASE_URL").expect("TEST_DATABASE_URL must be set");
+ env::set_var("DATABASE_URL", database_url);
+
+ let server = server()?;
+ let conn = server.db_pool.get().expect("db_pool error");
+
+ // run migrations; revert latest (dummy data); re-run latest
+ diesel_migrations::run_pending_migrations(&conn).unwrap();
+ diesel_migrations::revert_latest_migration(&conn).unwrap();
+ diesel_migrations::run_pending_migrations(&conn).unwrap();
+ Ok(server)
+}
+
/// HTTP header middleware
header! { (XClacksOverhead, "X-Clacks-Overhead") => [String] }
diff --git a/rust/tests/test_api_server.rs b/rust/tests/test_api_server.rs
index 4c16d4df..6126a0a2 100644
--- a/rust/tests/test_api_server.rs
+++ b/rust/tests/test_api_server.rs
@@ -2,7 +2,11 @@ extern crate fatcat;
extern crate fatcat_api;
extern crate iron;
extern crate iron_test;
+extern crate diesel;
+use fatcat::database_schema::*;
+use fatcat::api_helpers::*;
+use diesel::prelude::*;
use iron::{status, Headers};
use iron::mime::Mime;
use iron::headers::ContentType;
@@ -10,7 +14,7 @@ use iron_test::{request, response};
#[test]
fn test_basics() {
- let server = fatcat::server().unwrap();
+ let server = fatcat::test_server().unwrap();
let router = fatcat_api::router(server);
let response = request::get(
@@ -32,7 +36,7 @@ fn test_basics() {
#[test]
fn test_lookups() {
- let server = fatcat::server().unwrap();
+ let server = fatcat::test_server().unwrap();
let router = fatcat_api::router(server);
let response = request::get(
@@ -56,7 +60,7 @@ fn test_lookups() {
#[test]
fn test_post_container() {
- let server = fatcat::server().unwrap();
+ let server = fatcat::test_server().unwrap();
let router = fatcat_api::router(server);
let mut headers = Headers::new();
let mime: Mime = "application/json".parse().unwrap();
@@ -74,3 +78,66 @@ fn test_post_container() {
println!("{}", body);
//assert!(body.contains("test journal"));
}
+
+#[test]
+fn test_accept_editgroup() {
+ let server = fatcat::test_server().unwrap();
+ let conn = server.db_pool.get().expect("db_pool error");
+ let router = fatcat_api::router(server);
+ let mut headers = Headers::new();
+ let mime: Mime = "application/json".parse().unwrap();
+ headers.set(ContentType(mime));
+
+ let editgroup_id = get_or_create_editgroup(1, &conn).unwrap();
+
+ let c: i64 = container_ident::table
+ .filter(container_ident::is_live.eq(false))
+ .count()
+ .get_result(&conn).unwrap();
+ assert_eq!(c, 0);
+ let c: i64 = changelog::table
+ .filter(changelog::editgroup_id.eq(editgroup_id))
+ .count()
+ .get_result(&conn).unwrap();
+ assert_eq!(c, 0);
+
+ let response = request::post(
+ "http://localhost:9411/v0/container",
+ headers.clone(),
+ &format!("{{\"name\": \"test journal 1\", \"editgroup_id\": {}}}", editgroup_id),
+ &router,
+ ).unwrap();
+ assert_eq!(response.status, Some(status::Created));
+ let response = request::post(
+ "http://localhost:9411/v0/container",
+ headers.clone(),
+ &format!("{{\"name\": \"test journal 2\", \"editgroup_id\": {}}}", editgroup_id),
+ &router,
+ ).unwrap();
+ assert_eq!(response.status, Some(status::Created));
+
+ let c: i64 = container_ident::table
+ .filter(container_ident::is_live.eq(false))
+ .count()
+ .get_result(&conn).unwrap();
+ assert_eq!(c, 2);
+
+ let response = request::post(
+ &format!("http://localhost:9411/v0/editgroup/{}/accept", editgroup_id),
+ headers.clone(),
+ "",
+ &router,
+ ).unwrap();
+ assert_eq!(response.status, Some(status::Ok));
+
+ let c: i64 = container_ident::table
+ .filter(container_ident::is_live.eq(false))
+ .count()
+ .get_result(&conn).unwrap();
+ assert_eq!(c, 0);
+ let c: i64 = changelog::table
+ .filter(changelog::editgroup_id.eq(editgroup_id))
+ .count()
+ .get_result(&conn).unwrap();
+ assert_eq!(c, 1);
+}