Skip to content

Commit

Permalink
DIP-164 improved tests + fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
akameron committed Apr 5, 2016
1 parent d7f7310 commit f7e75ea
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 33 deletions.
63 changes: 30 additions & 33 deletions bluekai/bluekai_tsv.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,50 +17,47 @@ def mapPath(path):
except TypeError:
return path

def mapValue(value):
if isinstance(value, (dict, list)):
raise TypeError("Plural, Object and JSON value types are not supported")
elif value is None:
return ""
else:
return value

# Compile (path, regex) for each path
regex_mappings = [
(path, re.compile(path.replace('.', r'(?:\.|\.\d+\.)')))
for path in (paths or [])
]

walked_record = walk(record)
try:

walked_record = walk(record)

# (path, walked_path, value) of each walked_record that matches a
# regex and add path from regex_mapping.
walked_record = (
(path, walked_path, value)
for (walked_path, value) in walked_record
for (path, regex) in regex_mappings
if regex.fullmatch(walked_path)
)

# (path, walked_path, value) of each walked_record that matches a
# regex and add path from regex_mapping.
walked_record = (
(path, walked_path, value)
for (walked_path, value) in walked_record
for (path, regex) in regex_mappings
if regex.fullmatch(walked_path)
)
# Group by path
groups = groupby(walked_record, key=lambda each: each[0])

# Group by path
groups = groupby(walked_record, key=lambda each: each[0])
# Join groups values with ','.
attributes = (
(path, str.join(',', [ value for (_, _, value) in group if value is not None ]))
for (path, group) in groups
)

# Join groups values with ','.
attributes = (
(path, str.join(',', [ value for (_, _, value) in group if value is not None ]))
for (path, group) in groups
)
# Join attributes keys and values with "=".
attributes = (
"{}={}".format(mapPath(path), value)
for (path, value) in attributes
)

# Join attributes keys and values with "=".
attributes = (
"{}={}".format(mapPath(path), mapValue(value))
for (path, value) in attributes
)
# Join attributes with '|'.
attributes = str.join('|', attributes)

# Join attributes with '|'.
attributes = str.join('|', attributes)
# Create row by joining uuid and attributes.
row = "{}\t{}\n".format(record['uuid'], attributes)

# Create row by joining uuid and attributes.
row = "{}\t{}\n".format(record['uuid'], attributes)
except TypeError:
raise TypeError("Plural, Object and JSON value types are not supported")

return row
20 changes: 20 additions & 0 deletions tests/test_bluekai_tsv.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,26 @@ def test_fromRecord_none_types(self):
actual = fromRecord(record, ["none"])
expected = "a-b-c\tnone=\n"

def test_fromRecord_plural_types(self):
record = { "uuid": "a-b-c", "plurals": [ { 'plural': "a" }, { 'plural': "b" }]}
actual = fromRecord(record, ["plurals.plural"])
expected = "a-b-c\tplurals.plural=a,b\n"

def test_fromRecord_plural_types_with_mapping(self):
record = { "uuid": "a-b-c", "plurals": [ { 'plural': "a" }, { 'plural': "b" }]}
actual = fromRecord(record, {"plurals.plural": "plurals"})
expected = "a-b-c\tplurals=a,b\n"

def test_fromRecord_plural_types_with_error(self):
record = { "uuid": "a-b-c", "plurals": [ { 'plural': "a" }, { 'plural': "b" }]}
with self.assertRaises(TypeError):
fromRecord(record, ["plurals"])

def test_fromRecord_plural_object_with_error(self):
record = { "uuid": "a-b-c", "object": { 'plural': "a" } }
with self.assertRaises(TypeError):
fromRecord(record, ["object"])

def test_fromRecordsIterator(self):
record1 = { "uuid": "a-b-c", "key": "value1" }
record2 = { "uuid": "x-y-z", "key": "value2" }
Expand Down

0 comments on commit f7e75ea

Please sign in to comment.