|
| 1 | +use crate::schema::versions; |
| 2 | +use crate::tests::builders::PublishBuilder; |
| 3 | +use crate::tests::util::{RequestHelper, TestApp}; |
| 4 | +use claims::{assert_ok, assert_some}; |
| 5 | +use diesel::QueryDsl; |
| 6 | +use diesel_async::RunQueryDsl; |
| 7 | +use insta::{assert_json_snapshot, assert_snapshot}; |
| 8 | + |
| 9 | +const README_CONTENT: &str = |
| 10 | + "# Analysis Test Crate\n\nThis is a test crate for linecount analysis."; |
| 11 | + |
| 12 | +const MAIN_RS_CONTENT: &str = r#"//! Main binary for analysis test crate |
| 13 | +//! |
| 14 | +//! This is a simple hello world program. |
| 15 | +
|
| 16 | +fn main() { |
| 17 | + println!("Hello, world!"); |
| 18 | + let x = 42; |
| 19 | + let y = x + 1; |
| 20 | + println!("The answer is: {}", y); |
| 21 | +} |
| 22 | +
|
| 23 | +#[cfg(test)] |
| 24 | +mod tests { |
| 25 | + #[test] |
| 26 | + fn it_works() { |
| 27 | + assert_eq!(2 + 2, 4); |
| 28 | + } |
| 29 | +} |
| 30 | +"#; |
| 31 | + |
| 32 | +const LIB_RS_CONTENT: &str = r#"//! Analysis test library |
| 33 | +//! |
| 34 | +//! Contains utility functions for testing. |
| 35 | +
|
| 36 | +/// Adds two numbers together |
| 37 | +pub fn add(a: i32, b: i32) -> i32 { |
| 38 | + a + b |
| 39 | +} |
| 40 | +
|
| 41 | +/// Multiplies two numbers |
| 42 | +pub fn multiply(a: i32, b: i32) -> i32 { |
| 43 | + a * b |
| 44 | +} |
| 45 | +
|
| 46 | +#[cfg(test)] |
| 47 | +mod tests { |
| 48 | + use super::*; |
| 49 | +
|
| 50 | + #[test] |
| 51 | + fn test_add() { |
| 52 | + assert_eq!(add(2, 3), 5); |
| 53 | + } |
| 54 | +
|
| 55 | + #[test] |
| 56 | + fn test_multiply() { |
| 57 | + assert_eq!(multiply(4, 5), 20); |
| 58 | + } |
| 59 | +} |
| 60 | +"#; |
| 61 | + |
| 62 | +const INTEGRATION_TEST_CONTENT: &str = r#"//! Integration tests for analysis_test crate |
| 63 | +
|
| 64 | +use analysis_test::{add, multiply}; |
| 65 | +
|
| 66 | +#[test] |
| 67 | +fn integration_test_add() { |
| 68 | + assert_eq!(add(10, 15), 25); |
| 69 | +} |
| 70 | +
|
| 71 | +#[test] |
| 72 | +fn integration_test_multiply() { |
| 73 | + assert_eq!(multiply(3, 7), 21); |
| 74 | +} |
| 75 | +
|
| 76 | +#[test] |
| 77 | +fn combined_operations() { |
| 78 | + let result = multiply(add(2, 3), 4); |
| 79 | + assert_eq!(result, 20); |
| 80 | +} |
| 81 | +"#; |
| 82 | + |
| 83 | +#[tokio::test(flavor = "multi_thread")] |
| 84 | +async fn test_crate_files_are_analyzed() { |
| 85 | + let (app, _, _, token) = TestApp::full().with_token().await; |
| 86 | + let mut conn = app.db_conn().await; |
| 87 | + |
| 88 | + // Create a crate with multiple source files of different types |
| 89 | + let crate_to_publish = PublishBuilder::new("analysis_test", "1.0.0") |
| 90 | + .readme(README_CONTENT) |
| 91 | + .add_file("analysis_test-1.0.0/src/main.rs", MAIN_RS_CONTENT) |
| 92 | + .add_file("analysis_test-1.0.0/src/lib.rs", LIB_RS_CONTENT) |
| 93 | + .add_file( |
| 94 | + "analysis_test-1.0.0/tests/integration_test.rs", |
| 95 | + INTEGRATION_TEST_CONTENT, |
| 96 | + ); |
| 97 | + |
| 98 | + let response = token.publish_crate(crate_to_publish).await; |
| 99 | + assert_snapshot!(response.status(), @"200 OK"); |
| 100 | + |
| 101 | + let result = versions::table |
| 102 | + .select(versions::linecounts) |
| 103 | + .first(&mut conn) |
| 104 | + .await; |
| 105 | + |
| 106 | + let linecount_data: Option<serde_json::Value> = assert_ok!(result); |
| 107 | + let linecount_data = assert_some!(linecount_data); |
| 108 | + assert_json_snapshot!(linecount_data); |
| 109 | +} |
0 commit comments