-- | Tests job status equivalence with Python. Very similar to OpCodes test.
case_JobStatusPri_py_equiv :: Assertion
case_JobStatusPri_py_equiv = do
  let num_jobs = 2000::Int
  jobs <- genSample (vectorOf num_jobs $ do
                       num_ops <- choose (1, 5)
                       ops <- vectorOf num_ops genQueuedOpCode
                       jid <- genJobId
                       return $ QueuedJob jid ops justNoTs justNoTs justNoTs
                                          Nothing Nothing)
  let serialized = encode jobs
  -- check for non-ASCII fields, usually due to 'arbitrary :: String'
  mapM_ (\job -> when (any (not . isAscii) (encode job)) .
                 assertFailure $ "Job has non-ASCII fields: " ++ show job
        ) jobs
  py_stdout <-
     runPython "from ganeti import jqueue\n\
               \from ganeti import serializer\n\
               \import sys\n\
               \job_data = serializer.Load(sys.stdin.read())\n\
               \decoded = [jqueue._QueuedJob.Restore(None, o, False, False)\n\
               \           for o in job_data]\n\
               \encoded = [(job.CalcStatus(), job.CalcPriority())\n\
               \           for job in decoded]\n\
               \sys.stdout.buffer.write(serializer.Dump(encoded))" serialized
     >>= checkPythonResult
  let deserialised = decode py_stdout::Text.JSON.Result [(String, Int)]
  decoded <- case deserialised of
               Text.JSON.Ok jobs' -> return jobs'
               Error msg ->
                 assertFailure ("Unable to decode jobs: " ++ msg)
                 -- this already raised an exception, but we need it
                 -- for proper types
                 >> fail "Unable to decode jobs"
  assertEqual "Mismatch in number of returned jobs"
    (length decoded) (length jobs)
  mapM_ (\(py_sp, job) ->
           let hs_sp = (jobStatusToRaw $ calcJobStatus job,
                        calcJobPriority job)
           in assertEqual ("Different result after encoding/decoding for " ++
                           show job) hs_sp py_sp
        ) $ zip decoded jobs

-- | Tests listing of Job ids.
case_JobStatusPri_py_equiv :: Assertion
case_JobStatusPri_py_equiv = do
  let num_jobs = 2000::Int
  jobs <- genSample (vectorOf num_jobs $ do
                       num_ops <- choose (1, 5)
                       ops <- vectorOf num_ops genQueuedOpCode
                       jid <- genJobId
                       return $ QueuedJob jid ops justNoTs justNoTs justNoTs
                                          Nothing Nothing)
  let serialized = encode jobs
  -- check for non-ASCII fields, usually due to 'arbitrary :: String'
  mapM_ (\job -> when (any (not . isAscii) (encode job)) .
                 assertFailure $ "Job has non-ASCII fields: " ++ show job
        ) jobs
  py_stdout <-
     runPython "from ganeti import jqueue\n\
               \from ganeti import serializer\n\
               \import sys\n\
               \job_data = serializer.Load(sys.stdin.read())\n\
               \decoded = [jqueue._QueuedJob.Restore(None, o, False, False)\n\
               \           for o in job_data]\n\
               \encoded = [(job.CalcStatus(), job.CalcPriority())\n\
               \           for job in decoded]\n\
               \sys.stdout.buffer.write(serializer.Dump(encoded))" serialized
     >>= checkPythonResult
  let deserialised = decode py_stdout::Text.JSON.Result [(String, Int)]
  decoded <- case deserialised of
               Text.JSON.Ok jobs' -> return jobs'
               Error msg ->
                 assertFailure ("Unable to decode jobs: " ++ msg)
                 -- this already raised an exception, but we need it
                 -- for proper types
                 >> fail "Unable to decode jobs"
  assertEqual "Mismatch in number of returned jobs"
    (length decoded) (length jobs)
  mapM_ (\(py_sp, job) ->
           let hs_sp = (jobStatusToRaw $ calcJobStatus job,
                        calcJobPriority job)
           in assertEqual ("Different result after encoding/decoding for " ++
                           show job) hs_sp py_sp
        ) $ zip decoded jobs

-- | Tests listing of Job ids.
case_JobStatusPri_py_equiv = do
  let num_jobs = 2000::Int
  jobs <- genSample (vectorOf num_jobs $ do
                       num_ops <- choose (1, 5)
                       ops <- vectorOf num_ops genQueuedOpCode
                       jid <- genJobId
                       return $ QueuedJob jid ops justNoTs justNoTs justNoTs
                                          Nothing Nothing)
  let serialized = encode jobs
  -- check for non-ASCII fields, usually due to 'arbitrary :: String'
  mapM_ (\job -> when (any (not . isAscii) (encode job)) .
                 assertFailure $ "Job has non-ASCII fields: " ++ show job
        ) jobs
  py_stdout <-
     runPython "from ganeti import jqueue\n\
               \from ganeti import serializer\n\
               \import sys\n\
               \job_data = serializer.Load(sys.stdin.read())\n\
               \decoded = [jqueue._QueuedJob.Restore(None, o, False, False)\n\
               \           for o in job_data]\n\
               \encoded = [(job.CalcStatus(), job.CalcPriority())\n\
               \           for job in decoded]\n\
               \sys.stdout.buffer.write(serializer.Dump(encoded))" serialized
     >>= checkPythonResult
  let deserialised = decode py_stdout::Text.JSON.Result [(String, Int)]
  decoded <- case deserialised of
               Text.JSON.Ok jobs' -> return jobs'
               Error msg ->
                 assertFailure ("Unable to decode jobs: " ++ msg)
                 -- this already raised an exception, but we need it
                 -- for proper types
                 >> fail "Unable to decode jobs"
  assertEqual "Mismatch in number of returned jobs"
    (length decoded) (length jobs)
  mapM_ (\(py_sp, job) ->
           let hs_sp = (jobStatusToRaw $ calcJobStatus job,
                        calcJobPriority job)
           in assertEqual ("Different result after encoding/decoding for " ++
                           show job) hs_sp py_sp
        ) $ zip decoded jobs

-- | Tests listing of Job ids.