From 9f0c2e9fa7034e84aab78830748b22cb2b8fa305 Mon Sep 17 00:00:00 2001 From: dvmonroe Date: Sun, 23 Mar 2025 20:12:09 -0600 Subject: [PATCH 1/3] Add test for serializing variable values --- test/sanity/http/where_test.rb | 71 ++++++++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100644 test/sanity/http/where_test.rb diff --git a/test/sanity/http/where_test.rb b/test/sanity/http/where_test.rb new file mode 100644 index 0000000..5d4f85e --- /dev/null +++ b/test/sanity/http/where_test.rb @@ -0,0 +1,71 @@ +# frozen_string_literal: true + +require "test_helper" + +describe Sanity::Http::Where do + let(:klass) { Sanity::Http::Where } + let(:resource_class) do + Class.new do + def self.where_api_endpoint + "query/production" + end + + def self.project_id + "test-project" + end + + def self.dataset + "production" + end + + def self.api_version + "v1" + end + end + end + + describe "query parameter serialization" do + describe "with GET requests" do + subject { klass.new(resource_klass: resource_class, variables: variables) } + + describe "with different variable types" do + let(:variables) do + { + "string_var" => "hello", + "array_var" => [1, 2, 3], + "hash_var" => {"foo" => "bar"}, + "number_var" => 42, + "boolean_var" => true + } + end + + it "properly serializes variables in the query string" do + uri = subject.send(:uri) + query_params = URI.decode_www_form(uri.query).to_h + + assert_equal "\"hello\"", query_params["$string_var"] + assert_equal "[1,2,3]", query_params["$array_var"] + assert_equal "{\"foo\":\"bar\"}", query_params["$hash_var"] + assert_equal "42", query_params["$number_var"] + assert_equal "true", query_params["$boolean_var"] + end + end + end + + describe "with POST requests" do + subject { klass.new(resource_klass: resource_class, variables: variables, use_post: true) } + + let(:variables) do + { + "string_var" => "hello", + "array_var" => [1, 2, 3] + } + end + + it "includes variables in the request body" do + body = JSON.parse(subject.send(:request_body)) + assert_equal variables, body["params"] + end + end + end +end From bb0312be9f186f1b1a2757d1e3b62a3844a53ec4 Mon Sep 17 00:00:00 2001 From: dvmonroe Date: Sun, 23 Mar 2025 20:23:15 -0600 Subject: [PATCH 2/3] cleanup bad test --- test/sanity/configuration_test.rb | 11 +++++++++++ test/sanity/http/where_test.rb | 14 +------------- 2 files changed, 12 insertions(+), 13 deletions(-) diff --git a/test/sanity/configuration_test.rb b/test/sanity/configuration_test.rb index e6ca17c..6caf840 100644 --- a/test/sanity/configuration_test.rb +++ b/test/sanity/configuration_test.rb @@ -5,6 +5,17 @@ describe Sanity::Configuration do subject { Sanity::Configuration.new } + after do + Sanity.use_global_config = false + Sanity.configure do |config| + config.project_id = "" + config.dataset = "" + config.api_version = "" + config.token = "" + config.use_cdn = false + end + end + it { assert_equal "", subject.project_id } it { assert_equal "", subject.dataset } it { assert_equal "", subject.api_version } diff --git a/test/sanity/http/where_test.rb b/test/sanity/http/where_test.rb index 5d4f85e..8f70471 100644 --- a/test/sanity/http/where_test.rb +++ b/test/sanity/http/where_test.rb @@ -7,19 +7,7 @@ let(:resource_class) do Class.new do def self.where_api_endpoint - "query/production" - end - - def self.project_id - "test-project" - end - - def self.dataset - "production" - end - - def self.api_version - "v1" + "test" end end end From 0ba224f6de99680ae62f8bb0781705cadcbe92a5 Mon Sep 17 00:00:00 2001 From: dvmonroe Date: Sun, 23 Mar 2025 20:46:46 -0600 Subject: [PATCH 3/3] ensure bools and ints correct --- test/sanity/http/where_test.rb | 26 ++++++++++++++------------ 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/test/sanity/http/where_test.rb b/test/sanity/http/where_test.rb index 8f70471..e12aaf9 100644 --- a/test/sanity/http/where_test.rb +++ b/test/sanity/http/where_test.rb @@ -19,23 +19,25 @@ def self.where_api_endpoint describe "with different variable types" do let(:variables) do { - "string_var" => "hello", - "array_var" => [1, 2, 3], - "hash_var" => {"foo" => "bar"}, - "number_var" => 42, - "boolean_var" => true + string_var: "hello", + array_var: [1, 2, 3], + array_of_strings: ["foo", "bar"], + hash_var: {"foo" => "bar"}, + number_var: 42, + boolean_var: true } end it "properly serializes variables in the query string" do uri = subject.send(:uri) - query_params = URI.decode_www_form(uri.query).to_h - - assert_equal "\"hello\"", query_params["$string_var"] - assert_equal "[1,2,3]", query_params["$array_var"] - assert_equal "{\"foo\":\"bar\"}", query_params["$hash_var"] - assert_equal "42", query_params["$number_var"] - assert_equal "true", query_params["$boolean_var"] + decoded_query = URI.decode_www_form_component(uri.query) + + assert_includes decoded_query, "$string_var=\"hello\"" + assert_includes decoded_query, "$array_var=[1,2,3]" + assert_includes decoded_query, "$array_of_strings=[\"foo\",\"bar\"]" + assert_includes decoded_query, "$hash_var={\"foo\":\"bar\"}" + assert_includes decoded_query, "$number_var=42" + assert_includes decoded_query, "$boolean_var=true" end end end