From e9c232976e8d1cf488fb4f50b25a798f443dd7a8 Mon Sep 17 00:00:00 2001 From: Joe Nelson Date: Thu, 14 Aug 2014 13:23:41 -0700 Subject: [PATCH] Use version number for schema in tests like anticipated use case --- src/PgQuery.hs | 4 +-- test/{ => Unit}/PgQuerySpec.hs | 7 +++-- test/fixtures/schema.sql | 47 +++++++++++++++++----------------- 3 files changed, 28 insertions(+), 30 deletions(-) rename test/{ => Unit}/PgQuerySpec.hs (82%) diff --git a/src/PgQuery.hs b/src/PgQuery.hs index 6d19cbf5e..4280f8dfd 100644 --- a/src/PgQuery.hs +++ b/src/PgQuery.hs @@ -4,7 +4,7 @@ module PgQuery where -import Data.Text (Text) +import Data.Text (Text, pack) import Data.Functor ( (<$>) ) import Data.Maybe (fromMaybe) import Data.List (intersperse, intercalate) @@ -106,7 +106,7 @@ jsonArrayRows q = insert :: Int -> Text -> SqlRow -> Connection -> IO BL.ByteString insert schema table row conn = do - query <- populateSql conn ("insert into %I.%I ("++colIds++")", map toSql $ (show schema):table:cols) + query <- populateSql conn ("insert into %I.%I ("++colIds++")", map toSql $ (pack . show $ schema):table:cols) stmt <- prepare conn (query ++ " values ("++phs++") returning *") _ <- execute stmt values commit conn diff --git a/test/PgQuerySpec.hs b/test/Unit/PgQuerySpec.hs similarity index 82% rename from test/PgQuerySpec.hs rename to test/Unit/PgQuerySpec.hs index c2dce83a9..0f47f7284 100644 --- a/test/PgQuerySpec.hs +++ b/test/Unit/PgQuerySpec.hs @@ -1,6 +1,6 @@ {-# LANGUAGE OverloadedStrings #-} -module PgQuerySpec where +module Unit.PgQuerySpec where import Test.Hspec @@ -14,8 +14,7 @@ loadFixture :: String -> IO Connection loadFixture name = do conn <- connectPostgreSQL "postgres://postgres:@localhost:5432/dbapi_test" sql <- readFile $ "test/fixtures/" ++ name ++ ".sql" - runRaw conn "drop schema if exists public cascade" - runRaw conn "create schema public" + runRaw conn "drop schema if exists \"1\" cascade" runRaw conn sql return conn @@ -26,7 +25,7 @@ spec :: Spec spec = beforeAll (loadFixture "schema") $ do describe "insert" $ it "can insert into an empty table" $ \conn -> do - _ <- insert "public" "auto_incrementing_pk" (SqlRow [ + _ <- insert 1 "auto_incrementing_pk" (SqlRow [ ("non_nullable_string", toSql ("a string that isn't null" :: String)) ]) conn r <- quickQuery conn "select count(1) from auto_incrementing_pk" [] diff --git a/test/fixtures/schema.sql b/test/fixtures/schema.sql index b1b420073..bb02c69a5 100644 --- a/test/fixtures/schema.sql +++ b/test/fixtures/schema.sql @@ -4,7 +4,7 @@ -- Dumped from database version 9.3.4 -- Dumped by pg_dump version 9.3.1 --- Started on 2014-08-10 15:29:43 PDT +-- Started on 2014-08-14 13:19:39 PDT SET statement_timeout = 0; SET lock_timeout = 0; @@ -13,6 +13,14 @@ SET standard_conforming_strings = on; SET check_function_bodies = false; SET client_min_messages = warning; +-- +-- TOC entry 6 (class 2615 OID 226754) +-- Name: 1; Type: SCHEMA; Schema: -; Owner: - +-- + +CREATE SCHEMA "1"; + + -- -- TOC entry 172 (class 3079 OID 12018) -- Name: plpgsql; Type: EXTENSION; Schema: -; Owner: - @@ -22,7 +30,7 @@ CREATE EXTENSION IF NOT EXISTS plpgsql WITH SCHEMA pg_catalog; -- --- TOC entry 2209 (class 0 OID 0) +-- TOC entry 2206 (class 0 OID 0) -- Dependencies: 172 -- Name: EXTENSION plpgsql; Type: COMMENT; Schema: -; Owner: - -- @@ -30,15 +38,15 @@ CREATE EXTENSION IF NOT EXISTS plpgsql WITH SCHEMA pg_catalog; COMMENT ON EXTENSION plpgsql IS 'PL/pgSQL procedural language'; -SET search_path = public, pg_catalog; +SET search_path = "1", pg_catalog; SET default_tablespace = ''; SET default_with_oids = false; -- --- TOC entry 171 (class 1259 OID 226379) --- Name: auto_incrementing_pk; Type: TABLE; Schema: public; Owner: -; Tablespace: +-- TOC entry 170 (class 1259 OID 226755) +-- Name: auto_incrementing_pk; Type: TABLE; Schema: 1; Owner: -; Tablespace: -- CREATE TABLE auto_incrementing_pk ( @@ -50,8 +58,8 @@ CREATE TABLE auto_incrementing_pk ( -- --- TOC entry 170 (class 1259 OID 226377) --- Name: auto_incrementing_pk_id_seq; Type: SEQUENCE; Schema: public; Owner: - +-- TOC entry 171 (class 1259 OID 226762) +-- Name: auto_incrementing_pk_id_seq; Type: SEQUENCE; Schema: 1; Owner: - -- CREATE SEQUENCE auto_incrementing_pk_id_seq @@ -63,41 +71,32 @@ CREATE SEQUENCE auto_incrementing_pk_id_seq -- --- TOC entry 2210 (class 0 OID 0) --- Dependencies: 170 --- Name: auto_incrementing_pk_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: - +-- TOC entry 2207 (class 0 OID 0) +-- Dependencies: 171 +-- Name: auto_incrementing_pk_id_seq; Type: SEQUENCE OWNED BY; Schema: 1; Owner: - -- ALTER SEQUENCE auto_incrementing_pk_id_seq OWNED BY auto_incrementing_pk.id; -- --- TOC entry 2090 (class 2604 OID 226382) --- Name: id; Type: DEFAULT; Schema: public; Owner: - +-- TOC entry 2091 (class 2604 OID 226764) +-- Name: id; Type: DEFAULT; Schema: 1; Owner: - -- ALTER TABLE ONLY auto_incrementing_pk ALTER COLUMN id SET DEFAULT nextval('auto_incrementing_pk_id_seq'::regclass); -- --- TOC entry 2211 (class 0 OID 0) --- Dependencies: 170 --- Name: auto_incrementing_pk_id_seq; Type: SEQUENCE SET; Schema: public; Owner: - --- - -SELECT pg_catalog.setval('auto_incrementing_pk_id_seq', 1, true); - - --- --- TOC entry 2093 (class 2606 OID 226388) --- Name: auto_incrementing_pk_pkey; Type: CONSTRAINT; Schema: public; Owner: -; Tablespace: +-- TOC entry 2093 (class 2606 OID 226766) +-- Name: auto_incrementing_pk_pkey; Type: CONSTRAINT; Schema: 1; Owner: -; Tablespace: -- ALTER TABLE ONLY auto_incrementing_pk ADD CONSTRAINT auto_incrementing_pk_pkey PRIMARY KEY (id); --- Completed on 2014-08-10 15:29:43 PDT +-- Completed on 2014-08-14 13:19:39 PDT -- -- PostgreSQL database dump complete