Skip to content

Commit

Permalink
Merge pull request #138 from block/myron/simplify-define-fake-data-batch
Browse files Browse the repository at this point in the history
Simplify `define_fake_data_batch_for`.
  • Loading branch information
myronmarston authored Feb 4, 2025
2 parents 9e0f203 + d8b061a commit a073623
Show file tree
Hide file tree
Showing 7 changed files with 13 additions and 12 deletions.
4 changes: 3 additions & 1 deletion Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,15 @@ configure_local_rake_tasks = ->(tasks) do
tasks.env_port_mapping = {test: test_port}
tasks.output = schema_def_output

tasks.define_fake_data_batch_for(:widgets) do |batch|
tasks.define_fake_data_batch_for(:widgets) do
require "rspec/core" # the factories file expects RSpec to be loaded, so load it.

# spec_support is not a full-fledged gem and is not on the load path, so we have to
# add it's lib dir to the load path before we can require things from it.
$LOAD_PATH.unshift ::File.join(__dir__, "spec_support", "lib")
require "elastic_graph/spec_support/factories"

batch = []
batch.concat(manufacturers = Array.new(10) { FactoryBot.build(:manufacturer) })
batch.concat(electrical_parts = Array.new(10) { FactoryBot.build(:electrical_part, manufacturer: manufacturers.sample) })
batch.concat(mechanical_parts = Array.new(10) { FactoryBot.build(:mechanical_part, manufacturer: manufacturers.sample) })
Expand All @@ -76,6 +77,7 @@ configure_local_rake_tasks = ->(tasks) do

batch.concat(sponsors = Array.new(10) { FactoryBot.build(:sponsor) })
batch.concat(Array.new(10) { FactoryBot.build(:team, sponsors: sponsors.sample(rand(3))) })
batch
end

tested_datastore_versions = ::YAML.load_file(::File.expand_path("config/tested_datastore_versions.yaml", __dir__))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@ def index_fake_data(num_batches)
publishing_threads = Array.new(PARALLELISM) { new_publishing_thread(batch_queue) }

num_batches.times do
batch = [] # : ::Array[::Hash[::String, untyped]]
@fake_data_batch_generator.call(batch)
batch = @fake_data_batch_generator.call
@output.puts "Generated batch of #{batch.size} documents..."
batch_queue << batch
end
Expand Down
4 changes: 2 additions & 2 deletions elasticgraph-local/lib/elastic_graph/local/rake_tasks.rb
Original file line number Diff line number Diff line change
Expand Up @@ -336,8 +336,8 @@ class RakeTasks < ::Rake::TaskLib
# local_config_yaml: "config/settings/local.yaml",
# path_to_schema: "config/schema.rb"
# ) do |tasks|
# tasks.define_fake_data_batch_for :campaigns do |batch|
# batch.concat(FactoryBot.build_list(:campaigns))
# tasks.define_fake_data_batch_for :campaigns do
# FactoryBot.build_list(:campaigns)
# end
# end
def define_fake_data_batch_for(type, &block)
Expand Down
4 changes: 2 additions & 2 deletions elasticgraph-local/sig/elastic_graph/local/rake_tasks.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@ module ElasticGraph
UI_PORT_OFFSET: ::Integer
VALID_PORT_RANGE: ::Range[::Integer]

def define_fake_data_batch_for: (::Symbol) { (::Array[::Hash[::String, untyped]]) -> void } -> void
def define_fake_data_batch_for: (::Symbol) { () -> ::Array[::Hash[::String, untyped]] } -> void

def initialize: (
local_config_yaml: ::String | ::Pathname,
path_to_schema: ::String | ::Pathname
) ?{ (RakeTasks) -> void } -> void

@local_config_yaml: ::String
@fake_data_batch_generator_by_type: ::Hash[::Symbol, ^(::Array[::Hash[::String, untyped]]) -> void]
@fake_data_batch_generator_by_type: ::Hash[::Symbol, ^() -> ::Array[::Hash[::String, untyped]]]

private

Expand Down
2 changes: 1 addition & 1 deletion elasticgraph-local/sig/elastic_graph/local/types.rbs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module ElasticGraph
module Local
type fakeDataBatchGenerator = ^(::Array[::Hash[::String, untyped]]) -> void
type fakeDataBatchGenerator = ^() -> ::Array[::Hash[::String, untyped]]
end
end
4 changes: 2 additions & 2 deletions elasticgraph-local/spec/acceptance/rake_tasks_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,8 @@ def run_rake(*cli_args, port:, daemon_timeout: nil, batch_size: 1)

yield t if block_given?

t.define_fake_data_batch_for(:widgets) do |batch|
batch.concat(Array.new(batch_size) { build(:widget) })
t.define_fake_data_batch_for(:widgets) do
Array.new(batch_size) { build(:widget) }
end
end
end
Expand Down
4 changes: 2 additions & 2 deletions elasticgraph/lib/elastic_graph/project_template/Rakefile.tt
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ ElasticGraph::Local::RakeTasks.new(
# IDFilterInput: "IDFilter"
}

tasks.define_fake_data_batch_for :artists do |batch|
batch.concat(<%= ElasticGraph.setup_env.app_module %>::FakeDataBatchGenerator.generate(artists: 100, venues: 10))
tasks.define_fake_data_batch_for :artists do
<%= ElasticGraph.setup_env.app_module %>::FakeDataBatchGenerator.generate(artists: 100, venues: 10)
end
end

Expand Down

0 comments on commit a073623

Please sign in to comment.