require "celsius/hash" describe Celsius::Hash do describe ".deep_find_key" do let(:hash) do { query: { bool: [ { query_string: { query: "a" } }, { query_string: { query: "b" } }, { match: { some_field: "some_value" } } ] }, facets: { creator: { terms: { field: "facet_creator" } }, lang: { terms: { field: "facet_lang", } } } } end it "returns values matching the given key" do expected_result = [ hash[:query][:bool][0][:query_string], hash[:query][:bool][1][:query_string], ] expect(described_class.deep_find_key(hash, :query_string)).to eq(expected_result) end context "if given an array of keys" do it "tries to find each element based on the former elements result" do expected_result = [ hash[:facets][:creator] ] expect(described_class.deep_find_key(hash, [:facets, :creator])).to eq(expected_result) end end end describe ".smart_fetch" do let(:hash) do { foo: "bar", muff: false } end it "fetches a value by key indifferently from the given hash" do expect(described_class.smart_fetch(hash, :foo)).to eq(hash[:foo]) expect(described_class.smart_fetch(hash, "foo")).to eq(hash[:foo]) expect(described_class.smart_fetch(hash, "muff")).to eq(hash[:muff]) end end describe ".smart_store" do it "stores a value with the given key" do expect(described_class.smart_store({}, :key, "value")).to eq({key: "value"}) end context "if the key allready exists" do it "converts the value to an array and pushes the value to it" do expect(described_class.smart_store({key: "foo"}, :key, "bar")).to eq({key: ["foo", "bar"]}) end end end end