|
| 1 | +#!/usr/bin/env python3 |
| 2 | +"""tests for transcribe.py""" |
| 3 | + |
| 4 | +from subprocess import getstatusoutput |
| 5 | +import os.path |
| 6 | +import re |
| 7 | +import string |
| 8 | +import random |
| 9 | +from shutil import rmtree |
| 10 | + |
| 11 | +prg = './transcribe.py' |
| 12 | +input1 = './inputs/input1.txt' |
| 13 | +input2 = './inputs/input2.txt' |
| 14 | + |
| 15 | + |
| 16 | +# -------------------------------------------------- |
| 17 | +def random_filename(): |
| 18 | + """generate a random filename""" |
| 19 | + |
| 20 | + return ''.join(random.choices(string.ascii_uppercase + string.digits, k=5)) |
| 21 | + |
| 22 | + |
| 23 | +# -------------------------------------------------- |
| 24 | +def test_exists(): |
| 25 | + """usage""" |
| 26 | + |
| 27 | + assert os.path.isfile(prg) |
| 28 | + |
| 29 | + |
| 30 | +# -------------------------------------------------- |
| 31 | +def test_usage(): |
| 32 | + """usage""" |
| 33 | + |
| 34 | + for flag in ['-h', '--help']: |
| 35 | + rv, out = getstatusoutput('{} {}'.format(prg, flag)) |
| 36 | + assert rv == 0 |
| 37 | + assert re.match("usage", out, re.IGNORECASE) |
| 38 | + |
| 39 | + |
| 40 | +# -------------------------------------------------- |
| 41 | +def test_no_args(): |
| 42 | + """die on no args""" |
| 43 | + |
| 44 | + rv, out = getstatusoutput(prg) |
| 45 | + assert rv != 0 |
| 46 | + assert re.match("usage", out, re.IGNORECASE) |
| 47 | + |
| 48 | + |
| 49 | +# -------------------------------------------------- |
| 50 | +def test_bad_file(): |
| 51 | + """die on missing input""" |
| 52 | + |
| 53 | + bad = random_filename() |
| 54 | + rv, out = getstatusoutput(f'{prg} {bad}') |
| 55 | + assert rv != 0 |
| 56 | + assert re.match('usage:', out, re.I) |
| 57 | + assert re.search(f"No such file or directory: '{bad}'", out) |
| 58 | + |
| 59 | + |
| 60 | +# -------------------------------------------------- |
| 61 | +def test_good_input1(): |
| 62 | + """runs on good input""" |
| 63 | + |
| 64 | + out_dir = 'out' |
| 65 | + try: |
| 66 | + if os.path.isdir(out_dir): |
| 67 | + rmtree(out_dir) |
| 68 | + |
| 69 | + rv, out = getstatusoutput(f'{prg} {input1}') |
| 70 | + assert rv == 0 |
| 71 | + assert out == 'Done, wrote 1 sequence in 1 file to directory "out".' |
| 72 | + assert os.path.isdir(out_dir) |
| 73 | + out_file = os.path.join(out_dir, 'input1.txt') |
| 74 | + assert os.path.isfile(out_file) |
| 75 | + assert open(out_file).read().rstrip() == 'GAUGGAACUUGACUACGUAAAUU' |
| 76 | + |
| 77 | + finally: |
| 78 | + if os.path.isdir(out_dir): |
| 79 | + rmtree(out_dir) |
| 80 | + |
| 81 | +# -------------------------------------------------- |
| 82 | +def test_good_input2(): |
| 83 | + """runs on good input""" |
| 84 | + |
| 85 | + out_dir = random_filename() |
| 86 | + try: |
| 87 | + if os.path.isdir(out_dir): |
| 88 | + rmtree(out_dir) |
| 89 | + |
| 90 | + rv, out = getstatusoutput(f'{prg} -o {out_dir} {input2}') |
| 91 | + assert rv == 0 |
| 92 | + assert out == f'Done, wrote 2 sequences in 1 file to directory "{out_dir}".' |
| 93 | + assert os.path.isdir(out_dir) |
| 94 | + out_file = os.path.join(out_dir, 'input2.txt') |
| 95 | + assert os.path.isfile(out_file) |
| 96 | + assert open(out_file).read().rstrip() == output2().rstrip() |
| 97 | + |
| 98 | + finally: |
| 99 | + if os.path.isdir(out_dir): |
| 100 | + rmtree(out_dir) |
| 101 | + |
| 102 | +# -------------------------------------------------- |
| 103 | +def test_good_multiple_inputs(): |
| 104 | + """runs on good input""" |
| 105 | + |
| 106 | + out_dir = random_filename() |
| 107 | + try: |
| 108 | + if os.path.isdir(out_dir): |
| 109 | + rmtree(out_dir) |
| 110 | + |
| 111 | + rv, out = getstatusoutput(f'{prg} --outdir {out_dir} {input1} {input2}') |
| 112 | + assert rv == 0 |
| 113 | + assert out == f'Done, wrote 3 sequences in 2 files to directory "{out_dir}".' |
| 114 | + assert os.path.isdir(out_dir) |
| 115 | + out_file1 = os.path.join(out_dir, 'input1.txt') |
| 116 | + out_file2 = os.path.join(out_dir, 'input2.txt') |
| 117 | + assert os.path.isfile(out_file1) |
| 118 | + assert os.path.isfile(out_file2) |
| 119 | + assert open(out_file1).read().rstrip() == 'GAUGGAACUUGACUACGUAAAUU' |
| 120 | + assert open(out_file2).read().rstrip() == output2().rstrip() |
| 121 | + |
| 122 | + finally: |
| 123 | + if os.path.isdir(out_dir): |
| 124 | + rmtree(out_dir) |
| 125 | + |
| 126 | +# -------------------------------------------------- |
| 127 | +def output2(): |
| 128 | + return """CUUAGGUCAGUGGUCUCUAAACUUUCGGUUCUGUCGUCUUCAUAGGCAAAUUUUUGAACCGGCAGACAAGCUAAUCCCUGUGCGGUUAGCUCAAGCAACAGAAUGUCCGAUCUUUGAACUUCCUAACGAACCGAACCUACUAUAAUUACAUACGAAUAAUGUAUGGGCUAGCGUUGGCUCAUCAUCAAGUCUGCGGUGAAAUGGGAACAUAUUCGCAUUGCAUAUAGGGCGUAUCUGACGAUCGAUUCGAGUUGGCUAGUCGUACCAAAUGAUUAUGGGCUGGAGGGCCAAUGUAUACGUCAGCCAGGCUAAACCACUGGACCGCUUGCAAUCCAUAGGAAGUAAAAUUACCCUUUUUAAACUCUCUAAGAUGUGGCGUCUCGUUCUUAAGGAGUAAUGAGACUGUGACAACAUUGGCAAGCACAGCCUCAGUAUAGCUACAGCACCGGUGCUAAUAGUAAAUGCAAACACCGUUUCAAGAGCCGAGCCUUUUUUUAAUGCAAGGUGACUUCAGAGGGAGUAAAUCGUGGCCGGGGACUGUCCAGAGCAAUGCAUUCCCGAGUGCGGGUACCCGUGGUGUGAGAGGAAUCGAUUUCGCGUGUGAUACCAUUAAUGGUCCUGUACUACUGUCAGUCAGCUUGAUUUGAAGUCGGCCGACAAGGUUGGUACAUAAUGGGCUUACUGGGAGCUUAGGUUAGCCUCUGGAAAACUUUAGAAUUUAUAUGGGUGUUUCUGUGUUCGUACAGGCCCCAGUCGGGCCAUCGUUGUUGAGCAUAGACCG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