|
| 1 | +use cargo_registry::schema::categories; |
| 2 | +use diesel::*; |
| 3 | +use diesel::pg::PgConnection; |
| 4 | +use dotenv::dotenv; |
| 5 | + |
| 6 | +use std::env; |
| 7 | + |
| 8 | +const ALGORITHMS: &'static str = r#" |
| 9 | +[algorithms] |
| 10 | +name = "Algorithms" |
| 11 | +description = """ |
| 12 | +Rust implementations of core algorithms such as hashing, sorting, \ |
| 13 | +searching, and more.\ |
| 14 | +""""#; |
| 15 | + |
| 16 | +const ALGORITHMS_AND_SUCH: &'static str = r#" |
| 17 | +[algorithms] |
| 18 | +name = "Algorithms" |
| 19 | +description = """ |
| 20 | +Rust implementations of core algorithms such as hashing, sorting, \ |
| 21 | +searching, and more.\ |
| 22 | +""" |
| 23 | +
|
| 24 | +[algorithms.categories.such] |
| 25 | +name = "Such" |
| 26 | +description = """ |
| 27 | +Other stuff |
| 28 | +""""#; |
| 29 | + |
| 30 | +const ALGORITHMS_AND_ANOTHER: &'static str = r#" |
| 31 | +[algorithms] |
| 32 | +name = "Algorithms" |
| 33 | +description = """ |
| 34 | +Rust implementations of core algorithms such as hashing, sorting, \ |
| 35 | +searching, and more.\ |
| 36 | +""" |
| 37 | +
|
| 38 | +[another] |
| 39 | +name = "Another" |
| 40 | +description = "Another category ho hum" |
| 41 | +"#; |
| 42 | + |
| 43 | +fn pg_connection() -> PgConnection { |
| 44 | + let _ = dotenv(); |
| 45 | + let database_url = |
| 46 | + env::var("TEST_DATABASE_URL").expect("TEST_DATABASE_URL must be set to run tests"); |
| 47 | + let conn = PgConnection::establish(&database_url).unwrap(); |
| 48 | + conn.begin_test_transaction().unwrap(); |
| 49 | + conn |
| 50 | +} |
| 51 | + |
| 52 | +fn select_slugs(conn: &PgConnection) -> Vec<String> { |
| 53 | + categories::table |
| 54 | + .select(categories::slug) |
| 55 | + .order(categories::slug) |
| 56 | + .load::<String>(conn) |
| 57 | + .unwrap() |
| 58 | +} |
| 59 | + |
| 60 | +#[test] |
| 61 | +fn sync_adds_new_categories() { |
| 62 | + let conn = pg_connection(); |
| 63 | + |
| 64 | + ::cargo_registry::categories::sync_with_connection(ALGORITHMS_AND_SUCH, &conn).unwrap(); |
| 65 | + |
| 66 | + let categories = select_slugs(&conn); |
| 67 | + assert_eq!(categories, vec!["algorithms", "algorithms::such"]); |
| 68 | +} |
| 69 | + |
| 70 | +#[test] |
| 71 | +fn sync_removes_missing_categories() { |
| 72 | + let conn = pg_connection(); |
| 73 | + |
| 74 | + ::cargo_registry::categories::sync_with_connection(ALGORITHMS_AND_SUCH, &conn).unwrap(); |
| 75 | + ::cargo_registry::categories::sync_with_connection(ALGORITHMS, &conn).unwrap(); |
| 76 | + |
| 77 | + let categories = select_slugs(&conn); |
| 78 | + assert_eq!(categories, vec!["algorithms"]); |
| 79 | +} |
| 80 | + |
| 81 | +#[test] |
| 82 | +fn sync_adds_and_removes() { |
| 83 | + let conn = pg_connection(); |
| 84 | + |
| 85 | + ::cargo_registry::categories::sync_with_connection(ALGORITHMS_AND_SUCH, &conn).unwrap(); |
| 86 | + ::cargo_registry::categories::sync_with_connection(ALGORITHMS_AND_ANOTHER, &conn).unwrap(); |
| 87 | + |
| 88 | + let categories = select_slugs(&conn); |
| 89 | + assert_eq!(categories, vec!["algorithms", "another"]); |
| 90 | +} |
0 commit comments