I am trying to write a unit test case Below is the partial code of the original function:
def disk_space(something, otherthing):
"""
Comments
"""
dir_map = something['dir_map']
for dir, expected_free_gb in dir_map.items():
try:
result = os.statvfs(dir)
block_size = result.f_frsize
avail_blocks = result.f_bavail
giga = 1024 * 1024 * 1024
free_size = avail_blocks * block_size / giga
if free_size >= float(expected_free_gb):
print "No need to clear space"
else:
print "Clear some space"
Unit Test Function:
@patch('os.statvfs', return_value=posix.statvfs_result(f_frsize=4096, f_bavail=2599495))
def test_disk_space(self, os_statvfs):
something
something
Now here, if I return value in posix.statvfs_result
data type, it gives the error:
TypeError: Required argument 'sequence' (pos 1) not found
And if I just give return_value=(f_frsize=4096, f_bavail=2599495)
it says:
str() is not having f_frsize.
Use below function
@patch('os.statvfs', return_value=os.statvfs_result((4096, 4096, 1909350, 1491513, 1394521, 971520, 883302, 883302, 0, 255)))
def test_disk_space(self, os_statvfs):
something
something
The values are corresponding to below fields so change it accrodingly. Also, note that values should be inside double braces.
posix.statvfs_result(f_bsize=4096, f_frsize=4096, f_blocks=1909350L, f_bfree=1491513L,
f_bavail=1394521L, f_files=971520L, f_ffree=883302L, f_fvail=883302L, f_flag=0,
f_namemax=255)