Skip to content

Instantly share code, notes, and snippets.

@mbriggs
Created September 3, 2018 18:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mbriggs/32be816c2aadaa866c79fe9183d1d9e5 to your computer and use it in GitHub Desktop.
Save mbriggs/32be816c2aadaa866c79fe9183d1d9e5 to your computer and use it in GitHub Desktop.
Fixtures
module Fixtures
class EventWritten
include TestBench::Fixture
attr_accessor :stream_name
attr_accessor :values
attr_accessor :command
attr_accessor :expected_version
attr_accessor :command_attributes
initializer :event_class, :write
def self.build(event_class, write, arguments)
instance = new(event_class, write)
arguments.set(instance)
instance
end
def self.call(event_class, write)
arguments = Arguments.new
yield arguments if block_given?
instance = build(event_class, write, arguments)
instance.()
end
def call
event = one_message
expected_stream_name = self.stream_name
command = self.command
values = self.values
expected_expected_version = self.expected_version
command_attributes = self.command_attributes
stream_name, expected_version = written(event) if event
event_name = event_class.name.split("::").last
context "event #{event_name}" do
test "written" do
refute event.nil?
end
unless expected_stream_name.nil?
test "wrote to #{expected_stream_name}" do
assert stream_name == expected_stream_name
end
end
unless expected_expected_version.nil?
test "wrote expected version [#{expected_expected_version} -> #{expected_version.inspect}]" do
assert expected_version == expected_expected_version
end
end
if command
AttributeEquality.(event, command, command_attributes)
end
if values
context "Attribute Set" do
values.each do |attribute, value|
value_set = event.send(attribute)
test "#{attribute} [#{value} -> #{value_set}]" do
assert value_set == value
end
end
end
end
end
end
def one_message
write.one_message do |msg|
msg.instance_of?(event_class)
end
end
def written(message)
stream_name = nil
expected_version = nil
write.written?(message) do |name, version|
stream_name = name
expected_version = version
end
[stream_name, expected_version]
end
class Arguments
attr_accessor :stream_name
attr_accessor :values
attr_reader :command
attr_reader :command_attributes
attr_accessor :expected_version
def set(fixture)
fixture.stream_name = stream_name
fixture.values = values
fixture.command = command
fixture.expected_version = expected_version
fixture.command_attributes = command_attributes
end
def follows(command, attrs)
@command = command
@command_attributes = attrs
end
end
end
end
Handle Commands
PrintRemoteFile
Printed
printing
downloaded
printed
printed requested file
event JobPrinted
written
wrote to printJob-00001111-0000-4000-8000-000000000000
wrote expected version [1 -> 1]
Attribute Equality [PrintRemoteFile -> JobPrinted]
job_id ["00001111-0000-4000-8000-000000000000" -> "00001111-0000-4000-8000-000000000000"]
time ["2000-01-01T00:00:00.000Z" -> "2000-01-01T00:00:00.000Z"]
user ["drew@notion.ca" -> "drew@notion.ca"]
Attribute Set
type [label -> label]
processed_time [2000-01-01T00:00:00.011Z -> 2000-01-01T00:00:00.011Z]
require_relative "../../automated_init"
context "Handle Commands" do
context "PrintRemoteFile" do
context "Printed" do
print = Controls::Commands::PrintRemoteFile.example
job = Controls::Job::New.example
version = Controls::Version.example
raw_processed_time = Controls::Time::Effective::Raw.example
processed_time = Clock::UTC.iso8601(raw_processed_time)
download_sink = Download::MemorySink.new
print_sink = Print::MemorySink.new
handler = Handlers::Commands.new
write = handler.write
download = handler.download
print_file = handler.print_file
handler.store.add(print.job_id, job, version)
handler.clock.now = raw_processed_time
download.telemetry.register(download_sink)
print_file.telemetry.register(print_sink)
handler.(print)
context "printing" do
downloaded = download_sink.one_record do |record|
record.signal == :downloaded
end
printed = print_sink.one_record do |record|
record.signal == :printed
end
test "downloaded" do
refute downloaded.nil?
end
test "printed" do
refute printed.nil?
end
test "printed requested file" do
downloaded_url = downloaded.data.uri.to_s
downloaded_path = downloaded.data.file.path
printed_path = printed.data
assert downloaded_url == print.url
assert downloaded_path == printed_path
end
end
Fixtures::EventWritten.(Messages::Events::JobPrinted, write) do |test|
test.follows print, [:job_id, :time, :user]
test.stream_name = "printJob-#{print.job_id}"
test.expected_version = version
test.values = {
type: "label",
processed_time: processed_time
}
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment