import shutil
from nose.tools import *
from bsAbstimmungen.importer import VotingImporter
from bsAbstimmungen.models import create_tables
from bsAbstimmungen.models import *
from sqlalchemy.orm import sessionmaker
from sqlalchemy import create_engine
from datetime import datetime


def test_fetch():

    engine = create_engine('sqlite:///:memory:')
    initialize(engine)
    create_tables(engine)
    session = sessionmaker(bind=engine)()
    importer = VotingImporter()

    f = datetime(year=2014, month=2, day=1)
    t = datetime(year=2014, month=2, day=28)

    importer.fetch(session, f, t)
    assert_equal(29, session.query(Vote).count())


def test_download():
    # TODO: test skip if already exists...
    files = [
        'http://abstimmungen.grosserrat-basel.ch/archiv/Amtsjahr_2013-2014'
        '/2013.02.06/Abst_0004_20130206_110027_0007_0000_ow.pdf',
        'http://abstimmungen.grosserrat-basel.ch/archiv/Amtsjahr_2013-2014'
        '/2013.03.20/Abst_0072_20130320_094625_0011_0000_ab.pdf'
    ]
    importer = VotingImporter(directory='test_cache')
    shutil.rmtree('test_cache')
    os.makedirs('test_cache')
    result = importer._download(files)
    assert_equal(2, len(result))
    assert_equal(
        'test_cache/Abst_0004_20130206_110027_0007_0000_ow.pdf',
        result[0]
    )
    assert_equal(
        'test_cache/Abst_0072_20130320_094625_0011_0000_ab.pdf',
        result[1]
    )
    assert_true(os.path.exists(result[0]))
    assert_true(os.path.getsize(result[0]) > 0)
    assert_true(os.path.exists(result[1]))
    assert_true(os.path.getsize(result[1]) > 0)
    shutil.rmtree('test_cache')

@@ -1,18 +1,4 @@
-import shutil%0A
 from
@@ -78,354 +78,246 @@
 rom 
-bsAbstimmungen.models import create_tables%0Afrom bsAbstimmungen.models import *%0Afrom sqlalchemy.orm import sessionmaker%0Afrom sqlalchemy import create_engine%0Afrom datetime import datetime%0A%0A%0Adef test_fetch():%0A%0A    engine = create_engine('sqlite:///:memory:')%0A    initialize(engine)%0A    create_tables(engine)%0A    session = sessionmaker(bind=engine)()
+datetime import datetime%0Afrom mock import patch%0Aimport os%0Aimport shutil%0A%0A%0A@patch('bsAbstimmungen.importer.votingimporter.VotingScraper.find')%0A@patch('bsAbstimmungen.importer.votingimporter.VotingParser.parse')%0Adef test_fetch(parse, find):
 %0A   
@@ -343,16 +343,28 @@
 mporter(
+'test_cache'
 )%0A%0A    f
@@ -456,37 +456,715 @@
     
-importer.fetch(session, f, t)
+find.return_value = %5B%0A        'http://abstimmungen.grosserrat-basel.ch/archiv/Amtsjahr_2014-2015/'%0A        '2014.02.12/Abst_0475_20140212_092150_0001_0000_ab.pdf',%0A        'http://abstimmungen.grosserrat-basel.ch/archiv/Amtsjahr_2014-2015/'%0A        '2014.02.12/Abst_0493_20140212_114415_0017_0000_sa.pdf'%5D%0A%0A    importer.fetch(None, f, t)%0A%0A    # Verify the files were downloaded...%0A    assert_true(%0A        os.path.exists('test_cache/Abst_0475_20140212_092150_0001_0000_ab.pdf')%0A    )%0A    assert_true(%0A        os.path.exists('test_cache/Abst_0493_20140212_114415_0017_0000_sa.pdf')%0A    )%0A%0A    # Verify the scraper is called%0A    find.assert_called_with(f, t)%0A%0A    # Verify the parse method was called for both
 %0A   
@@ -1180,40 +1180,291 @@
 qual
+s
 (2
-9
 , 
-session.query(Vote).count()
+len(parse.mock_calls))%0A    parse.assert_any_call(%0A        None, 'test_cache/Abst_0493_20140212_114415_0017_0000_sa.pdf'%0A    )%0A    parse.assert_any_call(%0A        None, 'test_cache/Abst_0475_20140212_092150_0001_0000_ab.pdf'%0A    )%0A%0A    # Clean up...%0A    shutil.rmtree('test_cache'
 )%0A%0A%0A

Improve voting importer test