Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Json check #52

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 8 additions & 13 deletions lib/arel_extensions/visitors/to_sql.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ class Arel::Visitors::ToSql
def make_json_string expr
Arel.quoted('"') \
+ expr
.coalesce('')
.replace('\\', '\\\\')
.replace('"', '\"')
.replace("\b", '\b')
Expand All @@ -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('
Expand Down Expand Up @@ -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

Expand All @@ -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 += '}'
Expand All @@ -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
Expand Down
15 changes: 10 additions & 5 deletions test/with_ar/all_agnostic_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1080,13 +1080,18 @@ 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)))
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'}),
Expand Down