Package Martel :: Package test :: Module test_RecordReader
[hide private]
[frames] | no frames]

Source Code for Module Martel.test.test_RecordReader

  1  import os, string 
  2  import Martel 
  3  from Martel import RecordReader 
  4  from cStringIO import StringIO 
  5   
6 -def test_count(reader, check_remainder = 1):
7 count = 0 8 while 1: 9 record = reader.next() 10 if record is None: 11 if check_remainder: 12 infile, text = reader.remainder() 13 if text: 14 raise AssertionError, text 15 line = infile.readline() 16 if line: 17 raise AssertionError, repr(line) 18 return count 19 #print repr(record) 20 #print string.split(record, "\n")[0] 21 count = count + 1
22
23 -def test_fasta():
24 s = """\ 25 >seq1 26 SEQUENCE 27 28 >seq2 29 MULTI 30 LINE 31 SEQUENCE 32 33 >seq3 34 35 >seq4 36 37 """ 38 reader = RecordReader.StartsWith(StringIO(s), ">") 39 assert test_count(reader) == 4 40 41 reader = RecordReader.StartsWith(StringIO(""), ">") 42 assert test_count(reader) == 0 43 44 reader = RecordReader.StartsWith(StringIO(">seq\n"), ">") 45 assert test_count(reader) == 1
46 47 sp_sample = os.path.join("samples", "sample.swissprot") 48
49 -def test_start():
50 reader = RecordReader.StartsWith(open(sp_sample), "ID") 51 assert test_count(reader, 0) == 8
52
53 -def test_start_lines():
54 lookahead = open(sp_sample).read() 55 reader = RecordReader.StartsWith(StringIO(""), "ID", lookahead = lookahead) 56 assert test_count(reader) == 8
57
58 -def test_end():
59 reader = RecordReader.EndsWith(open(sp_sample), "//\n") 60 assert test_count(reader) == 8
61
62 -def test_end_lines():
63 lookahead = open(sp_sample).read() 64 reader = RecordReader.EndsWith(StringIO(""), "//\n", lookahead = lookahead) 65 assert test_count(reader) == 8
66 67
68 -def test_until():
69 s = "a\nID\nb\nID\n" 70 reader = RecordReader.Until(StringIO(s), "ID") 71 assert test_count(reader, check_remainder = 0) == 1 72 assert reader.remainder()[1]
73
74 -def test_until_lines():
75 lookahead = "a\nID\nb\nID\n" 76 reader = RecordReader.Until(StringIO(""), "ID", lookahead = lookahead) 77 assert test_count(reader, check_remainder = 0) == 1 78 assert reader.remainder()[1]
79
80 -def test_count_lines():
81 s = "1\n2\n3\n4\n5\n6\n7\n8\n" 82 reader = RecordReader.CountLines(StringIO(s), 2) 83 assert test_count(reader) == 4
84
85 -def test_count_lines_lines():
86 lookahead = "1\n2\n3\n4\n5\n6\n7\n8\n" 87 reader = RecordReader.CountLines(StringIO(""), 2, lookahead = lookahead) 88 assert test_count(reader) == 4
89
90 -def test_nothing():
91 s = "1\n2\n3\n4\n5\n6\n7\n8\n" 92 infile = StringIO(s) 93 reader = RecordReader.Nothing(infile) 94 assert test_count(reader, check_remainder = 0) == 0 95 assert infile.readline() == "1\n"
96
97 -def test_nothing_lines():
98 lookahead = "1\n2\n3\n4\n5\n6\n7\n8\n" 99 reader = RecordReader.Nothing(StringIO(""), lookahead = lookahead) 100 assert test_count(reader, check_remainder = 0) == 0 101 file, result = reader.remainder() 102 assert result == lookahead, (result, lookahead)
103 104
105 -def test_everything():
106 s = "1\n2\n3\n4\n5\n6\n7\n8\n" 107 infile = StringIO(s) 108 reader = RecordReader.Everything(infile) 109 assert test_count(reader) == 1 110 assert not infile.readline()
111
112 -def test_everything_lines():
113 lookahead = "1\n2\n3\n4\n5\n6\n7\n8\n" 114 reader = RecordReader.Everything(StringIO(""), lookahead = lookahead) 115 assert test_count(reader) == 1
116
117 -def test():
118 test_start() 119 test_start_lines() 120 test_end() 121 test_end_lines() 122 test_until() 123 test_until_lines() 124 test_count_lines() 125 test_count_lines_lines() 126 test_nothing() 127 test_nothing_lines() 128 test_everything() 129 test_everything_lines() 130 131 test_fasta()
132 133 134 if __name__ == "__main__": 135 test() 136 print "All tests passed." 137