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

add test cases to sequel gem implementation #412

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ library_gemfile: &library_gemfile
- "./gemfiles/sinatra_22.gemfile"
- "./gemfiles/sinatra_30.gemfile"
- "./gemfiles/sinatra_40.gemfile"
- "./gemfiles/sequel_50.gemfile"
- "./gemfiles/shoryuken_50.gemfile"
- "./gemfiles/shoryuken_60.gemfile"
- "./gemfiles/mongo_216.gemfile"
Expand Down
15 changes: 15 additions & 0 deletions gemfiles/sequel_50.gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# This file was generated by Appraisal

# (c) Copyright IBM Corp. 2024

source "https://rubygems.org"

gem "minitest-reporters"
gem "webmock"
gem "puma"
gem "rack-test"
gem "simplecov", "~> 0.21.2"
gem "sequel", ">= 5.0"
gem "sqlite3", "~> 1.4"

gemspec path: "../"
2 changes: 1 addition & 1 deletion lib/instana/tracing/span.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class Span
:memcache, :'net-http', :rack, :render, :'rpc-client',
:'rpc-server', :'sidekiq-client', :'sidekiq-worker',
:redis, :'resque-client', :'resque-worker', :'graphql.server', :dynamodb, :s3, :sns, :sqs, :'aws.lambda.entry', :activejob, :log, :"mail.actionmailer",
:"aws.lambda.invoke", :mongo ].freeze
:"aws.lambda.invoke", :mongo ].freeze
ENTRY_SPANS = [ :rack, :'resque-worker', :'rpc-server', :'sidekiq-worker', :'graphql.server', :sqs,
:'aws.lambda.entry' ].freeze
EXIT_SPANS = [ :activerecord, :excon, :'net-http', :'resque-client',
Expand Down
105 changes: 105 additions & 0 deletions test/instrumentation/sequel_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
# (c) Copyright IBM Corp. 2024

require 'test_helper'
require 'sequel'

class SequelTest < Minitest::Test
def setup
skip unless ENV['DATABASE_URL']
db_url = ENV['DATABASE_URL'].sub("sqlite3", "sqlite")
@db = Sequel.connect(db_url)

@db.create_table!(:blocks) do
String :name
String :color
end
@model = @db[:blocks]
end

def teardown
@db.drop_table(:blocks)
@db.disconnect
arjun-rajappa marked this conversation as resolved.
Show resolved Hide resolved
end

def test_config_defaults
assert ::Instana.config[:sanitize_sql] == true
assert ::Instana.config[:sequel].is_a?(Hash)
assert ::Instana.config[:sequel].key?(:enabled)
assert_equal true, ::Instana.config[:sequel][:enabled]
end

def test_create
Instana::Tracer.start_or_continue_trace(:sequel_test, {}) do
@model.insert(name: 'core', color: 'blue')
end
spans = ::Instana.processor.queued_spans
assert_equal 2, spans.length
span = find_first_span_by_name(spans, :sequel)
data = span[:data][:sequel]
assert data[:sql].start_with?('INSERT INTO')
Copy link
Contributor

Choose a reason for hiding this comment

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

This should also be checked: assert_equal 0, span[:ec]
Furthermore we should also ensure that the insert was successful.

end

def test_read
@model.insert(name: 'core', color: 'blue')
Instana::Tracer.start_or_continue_trace(:sequel_test, {}) do
@model.where(name: 'core').first
end
spans = ::Instana.processor.queued_spans
assert_equal 2, spans.length
span = find_first_span_by_name(spans, :sequel)
data = span[:data][:sequel]
assert data[:sql].start_with?('SELECT')
Copy link
Contributor

Choose a reason for hiding this comment

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

This should also be checked: assert_equal 0, span[:ec].
Furthermore we should ensure that the find operation was successful and has returned what it was supposed to return.

assert_nil span[:ec]
end

def test_update
@model.insert(name: 'core', color: 'blue')
Instana::Tracer.start_or_continue_trace(:sequel_test, {}) do
@model.where(name: 'core').update(color: 'red')
end
spans = ::Instana.processor.queued_spans
assert_equal 2, spans.length
span = find_first_span_by_name(spans, :sequel)
data = span[:data][:sequel]
assert data[:sql].start_with?('UPDATE')
Copy link
Contributor

Choose a reason for hiding this comment

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

assert_equal 0, span[:ec] Was the update even successful?

assert_nil span[:ec]
end

def test_delete
@model.insert(name: 'core', color: 'blue')
Instana::Tracer.start_or_continue_trace(:sequel_test, {}) do
@model.where(name: 'core').delete
end
spans = ::Instana.processor.queued_spans
assert_equal 2, spans.length
span = find_first_span_by_name(spans, :sequel)
data = span[:data][:sequel]
assert data[:sql].start_with?('DELETE')
Copy link
Contributor

Choose a reason for hiding this comment

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

assert_equal 0, span[:ec]. Was the delete op successful?

assert_nil span[:ec]
end

def test_raw
Instana::Tracer.start_or_continue_trace(:sequel_test, {}) do
@db.run('SELECT 1')
end
spans = ::Instana.processor.queued_spans
assert_equal 2, spans.length
span = find_first_span_by_name(spans, :sequel)
data = span[:data][:sequel]
assert 'SELECT 1', data[:sql]
Copy link
Contributor

Choose a reason for hiding this comment

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

assert_equal 0, span[:ec]. Was the op successful?

assert_nil span[:ec]
end

def test_raw_error
assert_raises Sequel::DatabaseError do
Instana::Tracer.start_or_continue_trace(:sequel_test, {}) do
@db.run('INVALID')
end
end
spans = ::Instana.processor.queued_spans
assert_equal 2, spans.length
span = find_first_span_by_name(spans, :sequel)

Copy link
Contributor

Choose a reason for hiding this comment

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

We should check that the execution has indeed failed.

assert_equal 1, span[:ec]
end
end
Loading