Skip to content

Commit

Permalink
Fix #20
Browse files Browse the repository at this point in the history
  • Loading branch information
di committed Apr 15, 2016
1 parent 74cce1a commit b318524
Show file tree
Hide file tree
Showing 7 changed files with 480 additions and 37 deletions.
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ def readme():
include_package_data=True,
zip_safe=False,
install_requires=['boto'],
tests_require=['pytest', 'flake8'],
tests_require=['pretend', 'pytest', 'flake8'],
cmdclass={'test': PyTest},
entry_points={
'console_scripts': [
Expand Down
Empty file added vladiate/examples/__init__.py
Empty file.
36 changes: 19 additions & 17 deletions vladiate/main.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
from Queue import Empty
try:
from Queue import Empty
except:
from queue import Empty
from multiprocessing import Pool, Queue
from vladiate import Vlad
from vladiate import logs
Expand Down Expand Up @@ -68,10 +71,11 @@ def is_vlad(tup):
hasattr(item, "validators") and not name.startswith('_'))


def find_vladfile(vladfile):
def find_vladfile(vladfile, path='.'):
"""
Attempt to locate a vladfile, either explicitly or by searching parent dirs.
"""
assert os.path.isdir(path)
# Obtain env value
names = [vladfile]
# Create .py version if necessary
Expand All @@ -86,17 +90,11 @@ def find_vladfile(vladfile):
if name.endswith('.py') or _is_package(expanded):
return os.path.abspath(expanded)
else:
# Otherwise, start in cwd and work downwards towards filesystem root
path = '.'
# Stop before falling off root of filesystem (should be platform
# agnostic)
while os.path.split(os.path.abspath(path))[1]:
for name in names:
joined = os.path.join(path, name)
if os.path.exists(joined):
if name.endswith('.py') or _is_package(joined):
return os.path.abspath(joined)
path = os.path.join('..', path)
for name in names:
joined = os.path.join(path, name)
if os.path.exists(joined):
if name.endswith('.py') or _is_package(joined):
return os.path.abspath(joined)
# Implicit 'return None' if nothing was found


Expand Down Expand Up @@ -197,8 +195,8 @@ def main():
else:
proc_pool = Pool(
arguments.processes
if arguments.processes <= vlad_classes
else vlad_classes
if arguments.processes <= len(vlad_classes)
else len(vlad_classes)
)
proc_pool.map(_vladiate, vlad_classes)
try:
Expand All @@ -208,5 +206,9 @@ def main():
pass
return os.EX_OK

if __name__ == '__main__':
exit(main())

def run(name):
if name == '__main__':
exit(main())

run(__name__)
47 changes: 46 additions & 1 deletion vladiate/test/test_inputs.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import pytest
from pretend import stub, call, call_recorder

from ..inputs import S3File, StringIO, String
from ..inputs import S3File, StringIO, String, VladInput
from ..vlad import Vlad


Expand Down Expand Up @@ -32,3 +33,47 @@ def test_string_input_works(kwargs):
source = String(**kwargs)
validators = {'ColA': [], 'ColB': []}
assert Vlad(source=source, validators=validators).validate()


def test_open_s3file(monkeypatch):
new_key = call_recorder(lambda *args, **kwargs: stub(
get_contents_as_string=lambda: 'contents'.encode()
))

get_bucket = call_recorder(lambda *args, **kwargs: stub(new_key=new_key))

mock_boto = stub(connect_s3=lambda: stub(get_bucket=get_bucket))

monkeypatch.setattr('vladiate.inputs.boto', mock_boto)

result = S3File('s3://some.bucket/some/s3/key.csv').open()

assert get_bucket.calls == [
call('some.bucket')
]

assert new_key.calls == [
call('/some/s3/key.csv')
]

assert result.readlines() == [b'contents']


def test_repr_s3file():
s3_file = S3File('s3://some.bucket/some/s3/key.csv')
assert repr(s3_file) == "S3File('s3://some.bucket/some/s3/key.csv')"


def test_base_class_raises():
with pytest.raises(NotImplementedError):
VladInput()

class PartiallyImplemented(VladInput):
def __init__(self):
pass

with pytest.raises(NotImplementedError):
PartiallyImplemented().open()

with pytest.raises(NotImplementedError):
repr(PartiallyImplemented())
Loading

0 comments on commit b318524

Please sign in to comment.