Skip to content

Commit

Permalink
add test case
Browse files Browse the repository at this point in the history
  • Loading branch information
jswhit2 committed Oct 21, 2024
1 parent 0deeb12 commit 3d9b9d9
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions test/test_get_fill_value.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import unittest, os, tempfile
import netCDF4
from numpy.testing import assert_array_equal
import numpy as np

fill_val = np.array(9.9e31)

class TestGetFillValue(unittest.TestCase):
def setUp(self):
self.testfile = tempfile.NamedTemporaryFile(suffix='.nc', delete=False).name
f = netCDF4.Dataset(self.testfile, 'w')
dim = f.createDimension('x',10)
for dt in netCDF4.default_fillvals.keys():
if not dt.startswith('c'):
v = f.createVariable(dt+'_var',dt,dim)
v = f.createVariable('float_var',np.float64,dim,fill_value=fill_val)
f.close()

def tearDown(self):
os.remove(self.testfile)

def runTest(self):
f = netCDF4.Dataset(self.testfile, "r")
# no _FillValue set, test that default fill value returned
for dt in netCDF4.default_fillvals.keys():
if not dt.startswith('c'):
fillval = np.array(netCDF4.default_fillvals[dt])
if dt == 'S1': fillval = fillval.astype(dt)
v = f[dt+'_var']
assert_array_equal(fillval, v.get_fill_value())
# _FillValue attribute is set.
v = f['float_var']
assert_array_equal(fill_val, v.get_fill_value())

if __name__ == '__main__':
unittest.main()

0 comments on commit 3d9b9d9

Please sign in to comment.