Skip to content

Commit

Permalink
Do not reject FalseClass from tags/values
Browse files Browse the repository at this point in the history
false is blank

Loading production environment (Rails 6.0.3.6)
[1] pry(main)> false.blank?
=> true

Also spec out how Values should behave.
  • Loading branch information
hennevogel committed Apr 13, 2021
1 parent 0287553 commit 383f436
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 2 deletions.
2 changes: 1 addition & 1 deletion lib/influxdb/rails/tags.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ def initialize(config:, tags: {}, additional_tags: InfluxDB::Rails.current.tags)

def to_h
expanded_tags.reject do |_, value|
value.blank?
value.to_s.blank?
end
end

Expand Down
2 changes: 1 addition & 1 deletion lib/influxdb/rails/values.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ def initialize(values: {}, additional_values: InfluxDB::Rails.current.values)

def to_h
expanded_values.reject do |_, value|
value.blank?
value.to_s.blank?
end
end

Expand Down
47 changes: 47 additions & 0 deletions spec/unit/tags.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
require "spec_helper"

RSpec.describe InfluxDB::Rails::Tags do
let(:config) { InfluxDB::Rails::Configuration.new }

describe ".to_h" do
it "returns TrueClass" do
subject = InfluxDB::Rails::Tags.new(config: config, tags: { hans: true })
expect(subject.to_h).to a_hash_including(hans: true)
end

it "returns FalseClass" do
subject = InfluxDB::Rails::Tags.new(config: config, tags: { hans: false })
expect(subject.to_h).to a_hash_including(hans: false)
end

it "returns strings" do
subject = InfluxDB::Rails::Tags.new(config: config, tags: { hans: "franz" })
expect(subject.to_h).to a_hash_including(hans: "franz")
end

it "returns strings containing blank" do
subject = InfluxDB::Rails::Tags.new(config: config, tags: { hans: "franz hans" })
expect(subject.to_h).to a_hash_including(hans: "franz hans")
end

it "removes empty strings" do
subject = InfluxDB::Rails::Tags.new(config: config, tags: { hans: "", franz: " " })
expect(subject.to_h).not_to a_hash_including(hans: "", franz: " ")
end

it "returns symbols" do
subject = InfluxDB::Rails::Tags.new(config: config, tags: { hans: :franz })
expect(subject.to_h).to a_hash_including(hans: :franz)
end

it "removes nil" do
subject = InfluxDB::Rails::Tags.new(config: config, tags: { hans: nil })
expect(subject.to_h).not_to a_hash_including(hans: nil)
end

it "leaves arrays alone" do
subject = InfluxDB::Rails::Tags.new(config: config, tags: { hans: [], franz: %w[a b] })
expect(subject.to_h).to a_hash_including(hans: [], franz: %w[a b])
end
end
end

0 comments on commit 383f436

Please sign in to comment.