Skip to content

Commit

Permalink
Add docs for streams, fix sync stream and add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
bdjilka committed Sep 26, 2023
1 parent 3d6d319 commit c344b22
Show file tree
Hide file tree
Showing 6 changed files with 323 additions and 22 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ You can download its zip file from the [Github Releases](https://github.com/clou
* [Reports](docs/reports_usage.md)
* [Translations](docs/translations_usage.md)
* [Projects](docs/project_usage.md)
* [Streams](docs/stream_usage.md)


## Development
Expand Down
27 changes: 10 additions & 17 deletions connect/cli/plugins/commerce/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -886,7 +886,7 @@ def update_general_information(
)
.first()
)
body = {}
body = {'context': {}}
updated = 0
errors_on_update = 0
for n in range(2, sheet.max_row + 1):
Expand All @@ -901,31 +901,24 @@ def update_general_information(
h.value == 'Product ID'
and stream.get('context', {}).get('product', {}).get('id', None) != v.value
):
if 'context' in body:
body['context'].update({'product': {'id': v.value}})
else:
body['context'] = {'product': {'id': v.value}}
body['context']['product'] = {'id': v.value}
updated += 1
elif (
h.value == 'Partner ID'
and stream.get('context', {}).get('account', {}).get('id', None) != v.value
):
if 'context' in body:
body['context'].update({'account': {'id': v.value}})
else:
body['context'] = {'account': {'id': v.value}}
body['context']['account'] = {'id': v.value}
updated += 1
elif (
h.value == 'Marketplace ID'
and stream.get('context', {}).get('marketplace', {}).get('id', None) != v.value
):
if 'context' in body:
body['context'].update({'marketplace': {'id': v.value}})
else:
body['context'] = {'marketplace': {'id': v.value}}
body['context']['marketplace'] = {'id': v.value}
updated += 1

if updated:
if not body['context']:
del body['context']
try:
client.ns(collection).streams[stream_id].update(
json=body,
Expand Down Expand Up @@ -968,14 +961,14 @@ def update_transformations(

try:
to_update = {}
if origin_trf['settings'] != settings.value:
to_update['settings'] = settings.value
if origin_trf['settings'] != json.loads(settings.value):
to_update['settings'] = json.loads(settings.value)
if descr.value and (
'description' not in origin_trf or origin_trf['description'] != descr.value
):
to_update['description'] = descr.value
if origin_trf['position'] != position.value:
to_update['position'] = position.value
if int(origin_trf['position']) != position.value:
to_update['position'] = int(position.value)

if to_update:
client.ns(collection).streams[stream_id].transformations[id.value].update(
Expand Down
73 changes: 73 additions & 0 deletions docs/stream_usage.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
# Streams management

The streams management area offers commands that allow users to clone and get listing of streams,
export or synchronize to/from Excel file.

To access the group of commands related to the management of streams you must invoke the CLI with the `commerce stream` command:

```sh
$ ccli commerce stream

Usage: ccli commerce [OPTIONS] COMMAND [ARGS]...

Options:
-h, --help Show this message and exit.

Commands:
clone Create a clone of a stream.
export Export commerce billing or pricing streams.
list List commerce billing and pricing streams.
sync Synchronize a stream from an excel file.
```

### Clone streams

To clone existing stream you can run:

```sh
$ccli commerce stream clone [OPTIONS] stream_id
```

```
Options:
-d, --destination_account TEXT Destination account ID
-n, --new-stream-name TEXT Cloned stream name
-v, --validate Executes the validate action after the clone.
```

### List streams

To list available streams you can run:

```sh
$ ccli commerce stream list [OPTIONS]
```

```
Options:
-q, --query TEXT RQL query expression.
```

### Export streams

To export stream you can run:

```sh
$ ccli commerce stream export [OPTIONS] stream_id
```

```
Options:
-o, --out FILE Output Excel file name.
-p, --output_path DIRECTORY Directory where to store the export.
```

### Synchronize streams

To synchronize a stream from an Excel file you can run:

```sh
$ ccli commerce stream sync input_file
```

Structure of sync file can be taken from `tests/fixtures/commerce/stream_sync.xlsx` file.
5 changes: 5 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,11 @@ def sample_translation_workbook(fs):
return load_workbook('./tests/fixtures/translation.xlsx')


@pytest.fixture(scope='function')
def sample_stream_workbook(fs):
return load_workbook('./tests/fixtures/commerce/stream_sync.xlsx')


@pytest.fixture(scope='function')
def mocked_translation_response():
with open('./tests/fixtures/translation_response.json') as response:
Expand Down
27 changes: 27 additions & 0 deletions tests/plugins/commerce/test_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -942,3 +942,30 @@ def test_sync_stream(

assert result.exit_code == 0
assert mocked_cmd_console.echo.call_count == 2


def test_sync_stream_no_stream(
mocker,
ccli,
config_mocker,
):
mocker.patch('connect.cli.plugins.commerce.commands.console')
mocker.patch('connect.cli.plugins.commerce.utils.get_work_book')
mocker.patch(
'connect.cli.plugins.commerce.utils.guess_if_billing_or_pricing_stream',
return_value=None,
)
runner = CliRunner()
cmd = [
'commerce',
'stream',
'sync',
'STR-7748-7021-7449',
]
result = runner.invoke(
ccli,
cmd,
)

assert result.exit_code == 1
assert 'Stream STR-7748-7021-7449 not found for the current account VA-000.' in result.output
Loading

0 comments on commit c344b22

Please sign in to comment.