Skip to content

Commit

Permalink
Clean up tests. PR comments
Browse files Browse the repository at this point in the history
  • Loading branch information
infomiho committed Sep 2, 2024
1 parent a197218 commit 362ccff
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 106 deletions.
2 changes: 1 addition & 1 deletion waspc/src/Wasp/Psl/Ast/Attribute.hs
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,4 @@ data Attribute = Attribute

-- | @db.Uuid or @db.String or @db.VarChar are examples of native db types.
isNativeDbTypeAttr :: Attribute -> Bool
isNativeDbTypeAttr = isPrefixOf "db." . _attrName
isNativeDbTypeAttr = ("db." `isPrefixOf`) . _attrName
135 changes: 30 additions & 105 deletions waspc/test/Generator/AuthInjectionTest.hs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
module Generator.AuthInjectionTest where

import Data.Maybe (maybeToList)
import NeatInterpolation (trimming)
import Test.Tasty.Hspec
import Util.Prisma (getPrismaModelBody)
import qualified Wasp.AppSpec.Entity as AS.Entity
import Wasp.Generator.DbGenerator.Auth (injectAuth)
import Wasp.Generator.Monad (runGenerator)
Expand Down Expand Up @@ -128,112 +130,35 @@ spec_GeneratorAuthInjectionTest = do
userIdFieldAttributes

authIdentityEntity =
( "AuthIdentity",
AS.Entity.makeEntity
( Psl.Model.Body
[ Psl.Model.ElementField $
Psl.Model.Field
"providerName"
Psl.Model.String
[]
[],
Psl.Model.ElementField $
Psl.Model.Field
"providerUserId"
Psl.Model.String
[]
[],
Psl.Model.ElementField $
Psl.Model.Field
"providerData"
Psl.Model.String
[]
[ Psl.Attribute.Attribute "default" [Psl.Argument.ArgUnnamed $ Psl.Argument.StringExpr "{}"]
],
Psl.Model.ElementField $
Psl.Model.Field
"authId"
Psl.Model.String
[]
[],
Psl.Model.ElementField $
Psl.Model.Field
"auth"
(Psl.Model.UserType "Auth")
[]
[ Psl.Attribute.Attribute
"relation"
[ Psl.Argument.ArgNamed "fields" (Psl.Argument.ArrayExpr [Psl.Argument.IdentifierExpr "authId"]),
Psl.Argument.ArgNamed "references" (Psl.Argument.ArrayExpr [Psl.Argument.IdentifierExpr "id"]),
Psl.Argument.ArgNamed "onDelete" (Psl.Argument.IdentifierExpr "Cascade")
]
],
Psl.Model.ElementBlockAttribute $
Psl.Attribute.Attribute "id" [Psl.Argument.ArgUnnamed $ Psl.Argument.ArrayExpr [Psl.Argument.IdentifierExpr "providerName", Psl.Argument.IdentifierExpr "providerUserId"]]
]
)
)
makeEntity
"AuthIdentity"
[trimming|
providerName String
providerUserId String
providerData String @default("{}")
authId String
auth Auth @relation(fields: [authId], references: [id], onDelete: Cascade)

@@id([providerName, providerUserId])
|]

sessionEntity =
( "Session",
AS.Entity.makeEntity
( Psl.Model.Body
[ Psl.Model.ElementField $
Psl.Model.Field
"id"
Psl.Model.String
[]
[ Psl.Attribute.Attribute "id" [],
Psl.Attribute.Attribute "unique" []
],
Psl.Model.ElementField $
Psl.Model.Field
"expiresAt"
Psl.Model.DateTime
[]
[],
Psl.Model.ElementField $
Psl.Model.Field
"userId"
Psl.Model.String
[]
[],
Psl.Model.ElementField $
Psl.Model.Field
"auth"
(Psl.Model.UserType "Auth")
[]
[ Psl.Attribute.Attribute
"relation"
[ Psl.Argument.ArgNamed "references" (Psl.Argument.ArrayExpr [Psl.Argument.IdentifierExpr "id"]),
Psl.Argument.ArgNamed "fields" (Psl.Argument.ArrayExpr [Psl.Argument.IdentifierExpr "userId"]),
Psl.Argument.ArgNamed "onDelete" (Psl.Argument.IdentifierExpr "Cascade")
]
],
Psl.Model.ElementBlockAttribute $
Psl.Attribute.Attribute
"index"
[ Psl.Argument.ArgUnnamed $ Psl.Argument.ArrayExpr [Psl.Argument.IdentifierExpr "userId"]
]
]
)
)
makeEntity
"Session"
[trimming|
id String @id @unique
expiresAt DateTime
userId String
auth Auth @relation(references: [id], fields: [userId], onDelete: Cascade)

@@index([userId])
|]

someOtherEntity =
( "SomeOtherEntity",
AS.Entity.makeEntity
( Psl.Model.Body
[ Psl.Model.ElementField $
Psl.Model.Field
"id"
Psl.Model.Int
[]
[ Psl.Attribute.Attribute "id" [],
Psl.Attribute.Attribute
"default"
[ Psl.Argument.ArgUnnamed $ Psl.Argument.FuncExpr "autoincrement" []
]
]
]
)
)
makeEntity
"SomeOtherEntity"
[trimming|
id Int @id @default(autoincrement())
|]

makeEntity name bodyText = (name, AS.Entity.makeEntity $ getPrismaModelBody bodyText)
5 changes: 5 additions & 0 deletions waspc/test/Util/Prisma.hs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,13 @@ module Util.Prisma where

import Data.Either (fromRight)
import qualified Data.Text as T
import qualified Wasp.Psl.Ast.Model as Psl.Model
import qualified Wasp.Psl.Ast.Schema as Psl.Schema
import qualified Wasp.Psl.Parser.Model as Psl.Model.Parser
import qualified Wasp.Psl.Parser.Schema as Psl.Schema.Parser

getPrismaSchema :: T.Text -> Psl.Schema.Schema
getPrismaSchema = fromRight (error "Failed to parse Prisma schema") . Psl.Schema.Parser.parsePrismaSchema . T.unpack

getPrismaModelBody :: T.Text -> Psl.Model.Body
getPrismaModelBody = fromRight (error "Failed to parse Prisma model body") . Psl.Model.Parser.parseBody . T.unpack

0 comments on commit 362ccff

Please sign in to comment.