-
Notifications
You must be signed in to change notification settings - Fork 2
/
test_inputsource.py
97 lines (76 loc) · 2.72 KB
/
test_inputsource.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
import pytest
import io, os, sys, inspect #,codecs
import warnings
from amara3 import inputsource, iri
from amara3.iri import IriError
from amara3.inputsource import factory
import os, inspect
def module_path(local_function):
''' returns the module path without the use of __file__. Requires a function defined
locally in the module.
from http://stackoverflow.com/questions/729583/getting-file-path-of-imported-module'''
return os.path.abspath(inspect.getsourcefile(local_function))
#hack to locate test resource (data) files regardless of from where nose was run
RESOURCEPATH = os.path.normpath(os.path.join(module_path(lambda _: None), '../resource/'))
def test_string_is():
inp = inputsource('abc')
assert inp.stream.__class__ == io.StringIO
assert inp.iri is None
assert inp.stream.read() == 'abc'
def test_factory_string_is():
inpl = factory('abc')
for inp in inpl:
assert inp.stream.__class__ == io.StringIO
assert inp.iri is None
assert inp.stream.read() == 'abc'
def test_stringlist_is():
inpl = factory(['abc', 'def', 'ghi'])
inp = inpl[0]
assert inp.stream.__class__ == io.StringIO
assert inp.iri is None
assert inp.stream.read() == 'abc'
inp = inpl[1]
assert inp.stream.__class__ == io.StringIO
assert inp.iri is None
assert inp.stream.read() == 'def'
inp = inpl[2]
assert inp.stream.__class__ == io.StringIO
assert inp.iri is None
assert inp.stream.read() == 'ghi'
def test_file_is():
fname = os.path.join(RESOURCEPATH, 'spam.txt')
inp = inputsource(open(fname))
assert inp.iri is None
assert inp.stream.read() == 'monty\n'
def test_factory_file_is():
fname = os.path.join(RESOURCEPATH, 'spam.txt')
inpl = factory(open(fname))
for inp in inpl:
assert inp.iri is None
assert inp.stream.read() == 'monty\n'
def test_stringio_is():
inp = inputsource(io.StringIO('abc'))
assert inp.iri is None
assert inp.stream.read() == 'abc'
def test_bytesio_is():
inp = inputsource(io.BytesIO('abc'.encode('utf-8')))
assert inp.iri is None
assert inp.stream.read() == b'abc'
def test_filelist_is():
files = [ open(os.path.join(RESOURCEPATH, f)) for f in ('spam.txt', 'eggs.txt') ]
inpl = factory(files)
inp = inpl[0]
assert inp.iri is None
assert inp.stream.read() == 'monty\n'
inp = inpl[1]
assert inp.iri is None
assert inp.stream.read() == 'python\n'
def test_zip_is():
zf = open(os.path.join(RESOURCEPATH, 'speggs.zip'), 'rb')
inpl = factory(zf, zipcheck=True)
inp = next(inpl)
assert inp.iri is None
assert inp.stream.read() == b'python\n'
inp = next(inpl)
assert inp.iri is None
assert inp.stream.read() == b'monty\n'