我正在考虑创建一些子类Django模型字段并将它们作为包发布在PyPI上.我喜欢为我的代码编写单元测试(例如TDD),但我对于如何为这个特定的库编写测试感到有些困惑.
我想到的第一个想法是创建一个Django项目,它使用我的子类并只使用Django测试工具,但这看起来并不优雅.必须有更好的方法!
有没有一种方法以某种方式引导Django这种类型的东西?我很感激有人指着我正确的方向.谢谢!
我有一些看起来像这样的代码,忽略了与我的问题无关的所有代码:
import qualified Control.Monad.Reader as Reader
data FooEnv = FooEnv { bar :: Int -> Int }
type FooReader = Reader.Reader FooEnv
foo :: Int -> FooReader String
foo i = Reader.liftM show $ bar' i
where
bar' i' = do
bar'' <- Reader.asks bar
return $ bar'' i'
Run Code Online (Sandbox Code Playgroud)
有没有办法重构这个?具体来说,嵌套bar'
函数最让我困扰.这可以浓缩成一行吗?
在Yesod书的Persistent章节中,给出了这个实体的一个例子
{-# LANGUAGE QuasiQuotes, TypeFamilies, GeneralizedNewtypeDeriving, TemplateHaskell, OverloadedStrings, GADTs #-}
import Database.Persist
import Database.Persist.TH
import Database.Persist.Sqlite
import Control.Monad.IO.Class (liftIO)
mkPersist sqlSettings [persist|
Person
name String
age Int
deriving Show
|]
Run Code Online (Sandbox Code Playgroud)
生成代码
{-# LANGUAGE TypeFamilies, GeneralizedNewtypeDeriving, OverloadedStrings, GADTs #-}
import Database.Persist
import Database.Persist.Store
import Database.Persist.Sqlite
import Database.Persist.GenericSql.Raw (SqlBackend)
import Database.Persist.EntityDef
import Control.Monad.IO.Class (liftIO)
import Control.Applicative
data Person = Person
{ personName :: String
, personAge :: Int
}
deriving (Show, Read, Eq)
type PersonId = Key Person …
Run Code Online (Sandbox Code Playgroud) 所以我正在使用Data.Set.Monad,它似乎不像Data.Set那样是Data.Foldable的实例.我决定尝试自己添加这个实例作为实验:
import Data.Foldable (Foldable, foldr)
import qualified Data.Set.Monad as S (Set, foldr)
instance Foldable S.Set where
foldr = S.foldr
Run Code Online (Sandbox Code Playgroud)
我收到编译错误:
No instance for (Ord a) arising from a use of ‘S.foldr’
Possible fix:
add (Ord a) to the context of
the type signature for foldr :: (a -> b -> b) -> b -> Set a -> b
In the expression: S.foldr
In an equation for ‘foldr’: foldr = S.foldr
In the instance declaration …
Run Code Online (Sandbox Code Playgroud) haskell ×2
django ×1
do-notation ×1
entity ×1
monads ×1
persistent ×1
reader-monad ×1
refactoring ×1
subclass ×1
typeclass ×1
unit-testing ×1
yesod ×1