From 383f436676eca820bb9ff0708199889ea4b37ee6 Mon Sep 17 00:00:00 2001 From: Henne Vogelsang Date: Tue, 13 Apr 2021 13:20:14 +0200 Subject: [PATCH] Do not reject FalseClass from tags/values false is blank Loading production environment (Rails 6.0.3.6) [1] pry(main)> false.blank? => true Also spec out how Values should behave. --- lib/influxdb/rails/tags.rb | 2 +- lib/influxdb/rails/values.rb | 2 +- spec/unit/tags.rb | 47 ++++++++++++++++++++++++++++++++++++ 3 files changed, 49 insertions(+), 2 deletions(-) create mode 100644 spec/unit/tags.rb 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