Skip to content

Commit 43c737c

Browse files
committed
Add test for command line script
1 parent ff86bff commit 43c737c

File tree

2 files changed

+105
-1
lines changed

2 files changed

+105
-1
lines changed

pystock_crawler/tests/test_cmdline.py

+104
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
import os
2+
import shutil
3+
import unittest
4+
5+
import pystock_crawler
6+
7+
from envoy import run
8+
9+
10+
TEST_DIR = './test_data'
11+
12+
13+
class PrintTest(unittest.TestCase):
14+
15+
def test_no_args(self):
16+
r = run('./bin/pystock-crawler')
17+
self.assertIn('Usage:', r.std_err)
18+
19+
def test_print_help(self):
20+
r = run('./bin/pystock-crawler -h')
21+
self.assertIn('Usage:', r.std_out)
22+
23+
r2 = run('./bin/pystock-crawler --help')
24+
self.assertEqual(r.std_out, r2.std_out)
25+
26+
def test_print_version(self):
27+
r = run('./bin/pystock-crawler -v')
28+
self.assertEqual(r.std_out, 'pystock-crawler %s\n' % pystock_crawler.__version__)
29+
30+
r2 = run('./bin/pystock-crawler --version')
31+
self.assertEqual(r.std_out, r2.std_out)
32+
33+
34+
class CrawlSymbolsTest(unittest.TestCase):
35+
36+
def setUp(self):
37+
if os.path.isdir(TEST_DIR):
38+
shutil.rmtree(TEST_DIR)
39+
os.mkdir(TEST_DIR)
40+
41+
self.args = {
42+
'output': os.path.join(TEST_DIR, 'symbols.txt'),
43+
'log_file': os.path.join(TEST_DIR, 'symbols.log'),
44+
'working_dir': TEST_DIR
45+
}
46+
47+
def assert_cache(self):
48+
# Check if cache is there
49+
cache_dir = os.path.join(TEST_DIR, '.scrapy', 'httpcache', 'nasdaq.leveldb')
50+
self.assertTrue(os.path.isdir(cache_dir))
51+
52+
def get_output_content(self):
53+
output_path = self.args['output']
54+
self.assertTrue(os.path.isfile(output_path))
55+
56+
with open(output_path) as f:
57+
content = f.read()
58+
return content
59+
60+
def assert_nyse_output(self):
61+
# Check if some common NYSE symbols are in output
62+
content = self.get_output_content()
63+
self.assertIn('JPM', content)
64+
self.assertIn('KO', content)
65+
self.assertIn('WMT', content)
66+
67+
# NASDAQ symbols shouldn't be
68+
self.assertNotIn('AAPL', content)
69+
self.assertNotIn('GOOG', content)
70+
self.assertNotIn('YHOO', content)
71+
72+
def assert_nyse_and_nasdaq_output(self):
73+
# Check if some common NYSE symbols are in output
74+
content = self.get_output_content()
75+
self.assertIn('JPM', content)
76+
self.assertIn('KO', content)
77+
self.assertIn('WMT', content)
78+
79+
# Check if some common NASDAQ symbols are in output
80+
self.assertIn('AAPL', content)
81+
self.assertIn('GOOG', content)
82+
self.assertIn('YHOO', content)
83+
84+
def assert_log(self):
85+
# Check if log file is there
86+
log_path = self.args['log_file']
87+
self.assertTrue(os.path.isfile(log_path))
88+
89+
def tearDown(self):
90+
shutil.rmtree(TEST_DIR)
91+
92+
def test_crawl_nyse(self):
93+
r = run('./bin/pystock-crawler symbols NYSE -o %(output)s -l %(log_file)s -w %(working_dir)s' % self.args)
94+
self.assertEqual(r.status_code, 0)
95+
self.assert_cache()
96+
self.assert_nyse_output()
97+
self.assert_log()
98+
99+
def test_crawl_nyse_and_nasdaq(self):
100+
r = run('./bin/pystock-crawler symbols NYSE,NASDAQ -o %(output)s -l %(log_file)s -w %(working_dir)s' % self.args)
101+
self.assertEqual(r.status_code, 0)
102+
self.assert_cache()
103+
self.assert_nyse_and_nasdaq_output()
104+
self.assert_log()

pytest.ini

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
[pytest]
2-
addopts = --cov-report term-missing --cov pystock_crawler pystock_crawler/tests/
2+
addopts = --cov-report term-missing --cov pystock_crawler --cov bin pystock_crawler/tests/

0 commit comments

Comments
 (0)