-
Notifications
You must be signed in to change notification settings - Fork 8
upgrade for purescript 0.10 #25
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
exports.traceAny = function (a) { | ||
return function () { | ||
console.log(a); | ||
return {}; | ||
}; | ||
}; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,12 +2,12 @@ module Database.Mongo.Results | |
( WriteResult() | ||
) where | ||
|
||
import Prelude | ||
import Data.Argonaut ((.?), jsonEmptyObject) | ||
import Prelude (pure, bind, ($)) | ||
import Data.Argonaut ((.?), (:=), (~>), jsonEmptyObject) | ||
import Data.Argonaut.Encode (class EncodeJson) | ||
import Data.Argonaut.Decode (class DecodeJson, decodeJson) | ||
import Data.Either | ||
import Data.Maybe | ||
import Data.Either (Either(..)) | ||
import Data.Maybe (Maybe(..), fromMaybe) | ||
|
||
newtype WriteResult = WriteResult | ||
{ success :: Boolean | ||
|
@@ -33,14 +33,23 @@ instance decodeJsonWriteResult :: DecodeJson WriteResult where | |
} | ||
|
||
instance encodeJsonWriteResult :: EncodeJson WriteResult where | ||
encodeJson (WriteResult w) = jsonEmptyObject | ||
encodeJson (WriteResult w) | ||
= "ok" := boolToJsNumber w.success | ||
~> "n" := w.total | ||
~> "nInserted" := fromMaybe 0.0 w.inserted | ||
~> "nModified" := fromMaybe 0.0 w.modified | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 Awesome, I noticed this was broken as well. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks :) BTW, I have a question: I used There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think that's purescript works. |
||
~> jsonEmptyObject | ||
|
||
boolToJsNumber :: Boolean -> Int | ||
boolToJsNumber false = 0 | ||
boolToJsNumber true = 1 | ||
|
||
-- node mongodb module sends back `1` to mean `true`, this is why we need types | ||
-- as Javascript is abused! | ||
jsNumberToBool :: Either String Int -> Boolean | ||
jsNumberToBool :: Int -> Boolean | ||
jsNumberToBool e = case e of | ||
Left _ -> false | ||
Right x -> if x == 1 then true else false | ||
1 -> true | ||
_ -> false | ||
|
||
extract :: Either String Number -> Maybe Number | ||
extract e = case e of | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
module Test.Data.Event where | ||
|
||
import Prelude | ||
import Data.Argonaut (jsonEmptyObject, (~>), (:=), (.?)) | ||
import Data.Argonaut.Encode.Class (class EncodeJson) | ||
import Data.Argonaut.Decode (decodeJson) | ||
import Data.Argonaut.Decode.Class (class DecodeJson) | ||
import Data.Maybe (Maybe) | ||
|
||
newtype Event = Event | ||
{ name :: Maybe String | ||
} | ||
|
||
instance decodeJsonEvent :: DecodeJson Event where | ||
decodeJson json = do | ||
obj <- decodeJson json | ||
name <- obj .? "name" | ||
pure $ Event | ||
{ name : name | ||
} | ||
|
||
instance encodeJsonEvent :: EncodeJson Event where | ||
encodeJson (Event e) | ||
= "name" := e.name | ||
~> jsonEmptyObject | ||
|
||
instance showEvent :: Show Event where | ||
show (Event e) = "Event " <> | ||
"{ name: " <> show e.name <> | ||
"}" | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
module Test.Database.Mongo.Find where | ||
|
||
import Prelude (Unit, bind, (>), ($)) | ||
import Data.Array (length) | ||
import Data.Maybe (Maybe(..)) | ||
import Database.Mongo.Bson.BsonValue ((:=)) | ||
import Control.Monad.Eff.Class (liftEff) | ||
import Control.Monad.Eff.Console (log) | ||
import Test.Data.Event (Event(..)) | ||
import Test.Assert (assert) | ||
import Database.Mongo.Mongo (Database, collect, find, collection) | ||
import Test.Type (Test) | ||
|
||
evt :: Event | ||
evt = Event | ||
{ name : Just "Wow" | ||
} | ||
|
||
main :: Database -> Test Unit | ||
main database = do | ||
liftEff $ log "should find" | ||
col <- collection "events" database | ||
cur <- find [ "name" := "Wow" ] [ "name" := 1.0 ] col | ||
res <- collect cur | ||
liftEff $ assert $ length (res :: Array Event) > 0 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
module Test.Database.Mongo.Insert where | ||
|
||
import Prelude (Unit, bind, ($), (==), (<<<)) | ||
import Data.Maybe (Maybe(..)) | ||
import Data.Tuple (Tuple) | ||
import Data.StrMap (fromFoldable) | ||
import Data.Argonaut (fromObject, (:=)) | ||
import Data.Argonaut.Encode (encodeJson) | ||
import Data.Argonaut.Core (Json) | ||
import Database.Mongo.Mongo (Database, insertOne, collection) | ||
import Database.Mongo.Options (defaultInsertOptions) | ||
import Control.Monad.Eff.Class (liftEff) | ||
import Control.Monad.Eff.Console (log) | ||
|
||
import Test.Data.Event (Event(..)) | ||
import Test.Assert | ||
import Test.Type (Test) | ||
|
||
evt :: Event | ||
evt = Event | ||
{ name : Just "Wow" | ||
} | ||
|
||
obj :: Array (Tuple String Json) -> Json | ||
obj = fromObject <<< fromFoldable | ||
|
||
main :: Database -> Test Unit | ||
main database = do | ||
liftEff $ log "should insert" | ||
col <- collection "events" database | ||
res <- insertOne evt defaultInsertOptions col | ||
liftEff $ assert $ (obj [ "ok" := 1, "n" := 1, "nInserted" := 0, "nModified" := 0]) == (encodeJson res) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
module Test.Database.Mongo.Update where | ||
|
||
import Prelude (Unit, bind, (==), ($), (<<<)) | ||
import Data.Maybe (Maybe(..)) | ||
import Data.Tuple (Tuple) | ||
import Data.StrMap (fromFoldable) | ||
import Data.Argonaut as A | ||
import Data.Argonaut.Core (Json) | ||
import Data.Argonaut.Encode (encodeJson) | ||
import Control.Monad.Eff.Class (liftEff) | ||
import Control.Monad.Eff.Console (log) | ||
import Test.Data.Event (Event(..)) | ||
import Test.Assert (assert) | ||
import Database.Mongo.Mongo (Database, updateOne, collection) | ||
import Database.Mongo.Bson.BsonValue as B | ||
import Database.Mongo.Options | ||
import Test.Type (Test) | ||
|
||
evt :: Event | ||
evt = Event | ||
{ name : Just "Wow" | ||
} | ||
|
||
obj :: Array (Tuple String Json) -> Json | ||
obj = A.fromObject <<< fromFoldable | ||
|
||
main :: Database -> Test Unit | ||
main database = do | ||
liftEff $ log "should update" | ||
col <- collection "events" database | ||
res <- updateOne ["name" B.:= "Wow"] ["name" B.:= "lol"] defaultUpdateOptions col | ||
liftEff $ assert $ (obj [ "ok" A.:= 1, "n" A.:= 1, "nModified" A.:= 1, "nInserted" A.:= 0]) == (encodeJson res) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Didn't know you could do that with infixed symbols. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't like this actually.
That's why I used this workaround here which will encode the What do you think? Is there better pattern for asserting the result? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Probably is, but let's leave it for now. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,44 @@ | ||
module Test.Main where | ||
|
||
import Control.Monad.Eff.Console | ||
import Prelude | ||
import Data.Either (Either(..)) | ||
import Control.Monad.Eff (Eff) | ||
import Control.Monad.Eff.Console (log) as Eff | ||
import Control.Monad.Eff.Exception (EXCEPTION, Error) | ||
import Control.Monad.Aff (Aff, launchAff, attempt) | ||
import Control.Monad.Aff.Console (CONSOLE, log, logShow) | ||
import Database.Mongo.Mongo (DB, Database, close, connect) | ||
import Test.Database.Mongo.Insert as Insert | ||
import Test.Database.Mongo.Find as Find | ||
import Test.Database.Mongo.Update as Update | ||
import Test.Assert (ASSERT) | ||
import Test.Type (Test) | ||
|
||
main = log "Write some tests." | ||
connectDB :: forall e. String -> Aff ( db :: DB | e) (Either Error Database) | ||
connectDB dbUri = attempt $ connect dbUri | ||
|
||
testInsert :: Database -> Test Unit | ||
testInsert = Insert.main | ||
|
||
testFind :: Database -> Test Unit | ||
testFind = Find.main | ||
|
||
testUpdate :: Database -> Test Unit | ||
testUpdate = Update.main | ||
|
||
uri :: String | ||
uri = "mongodb://127.0.0.1:27017/purescript-node-mongodb-test" | ||
|
||
main :: Eff (console :: CONSOLE, db :: DB, err :: EXCEPTION, assert :: ASSERT) Unit | ||
main = do | ||
Eff.log "Testing purescript-node-mongodb" | ||
void $ launchAff do | ||
log "connecting db" | ||
eitherDatabase <- connectDB uri | ||
case eitherDatabase of | ||
Left error -> logShow error | ||
Right database -> do | ||
testInsert database | ||
testFind database | ||
testUpdate database | ||
close database |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
module Test.Type where | ||
|
||
import Control.Monad.Aff (Aff) | ||
import Control.Monad.Aff.Console (CONSOLE) | ||
import Database.Mongo.Mongo (DB) | ||
import Test.Assert (ASSERT) | ||
|
||
type Test a = forall e. Aff (console :: CONSOLE, db :: DB, assert :: ASSERT | e) a |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Delete this line pls.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since these examples are broken and I've basically converted all the examples into test cases, do you think we still need them?
We could just add a link to Readme.MD, saying "please check out the tests for more examples?"
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, I think that's a good idea!