From 17bd5571a0b222a14b11b4df3197978d90052c0f Mon Sep 17 00:00:00 2001 From: Nicolas Even Date: Wed, 20 Mar 2024 14:16:17 +0100 Subject: [PATCH] Add tests + fix missing newlines in ndjson --- .github/workflows/build.yml | 10 +++- CHANGELOG.md | 5 ++ Cargo.lock | 101 ++++++++++++++++++++++++++++++++++++ Cargo.toml | 1 + src/main.rs | 1 + tests/extract_json.rs | 53 +++++++++++++++++++ 6 files changed, 170 insertions(+), 1 deletion(-) create mode 100644 tests/extract_json.rs diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 7268cfa..b9e81c2 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -1,4 +1,4 @@ -name: Build +name: Push on: push: @@ -18,23 +18,31 @@ jobs: platform: - target: x86_64-unknown-linux-gnu os: ubuntu-22.04 + command: both - target: aarch64-unknown-linux-gnu os: ubuntu-22.04 + command: build - target: aarch64-apple-darwin os: macos-14 + command: build steps: - name: Checkout uses: actions/checkout@v4 + - name: Start PG for the tests + run: docker compose up -d + if: ${{ matrix.platform.command == 'both' }} + - name: Build binary uses: houseabsolute/actions-rust-cross@v0 with: target: ${{ matrix.platform.target }} args: "--release" strip: true + command: ${{ matrix.platform.command }} - name: Upload binary uses: actions/upload-artifact@v4 diff --git a/CHANGELOG.md b/CHANGELOG.md index 2b0f0f3..3a302d6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,8 @@ +## (main) - XXXX-XX-XX + +- Add tests +- Fix missing newlines in NDJSON + ## 0.2.0 - 2024-03-20 - Add linux aarch64 support diff --git a/Cargo.lock b/Cargo.lock index ab2f367..7fe7b93 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -74,6 +74,21 @@ dependencies = [ "windows-sys 0.52.0", ] +[[package]] +name = "assert_cmd" +version = "2.0.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ed72493ac66d5804837f480ab3766c72bdfab91a65e565fc54fa9e42db0073a8" +dependencies = [ + "anstyle", + "bstr", + "doc-comment", + "predicates", + "predicates-core", + "predicates-tree", + "wait-timeout", +] + [[package]] name = "async-trait" version = "0.1.78" @@ -133,6 +148,17 @@ dependencies = [ "generic-array", ] +[[package]] +name = "bstr" +version = "1.9.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "05efc5cfd9110c8416e471df0e96702d58690178e206e61b7173706673c93706" +dependencies = [ + "memchr", + "regex-automata", + "serde", +] + [[package]] name = "bumpalo" version = "3.15.4" @@ -244,6 +270,12 @@ dependencies = [ "typenum", ] +[[package]] +name = "difflib" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6184e33543162437515c2e2b48714794e37845ec9851711914eec9d308f6ebe8" + [[package]] name = "digest" version = "0.10.7" @@ -255,6 +287,12 @@ dependencies = [ "subtle", ] +[[package]] +name = "doc-comment" +version = "0.3.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fea41bba32d969b513997752735605054bc0dfa92b4c56bf1189f2e174be7a10" + [[package]] name = "errno" version = "0.3.8" @@ -628,6 +666,7 @@ checksum = "e3148f5046208a5d56bcfc03053e3ca6334e51da8dfb19b6cdc8b306fae3283e" name = "pgextract" version = "0.2.0" dependencies = [ + "assert_cmd", "clap", "native-tls", "postgres", @@ -733,6 +772,33 @@ version = "0.2.17" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5b40af805b3121feab8a3c29f04d8ad262fa8e0561883e7653e024ae4479e6de" +[[package]] +name = "predicates" +version = "3.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "68b87bfd4605926cdfefc1c3b5f8fe560e3feca9d5552cf68c466d3d8236c7e8" +dependencies = [ + "anstyle", + "difflib", + "predicates-core", +] + +[[package]] +name = "predicates-core" +version = "1.0.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b794032607612e7abeb4db69adb4e33590fa6cf1149e95fd7cb00e634b92f174" + +[[package]] +name = "predicates-tree" +version = "1.0.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "368ba315fb8c5052ab692e68a0eefec6ec57b23a36959c14496f0b0df2c0cecf" +dependencies = [ + "predicates-core", + "termtree", +] + [[package]] name = "proc-macro2" version = "1.0.79" @@ -876,6 +942,26 @@ dependencies = [ "libc", ] +[[package]] +name = "serde" +version = "1.0.197" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3fb1c873e1b9b056a4dc4c0c198b24c3ffa059243875552b2bd0933b1aee4ce2" +dependencies = [ + "serde_derive", +] + +[[package]] +name = "serde_derive" +version = "1.0.197" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7eb0b34b42edc17f6b7cac84a52a1c5f0e1bb2227e997ca9011ea3dd34e8610b" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "sha2" version = "0.10.8" @@ -964,6 +1050,12 @@ dependencies = [ "windows-sys 0.52.0", ] +[[package]] +name = "termtree" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3369f5ac52d5eb6ab48c6b4ffdc8efbcad6b89c765749064ba298f2c68a16a76" + [[package]] name = "tinyvec" version = "1.6.0" @@ -1108,6 +1200,15 @@ version = "0.9.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f" +[[package]] +name = "wait-timeout" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9f200f5b12eb75f8c1ed65abd4b2db8a6e1b138a20de009dacee265a2498f3f6" +dependencies = [ + "libc", +] + [[package]] name = "wasi" version = "0.11.0+wasi-snapshot-preview1" diff --git a/Cargo.toml b/Cargo.toml index 2d5c635..83ab14a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,6 +6,7 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] +assert_cmd = "2.0.14" clap = { version = "4.5.3", features = ["derive"] } native-tls = { version = "0.2.11", features = ["vendored"] } postgres = "0.19.7" diff --git a/src/main.rs b/src/main.rs index ba588cc..a2328f5 100644 --- a/src/main.rs +++ b/src/main.rs @@ -61,6 +61,7 @@ fn main() -> Result<(), Box> { iter.for_each(|row| { let data: String = row.get(0); out.write_all(data.as_bytes()).unwrap(); + out.write_all(b"\n").unwrap(); Ok(()) })?; diff --git a/tests/extract_json.rs b/tests/extract_json.rs new file mode 100644 index 0000000..ddec643 --- /dev/null +++ b/tests/extract_json.rs @@ -0,0 +1,53 @@ +use assert_cmd::prelude::*; +use std::process::Command; + +macro_rules! test_extract_json { + ($name: ident, $in: expr, $out: expr) => { + #[test] + fn $name() -> Result<(), Box> { + let mut cmd = Command::cargo_bin("pgextract")?; + let out = cmd + .args([ + "extract", + "-u", + "postgres://postgres:example@localhost", + "-f", + "ndjson", + $in, + ]) + .unwrap(); + + assert_eq!(String::from_utf8(out.stdout).unwrap(), $out); + Ok(()) + } + }; +} + +test_extract_json!( + two_lines, + "select * from (values ('a'),('b')) as t(col)", + r#"{"col":"a"} +{"col":"b"} +"# +); + +test_extract_json!( + text, + "select 'a'::text col", + r#"{"col":"a"} +"# +); + +test_extract_json!( + int, + "select 1234::bigint col", + r#"{"col":1234} +"# +); + +test_extract_json!( + date, + "select timestamptz '2023-01-01T12:23:45Z' col", + r#"{"col":"2023-01-01T12:23:45+00:00"} +"# +);