Skip to content

Commit

Permalink
Merge pull request #5 from nlopes/nlopes-add-postgres-tests
Browse files Browse the repository at this point in the history
wip
  • Loading branch information
nlopes authored Sep 9, 2023
2 parents 072b1ad + 1eaa98e commit be87051
Show file tree
Hide file tree
Showing 8 changed files with 264 additions and 7 deletions.
1 change: 1 addition & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ services:
ports:
- "127.0.0.1:5432:5432"
environment:
- POSTGRES_USER=root
- POSTGRES_HOST_AUTH_METHOD=trust
volumes:
- .data/postgresql-15/:/var/lib/postgresql
Expand Down
104 changes: 104 additions & 0 deletions fixtures/diesel/postgres/diesel-postgres-structure.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
--
-- PostgreSQL database dump
--

-- Dumped from database version 15.2
-- Dumped by pg_dump version 15.4 (Homebrew)

SET statement_timeout = 0;
SET lock_timeout = 0;
SET idle_in_transaction_session_timeout = 0;
SET client_encoding = 'UTF8';
SET standard_conforming_strings = on;
SELECT pg_catalog.set_config('search_path', '', false);
SET check_function_bodies = false;
SET xmloption = content;
SET client_min_messages = warning;
SET row_security = off;

SET default_tablespace = '';

SET default_table_access_method = heap;

--
-- Name: __diesel_schema_migrations; Type: TABLE; Schema: public; Owner: -
--

CREATE TABLE public.__diesel_schema_migrations (
version character varying(50) NOT NULL,
run_on timestamp without time zone DEFAULT CURRENT_TIMESTAMP NOT NULL
);


--
-- Name: _sqlx_migrations; Type: TABLE; Schema: public; Owner: -
--

CREATE TABLE public._sqlx_migrations (
version bigint NOT NULL,
description text NOT NULL,
installed_on timestamp with time zone DEFAULT now() NOT NULL,
success boolean NOT NULL,
checksum bytea NOT NULL,
execution_time bigint NOT NULL
);


--
-- Name: diesel_users; Type: TABLE; Schema: public; Owner: -
--

CREATE TABLE public.diesel_users (
id text NOT NULL,
email text NOT NULL,
created_at timestamp with time zone DEFAULT now() NOT NULL
);


--
-- Name: sqlx_users; Type: TABLE; Schema: public; Owner: -
--

CREATE TABLE public.sqlx_users (
id character varying(32) NOT NULL,
email text NOT NULL,
created_at timestamp without time zone DEFAULT CURRENT_TIMESTAMP NOT NULL
);


--
-- Name: __diesel_schema_migrations __diesel_schema_migrations_pkey; Type: CONSTRAINT; Schema: public; Owner: -
--

ALTER TABLE ONLY public.__diesel_schema_migrations
ADD CONSTRAINT __diesel_schema_migrations_pkey PRIMARY KEY (version);


--
-- Name: _sqlx_migrations _sqlx_migrations_pkey; Type: CONSTRAINT; Schema: public; Owner: -
--

ALTER TABLE ONLY public._sqlx_migrations
ADD CONSTRAINT _sqlx_migrations_pkey PRIMARY KEY (version);


--
-- Name: diesel_users diesel_users_pkey; Type: CONSTRAINT; Schema: public; Owner: -
--

ALTER TABLE ONLY public.diesel_users
ADD CONSTRAINT diesel_users_pkey PRIMARY KEY (id);


--
-- Name: sqlx_users sqlx_users_pkey; Type: CONSTRAINT; Schema: public; Owner: -
--

ALTER TABLE ONLY public.sqlx_users
ADD CONSTRAINT sqlx_users_pkey PRIMARY KEY (id);


--
-- PostgreSQL database dump complete
--

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
-- This file should undo anything in `up.sql`
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
CREATE TABLE diesel_users (
id TEXT PRIMARY KEY NOT NULL,
email TEXT NOT NULL,
created_at TIMESTAMPTZ DEFAULT NOW() NOT NULL
);
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
CREATE TABLE sqlx_users (
id VARCHAR(32) PRIMARY KEY NOT NULL,
email TEXT NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL
);
104 changes: 104 additions & 0 deletions fixtures/sqlx/postgres/sqlx-postgres-structure.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
--
-- PostgreSQL database dump
--

-- Dumped from database version 15.2
-- Dumped by pg_dump version 15.4 (Homebrew)

SET statement_timeout = 0;
SET lock_timeout = 0;
SET idle_in_transaction_session_timeout = 0;
SET client_encoding = 'UTF8';
SET standard_conforming_strings = on;
SELECT pg_catalog.set_config('search_path', '', false);
SET check_function_bodies = false;
SET xmloption = content;
SET client_min_messages = warning;
SET row_security = off;

SET default_tablespace = '';

SET default_table_access_method = heap;

--
-- Name: __diesel_schema_migrations; Type: TABLE; Schema: public; Owner: -
--

CREATE TABLE public.__diesel_schema_migrations (
version character varying(50) NOT NULL,
run_on timestamp without time zone DEFAULT CURRENT_TIMESTAMP NOT NULL
);


--
-- Name: _sqlx_migrations; Type: TABLE; Schema: public; Owner: -
--

CREATE TABLE public._sqlx_migrations (
version bigint NOT NULL,
description text NOT NULL,
installed_on timestamp with time zone DEFAULT now() NOT NULL,
success boolean NOT NULL,
checksum bytea NOT NULL,
execution_time bigint NOT NULL
);


--
-- Name: diesel_users; Type: TABLE; Schema: public; Owner: -
--

CREATE TABLE public.diesel_users (
id text NOT NULL,
email text NOT NULL,
created_at timestamp with time zone DEFAULT now() NOT NULL
);


--
-- Name: sqlx_users; Type: TABLE; Schema: public; Owner: -
--

CREATE TABLE public.sqlx_users (
id character varying(32) NOT NULL,
email text NOT NULL,
created_at timestamp without time zone DEFAULT CURRENT_TIMESTAMP NOT NULL
);


--
-- Name: __diesel_schema_migrations __diesel_schema_migrations_pkey; Type: CONSTRAINT; Schema: public; Owner: -
--

ALTER TABLE ONLY public.__diesel_schema_migrations
ADD CONSTRAINT __diesel_schema_migrations_pkey PRIMARY KEY (version);


--
-- Name: _sqlx_migrations _sqlx_migrations_pkey; Type: CONSTRAINT; Schema: public; Owner: -
--

ALTER TABLE ONLY public._sqlx_migrations
ADD CONSTRAINT _sqlx_migrations_pkey PRIMARY KEY (version);


--
-- Name: diesel_users diesel_users_pkey; Type: CONSTRAINT; Schema: public; Owner: -
--

ALTER TABLE ONLY public.diesel_users
ADD CONSTRAINT diesel_users_pkey PRIMARY KEY (id);


--
-- Name: sqlx_users sqlx_users_pkey; Type: CONSTRAINT; Schema: public; Owner: -
--

ALTER TABLE ONLY public.sqlx_users
ADD CONSTRAINT sqlx_users_pkey PRIMARY KEY (id);


--
-- PostgreSQL database dump complete
--

41 changes: 41 additions & 0 deletions src/postgres/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,44 @@ async fn migrate<P: AsRef<std::path::Path>>(
.map(|_| ());
Ok(())
}

#[cfg(test)]
mod tests {
async fn call_write_structure_sql<M: AsRef<str>, D: AsRef<str>>(
fixtures_path: M,
destination_filename: D,
) -> Result<(), crate::error::Error> {
let destination_path = std::env::temp_dir().join(destination_filename.as_ref());
let migrations_path = std::path::PathBuf::from(fixtures_path.as_ref()).join("migrations");
let _ = super::write_structure_sql(
super::DEFAULT_CONNECTION_URL,
migrations_path,
&destination_path,
)
.await?;
let expected = std::fs::read_to_string(dbg!(format!(
"./{}/{}",
fixtures_path.as_ref(),
destination_filename.as_ref()
)))?;
let contents = std::fs::read_to_string(destination_path)?;
assert_eq!(contents, expected);
Ok(())
}

#[cfg(all(feature = "sqlx", feature = "postgres"))]
#[tokio::test]
async fn test_write_structure_sql() -> Result<(), crate::error::Error> {
call_write_structure_sql("./fixtures/sqlx/postgres", "sqlx-postgres-structure.sql").await
}

#[cfg(all(feature = "diesel", feature = "postgres"))]
#[tokio::test]
async fn test_write_structure_sql() -> Result<(), crate::error::Error> {
call_write_structure_sql(
"./fixtures/diesel/postgres",
"diesel-postgres-structure.sql",
)
.await
}
}
10 changes: 3 additions & 7 deletions src/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,23 +42,19 @@ mod tests {
Ok(())
}

// IMPORTANT: The tests below this notice will fail if mysqldump is not found in the
// PATH.
#[tokio::test]
async fn test_run_found() -> Result<(), crate::Error> {
use super::{run, Command};
let result = run(Command::new("mysqldump").arg("--version")).await;
let result = run(Command::new("which").arg("which")).await;
assert!(matches!(result, Ok(())));
Ok(())
}

#[tokio::test]
async fn test_run_invalid_arguments() -> Result<(), crate::Error> {
use super::{run, Command};
let result = run(Command::new("mysqldump").arg("--norberto")).await;
assert!(matches!(
result,
Err(crate::Error::CommandRunError(error_message)) if error_message == "output: \nstderr: mysqldump: [ERROR] unknown option '--norberto'.\n"));
let result = run(Command::new("which").arg("--norberto")).await;
assert!(matches!(result, Err(crate::Error::CommandRunError(_))));
Ok(())
}
}

0 comments on commit be87051

Please sign in to comment.