def setUp(self): """Define some sample data that will be used by the tests.""" self.res1 = RichnessEstimatesResults() self.res2 = RichnessEstimatesResults() self.res2.addSample('S2', 52) self.res2.addSampleEstimate('S2', 1, 3, 0.4, 2.5, 3.5) self.res2.addSample('S1', 42) self.res2.addSampleEstimate('S1', 10, 20, 2.5, 2.5, 3.5) self.res2.addSampleEstimate('S1', 20, 30, 3.5, 2.5, 3.5) self.res2.addSampleEstimate('S1', 5, 21, 1.5, 2.5, 3.5)
def test_toTable(self): """Test writing results container to a table.""" # Empty results. out_f = StringIO() self.res1.toTable(out_f) self.assertEqual( out_f.getvalue(), "SampleID\tSize\tEstimate\tStd Err\tCI (lower)\tCI (upper)\n") out_f.close() # Results with multiple samples. exp = """SampleID\tSize\tEstimate\tStd Err\tCI (lower)\tCI (upper) S1\t5\t21\t1.5\t2.5\t3.5 S1\t10\t20\t2.5\t2.5\t3.5 S1\t20\t30\t3.5\t2.5\t3.5 S2\t1\t3\t0.4\t2.5\t3.5 """ out_f = StringIO() self.res2.toTable(out_f) self.assertEqual(out_f.getvalue(), exp) out_f.close() # Custom header. exp = """foo\tbar\tbaz\tbazaar\tbazaaar\tbazaaaar S1\t5\t21\t1.5\t2.5\t3.5 """ out_f = StringIO() self.res1.addSample('S1', 42) self.res1.addSampleEstimate('S1', 5, 21, 1.5, 2.5, 3.5) self.res1.toTable( out_f, header=['foo', 'bar', 'baz', 'bazaar', 'bazaaar', 'bazaaaar']) self.assertEqual(out_f.getvalue(), exp) out_f.close() # Invalid header. with self.assertRaises(ValueError): out_f = StringIO() self.res1.toTable(out_f, header=['foo']) # Cells with None as their value. exp = """SampleID\tSize\tEstimate\tStd Err\tCI (lower)\tCI (upper) S1\t43\tN/A\tN/A\tN/A\tN/A """ out_f = StringIO() res = RichnessEstimatesResults() res.addSample('S1', 42) res.addSampleEstimate('S1', 43, None, None, None, None) res.toTable(out_f) self.assertEqual(out_f.getvalue(), exp) out_f.close()