Skip to content

Commit d26a544

Browse files
committed
Add docs for streams, fix sync stream and add tests
1 parent 3d6d319 commit d26a544

File tree

6 files changed

+446
-26
lines changed

6 files changed

+446
-26
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ You can download its zip file from the [Github Releases](https://github.com/clou
6868
* [Reports](docs/reports_usage.md)
6969
* [Translations](docs/translations_usage.md)
7070
* [Projects](docs/project_usage.md)
71+
* [Streams](docs/stream_usage.md)
7172

7273

7374
## Development

connect/cli/plugins/commerce/utils.py

Lines changed: 14 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -636,7 +636,7 @@ def print_results(results):
636636
)
637637

638638

639-
def print_errors(errors):
639+
def print_errors(errors): # pragma: no cover
640640
if errors:
641641
console.confirm(
642642
'Are you sure you want to display errors?',
@@ -886,7 +886,7 @@ def update_general_information(
886886
)
887887
.first()
888888
)
889-
body = {}
889+
body = {'context': {}}
890890
updated = 0
891891
errors_on_update = 0
892892
for n in range(2, sheet.max_row + 1):
@@ -901,31 +901,24 @@ def update_general_information(
901901
h.value == 'Product ID'
902902
and stream.get('context', {}).get('product', {}).get('id', None) != v.value
903903
):
904-
if 'context' in body:
905-
body['context'].update({'product': {'id': v.value}})
906-
else:
907-
body['context'] = {'product': {'id': v.value}}
904+
body['context']['product'] = {'id': v.value}
908905
updated += 1
909906
elif (
910907
h.value == 'Partner ID'
911908
and stream.get('context', {}).get('account', {}).get('id', None) != v.value
912909
):
913-
if 'context' in body:
914-
body['context'].update({'account': {'id': v.value}})
915-
else:
916-
body['context'] = {'account': {'id': v.value}}
910+
body['context']['account'] = {'id': v.value}
917911
updated += 1
918912
elif (
919913
h.value == 'Marketplace ID'
920914
and stream.get('context', {}).get('marketplace', {}).get('id', None) != v.value
921915
):
922-
if 'context' in body:
923-
body['context'].update({'marketplace': {'id': v.value}})
924-
else:
925-
body['context'] = {'marketplace': {'id': v.value}}
916+
body['context']['marketplace'] = {'id': v.value}
926917
updated += 1
927918

928919
if updated:
920+
if not body['context']:
921+
del body['context']
929922
try:
930923
client.ns(collection).streams[stream_id].update(
931924
json=body,
@@ -959,7 +952,7 @@ def update_transformations(
959952
origin_trf = None
960953
try:
961954
origin_trf = client.ns(collection).streams[stream_id].transformations[id.value].get()
962-
except ClientError as e:
955+
except ClientError:
963956
errors.append(
964957
f'The transformation {id.value} cannot be updated because it does not exist.'
965958
)
@@ -968,22 +961,22 @@ def update_transformations(
968961

969962
try:
970963
to_update = {}
971-
if origin_trf['settings'] != settings.value:
972-
to_update['settings'] = settings.value
964+
if origin_trf['settings'] != json.loads(settings.value):
965+
to_update['settings'] = json.loads(settings.value)
973966
if descr.value and (
974967
'description' not in origin_trf or origin_trf['description'] != descr.value
975968
):
976969
to_update['description'] = descr.value
977-
if origin_trf['position'] != position.value:
978-
to_update['position'] = position.value
970+
if int(origin_trf['position']) != position.value:
971+
to_update['position'] = int(position.value)
979972

980973
if to_update:
981974
client.ns(collection).streams[stream_id].transformations[id.value].update(
982975
json=to_update,
983976
)
984977
updated += 1
985978
progress.update(task, advance=1)
986-
except ClientError as e:
979+
except ClientError:
987980
errors.append(f'Error updating the transformation {id.value} with data {to_update}.')
988981

989982
try:
@@ -995,7 +988,7 @@ def update_transformations(
995988
client.ns(collection).streams[stream_id].transformations[cid].delete()
996989
deleted += 1
997990
except ClientError as e:
998-
errors.append(f'Error deleting the transformation {id.value}.')
991+
errors.append(f'Error deleting the transformation {e}.')
999992

1000993
results.append(('Transformations', sheet.max_row - 1, 0, updated, deleted, 0, len(errors)))
1001994

docs/stream_usage.md

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
# Streams management
2+
3+
The streams management area offers commands that allow users to clone and get listing of streams,
4+
export or synchronize to/from Excel file.
5+
6+
To access the group of commands related to the management of streams you must invoke the CLI with the `commerce stream` command:
7+
8+
```sh
9+
$ ccli commerce stream
10+
11+
Usage: ccli commerce [OPTIONS] COMMAND [ARGS]...
12+
13+
Options:
14+
-h, --help Show this message and exit.
15+
16+
Commands:
17+
clone Create a clone of a stream.
18+
export Export commerce billing or pricing streams.
19+
list List commerce billing and pricing streams.
20+
sync Synchronize a stream from an excel file.
21+
```
22+
23+
### Clone streams
24+
25+
To clone existing stream you can run:
26+
27+
```sh
28+
$ccli commerce stream clone [OPTIONS] stream_id
29+
```
30+
31+
```
32+
Options:
33+
-d, --destination_account TEXT Destination account ID
34+
-n, --new-stream-name TEXT Cloned stream name
35+
-v, --validate Executes the validate action after the clone.
36+
```
37+
38+
### List streams
39+
40+
To list available streams you can run:
41+
42+
```sh
43+
$ ccli commerce stream list [OPTIONS]
44+
```
45+
46+
```
47+
Options:
48+
-q, --query TEXT RQL query expression.
49+
```
50+
51+
### Export streams
52+
53+
To export stream you can run:
54+
55+
```sh
56+
$ ccli commerce stream export [OPTIONS] stream_id
57+
```
58+
59+
```
60+
Options:
61+
-o, --out FILE Output Excel file name.
62+
-p, --output_path DIRECTORY Directory where to store the export.
63+
```
64+
65+
### Synchronize streams
66+
67+
To synchronize a stream from an Excel file you can run:
68+
69+
```sh
70+
$ ccli commerce stream sync input_file
71+
```
72+
73+
Structure of sync file can be taken from `tests/fixtures/commerce/stream_sync.xlsx` file.

tests/conftest.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,11 @@ def sample_translation_workbook(fs):
336336
return load_workbook('./tests/fixtures/translation.xlsx')
337337

338338

339+
@pytest.fixture(scope='function')
340+
def sample_stream_workbook(fs):
341+
return load_workbook('./tests/fixtures/commerce/stream_sync.xlsx')
342+
343+
339344
@pytest.fixture(scope='function')
340345
def mocked_translation_response():
341346
with open('./tests/fixtures/translation_response.json') as response:

tests/plugins/commerce/test_commands.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -942,3 +942,30 @@ def test_sync_stream(
942942

943943
assert result.exit_code == 0
944944
assert mocked_cmd_console.echo.call_count == 2
945+
946+
947+
def test_sync_stream_no_stream(
948+
mocker,
949+
ccli,
950+
config_mocker,
951+
):
952+
mocker.patch('connect.cli.plugins.commerce.commands.console')
953+
mocker.patch('connect.cli.plugins.commerce.utils.get_work_book')
954+
mocker.patch(
955+
'connect.cli.plugins.commerce.utils.guess_if_billing_or_pricing_stream',
956+
return_value=None,
957+
)
958+
runner = CliRunner()
959+
cmd = [
960+
'commerce',
961+
'stream',
962+
'sync',
963+
'STR-7748-7021-7449',
964+
]
965+
result = runner.invoke(
966+
ccli,
967+
cmd,
968+
)
969+
970+
assert result.exit_code == 1
971+
assert 'Stream STR-7748-7021-7449 not found for the current account VA-000.' in result.output

0 commit comments

Comments
 (0)