Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Revise testing approach #319

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 7 additions & 51 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](#{URI::Generic.from_path(path: dummy_root + "/db/schema.rb")})

**id**: integer (PK)

**first_name**: string
**title**: string

**last_name**: 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,7 +100,7 @@ class CompositePrimaryKey < ApplicationRecord

**product_id**: integer (PK)

**note**: string
**note**: text
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(the stubbed response was incorrect!)


**created_at**: datetime

Expand Down Expand Up @@ -238,6 +192,8 @@ class ActiveRecord::Base

def hover_on_source(source, position)
with_server(source) do |server, uri|
sleep(0.1) while RubyLsp::Addon.addons.first.instance_variable_get(:@client).is_a?(NullClient)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same approach as we're using in definition_test, but we should really extract this to provide a better experience for addon developers.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree that we should extract it into a helper. And I also think in that helper we should use addons.find { |addon| addon.name == "Ruby LSP Rails" } instead. I think addons.first will probably break if we someday add another addon to this project's Gemfile as a development dependency?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, we can extract that part out too, e.g. RubyLsp::Addon.get(...).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It does feel like something addons can use during testing 👍

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


server.process_message(
id: 1,
method: "textDocument/hover",
Expand Down
Loading