|
| 1 | +mod common; |
| 2 | + |
| 3 | +use tokio::fs; |
| 4 | + |
| 5 | +use ndc_bigquery_cli::*; |
| 6 | +use ndc_bigquery_configuration as configuration; |
| 7 | +use ndc_bigquery_configuration::ParsedConfiguration; |
| 8 | + |
| 9 | +#[tokio::test] |
| 10 | +async fn test_initialize_directory() -> anyhow::Result<()> { |
| 11 | + let dir = tempfile::tempdir()?; |
| 12 | + |
| 13 | + let context = Context { |
| 14 | + context_path: dir.path().to_owned(), |
| 15 | + environment: configuration::environment::EmptyEnvironment, |
| 16 | + release_version: None, |
| 17 | + }; |
| 18 | + run( |
| 19 | + Command::Initialize { |
| 20 | + with_metadata: false, |
| 21 | + }, |
| 22 | + context, |
| 23 | + ) |
| 24 | + .await?; |
| 25 | + |
| 26 | + let configuration_schema_file_path = dir.path().join("schema.json"); |
| 27 | + assert!(configuration_schema_file_path.exists()); |
| 28 | + common::assert_file_ends_with_newline(&configuration_schema_file_path).await?; |
| 29 | + |
| 30 | + let configuration_file_path = dir.path().join("configuration.json"); |
| 31 | + assert!(configuration_file_path.exists()); |
| 32 | + let contents = fs::read_to_string(configuration_file_path).await?; |
| 33 | + common::assert_ends_with_newline(&contents); |
| 34 | + let _: ParsedConfiguration = configuration::parse_configuration(&dir).await?; |
| 35 | + |
| 36 | + let metadata_file_path = dir |
| 37 | + .path() |
| 38 | + .join(".hasura-connector") |
| 39 | + .join("connector-metadata.yaml"); |
| 40 | + assert!(!metadata_file_path.exists()); |
| 41 | + |
| 42 | + Ok(()) |
| 43 | +} |
| 44 | + |
| 45 | +#[tokio::test] |
| 46 | +async fn test_initialize_version_is_unchanged() -> anyhow::Result<()> { |
| 47 | + let dir = tempfile::tempdir()?; |
| 48 | + |
| 49 | + let context = Context { |
| 50 | + context_path: dir.path().to_owned(), |
| 51 | + environment: configuration::environment::EmptyEnvironment, |
| 52 | + release_version: None, |
| 53 | + }; |
| 54 | + run( |
| 55 | + Command::Initialize { |
| 56 | + with_metadata: false, |
| 57 | + }, |
| 58 | + context, |
| 59 | + ) |
| 60 | + .await?; |
| 61 | + |
| 62 | + let configuration_file_path = dir.path().join("configuration.json"); |
| 63 | + assert!(configuration_file_path.exists()); |
| 64 | + let configuration_value: serde_json::Value = |
| 65 | + serde_json::from_str(fs::read_to_string(configuration_file_path).await?.as_str())?; |
| 66 | + |
| 67 | + let version = configuration_value |
| 68 | + .as_object() |
| 69 | + .unwrap() |
| 70 | + .get("version") |
| 71 | + .unwrap(); |
| 72 | + |
| 73 | + insta::assert_snapshot!(version.to_string()); |
| 74 | + Ok(()) |
| 75 | +} |
| 76 | + |
| 77 | +#[tokio::test] |
| 78 | +async fn test_do_not_initialize_when_files_already_exist() -> anyhow::Result<()> { |
| 79 | + let dir = tempfile::tempdir()?; |
| 80 | + fs::write( |
| 81 | + dir.path().join("random.file"), |
| 82 | + "this directory is no longer empty", |
| 83 | + ) |
| 84 | + .await?; |
| 85 | + |
| 86 | + let context = Context { |
| 87 | + context_path: dir.path().to_owned(), |
| 88 | + environment: configuration::environment::EmptyEnvironment, |
| 89 | + release_version: None, |
| 90 | + }; |
| 91 | + match run( |
| 92 | + Command::Initialize { |
| 93 | + with_metadata: false, |
| 94 | + }, |
| 95 | + context, |
| 96 | + ) |
| 97 | + .await |
| 98 | + { |
| 99 | + Ok(()) => panic!("Expected the command to fail."), |
| 100 | + Err(error) => match error.downcast::<Error>() { |
| 101 | + Err(input) => panic!("Expected a CLI error, but got {input}"), |
| 102 | + Ok(cli_error) => assert_eq!(cli_error, Error::DirectoryIsNotEmpty), |
| 103 | + }, |
| 104 | + } |
| 105 | + |
| 106 | + Ok(()) |
| 107 | +} |
| 108 | + |
| 109 | +#[tokio::test] |
| 110 | +async fn test_initialize_directory_with_metadata() -> anyhow::Result<()> { |
| 111 | + let dir = tempfile::tempdir()?; |
| 112 | + |
| 113 | + let context = Context { |
| 114 | + context_path: dir.path().to_owned(), |
| 115 | + environment: configuration::environment::EmptyEnvironment, |
| 116 | + release_version: None, |
| 117 | + }; |
| 118 | + run( |
| 119 | + Command::Initialize { |
| 120 | + with_metadata: true, |
| 121 | + }, |
| 122 | + context, |
| 123 | + ) |
| 124 | + .await?; |
| 125 | + |
| 126 | + let configuration_schema_file_path = dir.path().join("schema.json"); |
| 127 | + assert!(configuration_schema_file_path.exists()); |
| 128 | + common::assert_file_ends_with_newline(&configuration_schema_file_path).await?; |
| 129 | + |
| 130 | + let configuration_file_path = dir.path().join("configuration.json"); |
| 131 | + assert!(configuration_file_path.exists()); |
| 132 | + common::assert_file_ends_with_newline(&configuration_file_path).await?; |
| 133 | + |
| 134 | + let metadata_file_path = dir |
| 135 | + .path() |
| 136 | + .join(".hasura-connector") |
| 137 | + .join("connector-metadata.yaml"); |
| 138 | + assert!(metadata_file_path.exists()); |
| 139 | + let contents = fs::read_to_string(metadata_file_path).await?; |
| 140 | + common::assert_ends_with_newline(&contents); |
| 141 | + insta::assert_snapshot!(contents); |
| 142 | + |
| 143 | + Ok(()) |
| 144 | +} |
| 145 | + |
| 146 | +#[tokio::test] |
| 147 | +async fn test_initialize_directory_with_metadata_and_release_version() -> anyhow::Result<()> { |
| 148 | + let dir = tempfile::tempdir()?; |
| 149 | + |
| 150 | + let context = Context { |
| 151 | + context_path: dir.path().to_owned(), |
| 152 | + environment: configuration::environment::EmptyEnvironment, |
| 153 | + release_version: Some("v1.2.3"), |
| 154 | + }; |
| 155 | + run( |
| 156 | + Command::Initialize { |
| 157 | + with_metadata: true, |
| 158 | + }, |
| 159 | + context, |
| 160 | + ) |
| 161 | + .await?; |
| 162 | + |
| 163 | + let configuration_schema_file_path = dir.path().join("schema.json"); |
| 164 | + assert!(configuration_schema_file_path.exists()); |
| 165 | + common::assert_file_ends_with_newline(&configuration_schema_file_path).await?; |
| 166 | + |
| 167 | + let configuration_file_path = dir.path().join("configuration.json"); |
| 168 | + assert!(configuration_file_path.exists()); |
| 169 | + common::assert_file_ends_with_newline(&configuration_file_path).await?; |
| 170 | + |
| 171 | + let metadata_file_path = dir |
| 172 | + .path() |
| 173 | + .join(".hasura-connector") |
| 174 | + .join("connector-metadata.yaml"); |
| 175 | + assert!(metadata_file_path.exists()); |
| 176 | + let contents = fs::read_to_string(metadata_file_path).await?; |
| 177 | + common::assert_ends_with_newline(&contents); |
| 178 | + insta::assert_snapshot!(contents); |
| 179 | + |
| 180 | + Ok(()) |
| 181 | +} |
0 commit comments