import os

from stockcrawler import utils
from stockcrawler.tests.base import SAMPLE_DATA_DIR, TestCaseBase


class UtilsTest(TestCaseBase):

    def test_check_date_arg(self):
        utils.check_date_arg('19830305')
        utils.check_date_arg('19851122')
        utils.check_date_arg('19980720')
        utils.check_date_arg('20140212')

        # OK to pass an empty argument
        utils.check_date_arg('')

        with self.assertRaises(ValueError):
            utils.check_date_arg('1234')

        with self.assertRaises(ValueError):
            utils.check_date_arg('2014111')

        with self.assertRaises(ValueError):
            utils.check_date_arg('20141301')

        with self.assertRaises(ValueError):
            utils.check_date_arg('20140132')

    def test_load_symbols(self):
        try:
            filename = os.path.join(SAMPLE_DATA_DIR, 'test_symbols.txt')
            with open(filename, 'w') as f:
                f.write('AAPL\nGOOG\n# Comment\nFB\nTWTR\nAMZN\nSPY\n\nYHOO\n# The end\n')

            symbols = list(utils.load_symbols(filename))
            self.assertEqual(symbols, ['AAPL', 'GOOG', 'FB', 'TWTR', 'AMZN', 'SPY', 'YHOO'])
        finally:
            try:
                os.remove(filename)
            except OSError:
                pass

@@ -1,12 +1,29 @@
+import cStringIO%0A
 import os%0A%0Af
@@ -1303,8 +1303,329 @@
    pass%0A
+%0A    def test_parse_csv(self):%0A        f = cStringIO.StringIO('name,age%5CnAvon,30%5CnOmar,29%5CnJoe,45%5Cn')%0A        items = list(utils.parse_csv(f))%0A        self.assertEqual(items, %5B%0A            %7B 'name': 'Avon', 'age': '30' %7D,%0A            %7B 'name': 'Omar', 'age': '29' %7D,%0A            %7B 'name': 'Joe', 'age': '45' %7D%0A        %5D)%0A

Test utils.parse_csv().