async def test_lexicon_set_mutation(patch, logger, story): story.context = {} patch.object(story, 'line') patch.object(Lexicon, 'line_number_or_none') patch.object(Mutations, 'mutate') line = { 'ln': '1', 'name': ['out'], 'args': [ 'values', { '$OBJECT': 'mutation' } ], 'next': '2' } Mutations.mutate.return_value = 'mutated_result' result = await Lexicon.set(logger, story, line) story.resolve.assert_called_with(line['args'][0]) story.end_line.assert_called_with( line['ln'], assign={'paths': ['out'], '$OBJECT': 'path'}, output='mutated_result') story.line.assert_called_with(line['next']) Mutations.mutate.assert_called_with(line['args'][1], story.resolve(), story, line) assert result == Lexicon.line_number_or_none()
async def test_if_condition_elif_else(patch, logger, story, line, method): line['method'] = method patch.object(Lexicon, 'line_number_or_none') patch.object(story, 'next_block') ret = await Lexicon.if_condition(logger, story, line) story.next_block.assert_called_with(line) Lexicon.line_number_or_none.assert_called_with(story.next_block()) assert ret == Lexicon.line_number_or_none()
async def test_lexicon_for_loop(patch, logger, story, line, async_mock, execute_block_return): iterated_over_items = [] async def execute_block(our_logger, our_story, our_line): iterated_over_items.append(story.context['element']) assert our_logger == logger assert our_story == story assert our_line == line return execute_block_return patch.object(Lexicon, 'execute', new=async_mock()) patch.object(Lexicon, 'line_number_or_none') patch.object(Lexicon, 'execute_block', side_effect=execute_block) patch.object(story, 'next_block') line['args'] = [ {'$OBJECT': 'path', 'paths': ['elements']} ] line['output'] = ['element'] story.context = {'elements': ['one', 'two', 'three']} story.resolve.return_value = ['one', 'two', 'three'] story.environment = {} result = await Lexicon.for_loop(logger, story, line) if execute_block_return == LineSentinels.BREAK: assert iterated_over_items == ['one'] assert result == Lexicon.line_number_or_none(story.next_block(line)) elif LineSentinels.is_sentinel(execute_block_return): assert iterated_over_items == ['one'] assert result == execute_block_return else: assert iterated_over_items == story.context['elements'] assert result == Lexicon.line_number_or_none(story.next_block(line)) # Ensure no leakage of the element assert story.context.get('element') is None
async def test_lexicon_execute_streaming_container(patch, story, async_mock): line = { 'enter': '10', 'ln': '9', LineConstants.service: 'foo', 'output': 'output', 'next': '11' } patch.object(Services, 'start_container', new=async_mock()) patch.object(Lexicon, 'line_number_or_none') patch.many(story, ['end_line', 'line']) Metrics.container_start_seconds_total = Mock() ret = await Lexicon.execute(story.logger, story, line) Services.start_container.mock.assert_called_with(story, line) story.end_line.assert_called_with( line['ln'], output=Services.start_container.mock.return_value, assign={'paths': line.get('output')}) Metrics.container_start_seconds_total.labels().observe.assert_called_once() story.line.assert_called_with(line['next']) Lexicon.line_number_or_none.assert_called_with(story.line()) assert ret == Lexicon.line_number_or_none()
async def test_lexicon_execute(patch, logger, story, line, async_mock, name): line['enter'] = None if name is not None: line['name'] = [name] output = MagicMock() patch.object(Services, 'execute', new=async_mock(return_value=output)) patch.object(Lexicon, 'line_number_or_none') patch.object(story, 'line') result = await Lexicon.execute(logger, story, line) Services.execute.mock.assert_called_with(story, line) if name is not None: story.end_line.assert_called_with(line['ln'], output=output, assign={'paths': [name]}) else: story.end_line.assert_called_with(line['ln'], output=output, assign=None) story.line.assert_called_with(line['next']) assert result == Lexicon.line_number_or_none()
def test_next_line_or_none(): line = {'ln': '10'} assert Lexicon.line_number_or_none(line) == '10' assert Lexicon.line_number_or_none(None) is None