Skip to content

Commit d05f6e0

Browse files
authored
Merge pull request #41 from alingse/add-test-1
add test
2 parents 4872114 + 848ed7e commit d05f6e0

File tree

5 files changed

+99
-87
lines changed

5 files changed

+99
-87
lines changed

jsoncsv/dumptool.py

100755100644
Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@
22
# author@alingse
33
# 2015.10.09
44

5-
import unicodecsv as csv
65
import json
6+
7+
import unicodecsv as csv
78
import xlwt
89

910

1011
class Dump(object):
11-
1212
def __init__(self, fin, fout, **kwargs):
1313
self.fin = fin
1414
self.fout = fout
@@ -33,7 +33,6 @@ def dump(self):
3333

3434

3535
class ReadHeadersMixin(object):
36-
3736
@staticmethod
3837
def load_headers(fin, read_row=None, sort_type=None):
3938
headers = set()
@@ -58,14 +57,14 @@ def load_headers(fin, read_row=None, sort_type=None):
5857

5958

6059
class DumpExcel(Dump, ReadHeadersMixin):
61-
6260
def initialize(self, **kwargs):
6361
super(DumpExcel, self).initialize(**kwargs)
6462
self._read_row = kwargs.get('read_row')
6563
self._sort_type = kwargs.get('sort_type')
6664

6765
def prepare(self):
68-
headers, datas = self.load_headers(self.fin, self._read_row, self._sort_type)
66+
headers, datas = self.load_headers(self.fin, self._read_row,
67+
self._sort_type)
6968
self._headers = headers
7069
self._datas = datas
7170

@@ -87,7 +86,6 @@ def dump_file(self):
8786

8887

8988
class DumpCSV(DumpExcel):
90-
9189
def initialize(self, **kwargs):
9290
super(DumpCSV, self).initialize(**kwargs)
9391
self.csv_writer = None
@@ -110,7 +108,6 @@ def patch_obj(self, obj):
110108

111109

112110
class DumpXLS(DumpExcel):
113-
114111
def initialize(self, **kwargs):
115112
super(DumpXLS, self).initialize(**kwargs)
116113

jsoncsv/jsontool.py

100755100644
Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,15 @@
11
# coding=utf-8
22
# author@alingse
33
# 2016.05.27
4-
from __future__ import absolute_import
5-
from __future__ import unicode_literals
4+
from __future__ import absolute_import, unicode_literals
65

76
import json
87
from copy import deepcopy
98
from itertools import groupby
109
from operator import itemgetter
1110

1211
from jsoncsv import PY2
13-
from jsoncsv.utils import encode_safe_key
14-
from jsoncsv.utils import decode_safe_key
15-
12+
from jsoncsv.utils import decode_safe_key, encode_safe_key
1613

1714
__all__ = [
1815
'convert_json',

jsoncsv/main.py

Lines changed: 55 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -1,64 +1,48 @@
11
# coding=utf-8
22
import click
33

4-
from jsoncsv import jsontool
5-
from jsoncsv import dumptool
4+
from jsoncsv import dumptool, jsontool
65
from jsoncsv.dumptool import dump_excel
76
from jsoncsv.jsontool import convert_json
87
from jsoncsv.utils import unit_char
98

109

1110
def separator_type(sep):
1211
if len(sep) != 1:
13-
raise click.BadOptionUsage(
14-
option_name='separator',
15-
message='separator can only be a char')
12+
raise click.BadOptionUsage(option_name='separator',
13+
message='separator can only be a char')
1614
if sep == unit_char:
17-
raise click.BadOptionUsage(
18-
option_name='separator',
19-
message='separator can not be `\\` ')
15+
raise click.BadOptionUsage(option_name='separator',
16+
message='separator can not be `\\` ')
2017
return sep
2118

2219

2320
@click.command()
24-
@click.option(
25-
'-A',
26-
'--array',
27-
'json_array',
28-
is_flag=True,
29-
default=False,
30-
help='read input file as json array')
31-
@click.option(
32-
'-s',
33-
'--sep',
34-
'separator',
35-
type=separator_type,
36-
default='.',
37-
help='separator')
38-
@click.option(
39-
'--safe',
40-
is_flag=True,
41-
help='use safe mode')
42-
@click.option(
43-
'-r',
44-
'--restore',
45-
'restore',
46-
is_flag=True,
47-
help='restore expanded json')
48-
@click.option(
49-
'-e',
50-
'--expand',
51-
'expand',
52-
is_flag=True,
53-
help='expand json (default True)')
54-
@click.argument(
55-
'input',
56-
type=click.File('r', encoding='utf-8'),
57-
default='-')
58-
@click.argument(
59-
'output',
60-
type=click.File('w', encoding='utf-8'),
61-
default='-')
21+
@click.option('-A',
22+
'--array',
23+
'json_array',
24+
is_flag=True,
25+
default=False,
26+
help='read input file as json array')
27+
@click.option('-s',
28+
'--sep',
29+
'separator',
30+
type=separator_type,
31+
default='.',
32+
help='separator')
33+
@click.option('--safe', is_flag=True, help='use safe mode')
34+
@click.option('-r',
35+
'--restore',
36+
'restore',
37+
is_flag=True,
38+
help='restore expanded json')
39+
@click.option('-e',
40+
'--expand',
41+
'expand',
42+
is_flag=True,
43+
help='expand json (default True)')
44+
@click.argument('input', type=click.File('r', encoding='utf-8'), default='-')
45+
@click.argument('output', type=click.File('w', encoding='utf-8'), default='-')
6246
def jsoncsv(output, input, expand, restore, safe, separator, json_array):
6347
if expand and restore:
6448
raise click.UsageError('can not choose both, default is `-e`')
@@ -68,41 +52,37 @@ def jsoncsv(output, input, expand, restore, safe, separator, json_array):
6852
else:
6953
func = jsontool.restore
7054

71-
convert_json(input, output, func, separator=separator, safe=safe, json_array=json_array)
55+
convert_json(input,
56+
output,
57+
func,
58+
separator=separator,
59+
safe=safe,
60+
json_array=json_array)
7261

7362
input.close()
7463
output.close()
7564

7665

7766
@click.command()
78-
@click.option(
79-
'-t',
80-
'--type',
81-
'type_',
82-
type=click.Choice(['csv', 'xls']),
83-
default='csv',
84-
help='choose dump format')
85-
@click.option(
86-
'-r',
87-
'--row',
88-
type=int,
89-
default=None,
90-
help='number of pre-read `row` lines to load `headers`')
91-
@click.option(
92-
'-s',
93-
'--sort',
94-
'sort_',
95-
is_flag=True,
96-
default=False,
97-
help='enable sort the headers keys')
98-
@click.argument(
99-
'input',
100-
type=click.File('r', encoding='utf-8'),
101-
default='-')
102-
@click.argument(
103-
'output',
104-
type=click.File('wb'),
105-
default='-')
67+
@click.option('-t',
68+
'--type',
69+
'type_',
70+
type=click.Choice(['csv', 'xls']),
71+
default='csv',
72+
help='choose dump format')
73+
@click.option('-r',
74+
'--row',
75+
type=int,
76+
default=None,
77+
help='number of pre-read `row` lines to load `headers`')
78+
@click.option('-s',
79+
'--sort',
80+
'sort_',
81+
is_flag=True,
82+
default=False,
83+
help='enable sort the headers keys')
84+
@click.argument('input', type=click.File('r', encoding='utf-8'), default='-')
85+
@click.argument('output', type=click.File('wb'), default='-')
10686
def mkexcel(output, input, sort_, row, type_):
10787
klass = dumptool.DumpCSV
10888
if type_ == "xls":

tests/test_jsoncsv.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,13 @@ def test_jsoncsv_with_error_args(self):
5050
result = runner.invoke(jsoncsv, args=args)
5151
assert result.exit_code != 0
5252

53+
def test_jsoncsv_with_error_sep_args(self):
54+
runner = CliRunner()
55+
args = ['-s', '\\', '-e', 'fixture/files/raw.0.json',
56+
'fixture/files/tmp.expand.0.json']
57+
result = runner.invoke(jsoncsv, args=args)
58+
assert result.exit_code != 0
59+
5360
def test_jsoncsv_with_error_args_expand_and_restore(self):
5461
runner = CliRunner()
5562
args = ['-r', '-e', 'fixture/files/raw.0.json',

tests/test_mkexcel.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# coding=utf-8
2+
# author@alingse
3+
# 2020.03.10
4+
5+
import unittest
6+
from click.testing import CliRunner
7+
8+
from jsoncsv.main import mkexcel
9+
10+
11+
class Testmkexcel(unittest.TestCase):
12+
def test_mkexcel_csv(self):
13+
runner = CliRunner()
14+
args = ['fixture/files/expand.0.json',
15+
'fixture/files/tmp.expand.0.csv']
16+
result = runner.invoke(mkexcel, args=args)
17+
assert result.exit_code == 0
18+
19+
def test_mkexcel_xls(self):
20+
runner = CliRunner()
21+
args = ['-t', 'xls', 'fixture/files/expand.0.json',
22+
'fixture/files/tmp.expand.0.xls']
23+
result = runner.invoke(mkexcel, args=args)
24+
assert result.exit_code == 0
25+
26+
def test_mkexcel_with_error(self):
27+
runner = CliRunner()
28+
args = ['-t', 'xlsx', 'fixture/files/expand.0.json',
29+
'fixture/files/tmp.expand.0.xls']
30+
result = runner.invoke(mkexcel, args=args)
31+
assert result.exit_code == 2

0 commit comments

Comments
 (0)