-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[wip] csv dialect #1
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
0578f48
csv Python 2.7 compatibility
0c08a4c
Merge branch 'master' into csv
d74ad4c
csv dialect python3 fix
18213a9
Merge branch 'master' into csv
94aa297
test file
cfa2822
[wip] separate PY2, PY3 csv dialect
1643057
[wip] test fix
ef75a87
test fix
965611a
Merged
364799b
add few test case
52c5d46
Merge remote-tracking branch 'origin/master' into csv
dd2da17
fix csv reader in py2 and py3
50b7397
modifications
ab1cb06
minor refactory on writer
00f0ca5
merge
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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,174 @@ | ||
# -*- coding: utf-8 -*- | ||
|
||
from __future__ import absolute_import | ||
import io | ||
import os | ||
import riwo | ||
import daprot as dp | ||
import string | ||
import random | ||
import datetime | ||
import unittest | ||
import requests | ||
from riwo.compat import * | ||
from . import CommonReader, __dir__, __remote__ | ||
|
||
class IndexSchema(dp.SchemaFlow): | ||
id = dp.Field(0) | ||
name = dp.Field(1, type=unicode, transforms=unicode.strip) | ||
price = dp.Field(2) | ||
quantity = dp.Field(3, type=long) | ||
updated_at = dp.Field(4) | ||
|
||
class HeaderedSchema(dp.SchemaFlow): | ||
id = dp.Field() | ||
name = dp.Field(type=unicode, transforms=unicode.strip) | ||
price = dp.Field() | ||
quantity = dp.Field(type=long) | ||
updated_at = dp.Field() | ||
|
||
class LocalReader(CommonReader, unittest.TestCase): | ||
def setUp(self): | ||
self.resource = io.open(os.path.join(__dir__, 'test.csv'), 'r', encoding='utf-8') | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is just a note for us. We should add tests for the |
||
self.reader = riwo.csv.Reader(self.resource, IndexSchema, delimiter=';') | ||
|
||
class LocalHeaderReader(CommonReader, unittest.TestCase): | ||
def setUp(self): | ||
self.resource = io.open(os.path.join(__dir__, 'test-header.csv'), 'r', encoding='utf-8') | ||
self.reader = riwo.csv.Reader(self.resource, HeaderedSchema, use_header=True) | ||
|
||
class RequestsReader(CommonReader, unittest.TestCase): | ||
def setUp(self): | ||
self.resource = requests.get(os.path.join(__remote__, 'test.csv')) | ||
self.reader = riwo.csv.Reader(self.resource, IndexSchema, delimiter=';') | ||
|
||
class RequestsHeaderReader(CommonReader, unittest.TestCase): | ||
def setUp(self): | ||
self.resource = requests.get(os.path.join(__remote__, 'test-header.csv')) | ||
self.reader = riwo.csv.Reader(self.resource, HeaderedSchema, use_header=True) | ||
|
||
class UrllibReader(CommonReader, unittest.TestCase): | ||
def setUp(self): | ||
self.resource = urlopen(os.path.join(__remote__, 'test.csv')) | ||
self.reader = riwo.csv.Reader(self.resource, IndexSchema, delimiter=';') | ||
|
||
class UrllibHeaderReader(CommonReader, unittest.TestCase): | ||
def setUp(self): | ||
self.resource = urlopen(os.path.join(__remote__, 'test-header.csv')) | ||
self.reader = riwo.csv.Reader(self.resource, HeaderedSchema, use_header=True) | ||
|
||
class LocalWriter(unittest.TestCase): | ||
class Schema(dp.SchemaFlow): | ||
id = dp.Field() | ||
name = dp.Field() | ||
price = dp.Field() | ||
quantity = dp.Field() | ||
updated_at = dp.Field() | ||
|
||
class NestedSchema(dp.SchemaFlow): | ||
inner_schema = dp.DictOf(IndexSchema) | ||
|
||
iterable_input = CommonReader.expected_result | ||
test_file_base = os.path.join(__dir__, 'output-{token}.csv') | ||
|
||
def setUp(self): | ||
self.test_file_path = self.test_file_base.format(token=''.join([random.choice(string.ascii_uppercase) for i in range(6)])) | ||
self.resource = io.open(self.test_file_path, 'w', encoding='utf-8') | ||
|
||
def test_add_header(self): | ||
self.writer = riwo.csv.Writer(self.resource, self.iterable_input, self.Schema, add_header=True) | ||
self.writer.write() | ||
self.content = u'''\ | ||
id,name,price,quantity,updated_at | ||
P0001,đói,449,1,2015.09.20 20:00 | ||
P0002,배고픈,399,1,2015.09.20 20:02 | ||
P0003,голодный,199,10, | ||
P0004,Űrállomás krízis,"999,5",1,2015.09.20 12:47 | ||
P0005,Ovális iroda,2 399,1,2015.09.20 07:31 | ||
''' | ||
|
||
def test_not_add_header(self): | ||
self.writer = riwo.csv.Writer(self.resource, self.iterable_input, self.Schema, add_header=False) | ||
self.writer.write() | ||
self.content = u'''\ | ||
P0001,đói,449,1,2015.09.20 20:00 | ||
P0002,배고픈,399,1,2015.09.20 20:02 | ||
P0003,голодный,199,10, | ||
P0004,Űrállomás krízis,"999,5",1,2015.09.20 12:47 | ||
P0005,Ovális iroda,2 399,1,2015.09.20 07:31 | ||
''' | ||
|
||
def test_delimiter(self): | ||
self.writer = riwo.csv.Writer(self.resource, self.iterable_input, self.Schema, delimiter='\001') | ||
self.writer.write() | ||
self.content = u'''\ | ||
id\001name\001price\001quantity\001updated_at | ||
P0001\001đói\001449\0011\0012015.09.20 20:00 | ||
P0002\001배고픈\001399\0011\0012015.09.20 20:02 | ||
P0003\001голодный\001199\00110\001 | ||
P0004\001Űrállomás krízis\001999,5\0011\0012015.09.20 12:47 | ||
P0005\001Ovális iroda\0012 399\0011\0012015.09.20 07:31 | ||
''' | ||
|
||
def test_quote(self): | ||
self.writer = riwo.csv.Writer(self.resource, self.iterable_input, self.Schema, \ | ||
delimiter='\\', quotechar='`', quoting=riwo.csv.QUOTE_ALL) | ||
self.writer.write() | ||
self.content = u'''\ | ||
`id`\`name`\`price`\`quantity`\`updated_at` | ||
`P0001`\`đói`\`449`\`1`\`2015.09.20 20:00` | ||
`P0002`\`배고픈`\`399`\`1`\`2015.09.20 20:02` | ||
`P0003`\`голодный`\`199`\`10`\`` | ||
`P0004`\`Űrállomás krízis`\`999,5`\`1`\`2015.09.20 12:47` | ||
`P0005`\`Ovális iroda`\`2 399`\`1`\`2015.09.20 07:31` | ||
''' | ||
|
||
def test_defined_schema_writer(self): | ||
self.writer = riwo.csv.Writer(self.resource, self.iterable_input, self.Schema) | ||
self.writer.write() | ||
self.content = u'''\ | ||
id,name,price,quantity,updated_at | ||
P0001,đói,449,1,2015.09.20 20:00 | ||
P0002,배고픈,399,1,2015.09.20 20:02 | ||
P0003,голодный,199,10, | ||
P0004,Űrállomás krízis,"999,5",1,2015.09.20 12:47 | ||
P0005,Ovális iroda,2 399,1,2015.09.20 07:31 | ||
''' | ||
|
||
def test_schema_flow_writer(self): | ||
self.writer = riwo.csv.Writer(self.resource, self.Schema(self.iterable_input, mapper=dp.mapper.NAME)) | ||
self.writer.write() | ||
self.content = u'''\ | ||
id,name,price,quantity,updated_at | ||
P0001,đói,449,1,2015.09.20 20:00 | ||
P0002,배고픈,399,1,2015.09.20 20:02 | ||
P0003,голодный,199,10, | ||
P0004,Űrállomás krízis,"999,5",1,2015.09.20 12:47 | ||
P0005,Ovális iroda,2 399,1,2015.09.20 07:31 | ||
''' | ||
|
||
def test_without_schema(self): | ||
with self.assertRaises(riwo.exceptions.SchemaRequired): | ||
riwo.csv.Writer(self.resource, self.iterable_input) | ||
self.content = u'' | ||
|
||
def test_unmarshal(self): | ||
self.writer = riwo.csv.Writer(self.resource, [], self.Schema) | ||
self.content = u'' | ||
|
||
current_datetime = datetime.datetime.now() | ||
self.assertEqual(self.writer.unmarshal_item(current_datetime), current_datetime.isoformat()) | ||
self.assertEqual(self.writer.unmarshal_item(4.21), u'4.21') | ||
self.assertEqual(self.writer.unmarshal_item(u'голодный'), u'голодный') | ||
|
||
def test_requisites(self): | ||
with self.assertRaises(riwo.exceptions.NestedSchemaNotSupported): | ||
riwo.csv.Writer(self.resource, [], self.NestedSchema) | ||
self.content = u'' | ||
|
||
def tearDown(self): | ||
self.resource.close() | ||
with io.open(self.test_file_path, 'r', encoding='utf-8') as f: | ||
content = f.read() | ||
self.assertEqual(self.content, content) | ||
os.remove(self.test_file_path) |
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 @@ | ||
output-* |
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 |
---|---|---|
@@ -1,5 +1,5 @@ | ||
P0001,đói,449,1,2015.09.20 20:00 | ||
P0002,배고픈,399,1,2015.09.20 20:02 | ||
P0003,голодный,199,10, | ||
P0004,Űrállomás krízis,"999,5",1,2015.09.20 12:47 | ||
P0005,Ovális iroda,2 399,1,2015.09.20 07:31 | ||
P0001;đói;449;1;2015.09.20 20:00 | ||
P0002;배고픈;399;1;2015.09.20 20:02 | ||
P0003;голодный;199;10; | ||
P0004;Űrállomás krízis;"999,5";1;2015.09.20 12:47 | ||
P0005;Ovális iroda;2 399;1;2015.09.20 07:31 |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thanks. 👍