Skip to content

Commit 4f06a48

Browse files
committed
add test workflow
1 parent 92d1aad commit 4f06a48

File tree

3 files changed

+113
-0
lines changed

3 files changed

+113
-0
lines changed

.github/workflows/unittest.yml

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
name: Python application test with Unittest
2+
3+
on:
4+
push:
5+
branches: [ main ]
6+
pull_request:
7+
branches: [ main ]
8+
9+
jobs:
10+
build:
11+
runs-on: ${{ matrix.os }}
12+
strategy:
13+
matrix:
14+
os: [ubuntu-latest, macos-latest]
15+
python-version: ['3.7', '3.8', '3.9', '3.10']
16+
steps:
17+
- uses: actions/checkout@v2
18+
- name: Set up Python ${{ matrix.python-version }}
19+
uses: actions/setup-python@v2
20+
with:
21+
python-version: ${{ matrix.python-version }}
22+
- name: Install blaze
23+
run: |
24+
python -m pip install --upgrade pip
25+
pip install .
26+
- name: Run test with unittest
27+
run: |
28+
python -m unittest discover -s test/

setup.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
author_email="[email protected]",
1111
description='Barcode identification from Long reads for AnalyZing single cell gene Expression',
1212
packages=find_packages(),
13+
test_suite='test',
1314
long_description=long_description,
1415
long_description_content_type='text/markdown',
1516
package_data={'blaze': ['10X_bc/3M-february-2018.zip', '10X_bc/737K-august-2016.txt']},

test/test_blaze.py

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
import unittest
2+
import filecmp
3+
from blaze import blaze
4+
import blaze.helper as helper
5+
from blaze.config import *
6+
import os
7+
import gzip
8+
import shutil
9+
import difflib
10+
11+
class TestBlazeMain(unittest.TestCase):
12+
13+
def setUp(self):
14+
self.argv = \
15+
' --expect-cells=500 --threads=8 --output-prefix test_ test/data/'
16+
self.expected_normal_files = [
17+
'test_' + DEFAULT_GRB_OUT_RAW_BC,
18+
'test_' + DEFAULT_GRB_OUT_WHITELIST,
19+
'test_' + DEFAULT_EMPTY_DROP_FN,
20+
'test_' + DEFAULT_KNEE_PLOT_FN,
21+
'test_' + DEFAULT_BC_STAT_FN
22+
]
23+
self.expected_gz_files = ['test_' + DEFAULT_GRB_OUT_FASTQ]
24+
25+
self.expected_dir = 'test/expect_output/'
26+
27+
def decompress_gz_file(self, file_path):
28+
with gzip.open(file_path, 'rb') as f_in:
29+
with open(file_path+'.tmp', 'wb') as f_out: # remove .gz extension
30+
shutil.copyfileobj(f_in, f_out)
31+
return file_path+'.tmp'
32+
33+
def compare_gz_files(self, file1, file2):
34+
file1_decompressed = self.decompress_gz_file(file1)
35+
file2_decompressed = self.decompress_gz_file(file2)
36+
comparison_result = filecmp.cmp(file1_decompressed, file2_decompressed)
37+
38+
# Clean up decompressed files
39+
os.remove(file1_decompressed)
40+
os.remove(file2_decompressed)
41+
if comparison_result:
42+
return comparison_result
43+
else:
44+
self.diff_files(file1_decompressed, file2_decompressed)
45+
return comparison_result
46+
47+
def diff_files(self, file1, file2):
48+
with open(file1, 'r') as f1, open(file2, 'r') as f2:
49+
diff = difflib.unified_diff(
50+
f1.readlines(), # Read lines from the first file
51+
f2.readlines(), # Read lines from the second file
52+
fromfile=file1,
53+
tofile=file2,
54+
)
55+
# Output the difference to console or write to a file
56+
for i, line in enumerate(diff):
57+
if i > 10:
58+
break
59+
print(line, end='')
60+
61+
def test_main(self):
62+
# Run the main function
63+
blaze(self.argv)
64+
65+
# Check that the expected files were created and are correct
66+
for fn in self.expected_normal_files:
67+
self.assertTrue(os.path.exists(fn), f"File {fn} was not created")
68+
if not filecmp.cmp(fn, self.expected_dir + fn):
69+
self.diff_files(fn, self.expected_dir + fn)
70+
self.assertTrue(filecmp.cmp(fn, self.expected_dir + fn), f"File {fn} does not match the expected output (({self.expected_dir + fn}))")
71+
72+
73+
for fn in self.expected_gz_files:
74+
self.assertTrue(os.path.exists(fn), f"File {fn} was not created")
75+
self.assertTrue(self.compare_gz_files(fn, self.expected_dir + fn) , f"File {fn} does not match the expected output ({self.expected_dir + fn})")
76+
77+
def tearDown(self):
78+
# Remove created files after tests
79+
for fn in self.expected_normal_files + self.expected_gz_files:
80+
if os.path.exists(fn):
81+
os.remove(fn)
82+
83+
if __name__ == '__main__':
84+
unittest.main()

0 commit comments

Comments
 (0)