Clean up xml gen/parsing unit tests

This commit is contained in:
Krishnan Parthasarathi 2017-01-21 05:38:44 +05:30 committed by Aditya Manthramurthy
parent 2070a8e13f
commit 06214c1cae
2 changed files with 35 additions and 39 deletions

View File

@ -21,11 +21,19 @@ testMkCreateBucketConfig = do
assertEqual "CreateBucketConfiguration xml should match: " expected $
mkCreateBucketConfig "EU"
where
expected = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><CreateBucketConfiguration xmlns=\"http://s3.amazonaws.com/doc/2006-03-01/\"><LocationConstraint>EU</LocationConstraint></CreateBucketConfiguration>"
expected = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\
\<CreateBucketConfiguration xmlns=\"http://s3.amazonaws.com/doc/2006-03-01/\">\
\<LocationConstraint>EU</LocationConstraint>\
\</CreateBucketConfiguration>"
testMkCompleteMultipartUploadRequest :: Assertion
testMkCompleteMultipartUploadRequest =
assertEqual "completeMultipartUpload xml should match: " expected $
mkCompleteMultipartUploadRequest [PartInfo 1 "abc"]
where
expected = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><CompleteMultipartUpload><Part><PartNumber>1</PartNumber><ETag>abc</ETag></Part></CompleteMultipartUpload>"
expected = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\
\<CompleteMultipartUpload>\
\<Part>\
\<PartNumber>1</PartNumber><ETag>abc</ETag>\
\</Part>\
\</CompleteMultipartUpload>"

View File

@ -17,45 +17,33 @@ xmlParserTests = testGroup "XML Parser Tests"
, testCase "Test parseNewMultipartUpload" testParseNewMultipartUpload
]
euLocationXml :: LByteString
euLocationXml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\
\<LocationConstraint xmlns=\"http://s3.amazonaws.com/doc/2006-03-01/\">EU</LocationConstraint>"
badLocationXml :: LByteString
badLocationXml = "ClearlyInvalidXml"
usLocationXml :: LByteString
usLocationXml = "<LocationConstraint xmlns=\"http://s3.amazonaws.com/doc/2006-03-01/\"/>"
testValidParseLocation :: Assertion
testValidParseLocation = do
txt <- runExceptT $ parseLocation euLocationXml
let location = case txt of
Right loc -> loc
Left _ -> ""
(isRight txt && location == "EU") @? ("Parsing failed unexpectedly => " ++ show txt)
testInvalidParseLocation :: Assertion
testInvalidParseLocation = do
txt <- runExceptT $ parseLocation badLocationXml
(isLeft txt) @? ("Parsing succeeded unexpectedly => " ++ show txt)
testEmptyParseLocation :: Assertion
testEmptyParseLocation = do
txt <- runExceptT $ parseLocation usLocationXml
let location = case txt of
Right loc -> loc
Left _ -> ""
(isRight txt && location == "") @? ("Parsing failed unexpectedly => " ++ show txt)
testParseLocation :: Assertion
testParseLocation = do
-- 1. Test parsing of a valid location xml.
testValidParseLocation
-- 2. Test parsing of an invalid location xml.
testInvalidParseLocation
-- 3. Test parsing of a valid, empty location xml.
testEmptyParseLocation
-- 1. Test parsing of an invalid location constraint xml.
parsedLocationE <- runExceptT $ parseLocation "ClearlyInvalidXml"
case parsedLocationE of
Right loc -> assertFailure $ "Parsing should have failed => " ++ show parsedLocationE
Left _ -> return ()
forM_ cases $ \(xmldata, expectedLocation) -> do
parsedLocationE <- runExceptT $ parseLocation xmldata
case parsedLocationE of
Right parsedLocation -> parsedLocation @?= expectedLocation
_ -> assertFailure $ "Parsing failed => " ++ show parsedLocationE
where
cases = [
-- 2. Test parsing of a valid location xml.
("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\
\<LocationConstraint xmlns=\"http://s3.amazonaws.com/doc/2006-03-01/\">EU</LocationConstraint>",
"EU"
)
,
-- 3. Test parsing of a valid, empty location xml.
("<LocationConstraint xmlns=\"http://s3.amazonaws.com/doc/2006-03-01/\"/>",
""
)
]
testParseNewMultipartUpload :: Assertion
testParseNewMultipartUpload = do