Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Enum tests #1835

Merged
merged 2 commits into from
Oct 2, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 19 additions & 19 deletions Test/SchemaCompilerSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ tests = do
let output = compileStatementPreview [statement] statement |> Text.strip

output `shouldBe` [trimming|
data Mood = Happy | VeryHappy | Sad | VerySad deriving (Eq, Show, Read, Enum)
data Mood = Happy | VeryHappy | Sad | VerySad deriving (Eq, Show, Read, Enum, Bounded)
instance FromField Mood where
fromField field (Just value) | value == (Data.Text.Encoding.encodeUtf8 "happy") = pure Happy
fromField field (Just value) | value == (Data.Text.Encoding.encodeUtf8 "very happy") = pure VeryHappy
Expand Down Expand Up @@ -56,7 +56,7 @@ tests = do
let output = compileStatementPreview [statement] statement |> Text.strip

output `shouldBe` [trimming|
data Province = Alberta | Britishcolumbia | Saskatchewan | Manitoba | Ontario | Quebec | Novascotia | Newbrunswick | Princeedwardisland | Newfoundlandandlabrador deriving (Eq, Show, Read, Enum)
data Province = Alberta | Britishcolumbia | Saskatchewan | Manitoba | Ontario | Quebec | Novascotia | Newbrunswick | Princeedwardisland | Newfoundlandandlabrador deriving (Eq, Show, Read, Enum, Bounded)
instance FromField Province where
fromField field (Just value) | value == (Data.Text.Encoding.encodeUtf8 "Alberta") = pure Alberta
fromField field (Just value) | value == (Data.Text.Encoding.encodeUtf8 "BritishColumbia") = pure Britishcolumbia
Expand Down Expand Up @@ -102,7 +102,7 @@ tests = do
let output = compileStatementPreview [enum1, enum2] enum1 |> Text.strip

output `shouldBe` [trimming|
data PropertyType = PropertyTypeApartment | House deriving (Eq, Show, Read, Enum)
data PropertyType = PropertyTypeApartment | House deriving (Eq, Show, Read, Enum, Bounded)
instance FromField PropertyType where
fromField field (Just value) | value == (Data.Text.Encoding.encodeUtf8 "APARTMENT") = pure PropertyTypeApartment
fromField field (Just value) | value == (Data.Text.Encoding.encodeUtf8 "HOUSE") = pure House
Expand Down Expand Up @@ -388,55 +388,55 @@ tests = do

compileOutput `shouldBe` [trimming|
data LandingPage' paragraphCtasLandingPages paragraphCtasToLandingPages = LandingPage {id :: (Id' "landing_pages"), paragraphCtasLandingPages :: paragraphCtasLandingPages, paragraphCtasToLandingPages :: paragraphCtasToLandingPages, meta :: MetaBag} deriving (Eq, Show)

type instance PrimaryKey "landing_pages" = UUID
type instance Include "paragraphCtasLandingPages" (LandingPage' paragraphCtasLandingPages paragraphCtasToLandingPages) = LandingPage' [ParagraphCta] paragraphCtasToLandingPages
type instance Include "paragraphCtasToLandingPages" (LandingPage' paragraphCtasLandingPages paragraphCtasToLandingPages) = LandingPage' paragraphCtasLandingPages [ParagraphCta]

type LandingPage = LandingPage' (QueryBuilder.QueryBuilder "paragraph_ctas") (QueryBuilder.QueryBuilder "paragraph_ctas")

type instance GetTableName (LandingPage' _ _) = "landing_pages"
type instance GetModelByTableName "landing_pages" = LandingPage

instance Default (Id' "landing_pages") where def = Id def

instance () => Table (LandingPage' paragraphCtasLandingPages paragraphCtasToLandingPages) where
tableName = "landing_pages"
tableNameByteString = Data.Text.Encoding.encodeUtf8 "landing_pages"
columnNames = ["id"]
primaryKeyCondition LandingPage { id } = [("id", toField id)]
{-# INLINABLE primaryKeyCondition #-}


instance InputValue LandingPage where inputValue = IHP.ModelSupport.recordToInputValue


instance FromRow LandingPage where
fromRow = do
id <- field
let theRecord = LandingPage id def def def { originalDatabaseRecord = Just (Data.Dynamic.toDyn theRecord) }
pure theRecord


type instance GetModelName (LandingPage' _ _) = "LandingPage"

instance CanCreate LandingPage where
create :: (?modelContext :: ModelContext) => LandingPage -> IO LandingPage
create model = do
List.head <$> sqlQuery "INSERT INTO landing_pages (id) VALUES (?) RETURNING id" (Only (fieldWithDefault #id model))
createMany [] = pure []
createMany models = do
sqlQuery (Query $ "INSERT INTO landing_pages (id) VALUES " <> (ByteString.intercalate ", " (List.map (\_ -> "(?)") models)) <> " RETURNING id") (List.concat $ List.map (\model -> [toField (fieldWithDefault #id model)]) models)

instance CanUpdate LandingPage where
updateRecord model = do
List.head <$> sqlQuery "UPDATE landing_pages SET id = ? WHERE id = ? RETURNING id" ((fieldWithUpdate #id model, model.id))

instance Record LandingPage where
{-# INLINE newRecord #-}
newRecord = LandingPage def def def def


instance QueryBuilder.FilterPrimaryKey "landing_pages" where
filterWhereId id builder =
builder |> QueryBuilder.filterWhere (#id, id)
Expand Down
Loading