Skip to content

Commit

Permalink
Add a simple test for schema functionality.
Browse files Browse the repository at this point in the history
  • Loading branch information
arrowd committed Feb 1, 2023
1 parent 3e8fd0a commit 1942681
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 2 deletions.
3 changes: 3 additions & 0 deletions persistent-sqlite/test/main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ import qualified RawSqlTest
import qualified ReadWriteTest
import qualified Recursive
import qualified RenameTest
import qualified SchemaTest
import qualified SumTypeTest
import qualified TransactionLevelTest
import qualified TypeLitFieldDefsTest
Expand Down Expand Up @@ -175,6 +176,7 @@ main = do
, MigrationColumnLengthTest.migration
, TransactionLevelTest.migration
, LongIdentifierTest.migration
, SchemaTest.migration
]
PersistentTest.cleanDB
ForeignKey.cleanDB
Expand Down Expand Up @@ -243,6 +245,7 @@ main = do
MigrationTest.specsWith db
LongIdentifierTest.specsWith db
GeneratedColumnTestSQL.specsWith db
SchemaTest.specsWith db

it "issue #328" $ asIO $ runSqliteInfo (mkSqliteConnectionInfo ":memory:") $ do
void $ runMigrationSilent migrateAll
Expand Down
5 changes: 3 additions & 2 deletions persistent-test/persistent-test.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ bug-reports: https://github.com/yesodweb/persistent/issues
extra-source-files: ChangeLog.md

library
exposed-modules:
exposed-modules:
CompositeTest
CustomPersistField
CustomPersistFieldTest
Expand Down Expand Up @@ -51,6 +51,7 @@ library
RenameTest
Recursive
SumTypeTest
SchemaTest
TransactionLevelTest
TreeTest
TypeLitFieldDefsTest
Expand All @@ -60,7 +61,7 @@ library

hs-source-dirs: src

build-depends:
build-depends:
base >= 4.9 && < 5
, aeson >= 1.0
, blaze-html >= 0.9
Expand Down
32 changes: 32 additions & 0 deletions persistent-test/src/SchemaTest.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{-# LANGUAGE UndecidableInstances #-}
module SchemaTest where

import Init
import qualified PersistentTestModels as PTM

share [mkPersist persistSettings, mkMigrate "migration"] [persistLowerCase|
Person schema=my_special_schema
name Text
age Int
weight Int
deriving Show Eq
|]

cleanDB :: (MonadIO m, PersistQuery backend, PersistEntityBackend Person ~ backend) => ReaderT backend m ()
cleanDB = do
deleteWhere ([] :: [Filter Person])
deleteWhere ([] :: [Filter PTM.Person])

specsWith :: (MonadIO m, MonadFail m) => RunDb SqlBackend m -> Spec
specsWith runDb = describe "schema support" $ do
it "insert a Person under non-default schema" $ runDb $ do
insert_ $ Person "name" 1 2
return ()
it "insert PTM.Person and Person and check they end up in different tables" $ runDb $ do
cleanDB
insert_ $ Person "name" 1 2
insert_ $ PTM.Person "name2" 3 Nothing
schemaPersonCount <- count ([] :: [Filter Person])
ptmPersoncount <- count ([] :: [Filter PTM.Person])
-- both tables should contain only one record despite similarly named Entities
schemaPersonCount + ptmPersoncount @== 2
1 change: 1 addition & 0 deletions persistent/test/Database/Persist/THSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,7 @@ spec = describe "THSpec" $ do
EntityDef
{ entityHaskell = EntityNameHS "HasSimpleCascadeRef"
, entityDB = EntityNameDB "HasSimpleCascadeRef"
, entitySchema = Nothing
, entityId =
EntityIdField FieldDef
{ fieldHaskell = FieldNameHS "Id"
Expand Down

0 comments on commit 1942681

Please sign in to comment.