|
| 1 | +from eventdata.parameter_sources.elasticlogs_bulk_source import ElasticlogsBulkSource |
| 2 | + |
| 3 | + |
| 4 | +class StaticEventGenerator: |
| 5 | + def __init__(self, index, type, doc, at_most=-1): |
| 6 | + self.index = index |
| 7 | + self.type = type |
| 8 | + self.doc = doc |
| 9 | + self.at_most = at_most |
| 10 | + |
| 11 | + def generate_event(self): |
| 12 | + if self.at_most == 0: |
| 13 | + raise StopIteration() |
| 14 | + self.at_most -= 1 |
| 15 | + return self.doc, self.index, self.type |
| 16 | + |
| 17 | + |
| 18 | +class StaticTrack: |
| 19 | + def __init__(self): |
| 20 | + self.indices = [] |
| 21 | + |
| 22 | + |
| 23 | +def test_generates_a_complete_bulk(): |
| 24 | + expected_bulk_size = 10 |
| 25 | + |
| 26 | + generator = StaticEventGenerator(index="elasticlogs", type="_doc", doc='{"location": [-0.1485188,51.5250666]}') |
| 27 | + param_source = ElasticlogsBulkSource(track=StaticTrack(), params={ |
| 28 | + "index": "elasticlogs", |
| 29 | + "bulk-size": expected_bulk_size |
| 30 | + }, random_event=generator) |
| 31 | + client_param_source = param_source.partition(partition_index=0, total_partitions=1) |
| 32 | + |
| 33 | + generated_params = client_param_source.params() |
| 34 | + assert len(generated_params["body"].split("\n")) == 2 * expected_bulk_size |
| 35 | + assert generated_params["action-metadata-present"] is True |
| 36 | + assert generated_params["bulk-size"] == expected_bulk_size |
| 37 | + |
| 38 | + |
| 39 | +def test_generates_a_bulk_that_ends_prematurely(): |
| 40 | + generator = StaticEventGenerator(index="elasticlogs", type="_doc", doc='{"loc": [-0.14851,51.5250]}', at_most=5) |
| 41 | + param_source = ElasticlogsBulkSource(track=StaticTrack(), params={ |
| 42 | + "index": "elasticlogs", |
| 43 | + "bulk-size": 10 |
| 44 | + }, random_event=generator) |
| 45 | + client_param_source = param_source.partition(partition_index=0, total_partitions=1) |
| 46 | + |
| 47 | + generated_params = client_param_source.params() |
| 48 | + # the actual bulk size does not matter but instead that the generator stopped prematurely after 5 items |
| 49 | + assert len(generated_params["body"].split("\n")) == 10 |
| 50 | + assert generated_params["action-metadata-present"] is True |
| 51 | + assert generated_params["bulk-size"] == 5 |
0 commit comments