Skip to content

Commit

Permalink
Revise testing approach
Browse files Browse the repository at this point in the history
  • Loading branch information
andyw8 committed May 1, 2024
1 parent cb8857a commit 40a8bef
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 93 deletions.
4 changes: 3 additions & 1 deletion lib/ruby_lsp/ruby_lsp_rails/addon.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ def activate(global_state, message_queue)
@global_state = T.let(global_state, T.nilable(RubyLsp::GlobalState))
$stderr.puts("Activating Ruby LSP Rails addon v#{VERSION}")
# Start booting the real client in a background thread. Until this completes, the client will be a NullClient
Thread.new { @client = RunnerClient.create_client }
runner_thread = Thread.new { @client = RunnerClient.create_client }
# For tests, we want to wait for the client to be ready
runner_thread.join if ENV["RAILS_ENV"] == "test"
end

sig { override.void }
Expand Down
97 changes: 5 additions & 92 deletions test/ruby_lsp_rails/hover_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,6 @@ class HoverTest < ActiveSupport::TestCase
end

test "hook returns model column information" do
expected_response = {
schema_file: "#{dummy_root}/db/schema.rb",
columns: [
["id", "integer"],
["first_name", "string"],
["last_name", "string"],
["age", "integer"],
["created_at", "datetime"],
["updated_at", "datetime"],
],
primary_keys: ["id"],
}

RunnerClient.any_instance.stubs(model: expected_response)

response = hover_on_source(<<~RUBY, { line: 3, character: 0 })
class User < ApplicationRecord
end
Expand Down Expand Up @@ -70,40 +55,23 @@ class User < ApplicationRecord
end

test "return column information for namespaced models" do
expected_response = {
schema_file: "#{dummy_root}/db/schema.rb",
columns: [
["id", "integer"],
["first_name", "string"],
["last_name", "string"],
["age", "integer"],
["created_at", "datetime"],
["updated_at", "datetime"],
],
primary_keys: ["id"],
}

RunnerClient.any_instance.stubs(model: expected_response)

response = hover_on_source(<<~RUBY, { line: 4, character: 6 })
module Blog
class User < ApplicationRecord
class Post < ApplicationRecord
end
end
Blog::User
Blog::Post
RUBY

assert_equal(<<~CONTENT.chomp, response.contents.value)
[Schema](file://#{dummy_root}/db/schema.rb)
**id**: integer (PK)
**first_name**: string
**last_name**: string
**title**: string
**age**: integer
**body**: text
**created_at**: datetime
Expand All @@ -112,20 +80,6 @@ class User < ApplicationRecord
end

test "returns column information for models with composite primary keys" do
expected_response = {
schema_file: "#{dummy_root}/db/schema.rb",
columns: [
["order_id", "integer"],
["product_id", "integer"],
["note", "string"],
["created_at", "datetime"],
["updated_at", "datetime"],
],
primary_keys: ["order_id", "product_id"],
}

RunnerClient.any_instance.stubs(model: expected_response)

response = hover_on_source(<<~RUBY, { line: 3, character: 0 })
class CompositePrimaryKey < ApplicationRecord
end
Expand All @@ -146,55 +100,14 @@ class CompositePrimaryKey < ApplicationRecord
**product_id**: integer (PK)
**note**: string
**note**: text
**created_at**: datetime
**updated_at**: datetime
CONTENT
end

test "handles `db/structure.sql` instead of `db/schema.rb`" do
expected_response = {
schema_file: "#{dummy_root}/db/structure.sql",
columns: [],
primary_keys: [],
}

RunnerClient.any_instance.stubs(model: expected_response)

response = hover_on_source(<<~RUBY, { line: 3, character: 0 })
class User < ApplicationRecord
end
User
RUBY

assert_includes(
response.contents.value,
"[Schema](file://#{dummy_root}/db/structure.sql)",
)
end

test "handles neither `db/structure.sql` nor `db/schema.rb` being present" do
expected_response = {
schema_file: nil,
columns: [],
primary_keys: [],
}

RunnerClient.any_instance.stubs(model: expected_response)

response = hover_on_source(<<~RUBY, { line: 3, character: 0 })
class User < ApplicationRecord
end
User
RUBY

refute_match(/Schema/, response.contents.value)
end

test "shows documentation for routes DSLs" do
value = hover_on_source("root 'projects#index'", { line: 0, character: 0 }).contents.value

Expand Down

0 comments on commit 40a8bef

Please sign in to comment.