Sha256: 17f3563b52351c8d740a921059d8df9d46ac284484f7b077b7d72b065193e272

Contents?: true

Size: 1.21 KB

Versions: 2

Compression:

Stored size: 1.21 KB

Contents

require 'cases/helper_sqlserver'

if ActiveRecord::Base.connection.supports_json?
class JsonTestSQLServer < ActiveRecord::TestCase

  before do
    @o1 = SSTestDatatypeMigrationJson.create! json_col: { 'a' => 'a', 'b' => 'b', 'c' => 'c' }
    @o2 = SSTestDatatypeMigrationJson.create! json_col: { 'a' => nil, 'b' => 'b', 'c' => 'c' }
    @o3 = SSTestDatatypeMigrationJson.create! json_col: { 'x' => 1, 'y' => 2, 'z' => 3 }
    @o4 = SSTestDatatypeMigrationJson.create! json_col: { 'array' => [1, 2, 3] }
    @o5 = SSTestDatatypeMigrationJson.create! json_col: nil
  end

  it 'can return and save JSON data' do
    _(SSTestDatatypeMigrationJson.find(@o1.id).json_col).must_equal({ 'a' => 'a', 'b' => 'b', 'c' => 'c' })
    @o1.json_col = { 'a' => 'a' }
    _(@o1.json_col).must_equal({ 'a' => 'a' })
    @o1.save!
    _(@o1.reload.json_col).must_equal({ 'a' => 'a' })
  end

  it 'can use ISJSON function' do
    _(SSTestDatatypeMigrationJson.where('ISJSON(json_col) > 0').count).must_equal 4
    _(SSTestDatatypeMigrationJson.where('ISJSON(json_col) IS NULL').count).must_equal 1
  end

  it 'can use JSON_VALUE function' do
    _(SSTestDatatypeMigrationJson.where("JSON_VALUE(json_col, '$.b') = 'b'").count).must_equal 2
  end

end
end

Version data entries

2 entries across 2 versions & 2 rubygems

Version Path
activerecord-jdbcsqlserver-adapter-52.0.0 test/cases/json_test_sqlserver.rb
activerecord-sqlserver-adapter-5.2.1 test/cases/json_test_sqlserver.rb