-
Notifications
You must be signed in to change notification settings - Fork 297
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ccf0d90
commit 77545dc
Showing
15 changed files
with
253 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
{-# LANGUAGE DataKinds #-} | ||
{-# LANGUAGE DeriveAnyClass #-} | ||
{-# LANGUAGE UndecidableInstances #-} | ||
{-# OPTIONS_GHC -Wno-unused-top-binds #-} | ||
|
||
module TypeLitFieldDefsTest (specsWith, typeLitFieldDefsMigrate) where | ||
|
||
import Data.Maybe (fromJust) | ||
import GHC.TypeNats | ||
import Init | ||
|
||
newtype Finite (n :: Nat) = Finite Int | ||
deriving (Show, Eq) | ||
|
||
instance PersistField (Finite n) where | ||
toPersistValue (Finite n) = toPersistValue n | ||
fromPersistValue = fmap Finite . fromPersistValue | ||
|
||
instance PersistFieldSql (Finite n) where | ||
sqlType _ = sqlType (Proxy :: Proxy Int) | ||
|
||
newtype Labelled (t :: Symbol) = Labelled Int | ||
|
||
instance PersistField (Labelled n) where | ||
toPersistValue (Labelled n) = toPersistValue n | ||
fromPersistValue = fmap Labelled . fromPersistValue | ||
|
||
instance PersistFieldSql (Labelled n) where | ||
sqlType _ = sqlType (Proxy :: Proxy Int) | ||
|
||
share [mkPersist sqlSettings { mpsGeneric = True }, mkMigrate "typeLitFieldDefsMigrate"] [persistLowerCase| | ||
TypeLitFieldDefsNumeric | ||
one (Finite 1) | ||
twenty (Finite 20) | ||
deriving Eq Show | ||
|
||
TypeLitFieldDefsLabelled | ||
one (Labelled "one") | ||
twenty (Labelled "twenty") | ||
deriving Eq Show | ||
|] | ||
|
||
one :: Finite 1 | ||
one = Finite 1 | ||
|
||
oneLabelled :: Labelled "one" | ||
oneLabelled = Labelled 1 | ||
|
||
twenty :: Finite 20 | ||
twenty = Finite 20 | ||
|
||
twentyLabelled :: Labelled "twenty" | ||
twentyLabelled = Labelled 20 | ||
|
||
specsWith :: Runner backend m => RunDb backend m -> Spec | ||
specsWith runDb = | ||
describe "Type Lit Field Definitions" $ do | ||
it "runs appropriate migrations" $ runDb $ do | ||
numKey <- insert $ TypeLitFieldDefsNumeric one twenty | ||
num <- getJust numKey | ||
liftIO $ typeLitFieldDefsEntityNumeric num @?= one | ||
liftIO $ typeLitFieldDefsEntityNumeric num @?= twenty | ||
|
||
labelledKey <- insert $ TypeLitFieldDefsLabelled one twenty | ||
lbl <- getJust labelledKey | ||
liftIO $ typeLitFieldDefsEntityLabelled lbl @?= oneLabelled | ||
liftIO $ typeLitFieldDefsEntityLabelled lbl @?= twentyLabelled | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.