Skip to content

Commit

Permalink
Add default event stubbing!
Browse files Browse the repository at this point in the history
  • Loading branch information
alextwoods committed Aug 16, 2024
1 parent 9fa2617 commit 0c0e009
Show file tree
Hide file tree
Showing 7 changed files with 88 additions and 44 deletions.
13 changes: 10 additions & 3 deletions codegen/projections/white_label/lib/white_label/stubs.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 16 additions & 10 deletions codegen/projections/white_label/spec/event_stream_stubs_spec.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion codegen/projections/white_label/spec/middleware_spec.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -237,4 +237,25 @@
subject.start_event_stream({}, event_stream_handler: event_handler)
end
end

context 'default stubs' do
let(:default_message) { 'message' }
it 'stubs a default initial response and event' do
event_handler.on_initial_response(&handler)
event_handler.on_simple_event(&handler)

expect(handler).to receive(:call) do |event|
expect(event).to be_a(WhiteLabel::Types::StartEventStreamOutput)
expect(event.initial_structure.message).to eq(default_message)
end

expect(handler).to receive(:call) do |event|
expect(event)
.to be_a(WhiteLabel::Types::Events::SimpleEvent)
expect(event.message).to eq(default_message)
end

subject.start_event_stream({}, event_stream_handler: event_handler)
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,8 @@ module WhiteLabel
client: client.config.http_client,
stub_error_classes: anything,
stub_data_class: anything,
stub_message_encoder: anything)
stub_message_encoder: anything,
response_events: anything)
.and_call_original

client.kitchen_sink
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,6 @@ private void renderStubs() {
}

if (Streaming.isEventStreaming(model, outputShape)) {
eventStreamEventsToRender.add(o); // to handle initial-response events
for (MemberShape memberShape : outputShape.members()) {
if (StreamingTrait.isEventStream(model, memberShape)) {
UnionShape eventStreamUnion = model.expectShape(
Expand Down Expand Up @@ -343,27 +342,12 @@ private void renderStubs() {
// Render all shapes in alphabetical ordering
eventStreamEventsToRender
.forEach(shape -> {
if (shape.isOperationShape()) {
// initial-response
writer
.write("")
.openBlock("class $LInitialResponse",
symbolProvider.toSymbol(shape).getName())
.call(() -> renderInitialResponseEventStubMethod(
model.expectShape(
shape.asOperationShape().get().getOutputShape(),
StructureShape.class)
)
)
.closeBlock("end");
} else {
// Event stream event members MUST target only StructureShapes
writer
.write("")
.openBlock("class $L", symbolProvider.toSymbol(shape).getName())
.call(() -> renderEventStubMethod(shape.asStructureShape().get()))
.closeBlock("end");
}
// Event stream event members MUST target only StructureShapes
writer
.write("")
.openBlock("class $L", symbolProvider.toSymbol(shape).getName())
.call(() -> renderEventStubMethod(shape.asStructureShape().get()))
.closeBlock("end");
});
})
.closeBlock("end");
Expand Down Expand Up @@ -436,6 +420,8 @@ private void renderStubsForOperation(OperationShape operation, Shape outputShape
UnionShape eventStreamUnion = model.expectShape(
Streaming.getEventStreamMember(model, outputShape).get().getTarget(),
UnionShape.class);
renderDefaultEventMethod(eventStreamUnion);
writer.write("");
renderValidateEventMethod(eventStreamUnion);
writer.write("");
renderStubEventMethod(eventStreamUnion);
Expand Down Expand Up @@ -463,6 +449,26 @@ private void renderStubEventMethod(UnionShape eventStreamUnion) {
.closeBlock("end");
}

private void renderDefaultEventMethod(UnionShape eventStreamUnion) {
String name = symbolProvider.toSymbol(eventStreamUnion).getName();
writer
.write("")
.openBlock("def self.default_event(visited = [])")
.write("return nil if visited.include?('$L')", name)
.write("visited = visited + ['$L']", name)
.call(() -> {
MemberShape defaultMember = eventStreamUnion.members().iterator().next();
Shape target = model.expectShape(defaultMember.getTarget());
String className = symbolProvider.toSymbol(target).getName();
writer.openBlock("Params::$L.build(", className);
String symbolName = RubyFormatter.toSnakeCase(symbolProvider.toMemberName(defaultMember));
target.accept(new MemberDefaults("", ",", symbolName));
writer.write("context: 'default_event'");
writer.closeBlock(")");
})
.closeBlock("end");
}

private void renderValidateEventMethod(UnionShape eventStreamUnion) {
writer
.openBlock("def self.validate_event!(event, context:)")
Expand Down
16 changes: 9 additions & 7 deletions hearth/lib/hearth/middleware/send.rb
Original file line number Diff line number Diff line change
Expand Up @@ -276,13 +276,15 @@ def apply_initial_response(response, context)
end

def apply_stub_nil_event(context)
# TODO
# output = @stub_data_class.build(
# @stub_data_class.default,
# context: 'stub'
# )
# @stub_data_class.validate!(output, context: 'stub')
# @stub_data_class.stub(context.response, stub: output)
apply_initial_response_stub(
@stub_data_class.default,
context
)

apply_stub_event_structure(
@stub_data_class.default_event,
context
)
end

def apply_stub_event_api_error(api_error, context)
Expand Down

0 comments on commit 0c0e009

Please sign in to comment.