def testInitializedRead(self): """ It must be possible to read from a StringIO instance that is initialized on creation. """ s = StringIO('hey') self.assertEqual('hey', s.getvalue())
def testInitializedRead(self): """ It must be possible to read from a StringIO instance that is initialized on creation. """ s = StringIO('hey') self.assertEqual('hey', s.getvalue())
def testWriteRead(self): """ It must be possible to write and read to/from a StringIO instance as normal. """ s = StringIO() s.write('hey') self.assertEqual('hey', s.getvalue())
def testWriteRead(self): """ It must be possible to write and read to/from a StringIO instance as normal. """ s = StringIO() s.write('hey') self.assertEqual('hey', s.getvalue())
def testOpenNotCalledOnRepeatedCall(self): """ If a repeated call to pathogenSampleFiles.add is made with the same arguments, no file should be read because the original result value is cached. """ class Open(object): def __init__(self, test, manager): self.test = test self.manager = manager self.count = 0 def sideEffect(self, filename, *args, **kwargs): if self.count == 0: self.test.assertEqual('out/0.fasta', filename) self.count += 1 return StringIO('>id1\nACTG\n') elif self.count == 1: self.test.assertEqual('out/pathogen-0-sample-0.fasta', filename) self.count += 1 return self.manager else: self.test.fail( 'We are only supposed to be called twice. ' 'Filename: %r, Args: %r, Keyword args: %r.' % (filename, args, kwargs)) fp = StringIO( '0.63 41.3 44.2 9 9 12 gi|327410| protein 77 [Lausannevirus]\n' ) fastaIO = StringIO() @contextmanager def manager(): yield fastaIO pg = ProteinGrouper() pg.addFile('filename-1', fp) pathogenSampleFiles = PathogenSampleFiles(pg) sideEffect = Open(self, manager()).sideEffect with patch.object(builtins, 'open') as mockMethod: mockMethod.side_effect = sideEffect filename = pathogenSampleFiles.add('Lausannevirus', 'filename-1') self.assertEqual('out/pathogen-0-sample-0.fasta', filename) self.assertEqual('>id1\nACTG\n', fastaIO.getvalue()) # Repeated call. The side effect open will fail if open is # called at this point. filename = pathogenSampleFiles.add('Lausannevirus', 'filename-1') self.assertEqual('out/pathogen-0-sample-0.fasta', filename)
def testIdenticalReadsRemoved(self): """ If two proteins in the same pathogen are matched by the same read, the de-duplicated FASTA for the pathogen must have only one copy of the duplicated read. """ class Open(object): def __init__(self, test, manager): self.test = test self.manager = manager self.expectedFilenames = {'out/0.fasta', 'out/1.fasta', 'out/pathogen-0-sample-0.fasta'} def sideEffect(self, filename, *args, **kwargs): try: self.expectedFilenames.remove(filename) except KeyError: self.test.fail( 'Open called with unexpected filename: %r, Args: %r, ' 'Keyword args: %r.' % (filename, args, kwargs)) else: if filename == 'out/0.fasta': return StringIO('>id1\nACTG\n') elif filename == 'out/1.fasta': return StringIO('>id1\nACTG\n>id2\nCAGT\n') else: return self.manager fp = StringIO( '0.63 41.3 44.2 9 9 12 gi|327410| protein 77 [Lausannevirus]\n' '0.77 46.6 48.1 5 6 74 gi|327409| ubiquitin [Lausannevirus]\n' ) fastaIO = StringIO() @contextmanager def manager(): yield fastaIO pg = ProteinGrouper() pg.addFile('filename-1', fp) pathogenSampleFiles = PathogenSampleFiles(pg) opener = Open(self, manager()) with patch.object(builtins, 'open') as mockMethod: mockMethod.side_effect = opener.sideEffect filename = pathogenSampleFiles.add('Lausannevirus', 'filename-1') self.assertEqual('out/pathogen-0-sample-0.fasta', filename) self.assertEqual('>id1\nACTG\n>id2\nCAGT\n', fastaIO.getvalue()) # Make sure all expected filenames were seen by the mocked open. self.assertEqual(set(), opener.expectedFilenames)
def testWritePathogenIndex(self): """ The writePatogenIndex function must write a file with the expected content. """ pathogenSampleFiles = PathogenSampleFiles(None) pathogenSampleFiles._pathogens = { 'virus b': 4, 'virus a': 3, 'virus c': 9, } fp = StringIO() pathogenSampleFiles.writePathogenIndex(fp) self.assertEqual('3 virus a\n4 virus b\n9 virus c\n', fp.getvalue())
def testWriteSampleIndex(self): """ The writeSampleIndex function must write a file with the expected content. """ pathogenSampleFiles = PathogenSampleFiles(None) pathogenSampleFiles._samples = { 'NEO11': 500, 'NEO33': 24, 'NEO66': 333, } fp = StringIO() pathogenSampleFiles.writeSampleIndex(fp) self.assertEqual('24 NEO33\n333 NEO66\n500 NEO11\n', fp.getvalue())
def testTwoReads(self): """ It must be possible to access a FASTA file with two reads like a dict. """ pyfaidxIndex = StringIO() class Open(object): def __init__(self, test, manager): self.test = test self.manager = manager self.count = 0 def sideEffect(self, filename, *args, **kwargs): if self.count == 0: self.test.assertEqual('filename.fasta', filename) self.count += 1 return BytesIO(b'>id1\nACTG\n>id2\nAACCTTGG\n') elif self.count == 1: self.test.assertEqual('filename.fasta', filename) self.count += 1 return StringIO('>id1\nACTG\n>id2\nAACCTTGG\n') elif self.count == 2: self.count += 1 return self.manager elif self.count == 3: self.count += 1 return StringIO(pyfaidxIndex.getvalue()) else: self.test.fail( 'Open called too many times. Filename: %r, Args: %r, ' 'Keyword args: %r.' % (filename, args, kwargs)) @contextmanager def manager(): yield pyfaidxIndex sideEffect = Open(self, manager()).sideEffect with patch.object(builtins, 'open') as mockMethod: mockMethod.side_effect = sideEffect reads = FastaFaiReads('filename.fasta') self.assertEqual(DNARead('id1', 'ACTG'), reads['id1']) self.assertEqual(DNARead('id2', 'AACCTTGG'), reads['id2']) # Check that the fai index was built correctly. self.assertEqual(pyfaidxIndex.getvalue(), 'id1\t4\t5\t4\t5\nid2\t8\t15\t8\t9\n')
def testTwoReads(self): """ It must be possible to access a FASTA file with two reads like a dict. """ pyfaidxIndex = StringIO() class Open(object): def __init__(self, test, manager): self.test = test self.manager = manager self.count = 0 def sideEffect(self, filename, *args, **kwargs): if self.count == 0: self.test.assertEqual('filename.fasta', filename) self.count += 1 return BytesIO(b'>id1\nACTG\n>id2\nAACCTTGG\n') elif self.count == 1: self.test.assertEqual('filename.fasta', filename) self.count += 1 return StringIO('>id1\nACTG\n>id2\nAACCTTGG\n') elif self.count == 2: self.count += 1 return self.manager elif self.count == 3: self.count += 1 return StringIO(pyfaidxIndex.getvalue()) else: self.test.fail( 'Open called too many times. Filename: %r, Args: %r, ' 'Keyword args: %r.' % (filename, args, kwargs)) @contextmanager def manager(): yield pyfaidxIndex sideEffect = Open(self, manager()).sideEffect with patch.object(builtins, 'open') as mockMethod: mockMethod.side_effect = sideEffect reads = FastaFaiReads('filename.fasta') self.assertEqual(DNARead('id1', 'ACTG'), reads['id1']) self.assertEqual(DNARead('id2', 'AACCTTGG'), reads['id2']) # Check that the fai index was built correctly. self.assertEqual(pyfaidxIndex.getvalue(), 'id1\t4\t5\t4\t5\nid2\t8\t15\t8\t9\n')