Skip to content

Commit 1942681

Browse files
committed
Add a simple test for schema functionality.
1 parent 3e8fd0a commit 1942681

4 files changed

Lines changed: 39 additions & 2 deletions

File tree

persistent-sqlite/test/main.hs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ import qualified RawSqlTest
4343
import qualified ReadWriteTest
4444
import qualified Recursive
4545
import qualified RenameTest
46+
import qualified SchemaTest
4647
import qualified SumTypeTest
4748
import qualified TransactionLevelTest
4849
import qualified TypeLitFieldDefsTest
@@ -175,6 +176,7 @@ main = do
175176
, MigrationColumnLengthTest.migration
176177
, TransactionLevelTest.migration
177178
, LongIdentifierTest.migration
179+
, SchemaTest.migration
178180
]
179181
PersistentTest.cleanDB
180182
ForeignKey.cleanDB
@@ -243,6 +245,7 @@ main = do
243245
MigrationTest.specsWith db
244246
LongIdentifierTest.specsWith db
245247
GeneratedColumnTestSQL.specsWith db
248+
SchemaTest.specsWith db
246249

247250
it "issue #328" $ asIO $ runSqliteInfo (mkSqliteConnectionInfo ":memory:") $ do
248251
void $ runMigrationSilent migrateAll

persistent-test/persistent-test.cabal

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ bug-reports: https://github.com/yesodweb/persistent/issues
1515
extra-source-files: ChangeLog.md
1616

1717
library
18-
exposed-modules:
18+
exposed-modules:
1919
CompositeTest
2020
CustomPersistField
2121
CustomPersistFieldTest
@@ -51,6 +51,7 @@ library
5151
RenameTest
5252
Recursive
5353
SumTypeTest
54+
SchemaTest
5455
TransactionLevelTest
5556
TreeTest
5657
TypeLitFieldDefsTest
@@ -60,7 +61,7 @@ library
6061

6162
hs-source-dirs: src
6263

63-
build-depends:
64+
build-depends:
6465
base >= 4.9 && < 5
6566
, aeson >= 1.0
6667
, blaze-html >= 0.9

persistent-test/src/SchemaTest.hs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
{-# LANGUAGE UndecidableInstances #-}
2+
module SchemaTest where
3+
4+
import Init
5+
import qualified PersistentTestModels as PTM
6+
7+
share [mkPersist persistSettings, mkMigrate "migration"] [persistLowerCase|
8+
Person schema=my_special_schema
9+
name Text
10+
age Int
11+
weight Int
12+
deriving Show Eq
13+
|]
14+
15+
cleanDB :: (MonadIO m, PersistQuery backend, PersistEntityBackend Person ~ backend) => ReaderT backend m ()
16+
cleanDB = do
17+
deleteWhere ([] :: [Filter Person])
18+
deleteWhere ([] :: [Filter PTM.Person])
19+
20+
specsWith :: (MonadIO m, MonadFail m) => RunDb SqlBackend m -> Spec
21+
specsWith runDb = describe "schema support" $ do
22+
it "insert a Person under non-default schema" $ runDb $ do
23+
insert_ $ Person "name" 1 2
24+
return ()
25+
it "insert PTM.Person and Person and check they end up in different tables" $ runDb $ do
26+
cleanDB
27+
insert_ $ Person "name" 1 2
28+
insert_ $ PTM.Person "name2" 3 Nothing
29+
schemaPersonCount <- count ([] :: [Filter Person])
30+
ptmPersoncount <- count ([] :: [Filter PTM.Person])
31+
-- both tables should contain only one record despite similarly named Entities
32+
schemaPersonCount + ptmPersoncount @== 2

persistent/test/Database/Persist/THSpec.hs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,7 @@ spec = describe "THSpec" $ do
318318
EntityDef
319319
{ entityHaskell = EntityNameHS "HasSimpleCascadeRef"
320320
, entityDB = EntityNameDB "HasSimpleCascadeRef"
321+
, entitySchema = Nothing
321322
, entityId =
322323
EntityIdField FieldDef
323324
{ fieldHaskell = FieldNameHS "Id"

0 commit comments

Comments
 (0)