-
Notifications
You must be signed in to change notification settings - Fork 68
[SDK-427] Add unit-tests for ExportTask #1289
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
Merged
Merged
Changes from all commits
Commits
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 hidden or 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 hidden or 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,76 @@ | ||
| from unittest.mock import MagicMock | ||
|
|
||
| from labelbox.schema.export_task import ( | ||
| Converter, | ||
| FileConverter, | ||
| Range, | ||
| StreamType, | ||
| _MetadataFileInfo, | ||
| _MetadataHeader, | ||
| _TaskContext, | ||
| ) | ||
|
|
||
|
|
||
| class TestFileConverter: | ||
|
|
||
| def test_with_correct_ndjson(self, tmp_path, generate_random_ndjson): | ||
| directory = tmp_path / "file-converter" | ||
| directory.mkdir() | ||
| line_count = 10 | ||
|
Contributor
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. curious why can't we test the same with 2 lines |
||
| ndjson = generate_random_ndjson(line_count) | ||
| file_content = "\n".join(ndjson) + "\n" | ||
| input_args = Converter.ConverterInputArgs( | ||
| ctx=_TaskContext( | ||
| client=MagicMock(), | ||
| task_id="task-id", | ||
| stream_type=StreamType.RESULT, | ||
| metadata_header=_MetadataHeader(total_size=len(file_content), | ||
| total_lines=line_count), | ||
| ), | ||
| file_info=_MetadataFileInfo( | ||
| offsets=Range(start=0, end=len(file_content) - 1), | ||
| lines=Range(start=0, end=line_count - 1), | ||
| file="file.ndjson", | ||
| ), | ||
| raw_data=file_content, | ||
| ) | ||
| path = directory / "output.ndjson" | ||
| with FileConverter(file_path=path) as converter: | ||
| for output in converter.convert(input_args): | ||
| assert output.current_line == 0 | ||
| assert output.current_offset == 0 | ||
| assert output.file_path == path | ||
| assert output.total_lines == line_count | ||
| assert output.total_size == len(file_content) | ||
| assert output.bytes_written == len(file_content) | ||
|
|
||
| def test_with_no_newline_at_end(self, tmp_path, generate_random_ndjson): | ||
| directory = tmp_path / "file-converter" | ||
| directory.mkdir() | ||
| line_count = 10 | ||
| ndjson = generate_random_ndjson(line_count) | ||
| file_content = "\n".join(ndjson) | ||
| input_args = Converter.ConverterInputArgs( | ||
| ctx=_TaskContext( | ||
| client=MagicMock(), | ||
| task_id="task-id", | ||
| stream_type=StreamType.RESULT, | ||
| metadata_header=_MetadataHeader(total_size=len(file_content), | ||
| total_lines=line_count), | ||
| ), | ||
| file_info=_MetadataFileInfo( | ||
| offsets=Range(start=0, end=len(file_content) - 1), | ||
| lines=Range(start=0, end=line_count - 1), | ||
| file="file.ndjson", | ||
| ), | ||
| raw_data=file_content, | ||
| ) | ||
| path = directory / "output.ndjson" | ||
| with FileConverter(file_path=path) as converter: | ||
| for output in converter.convert(input_args): | ||
| assert output.current_line == 0 | ||
| assert output.current_offset == 0 | ||
| assert output.file_path == path | ||
| assert output.total_lines == line_count | ||
| assert output.total_size == len(file_content) | ||
| assert output.bytes_written == len(file_content) | ||
139 changes: 139 additions & 0 deletions
139
tests/unit/export_task/test_unit_file_retriever_by_line.py
This file contains hidden or 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,139 @@ | ||
| from unittest.mock import MagicMock, patch | ||
| from labelbox.schema.export_task import ( | ||
| FileRetrieverByLine, | ||
| _TaskContext, | ||
| _MetadataHeader, | ||
| StreamType, | ||
| ) | ||
|
|
||
|
|
||
| class TestFileRetrieverByLine: | ||
|
|
||
| def test_by_line_from_start(self, generate_random_ndjson, mock_response): | ||
| line_count = 10 | ||
| ndjson = generate_random_ndjson(line_count) | ||
| file_content = "\n".join(ndjson) + "\n" | ||
|
|
||
| mock_client = MagicMock() | ||
|
Contributor
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. like this client mocking |
||
| mock_client.execute = MagicMock( | ||
| return_value={ | ||
| "task": { | ||
| "exportFileFromLine": { | ||
| "offsets": { | ||
| "start": "0", | ||
| "end": len(file_content) - 1 | ||
| }, | ||
| "lines": { | ||
| "start": "0", | ||
| "end": str(line_count - 1) | ||
| }, | ||
| "file": "http://some-url.com/file.ndjson", | ||
| } | ||
| } | ||
| }) | ||
|
|
||
| mock_ctx = _TaskContext( | ||
| client=mock_client, | ||
| task_id="task-id", | ||
| stream_type=StreamType.RESULT, | ||
| metadata_header=_MetadataHeader(total_size=len(file_content), | ||
| total_lines=line_count), | ||
| ) | ||
|
|
||
| with patch("requests.get", return_value=mock_response(file_content)): | ||
| retriever = FileRetrieverByLine(mock_ctx, 0) | ||
| info, content = retriever.get_next_chunk() | ||
| assert info.offsets.start == 0 | ||
| assert info.offsets.end == len(file_content) - 1 | ||
| assert info.lines.start == 0 | ||
| assert info.lines.end == line_count - 1 | ||
| assert info.file == "http://some-url.com/file.ndjson" | ||
| assert content == file_content | ||
|
|
||
| def test_by_line_from_middle(self, generate_random_ndjson, mock_response): | ||
| line_count = 10 | ||
| ndjson = generate_random_ndjson(line_count) | ||
| file_content = "\n".join(ndjson) + "\n" | ||
|
|
||
| mock_client = MagicMock() | ||
| mock_client.execute = MagicMock( | ||
| return_value={ | ||
| "task": { | ||
| "exportFileFromLine": { | ||
| "offsets": { | ||
| "start": "0", | ||
| "end": len(file_content) - 1 | ||
| }, | ||
| "lines": { | ||
| "start": "0", | ||
| "end": str(line_count - 1) | ||
| }, | ||
| "file": "http://some-url.com/file.ndjson", | ||
| } | ||
| } | ||
| }) | ||
|
|
||
| mock_ctx = _TaskContext( | ||
| client=mock_client, | ||
| task_id="task-id", | ||
| stream_type=StreamType.RESULT, | ||
| metadata_header=_MetadataHeader(total_size=len(file_content), | ||
| total_lines=line_count), | ||
| ) | ||
|
|
||
| line_start = 5 | ||
| current_offset = file_content.find(ndjson[line_start]) | ||
|
|
||
| with patch("requests.get", return_value=mock_response(file_content)): | ||
| retriever = FileRetrieverByLine(mock_ctx, line_start) | ||
| info, content = retriever.get_next_chunk() | ||
| assert info.offsets.start == current_offset | ||
| assert info.offsets.end == len(file_content) - 1 | ||
| assert info.lines.start == line_start | ||
| assert info.lines.end == line_count - 1 | ||
| assert info.file == "http://some-url.com/file.ndjson" | ||
| assert content == file_content[current_offset:] | ||
|
|
||
| def test_by_line_from_last(self, generate_random_ndjson, mock_response): | ||
| line_count = 10 | ||
| ndjson = generate_random_ndjson(line_count) | ||
| file_content = "\n".join(ndjson) + "\n" | ||
|
|
||
| mock_client = MagicMock() | ||
| mock_client.execute = MagicMock( | ||
| return_value={ | ||
| "task": { | ||
| "exportFileFromLine": { | ||
| "offsets": { | ||
| "start": "0", | ||
| "end": len(file_content) - 1 | ||
| }, | ||
| "lines": { | ||
| "start": "0", | ||
| "end": str(line_count - 1) | ||
| }, | ||
| "file": "http://some-url.com/file.ndjson", | ||
| } | ||
| } | ||
| }) | ||
|
|
||
| mock_ctx = _TaskContext( | ||
| client=mock_client, | ||
| task_id="task-id", | ||
| stream_type=StreamType.RESULT, | ||
| metadata_header=_MetadataHeader(total_size=len(file_content), | ||
| total_lines=line_count), | ||
| ) | ||
|
|
||
| line_start = 9 | ||
| current_offset = file_content.find(ndjson[line_start]) | ||
|
|
||
| with patch("requests.get", return_value=mock_response(file_content)): | ||
| retriever = FileRetrieverByLine(mock_ctx, line_start) | ||
| info, content = retriever.get_next_chunk() | ||
| assert info.offsets.start == current_offset | ||
| assert info.offsets.end == len(file_content) - 1 | ||
| assert info.lines.start == line_start | ||
| assert info.lines.end == line_count - 1 | ||
| assert info.file == "http://some-url.com/file.ndjson" | ||
| assert content == file_content[current_offset:] | ||
96 changes: 96 additions & 0 deletions
96
tests/unit/export_task/test_unit_file_retriever_by_offset.py
This file contains hidden or 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,96 @@ | ||
| from unittest.mock import MagicMock, patch | ||
| from labelbox.schema.export_task import ( | ||
| FileRetrieverByOffset, | ||
| _TaskContext, | ||
| _MetadataHeader, | ||
| StreamType, | ||
| ) | ||
|
|
||
|
|
||
| class TestFileRetrieverByOffset: | ||
|
|
||
| def test_by_offset_from_start(self, generate_random_ndjson, mock_response): | ||
| line_count = 10 | ||
| ndjson = generate_random_ndjson(line_count) | ||
| file_content = "\n".join(ndjson) + "\n" | ||
|
|
||
| mock_client = MagicMock() | ||
| mock_client.execute = MagicMock( | ||
| return_value={ | ||
| "task": { | ||
| "exportFileFromOffset": { | ||
| "offsets": { | ||
| "start": "0", | ||
| "end": len(file_content) - 1 | ||
| }, | ||
| "lines": { | ||
| "start": "0", | ||
| "end": str(line_count - 1) | ||
| }, | ||
| "file": "http://some-url.com/file.ndjson", | ||
| } | ||
| } | ||
| }) | ||
|
|
||
| mock_ctx = _TaskContext( | ||
| client=mock_client, | ||
| task_id="task-id", | ||
| stream_type=StreamType.RESULT, | ||
| metadata_header=_MetadataHeader(total_size=len(file_content), | ||
| total_lines=line_count), | ||
| ) | ||
|
|
||
| with patch("requests.get", return_value=mock_response(file_content)): | ||
| retriever = FileRetrieverByOffset(mock_ctx, 0) | ||
| info, content = retriever.get_next_chunk() | ||
| assert info.offsets.start == 0 | ||
| assert info.offsets.end == len(file_content) - 1 | ||
| assert info.lines.start == 0 | ||
| assert info.lines.end == line_count - 1 | ||
| assert info.file == "http://some-url.com/file.ndjson" | ||
| assert content == file_content | ||
|
|
||
| def test_by_offset_from_middle(self, generate_random_ndjson, mock_response): | ||
| line_count = 10 | ||
| ndjson = generate_random_ndjson(line_count) | ||
| file_content = "\n".join(ndjson) + "\n" | ||
|
|
||
| mock_client = MagicMock() | ||
| mock_client.execute = MagicMock( | ||
| return_value={ | ||
| "task": { | ||
| "exportFileFromOffset": { | ||
| "offsets": { | ||
| "start": "0", | ||
| "end": len(file_content) - 1 | ||
| }, | ||
| "lines": { | ||
| "start": "0", | ||
| "end": str(line_count - 1) | ||
| }, | ||
| "file": "http://some-url.com/file.ndjson", | ||
| } | ||
| } | ||
| }) | ||
|
|
||
| mock_ctx = _TaskContext( | ||
| client=mock_client, | ||
| task_id="task-id", | ||
| stream_type=StreamType.RESULT, | ||
| metadata_header=_MetadataHeader(total_size=len(file_content), | ||
| total_lines=line_count), | ||
| ) | ||
|
|
||
| line_start = 5 | ||
| skipped_bytes = 15 | ||
| current_offset = file_content.find(ndjson[line_start]) + skipped_bytes | ||
|
|
||
| with patch("requests.get", return_value=mock_response(file_content)): | ||
| retriever = FileRetrieverByOffset(mock_ctx, current_offset) | ||
| info, content = retriever.get_next_chunk() | ||
| assert info.offsets.start == current_offset | ||
| assert info.offsets.end == len(file_content) - 1 | ||
| assert info.lines.start == 5 | ||
| assert info.lines.end == line_count - 1 | ||
| assert info.file == "http://some-url.com/file.ndjson" | ||
| assert content == file_content[current_offset:] |
Oops, something went wrong.
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.
why not use static json line for test predictability?