-- | Tests that querying any existing fields, the following three
-- properties hold: RSNormal corresponds to a Just value, any other
-- value corresponds to Nothing, and for a RSNormal and value field,
-- the type of the value corresponds to the type of the field as
-- declared in the FieldDefinition.
prop_queryNode_types :: Property
prop_queryNode_types =
  forAll (choose (0, maxNodes)) $ \numnodes ->
  forAll (genEmptyCluster numnodes) $ \cfg ->
  forAll (elements (Map.keys Node.fieldsMap)) $ \field -> monadicIO $ do
  QueryResult fdefs fdata <-
    run (query cfg False (Query (ItemTypeOpCode QRNode)
                          [field] EmptyFilter)) >>= resultProp
  _ <- stop $ conjoin
         [ counterexample ("Inconsistent result entries (" ++ show fdata ++ ")")
           (conjoin $ map (conjoin . zipWith checkResultType fdefs) fdata)
         , counterexample "Wrong field definitions length"
           (length fdefs ==? 1)
         , counterexample "Wrong field result rows length"
           (all ((== 1) . length) fdata)
         , counterexample "Wrong number of result rows"
           (length fdata ==? numnodes)
         ]
  return ()

-- | Test that queryFields with empty fields list returns all node fields.
prop_queryNode_types :: Property
prop_queryNode_types =
  forAll (choose (0, maxNodes)) $ \numnodes ->
  forAll (genEmptyCluster numnodes) $ \cfg ->
  forAll (elements (Map.keys Node.fieldsMap)) $ \field -> monadicIO $ do
  QueryResult fdefs fdata <-
    run (query cfg False (Query (ItemTypeOpCode QRNode)
                          [field] EmptyFilter)) >>= resultProp
  _ <- stop $ conjoin
         [ counterexample ("Inconsistent result entries (" ++ show fdata ++ ")")
           (conjoin $ map (conjoin . zipWith checkResultType fdefs) fdata)
         , counterexample "Wrong field definitions length"
           (length fdefs ==? 1)
         , counterexample "Wrong field result rows length"
           (all ((== 1) . length) fdata)
         , counterexample "Wrong number of result rows"
           (length fdata ==? numnodes)
         ]
  return ()

-- | Test that queryFields with empty fields list returns all node fields.
prop_queryNode_types =
  forAll (choose (0, maxNodes)) $ \numnodes ->
  forAll (genEmptyCluster numnodes) $ \cfg ->
  forAll (elements (Map.keys Node.fieldsMap)) $ \field -> monadicIO $ do
  QueryResult fdefs fdata <-
    run (query cfg False (Query (ItemTypeOpCode QRNode)
                          [field] EmptyFilter)) >>= resultProp
  _ <- stop $ conjoin
         [ counterexample ("Inconsistent result entries (" ++ show fdata ++ ")")
           (conjoin $ map (conjoin . zipWith checkResultType fdefs) fdata)
         , counterexample "Wrong field definitions length"
           (length fdefs ==? 1)
         , counterexample "Wrong field result rows length"
           (all ((== 1) . length) fdata)
         , counterexample "Wrong number of result rows"
           (length fdata ==? numnodes)
         ]
  return ()

-- | Test that queryFields with empty fields list returns all node fields.