From b85fff2ca82522d6431ae8e2461cb06875a66361 Mon Sep 17 00:00:00 2001 From: Akim Demaille Date: Wed, 19 Jan 2022 18:02:40 +0100 Subject: [PATCH 1/3] test: try to run the json tests --- test/with_ar/all_agnostic_test.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/with_ar/all_agnostic_test.rb b/test/with_ar/all_agnostic_test.rb index 860cf4d9..3dff2381 100644 --- a/test/with_ar/all_agnostic_test.rb +++ b/test/with_ar/all_agnostic_test.rb @@ -1080,7 +1080,7 @@ def test_levenshtein_distance end def test_json - skip "Can't be tested on travis" + skip "Can't be tested on postgresql" if @env_db == 'postgresql' # creation assert_equal 'Arthur', t(@arthur, Arel.json(@name)) assert_equal %w[Arthur Arthur], parse_json(t(@arthur, Arel.json(@name, @name))) From 508973b849968638d9b702b5b75a8740a04cd396 Mon Sep 17 00:00:00 2001 From: Akim Demaille Date: Wed, 19 Jan 2022 18:09:45 +0100 Subject: [PATCH 2/3] Revert "json: fix handling of null" This reverts commit df0bbf480819459f8e0910582a9d3513c8cbbbdd. We do not want to revert this commit. I do so just to check if the test suite does indeed catch the failures. --- lib/arel_extensions/visitors/to_sql.rb | 21 ++++++++------------- 1 file changed, 8 insertions(+), 13 deletions(-) diff --git a/lib/arel_extensions/visitors/to_sql.rb b/lib/arel_extensions/visitors/to_sql.rb index 6c33dd18..5eaee8c8 100644 --- a/lib/arel_extensions/visitors/to_sql.rb +++ b/lib/arel_extensions/visitors/to_sql.rb @@ -8,7 +8,6 @@ class Arel::Visitors::ToSql def make_json_string expr Arel.quoted('"') \ + expr - .coalesce('') .replace('\\', '\\\\') .replace('"', '\"') .replace("\b", '\b') @@ -19,10 +18,6 @@ def make_json_string expr + '"' end - def make_json_null - Arel.quoted('null') - end - # Math Functions def visit_ArelExtensions_Nodes_Abs o, collector collector << 'ABS(' @@ -627,20 +622,20 @@ def visit_Arel_Nodes_Or o, collector def json_value(o, v) case o.type_of_node(v) when :string - Arel.when(v.is_null).then(make_json_null).else(make_json_string(v)) + Arel.when(v.is_null).then(Arel.null).else(make_json_string(v)) when :date s = v.format('%Y-%m-%d') - Arel.when(s.is_null).then(make_json_null).else(make_json_string(s)) + Arel.when(s.is_null).then(Arel.null).else(make_json_string(s)) when :datetime s = v.format('%Y-%m-%dT%H:%M:%S') - Arel.when(s.is_null).then(make_json_null).else(make_json_string(s)) + Arel.when(s.is_null).then(Arel.null).else(make_json_string(s)) when :time s = v.format('%H:%M:%S') - Arel.when(s.is_null).then(make_json_null).else(make_json_string(s)) + Arel.when(s.is_null).then(Arel.null).else(make_json_string(s)) when :nil - make_json_null + Arel.null else - ArelExtensions::Nodes::Cast.new([v, :string]).coalesce(make_json_null) + ArelExtensions::Nodes::Cast.new([v, :string]).coalesce(Arel.null) end end @@ -662,7 +657,7 @@ def visit_ArelExtensions_Nodes_Json o, collector if i != 0 res += ', ' end - res += make_json_string(ArelExtensions::Nodes::Cast.new([k, :string])) + ': ' + res += make_json_string(ArelExtensions::Nodes::Cast.new([k, :string]).coalesce('')) + ': ' res += json_value(o, v) end res += '}' @@ -687,7 +682,7 @@ def visit_ArelExtensions_Nodes_JsonGroup o, collector if i != 0 res = res + ', ' end - kv = make_json_string(ArelExtensions::Nodes::Cast.new([k, :string])) + ': ' + kv = make_json_string(ArelExtensions::Nodes::Cast.new([k, :string]).coalesce('')) + ': ' kv += json_value(o, v) res = res + kv.group_concat(', ', order: Array(orders)).coalesce('') end From 1497ebeddaa273f50496fd98eb69470856de9342 Mon Sep 17 00:00:00 2001 From: Akim Demaille Date: Wed, 19 Jan 2022 18:16:53 +0100 Subject: [PATCH 3/3] wip --- test/with_ar/all_agnostic_test.rb | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/test/with_ar/all_agnostic_test.rb b/test/with_ar/all_agnostic_test.rb index 3dff2381..99cf505c 100644 --- a/test/with_ar/all_agnostic_test.rb +++ b/test/with_ar/all_agnostic_test.rb @@ -1083,10 +1083,15 @@ def test_json skip "Can't be tested on postgresql" if @env_db == 'postgresql' # creation assert_equal 'Arthur', t(@arthur, Arel.json(@name)) - assert_equal %w[Arthur Arthur], parse_json(t(@arthur, Arel.json(@name, @name))) - assert_equal ({'Arthur' => 'Arthur', 'Arthur2' => 'ArthurArthur'}), parse_json(t(@arthur, Arel.json({@name => @name, @name + '2' => @name + @name}))) - assert_equal ({'Arthur' => 'Arthur', 'Arthur2' => 1}), parse_json(t(@arthur, Arel.json({@name => @name, @name + '2' => 1}))) - assert_equal [{'age' => 21}, {'name' => 'Arthur', 'score' => 65.62}], parse_json(t(@arthur, Arel.json([{age: @age}, {name: @name, score: @score}]))) + assert_equal ['Arthur', 'Arthur'], parse_json(t(@arthur, Arel.json(@name, @name))) + assert_equal ['Arthur', nil], parse_json(t(@arthur, Arel.json(@name, nil))) + assert_equal ['comment', 'arrĂȘtĂ©'], parse_json(t(@arthur, Arel.json('comment', @comments))) + assert_equal ['comment', nil], parse_json(t(@lucas, Arel.json('comment', @comments))) + + assert_equal ({'Arthur' => 'Arthur', 'Arthur2' => 'ArthurArthur'}), parse_json(t(@arthur,Arel.json({@name => @name,@name+'2' => @name+@name}))) + assert_equal ({'Arthur' => 'Arthur','Arthur2' => 1}), parse_json(t(@arthur,Arel.json({@name => @name,@name+'2' => 1}))) + assert_equal ({'Arthur' => nil}), parse_json(t(@arthur, Arel.json({@name => nil}))) + assert_equal ([{'age' => 21},{'name' => 'Arthur','score' => 65.62}]), parse_json(t(@arthur,Arel.json([{age: @age},{name: @name,score: @score}]))) # aggregate assert_equal ({'5' => 'Lucas', '15' => 'Sophie', '23' => 'Myung', '25' => 'Laure'}),