Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
232 changes: 47 additions & 185 deletions src/DIRAC/RequestManagementSystem/Client/test/Test_File.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,3 @@
########################################################################
# File: FileTest.py
# Author: [email protected]
# Date: 2012/08/06 13:48:54
########################################################################

""" :mod: FileTest
=======================

Expand All @@ -19,187 +13,55 @@
from __future__ import division
__RCSID__ = "$Id$"

# #
# @file FileTest.py
# @author [email protected]
# @date 2012/08/06 13:49:05
# @brief Definition of FileTest class.
import pytest

# # imports
import unittest
# # from DIRAC
from DIRAC.RequestManagementSystem.Client.Operation import Operation
# # SUT
from DIRAC.RequestManagementSystem.Client.File import File

########################################################################


class FileTests(unittest.TestCase):
"""
.. class:: FileTest

"""

def setUp(self):
""" test setup """
self.fromDict = {
"Size": 1,
"LFN": "/test/lfn",
"ChecksumType": "ADLER32",
"Checksum": "123456",
"Status": "Waiting"}

def tearDown(self):
""" test tear down """
del self.fromDict

def test01ctors(self):
""" File construction and (de)serialisation """
# # empty default ctor
theFile = File()
self.assertEqual(isinstance(theFile, File), True)

# # fromDict
try:
theFile = File(self.fromDict)
except AttributeError as error:
print("AttributeError: %s" % str(error))

self.assertEqual(isinstance(theFile, File), True)
for key, value in self.fromDict.items():
self.assertEqual(getattr(theFile, key), value)

toJSON = theFile.toJSON()
self.assertEqual(toJSON["OK"], True, "JSON serialization error")

def test02props(self):
""" test props and attributes """
theFile = File()

# valid props
theFile.FileID = 1
self.assertEqual(theFile.FileID, 1)
theFile.Status = "Done"
self.assertEqual(theFile.Status, "Done")
theFile.LFN = "/some/path/somewhere"
self.assertEqual(theFile.LFN, "/some/path/somewhere")
theFile.PFN = "/some/path/somewhere"
self.assertEqual(theFile.PFN, "/some/path/somewhere")
theFile.Attempt = 1
self.assertEqual(theFile.Attempt, 1)
theFile.Size = 1
self.assertEqual(theFile.Size, 1)
theFile.GUID = "2bbabe80-e2f1-11e1-9b23-0800200c9a66"
self.assertEqual(theFile.GUID, "2bbabe80-e2f1-11e1-9b23-0800200c9a66")
theFile.ChecksumType = "adler32"
self.assertEqual(theFile.ChecksumType, "ADLER32")
theFile.Checksum = "123456"
self.assertEqual(theFile.Checksum, "123456")

# #
theFile.Checksum = None
theFile.ChecksumType = None
# self.assertEqual( theFile.Checksum, "" )
# self.assertEqual( theFile.ChecksumType, "" )

# # invalid props

# FileID
try:
theFile.FileID = "foo"
except Exception as error:
self.assertEqual(isinstance(error, ValueError), True)

# parent
parent = Operation({"OperationID": 99999})
parent += theFile

theFile.FileID = 0

# self.assertEqual( parent.OperationID, theFile.OperationID )
try:
theFile.OperationID = 111111
except Exception as error:
self.assertEqual(isinstance(error, AttributeError), True)
self.assertEqual(str(error), "can't set attribute")

# LFN
try:
theFile.LFN = 1
except Exception as error:
self.assertEqual(isinstance(error, TypeError), True)
self.assertEqual(str(error), "LFN has to be a string!")
try:
theFile.LFN = "../some/path"
except Exception as error:
self.assertEqual(isinstance(error, ValueError), True)
self.assertEqual(str(error), "LFN should be an absolute path!")

# PFN
try:
theFile.PFN = 1
except Exception as error:
self.assertEqual(isinstance(error, TypeError), True)
self.assertEqual(str(error), "PFN has to be a string!")
try:
theFile.PFN = "snafu"
except Exception as error:
self.assertEqual(isinstance(error, ValueError), True)
self.assertEqual(str(error), "Wrongly formatted PFN!")

# Size
try:
theFile.Size = "snafu"
except Exception as error:
self.assertEqual(isinstance(error, ValueError), True)
try:
theFile.Size = -1
except Exception as error:
self.assertEqual(isinstance(error, ValueError), True)
self.assertEqual(str(error), "Size should be a positive integer!")

# GUID
try:
theFile.GUID = "snafuu-uuu-uuu-uuu-uuu-u"
except Exception as error:
self.assertEqual(isinstance(error, ValueError), True)
self.assertEqual(str(error), "'snafuu-uuu-uuu-uuu-uuu-u' is not a valid GUID!")
try:
theFile.GUID = 2233345
except Exception as error:
self.assertEqual(isinstance(error, TypeError), True)
self.assertEqual(str(error), "GUID should be a string!")

# Attempt
try:
theFile.Attempt = "snafu"
except Exception as error:
self.assertEqual(isinstance(error, ValueError), True)
try:
theFile.Attempt = -1
except Exception as error:
self.assertEqual(isinstance(error, ValueError), True)
self.assertEqual(str(error), "Attempt should be a positive integer!")

# Status
try:
theFile.Status = None
except Exception as error:
self.assertEqual(isinstance(error, ValueError), True)
self.assertEqual(str(error), "Unknown Status: None!")

# Error
try:
theFile.Error = Exception("test")
except Exception as error:
self.assertEqual(isinstance(error, TypeError), True)
self.assertEqual(str(error), "Error has to be a string!")


# # test execution
if __name__ == "__main__":
testLoader = unittest.TestLoader()
fileTests = testLoader.loadTestsFromTestCase(FileTests)
suite = unittest.TestSuite([fileTests])
unittest.TextTestRunner(verbosity=3).run(suite)
def test_ctors():
""" File construction and (de)serialisation """
theFile = File()
assert isinstance(theFile, File)

fromDict = {
"Size": 1,
"LFN": "/test/lfn",
"ChecksumType": "ADLER32",
"Checksum": "123456",
"Status": "Waiting",
}
try:
theFile = File(fromDict)
except AttributeError as error:
print("AttributeError: %s" % str(error))

assert isinstance(theFile, File)
for key, value in fromDict.items():
assert getattr(theFile, key) == value

toJSON = theFile.toJSON()
assert toJSON["OK"], "JSON serialization error"


def test_valid_properties():
theFile = File()

theFile.FileID = 1
assert theFile.FileID == 1
theFile.Status = "Done"
assert theFile.Status == "Done"
theFile.LFN = "/some/path/somewhere"
assert theFile.LFN == "/some/path/somewhere"
theFile.PFN = "/some/path/somewhere"
assert theFile.PFN == "/some/path/somewhere"
theFile.Attempt = 1
assert theFile.Attempt == 1
theFile.Size = 1
assert theFile.Size == 1
theFile.GUID = "2bbabe80-e2f1-11e1-9b23-0800200c9a66"
assert theFile.GUID == "2bbabe80-e2f1-11e1-9b23-0800200c9a66"
theFile.ChecksumType = "adler32"
assert theFile.ChecksumType == "ADLER32"
theFile.Checksum = "123456"
assert theFile.Checksum == "123456"
Loading