feat: add raw SQL execution helpers (execute, executeWith, query_, queryWith)
Build and Test / build-and-test (push) Successful in 4m46s
Build and Test / build-and-test (push) Successful in 4m46s
This commit is contained in:
@@ -7,6 +7,7 @@ import Test.Hspec
|
||||
import qualified Test.AutoMigration as AutoMigration
|
||||
import qualified Test.EntityOperations as EntityOperations
|
||||
import qualified Test.FieldDefinition as FieldDefinition
|
||||
import qualified Test.Raw as Raw
|
||||
import qualified Test.SqlMarshaller as SqlMarshaller
|
||||
|
||||
main :: IO ()
|
||||
@@ -15,3 +16,4 @@ main = hspec $ do
|
||||
describe "SqlMarshaller" SqlMarshaller.sqlMarshallerTests
|
||||
describe "EntityOperations" EntityOperations.entityOperationsTests
|
||||
describe "AutoMigration" AutoMigration.autoMigrationTests
|
||||
describe "Raw" Raw.rawTests
|
||||
|
||||
@@ -23,7 +23,7 @@ autoMigrationTests = do
|
||||
pure ()
|
||||
closeConnection db
|
||||
|
||||
it "is idempotent — running twice does not error" $ do
|
||||
it "is idempotent -- running twice does not error" $ do
|
||||
db <- openConnection ":memory:"
|
||||
withConnection db $ do
|
||||
autoMigrateSchema defaultOptions [schemaTable personTable []]
|
||||
|
||||
@@ -0,0 +1,48 @@
|
||||
{-# LANGUAGE OverloadedStrings #-}
|
||||
|
||||
module Test.Raw where
|
||||
|
||||
import qualified Database.SQLite3 as SQLite3
|
||||
import Test.Hspec
|
||||
|
||||
import Orville.SQLite
|
||||
|
||||
rawTests :: Spec
|
||||
rawTests = do
|
||||
describe "execute" $ do
|
||||
it "runs a CREATE TABLE and INSERT statement" $ do
|
||||
db <- openConnection ":memory:"
|
||||
runOrvilleM db $ execute "CREATE TABLE raw_test (id INTEGER, name TEXT)"
|
||||
runOrvilleM db $ execute "INSERT INTO raw_test VALUES (1, 'hello')"
|
||||
rows <- runOrvilleM db $ query_ "SELECT * FROM raw_test"
|
||||
length rows `shouldBe` 1
|
||||
closeConnection db
|
||||
|
||||
describe "executeWith" $ do
|
||||
it "runs parameterized INSERT" $ do
|
||||
db <- openConnection ":memory:"
|
||||
runOrvilleM db $ execute "CREATE TABLE raw_test2 (id INTEGER, name TEXT)"
|
||||
runOrvilleM db $ executeWith "INSERT INTO raw_test2 VALUES (?, ?)" [SQLite3.SQLInteger 42, SQLite3.SQLText "world"]
|
||||
rows <- runOrvilleM db $ query_ "SELECT * FROM raw_test2"
|
||||
length rows `shouldBe` 1
|
||||
closeConnection db
|
||||
|
||||
describe "query_" $ do
|
||||
it "returns rows from a SELECT" $ do
|
||||
db <- openConnection ":memory:"
|
||||
runOrvilleM db $ execute "CREATE TABLE raw_test3 (a INTEGER)"
|
||||
runOrvilleM db $ execute "INSERT INTO raw_test3 VALUES (1)"
|
||||
runOrvilleM db $ execute "INSERT INTO raw_test3 VALUES (2)"
|
||||
rows <- runOrvilleM db $ query_ "SELECT * FROM raw_test3 ORDER BY a"
|
||||
length rows `shouldBe` 2
|
||||
closeConnection db
|
||||
|
||||
describe "queryWith" $ do
|
||||
it "returns rows matching parameters" $ do
|
||||
db <- openConnection ":memory:"
|
||||
runOrvilleM db $ execute "CREATE TABLE raw_test4 (name TEXT)"
|
||||
runOrvilleM db $ executeWith "INSERT INTO raw_test4 VALUES (?)" [SQLite3.SQLText "alpha"]
|
||||
runOrvilleM db $ executeWith "INSERT INTO raw_test4 VALUES (?)" [SQLite3.SQLText "beta"]
|
||||
rows <- runOrvilleM db $ queryWith "SELECT * FROM raw_test4 WHERE name = ?" [SQLite3.SQLText "beta"]
|
||||
length rows `shouldBe` 1
|
||||
closeConnection db
|
||||
Reference in New Issue
Block a user