11import pytest
22from pretend import stub , call , call_recorder
33
4+ from ..exceptions import MissingExtraException
45from ..inputs import S3File , StringIO , String , VladInput
56from ..vlad import Vlad
67
78
9+ def mock_boto (result ):
10+ try :
11+ import builtins
12+ except :
13+ import __builtin__ as builtins
14+ realimport = builtins .__import__
15+
16+ def badimport (name , * args , ** kwargs ):
17+ if name == 'boto' :
18+ return result ()
19+ return realimport (name , * args , ** kwargs )
20+
21+ builtins .__import__ = badimport
22+
23+
824@pytest .mark .parametrize ('kwargs' , [
925 ({'path' : 's3://some.bucket/some/s3/key.csv' }),
1026 ({'bucket' : 'some.bucket' , 'key' : '/some/s3/key.csv' }),
1127])
1228def test_s3_input_works (kwargs ):
29+ mock_boto (lambda : stub ())
1330 S3File (** kwargs )
1431
1532
@@ -21,6 +38,7 @@ def test_s3_input_works(kwargs):
2138 ({'key' : '/some/s3/key.csv' }),
2239])
2340def test_s3_input_fails (kwargs ):
41+ mock_boto (lambda : stub ())
2442 with pytest .raises (ValueError ):
2543 S3File (** kwargs )
2644
@@ -35,7 +53,7 @@ def test_string_input_works(kwargs):
3553 assert Vlad (source = source , validators = validators ).validate ()
3654
3755
38- def test_open_s3file (monkeypatch ):
56+ def test_open_s3file ():
3957 new_key = call_recorder (lambda * args , ** kwargs : stub (
4058 get_contents_as_string = lambda : 'contents' .encode ()
4159 ))
@@ -44,9 +62,10 @@ def test_open_s3file(monkeypatch):
4462
4563 mock_boto = stub (connect_s3 = lambda : stub (get_bucket = get_bucket ))
4664
47- monkeypatch .setattr ('vladiate.inputs.boto' , mock_boto )
65+ s3file = S3File ('s3://some.bucket/some/s3/key.csv' )
66+ s3file .boto = mock_boto
4867
49- result = S3File ( 's3://some.bucket/some/s3/key.csv' ) .open ()
68+ result = s3file .open ()
5069
5170 assert get_bucket .calls == [
5271 call ('some.bucket' )
@@ -77,3 +96,13 @@ def __init__(self):
7796
7897 with pytest .raises (NotImplementedError ):
7998 repr (PartiallyImplemented ())
99+
100+
101+ def test_s3file_raises_when_no_boto ():
102+ def import_result ():
103+ raise ImportError
104+
105+ mock_boto (import_result )
106+
107+ with pytest .raises (MissingExtraException ):
108+ S3File ()
0 commit comments