def test_change(self): "Test the change() function for various variables" infile = os.path.join("input", "satbang_test_rst") infile1 = os.path.join("outdir", "satbang_test_rst") outexp1 = os.path.join("outexp", "satbang_test_rst") outfil = os.path.join("outdir", "satbang_test.new") outexp = os.path.join("outexp", "satbang_test.new") # rewrite input to remove comments and extraneous blanks # (for diff'ing purposes while verifying test) sb1 = SatBangRst(infile) sb1.write(infile1) self.assertTrue(filecmp.cmp(infile1, outexp1)) # change instrument in record #1 sb2 = SatBangRst(infile1) (key, values) = sb2.key_and_values(0) (instrument, channel) = key sb2.change(key, "instrument", "istoo_n09") # change channel in record #2 (key, values) = sb2.key_and_values(1) (instrument, channel) = key sb2.change(key, "channel", "12") # change tlapmean in record #3 (key, values) = sb2.key_and_values(2) sb2.change(key, "tlapmean", "0.455304E-01") # change coeffs in record #4 (key, values) = sb2.key_and_values(3) coeffs = ["%7.3f"%(((-1)**n)*float(n)/8) for n in range(90)] sb2.change(key, "coeffs", coeffs) # change key in record #5 (key, values) = sb2.key_and_values(4) sb2.change(key, "key", ("amsub_n61", "44")) # write output sb2.write(outfil) # compare to expected output self.assertTrue(filecmp.cmp(outfil, outexp)) os.remove(infile1) os.remove(outfil)
def test_ioerror_coeffs(self): "Load() should fail if coeffs does not have 90 elements" infile = os.path.join("input", "satbang_test_rst") outfil = os.path.join("outdir", "satbang_rst.ioerror_coeffs") outexp = os.path.join("outexp", "satbang_rst.ioerror_coeffs") # blank last value of coeff sb = SatBangRst(infile) (key, values) = sb.key_and_values(10) values["coeffs"][89] = "" sb.change(key, "coeffs", values["coeffs"]) # this write works because coeffs is full length but with a blank sb.write(outfil) self.assertTrue(filecmp.cmp(outfil, outexp)) # attempt to load faulty output file self.assertRaises(Exception, SatBangRst, outfil) os.remove(outfil) # attempt to replace coeffs with write file with faulty record values["coeffs"].pop(); self.assertRaises(Exception, sb.change, key, "coeffs", values["coeffs"])