diff --git a/lib/influxdb/rails/tags.rb b/lib/influxdb/rails/tags.rb index 281a6bb..4f20f12 100644 --- a/lib/influxdb/rails/tags.rb +++ b/lib/influxdb/rails/tags.rb @@ -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 diff --git a/lib/influxdb/rails/values.rb b/lib/influxdb/rails/values.rb index c2e4ac6..fe91c02 100644 --- a/lib/influxdb/rails/values.rb +++ b/lib/influxdb/rails/values.rb @@ -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 diff --git a/spec/unit/tags.rb b/spec/unit/tags.rb new file mode 100644 index 0000000..5115b4d --- /dev/null +++ b/spec/unit/tags.rb @@ -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