Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix test utils and provide test cases for karton.core.test
- Loading branch information
Showing
8 changed files
with
188 additions
and
30 deletions.
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
Empty file.
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,23 @@ | ||
from karton.core import Karton, Task | ||
|
||
|
||
class MathKarton(Karton): | ||
identity = "karton.math" | ||
filters = [ | ||
{ | ||
"type": "math-task" | ||
} | ||
] | ||
|
||
def process(self, task: Task): | ||
numbers = task.get_payload("numbers") | ||
sum_of_numbers = sum(numbers) | ||
result = Task( | ||
headers={ | ||
"type": "math-result" | ||
}, | ||
payload={ | ||
"result": sum_of_numbers | ||
} | ||
) | ||
self.send_task(result) |
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,31 @@ | ||
from .math_karton import MathKarton | ||
from karton.core import Task | ||
from karton.core.test import KartonTestCase | ||
|
||
|
||
class MathKartonTestCase(KartonTestCase): | ||
"""Test a karton that accepts an array of integers in "numbers" payload and | ||
returns their sum in "result". | ||
""" | ||
karton_class = MathKarton | ||
|
||
def test_addition(self) -> None: | ||
# prepare a fake test task that matches the production format | ||
task = Task({ | ||
"type": "math-task", | ||
}, payload={ | ||
"numbers": [1, 2, 3, 4], | ||
}) | ||
|
||
# dry-run the fake task on the wrapped karton system | ||
results = self.run_task(task) | ||
|
||
# prepare a expected output task and check if it matches the one produced | ||
expected_task = Task({ | ||
"origin": "karton.math", | ||
"type": "math-result" | ||
}, payload={ | ||
"result": 10, | ||
}) | ||
|
||
self.assertTasksEqual(results, [expected_task]) |
Empty file.
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,23 @@ | ||
from karton.core import Karton, Task, Resource | ||
|
||
|
||
class ReverserKarton(Karton): | ||
identity = "karton.reverser" | ||
filters = [ | ||
{ | ||
"type": "reverse-task" | ||
} | ||
] | ||
|
||
def process(self, task: Task): | ||
input_file = task.get_resource("file") | ||
reversed_content = input_file.content[::-1] | ||
result = Task( | ||
headers={ | ||
"type": "reverse-result" | ||
}, | ||
payload={ | ||
"file": Resource("sample.txt", content=reversed_content) | ||
} | ||
) | ||
self.send_task(result) |
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,39 @@ | ||
from .reverser_karton import ReverserKarton | ||
from karton.core import Task, Resource | ||
from karton.core.test import KartonTestCase | ||
|
||
|
||
class ReverserKartonTestCase(KartonTestCase): | ||
""" | ||
Test a karton that expects a KartonResource in "file" key and spawns a new | ||
task containing that file reversed. | ||
""" | ||
|
||
karton_class = ReverserKarton | ||
|
||
def test_reverse(self) -> None: | ||
# prepare input data | ||
input_data = b"foobarbaz" | ||
# create fake, mini-independent resources | ||
input_sample = Resource("sample.txt", input_data) | ||
output_sample = Resource("sample.txt", input_data[::-1]) | ||
|
||
# prepare a fake test task that matches the production format | ||
task = Task({ | ||
"type": "reverse-task", | ||
}, payload={ | ||
"file": input_sample | ||
}) | ||
|
||
# dry-run the fake task on the wrapped karton system | ||
results = self.run_task(task) | ||
|
||
# prepare a expected output task and check if it matches the one produced | ||
expected_task = Task({ | ||
"origin": "karton.reverser", | ||
"type": "reverse-result" | ||
}, payload={ | ||
"file": output_sample, | ||
}) | ||
|
||
self.assertTasksEqual(results, [expected_task]) |