Skip to content

Commit

Permalink
Avoid OverloadedRecordDot Usage in Record Specs
Browse files Browse the repository at this point in the history
  • Loading branch information
halogenandtoast committed Oct 17, 2023
1 parent 85b9d5e commit 2356923
Showing 1 changed file with 12 additions and 25 deletions.
37 changes: 12 additions & 25 deletions test/Common/Record.hs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
{-# LANGUAGE CPP #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE DerivingStrategies #-}
{-# LANGUAGE DuplicateRecordFields #-}
Expand All @@ -18,10 +17,6 @@
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE UndecidableInstances #-}

#if __GLASGOW_HASKELL__ >= 902
{-# LANGUAGE OverloadedRecordDot #-}
#endif

-- Tests for `Database.Esqueleto.Record`.
module Common.Record (testDeriveEsqueletoRecord) where

Expand All @@ -41,8 +36,8 @@ import Database.Esqueleto.Record (
takeColumns,
takeMaybeColumns,
)
import GHC.Records

#if __GLASGOW_HASKELL__ >= 902
data MyRecord =
MyRecord
{ myName :: Text
Expand Down Expand Up @@ -132,14 +127,11 @@ mySubselectRecordQuery = do
table @User
`leftJoin`
myRecordQuery
`on` (do \(user :& record) -> just (user ^. #id) ==. record.myUser ?. #id)
pure $ record.myAddress
#endif
`on` (do \(user :& record) -> just (user ^. #id) ==. getField @"myUser" record ?. #id)
pure $ getField @"myAddress" record

testDeriveEsqueletoRecord :: SpecDb
testDeriveEsqueletoRecord = describe "deriveEsqueletoRecord" $ do
#if __GLASGOW_HASKELL__ >= 902

let setup :: MonadIO m => SqlPersistT m ()
setup = do
_ <- insert $ User { userAddress = Nothing, userName = "Rebecca" }
Expand Down Expand Up @@ -266,9 +258,9 @@ testDeriveEsqueletoRecord = describe "deriveEsqueletoRecord" $ do
records <- select $ do
from
( table @User
`leftJoin` myRecordQuery `on` (do \(user :& record) -> just (user ^. #id) ==. record.myUser ?. #id)
`leftJoin` myRecordQuery `on` (do \(user :& record) -> just (user ^. #id) ==. getField @"myUser" record ?. #id)
)
let sortedRecords = sortOn (\(Entity _ user :& _) -> user.userName) records
let sortedRecords = sortOn (\(Entity _ user :& _) -> getField @"userName" user) records
liftIO $ sortedRecords !! 0
`shouldSatisfy`
(\case (_ :& Just (MyRecord {myName = "Rebecca", myAddress = Nothing})) -> True
Expand All @@ -286,9 +278,9 @@ testDeriveEsqueletoRecord = describe "deriveEsqueletoRecord" $ do
records <- select $ do
from
( table @User
`leftJoin` myRecordQuery `on` (do \(user :& record) -> user ^. #address ==. record.myAddress ?. #id)
`leftJoin` myRecordQuery `on` (do \(user :& record) -> user ^. #address ==. getField @"myAddress" record ?. #id)
)
let sortedRecords = sortOn (\(Entity _ user :& _) -> user.userName) records
let sortedRecords = sortOn (\(Entity _ user :& _) -> getField @"userName" user) records
liftIO $ sortedRecords !! 0
`shouldSatisfy`
(\case (_ :& Nothing) -> True
Expand All @@ -307,9 +299,9 @@ testDeriveEsqueletoRecord = describe "deriveEsqueletoRecord" $ do
from
( table @User
`leftJoin` myNestedRecordQuery
`on` (do \(user :& record) -> just (user ^. #id) ==. record.myRecord.myUser ?. #id)
`on` (do \(user :& record) -> just (user ^. #id) ==. getField @"myUser" (getField @"myRecord" record) ?. #id)
)
let sortedRecords = sortOn (\(Entity _ user :& _) -> user.userName) records
let sortedRecords = sortOn (\(Entity _ user :& _) -> getField @"userName" user) records
liftIO $ sortedRecords !! 0
`shouldSatisfy`
(\case (_ :& Just (MyNestedRecord {myRecord = MyRecord {myName = "Rebecca", myAddress = Nothing}})) -> True
Expand All @@ -328,11 +320,11 @@ testDeriveEsqueletoRecord = describe "deriveEsqueletoRecord" $ do
from
( table @User
`leftJoin` myNestedRecordQuery
`on` (do \(user :& record) -> just (user ^. #id) ==. record.myRecord.myUser ?. #id)
`on` (do \(user :& record) -> just (user ^. #id) ==. getField @"myUser" (getField @"myRecord" record) ?. #id)
`leftJoin` myNestedRecordQuery
`on` (do \(user :& record1 :& record2) -> record1.myRecord.myUser ?. #id !=. record2.myRecord.myUser ?. #id)
`on` (do \(user :& record1 :& record2) -> getField @"myUser" (getField @"myRecord" record1) ?. #id !=. getField @"myUser" (getField @"myRecord" record2) ?. #id)
)
let sortedRecords = sortOn (\(Entity _ user :& _ :& _) -> user.userName) records
let sortedRecords = sortOn (\(Entity _ user :& _ :& _) -> getField @"userName" user) records
liftIO $ sortedRecords !! 0
`shouldSatisfy`
(\case ( _ :& _ :& Just ( MyNestedRecord { myRecord = MyRecord { myName = "Some Guy"
Expand All @@ -344,8 +336,3 @@ testDeriveEsqueletoRecord = describe "deriveEsqueletoRecord" $ do
`shouldSatisfy`
(\case (_ :& _ :& Just (MyNestedRecord {myRecord = MyRecord {myName = "Rebecca", myAddress = Nothing}})) -> True
_ -> False)

#else
it "is only supported in GHC 9.2 or above" $ \_ -> do
pending
#endif

0 comments on commit 2356923

Please sign in to comment.