|
| 1 | +""" |
| 2 | +Run wav2vec2aligner unit tests. |
| 3 | +How to run this test suite: |
| 4 | +If you installed wav2vec2aligner: |
| 5 | + python -m unittest aligner.tests.test_cli |
| 6 | +If you installed everyvoice: |
| 7 | + python -m unittest everyvoice.model.aligner.wav2vec2aligner.aligner.tests.test_cli |
| 8 | +""" |
| 9 | + |
| 10 | +import os |
| 11 | +import subprocess |
| 12 | +import tempfile |
| 13 | +from pathlib import Path |
| 14 | +from unittest import TestCase |
| 15 | + |
| 16 | +from typer.testing import CliRunner |
| 17 | + |
| 18 | +from ..classes import Segment |
| 19 | +from ..cli import app, complete_path |
| 20 | + |
| 21 | + |
| 22 | +class CLITest(TestCase): |
| 23 | + def setUp(self) -> None: |
| 24 | + self.runner = CliRunner() |
| 25 | + |
| 26 | + def test_main_help(self): |
| 27 | + for help in "-h", "--help": |
| 28 | + with self.subTest(help=help): |
| 29 | + result = self.runner.invoke(app, [help]) |
| 30 | + self.assertEqual(result.exit_code, 0) |
| 31 | + self.assertIn("align", result.stdout) |
| 32 | + self.assertIn("extract", result.stdout) |
| 33 | + |
| 34 | + def test_sub_help(self): |
| 35 | + for cmd in "align", "extract": |
| 36 | + for help in "-h", "--help": |
| 37 | + with self.subTest(cmd=cmd, help=help): |
| 38 | + result = self.runner.invoke(app, [cmd, help]) |
| 39 | + self.assertEqual(result.exit_code, 0) |
| 40 | + self.assertIn("Usage:", result.stdout) |
| 41 | + self.assertIn(cmd, result.stdout) |
| 42 | + |
| 43 | + def test_align_empty_file(self): |
| 44 | + with self.subTest("empty file"): |
| 45 | + result = self.runner.invoke(app, ["align", os.devnull, os.devnull]) |
| 46 | + self.assertNotEqual(result.exit_code, 0) |
| 47 | + self.assertIn("is empty", result.stdout) |
| 48 | + |
| 49 | + with self.subTest("file with only empty lines"): |
| 50 | + with tempfile.TemporaryDirectory() as tmpdir: |
| 51 | + textfile = os.path.join(tmpdir, "emptylines.txt") |
| 52 | + with open(textfile, "w", encoding="utf8") as f: |
| 53 | + f.write("\n \n \n") |
| 54 | + result = self.runner.invoke(app, ["align", textfile, os.devnull]) |
| 55 | + self.assertNotEqual(result.exit_code, 0) |
| 56 | + self.assertIn("is empty", result.stdout) |
| 57 | + |
| 58 | + def fetch_ras_test_file(self, filename, outputdir): |
| 59 | + from urllib.request import Request, urlopen |
| 60 | + |
| 61 | + repo, path = "https://github.com/ReadAlongs/Studio/", "/test/data/" |
| 62 | + request = Request(repo + "raw/refs/heads/main" + path + filename) |
| 63 | + request.add_header("Referer", repo + "blob/main" + path + filename) |
| 64 | + response = urlopen(request) |
| 65 | + with open(os.path.join(outputdir, filename), "wb") as f: |
| 66 | + f.write(response.read()) |
| 67 | + |
| 68 | + def test_align_something(self): |
| 69 | + with tempfile.TemporaryDirectory() as tmpdir: |
| 70 | + tmppath = Path(tmpdir) |
| 71 | + self.fetch_ras_test_file("ej-fra.txt", tmpdir) |
| 72 | + txt = tmppath / "ej-fra.txt" |
| 73 | + self.fetch_ras_test_file("ej-fra.m4a", tmpdir) |
| 74 | + m4a = tmppath / "ej-fra.m4a" |
| 75 | + wav = tmppath / "ej-fra.wav" |
| 76 | + # Under most circumstances, align can take a .m4a input file, but not |
| 77 | + # in CI. Since it's not a hard requirement, so just convert to .wav. |
| 78 | + subprocess.run(["ffmpeg", "-i", m4a, wav], capture_output=True) |
| 79 | + # os.system("ls -la " + tmpdir) |
| 80 | + textgrid = tmppath / "ej-fra-16000.TextGrid" |
| 81 | + wav_out = tmppath / "ej-fra-16000.wav" |
| 82 | + |
| 83 | + with self.subTest("ctc-segmenter align"): |
| 84 | + result = self.runner.invoke(app, ["align", str(txt), str(wav)]) |
| 85 | + if result.exit_code != 0: |
| 86 | + os.system("ls -la " + tmpdir) |
| 87 | + print(result.stdout) |
| 88 | + self.assertEqual(result.exit_code, 0) |
| 89 | + self.assertTrue(textgrid.exists()) |
| 90 | + self.assertTrue(wav_out.exists()) |
| 91 | + |
| 92 | + with self.subTest("ctc-segmenter extract"): |
| 93 | + result = self.runner.invoke( |
| 94 | + app, ["extract", str(textgrid), str(wav_out), str(tmppath / "out")] |
| 95 | + ) |
| 96 | + if result.exit_code != 0: |
| 97 | + print(result.stdout) |
| 98 | + self.assertEqual(result.exit_code, 0) |
| 99 | + self.assertTrue((tmppath / "out/metadata.psv").exists()) |
| 100 | + with open(txt, encoding="utf8") as txt_f: |
| 101 | + non_blank_line_count = sum(1 for line in txt_f if line.strip()) |
| 102 | + for i in range(non_blank_line_count): |
| 103 | + self.assertTrue((tmppath / f"out/wavs/segment{i}.wav")) |
| 104 | + |
| 105 | + |
| 106 | +class MiscTests(TestCase): |
| 107 | + def test_shell_complete(self): |
| 108 | + self.assertEqual(complete_path(), []) |
| 109 | + self.assertEqual(complete_path(None, None, None), []) |
| 110 | + |
| 111 | + def test_segment(self): |
| 112 | + segment = Segment("text", 500, 700, 0.42) |
| 113 | + self.assertEqual(len(segment), 200) |
| 114 | + self.assertEqual(repr(segment), "text (0.42): [ 500, 700)") |
0 commit comments