Skip to content

Commit

Permalink
Fix integration test.
Browse files Browse the repository at this point in the history
  • Loading branch information
LouisGariepy committed Feb 8, 2023
1 parent 2ec1ee9 commit f9005a7
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 18 deletions.
18 changes: 10 additions & 8 deletions test_integration/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ pub(crate) fn run_errors_test(
for mut suite in test_suites {
println!("{} {}", "[error]".magenta(), suite.name.magenta());
for test in suite.tests.iter_mut() {
// Reset db
reset_db(client)?;

// Generate file tree path
let temp_dir = tempfile::tempdir()?;

Expand All @@ -29,17 +32,17 @@ pub(crate) fn run_errors_test(
"schema.sql",
[
"CREATE TABLE author (id SERIAL, name TEXT);\n",
&test.schema,
test.schema.as_deref().unwrap_or_default(),
]
.concat(),
)?;

// Generate queries files
std::fs::create_dir("queries")?;
std::fs::write("queries/test.sql", &test.query)?;

// Reset db
reset_db(client)?;
std::fs::write(
"queries/test.sql",
test.query.as_deref().unwrap_or_default(),
)?;

// Run codegen
let result = cornucopia::load_schema(client, &["schema.sql"])
Expand Down Expand Up @@ -85,10 +88,9 @@ pub(crate) fn run_errors_test(
std::env::set_current_dir(&original_pwd)?;
}

// Update error message if needed
if apply {
// Format test descriptor and update error message if needed
let edited = toml::to_string_pretty(&suite.tests)?;
std::fs::write(suite.path, edited)?;
suite.write()?;
}
}

Expand Down
31 changes: 21 additions & 10 deletions test_integration/src/fixtures.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
use std::path::{Path, PathBuf};
use std::{
error::Error,
path::{Path, PathBuf},
};

use cornucopia::CodegenSettings;
use serde::{de::DeserializeOwned, Deserialize, Serialize};

#[derive(Deserialize)]
struct TestSuiteDeserializer<T> {
test: Vec<T>,
#[derive(Serialize, Deserialize)]
struct TestSuiteSerde<T> {
#[serde(rename = "test")]
tests: Vec<T>,
}

pub struct TestSuite<T> {
Expand All @@ -21,16 +25,25 @@ impl<T: DeserializeOwned> TestSuite<T> {
let name = file.file_name().to_string_lossy().to_string();
let path = file.path();
let content = std::fs::read_to_string(&path).unwrap();
let tests: TestSuiteDeserializer<T> = toml::from_str(&content).unwrap();
let test_suite: TestSuiteSerde<T> = toml::from_str(&content).unwrap();
TestSuite {
name,
tests: tests.test,
tests: test_suite.tests,
path,
}
})
}
}

impl<T: Serialize> TestSuite<T> {
pub(crate) fn write(self) -> Result<(), Box<dyn Error>> {
let suite = TestSuiteSerde { tests: self.tests };
let edited = toml::to_string_pretty(&suite)?;
std::fs::write(self.path, edited)?;
Ok(())
}
}

/// Codegen test case
#[derive(Debug, Deserialize)]
pub(crate) struct CodegenTest {
Expand Down Expand Up @@ -72,10 +85,8 @@ impl From<&CodegenTest> for CodegenSettings {
#[derive(Debug, Deserialize, Serialize)]
pub(crate) struct ErrorTest {
pub(crate) name: String,
#[serde(default)]
pub(crate) query: String,
#[serde(default)]
pub(crate) schema: String,
pub(crate) query: Option<String>,
pub(crate) schema: Option<String>,
pub(crate) error: String,
}

Expand Down

0 comments on commit f9005a7

Please sign in to comment.