|
| 1 | +use crate::schema::trustpub_configs_gitlab; |
| 2 | +use chrono::{DateTime, Utc}; |
| 3 | +use diesel::prelude::*; |
| 4 | +use diesel_async::{AsyncPgConnection, RunQueryDsl}; |
| 5 | +use serde::Serialize; |
| 6 | + |
| 7 | +#[derive(Debug, Identifiable, Queryable, Selectable, Serialize)] |
| 8 | +#[diesel(table_name = trustpub_configs_gitlab, check_for_backend(diesel::pg::Pg))] |
| 9 | +pub struct GitLabConfig { |
| 10 | + pub id: i32, |
| 11 | + pub created_at: DateTime<Utc>, |
| 12 | + pub crate_id: i32, |
| 13 | + pub namespace: String, |
| 14 | + pub namespace_id: Option<String>, |
| 15 | + pub project: String, |
| 16 | + pub workflow_filepath: String, |
| 17 | + pub environment: Option<String>, |
| 18 | +} |
| 19 | + |
| 20 | +#[derive(Debug, Insertable)] |
| 21 | +#[diesel(table_name = trustpub_configs_gitlab, check_for_backend(diesel::pg::Pg))] |
| 22 | +pub struct NewGitLabConfig<'a> { |
| 23 | + pub crate_id: i32, |
| 24 | + pub namespace: &'a str, |
| 25 | + pub project: &'a str, |
| 26 | + pub workflow_filepath: &'a str, |
| 27 | + pub environment: Option<&'a str>, |
| 28 | +} |
| 29 | + |
| 30 | +impl NewGitLabConfig<'_> { |
| 31 | + pub async fn insert(&self, conn: &mut AsyncPgConnection) -> QueryResult<GitLabConfig> { |
| 32 | + self.insert_into(trustpub_configs_gitlab::table) |
| 33 | + .returning(GitLabConfig::as_returning()) |
| 34 | + .get_result(conn) |
| 35 | + .await |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +#[cfg(test)] |
| 40 | +mod tests { |
| 41 | + use super::*; |
| 42 | + use crate::models::krate::*; |
| 43 | + use crate::schema::crates; |
| 44 | + use crates_io_test_db::TestDatabase; |
| 45 | + use diesel_async::RunQueryDsl; |
| 46 | + use insta::assert_debug_snapshot; |
| 47 | + |
| 48 | + #[tokio::test] |
| 49 | + async fn test_gitlab_config_insert_and_retrieve() { |
| 50 | + let test_db = TestDatabase::new(); |
| 51 | + let mut conn = test_db.async_connect().await; |
| 52 | + |
| 53 | + // Create a test crate first |
| 54 | + let test_crate = diesel::insert_into(crates::table) |
| 55 | + .values((crates::name.eq("test-crate"),)) |
| 56 | + .returning(Crate::as_returning()) |
| 57 | + .get_result(&mut conn) |
| 58 | + .await |
| 59 | + .unwrap(); |
| 60 | + |
| 61 | + // Create a new GitLab config |
| 62 | + let new_config = NewGitLabConfig { |
| 63 | + crate_id: test_crate.id, |
| 64 | + namespace: "rust-lang", |
| 65 | + project: "cargo", |
| 66 | + workflow_filepath: ".gitlab-ci.yml", |
| 67 | + environment: Some("production"), |
| 68 | + }; |
| 69 | + |
| 70 | + // Insert the config |
| 71 | + let inserted_config = new_config.insert(&mut conn).await.unwrap(); |
| 72 | + |
| 73 | + // Retrieve the config |
| 74 | + let retrieved_config = trustpub_configs_gitlab::table |
| 75 | + .filter(trustpub_configs_gitlab::id.eq(inserted_config.id)) |
| 76 | + .first::<GitLabConfig>(&mut conn) |
| 77 | + .await |
| 78 | + .unwrap(); |
| 79 | + |
| 80 | + // Snapshot test the structure |
| 81 | + insta::with_settings!({ filters => vec![(r"\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(?:\.\d+)?Z", "[datetime]")] }, { |
| 82 | + assert_debug_snapshot!(retrieved_config, @r#" |
| 83 | + GitLabConfig { |
| 84 | + id: 1, |
| 85 | + created_at: [datetime], |
| 86 | + crate_id: 1, |
| 87 | + namespace: "rust-lang", |
| 88 | + namespace_id: None, |
| 89 | + project: "cargo", |
| 90 | + workflow_filepath: ".gitlab-ci.yml", |
| 91 | + environment: Some( |
| 92 | + "production", |
| 93 | + ), |
| 94 | + } |
| 95 | + "#); |
| 96 | + }); |
| 97 | + } |
| 98 | +} |
0 commit comments