Revision 90634d95 htest/Test/Ganeti/Types.hs

b/htest/Test/Ganeti/Types.hs
47 47
import Ganeti.BasicTypes
48 48
import qualified Ganeti.Constants as C
49 49
import Ganeti.Types as Types
50
import Ganeti.JSON
50 51

  
51 52
{-# ANN module "HLint: ignore Use camelCase" #-}
52 53

  
......
343 344
-- | Tests JobId serialisation (both from string and ints).
344 345
prop_JobId_serialisation :: JobId -> Property
345 346
prop_JobId_serialisation jid =
346
  testSerialisation jid .&&.
347
  (J.readJSON . J.showJSON . show $ fromJobId jid) ==? J.Ok jid
347
  conjoin [ testSerialisation jid
348
          , (J.readJSON . J.showJSON . show $ fromJobId jid) ==? J.Ok jid
349
          , case (fromJVal . J.showJSON . negate $
350
                  fromJobId jid)::Result JobId of
351
              Bad _ -> passTest
352
              Ok jid' -> failTest $ "Parsed negative job id as id " ++
353
                         show (fromJobId jid')
354
          ]
355

  
356
-- | Tests that fractional job IDs are not accepted.
357
prop_JobId_fractional :: Property
358
prop_JobId_fractional =
359
  forAll (arbitrary `suchThat`
360
          (\d -> fromIntegral (truncate d::Int) /= d)) $ \d ->
361
  case J.readJSON (J.showJSON (d::Double)) of
362
    J.Error _ -> passTest
363
    J.Ok jid -> failTest $ "Parsed fractional value " ++ show d ++
364
                " as job id " ++ show (fromJobId jid)
365

  
366
-- | Tests that a job ID is not parseable from \"bad\" JSON values.
367
case_JobId_BadTypes :: Assertion
368
case_JobId_BadTypes = do
369
  let helper jsval = case J.readJSON jsval of
370
                       J.Error _ -> return ()
371
                       J.Ok jid -> assertFailure $ "Parsed " ++ show jsval
372
                                   ++ " as job id " ++ show (fromJobId jid)
373
  helper J.JSNull
374
  helper (J.JSBool True)
375
  helper (J.JSBool False)
376
  helper (J.JSArray [])
348 377

  
349 378
-- | Test 'JobDependency' serialisation.
350 379
prop_JobDependency_serialisation :: JobDependency -> Property
......
398 427
  , 'prop_FinalizedJobStatus_serialisation
399 428
  , 'case_FinalizedJobStatus_pyequiv
400 429
  , 'prop_JobId_serialisation
430
  , 'prop_JobId_fractional
431
  , 'case_JobId_BadTypes
401 432
  , 'prop_JobDependency_serialisation
402 433
  , 'prop_OpSubmitPriority_serialisation
403 434
  , 'prop_ELogType_serialisation

Also available in: Unified diff