forked from cgardens/target-csv
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Respect the schema passed in the stream.
* Extract headers from schema object and use those headers to write to the CSV. * Write tests that flatten and get_headers produce the same keys.
- Loading branch information
Showing
2 changed files
with
58 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import target_csv | ||
import unittest | ||
|
||
|
||
class TestTargetCsv(unittest.TestCase): | ||
|
||
def setUp(self): | ||
pass | ||
|
||
def test_get_headers(self): | ||
schema = {"properties": { | ||
'a': {'type': 'string'}, | ||
'b': {'type': 'array', 'items': {'type': 'string'}}, | ||
'c': {'type': 'object', 'properties': {'d': {'type': 'string'}}} | ||
}} | ||
|
||
assert target_csv.get_headers(schema) == ['a', 'b', 'c__d'] | ||
|
||
def test_get_headers_matches_flatten(self): | ||
schema = {'properties': { | ||
'a': {'type': 'string'}, | ||
'b': {'type': 'array', 'items': {'type': 'string'}}, | ||
'c': {'type': 'object', 'properties': {'d': {'type': 'string'}}} | ||
}} | ||
|
||
record = { | ||
'a': 'alpha', | ||
'b': ['beta'], | ||
'c': {'d': 'delta'} | ||
} | ||
|
||
keys_from_schema = target_csv.get_headers(schema) | ||
keys_from_records = list(target_csv.flatten(record).keys()) | ||
assert keys_from_schema == keys_from_records | ||
|
||
if __name__ == '__main__': | ||
unittest.main() |