<s> import datetime <EOL> import luigi <EOL> import luigi . postgres <EOL> from luigi . tools . range import RangeDaily <EOL> from helpers import unittest <EOL> import mock <EOL> from nose . plugins . attrib import attr <EOL> def datetime_to_epoch ( dt ) : <EOL> td = dt - datetime . datetime ( <NUM_LIT> , <NUM_LIT:1> , <NUM_LIT:1> ) <EOL> return td . days * <NUM_LIT> + td . seconds + td . microseconds / <NUM_LIT> <EOL> class MockPostgresCursor ( mock . Mock ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , existing_update_ids ) : <EOL> super ( MockPostgresCursor , self ) . __init__ ( ) <EOL> self . existing = existing_update_ids <EOL> def execute ( self , query , params ) : <EOL> if query . startswith ( '<STR_LIT>' ) : <EOL> self . fetchone_result = ( <NUM_LIT:1> , ) if params [ <NUM_LIT:0> ] in self . existing else None <EOL> else : <EOL> self . fetchone_result = None <EOL> def fetchone ( self ) : <EOL> return self . fetchone_result <EOL> class DummyPostgresImporter ( luigi . postgres . CopyToTable ) : <EOL> date = luigi . DateParameter ( ) <EOL> host = '<STR_LIT>' <EOL> database = '<STR_LIT>' <EOL> user = '<STR_LIT>' <EOL> password = '<STR_LIT>' <EOL> table = '<STR_LIT>' <EOL> columns = ( <EOL> ( '<STR_LIT>' , '<STR_LIT:text>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT:int>' ) , <EOL> ) <EOL> @ attr ( '<STR_LIT>' ) <EOL> class DailyCopyToTableTest ( unittest . TestCase ) : <EOL> maxDiff = None <EOL> @ mock . patch ( '<STR_LIT>' ) <EOL> def test_bulk_complete ( self , mock_connect ) : <EOL> mock_cursor = MockPostgresCursor ( [ <EOL> DummyPostgresImporter ( date = datetime . datetime ( <NUM_LIT> , <NUM_LIT:1> , <NUM_LIT:3> ) ) . task_id <EOL> ] ) <EOL> mock_connect . return_value . cursor . return_value = mock_cursor <EOL> task = RangeDaily ( of = DummyPostgresImporter , <EOL> start = datetime . date ( <NUM_LIT> , <NUM_LIT:1> , <NUM_LIT:2> ) , <EOL> now = datetime_to_epoch ( datetime . datetime ( <NUM_LIT> , <NUM_LIT:1> , <NUM_LIT:7> ) ) ) <EOL> actual = sorted ( [ t . task_id for t in task . requires ( ) ] ) <EOL> self . assertEqual ( actual , sorted ( [ <EOL> DummyPostgresImporter ( date = datetime . datetime ( <NUM_LIT> , <NUM_LIT:1> , <NUM_LIT:2> ) ) . task_id , <EOL> DummyPostgresImporter ( date = datetime . datetime ( <NUM_LIT> , <NUM_LIT:1> , <NUM_LIT:4> ) ) . task_id , <EOL> DummyPostgresImporter ( date = datetime . datetime ( <NUM_LIT> , <NUM_LIT:1> , <NUM_LIT:5> ) ) . task_id , <EOL> DummyPostgresImporter ( date = datetime . datetime ( <NUM_LIT> , <NUM_LIT:1> , <NUM_LIT:6> ) ) . task_id , <EOL> ] ) ) <EOL> self . assertFalse ( task . complete ( ) ) <EOL> class DummyPostgresQuery ( luigi . postgres . PostgresQuery ) : <EOL> date = luigi . DateParameter ( ) <EOL> host = '<STR_LIT>' <EOL> database = '<STR_LIT>' <EOL> user = '<STR_LIT>' <EOL> password = '<STR_LIT>' <EOL> table = '<STR_LIT>' <EOL> columns = ( <EOL> ( '<STR_LIT>' , '<STR_LIT:text>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT:int>' ) , <EOL> ) <EOL> query = '<STR_LIT>' <EOL> @ attr ( '<STR_LIT>' ) <EOL> class PostgresQueryTest ( unittest . TestCase ) : <EOL> maxDiff = None <EOL> @ mock . patch ( '<STR_LIT>' ) <EOL> def test_bulk_complete ( self , mock_connect ) : <EOL> mock_cursor = MockPostgresCursor ( [ <EOL> '<STR_LIT>' <EOL> ] ) <EOL> mock_connect . return_value . cursor . return_value = mock_cursor <EOL> task = RangeDaily ( of = DummyPostgresQuery , <EOL> start = datetime . date ( <NUM_LIT> , <NUM_LIT:1> , <NUM_LIT:2> ) , <EOL> now = datetime_to_epoch ( datetime . datetime ( <NUM_LIT> , <NUM_LIT:1> , <NUM_LIT:7> ) ) ) <EOL> actual = [ t . task_id for t in task . requires ( ) ] <EOL> self . assertEqual ( actual , [ <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> ] ) <EOL> self . assertFalse ( task . complete ( ) ) </s>
