Sha256: ad1dff680d9a2e778d155ad4736ea5ad0874069940d494a72c923d83d9c4b618

Contents?: true

Size: 1.74 KB

Versions: 168

Compression:

Stored size: 1.74 KB

Contents

#!/usr/bin/env ruby
# encoding: UTF-8

$: << File.dirname(__FILE__)
%w(lib ext test).each do |dir|
  $LOAD_PATH.unshift File.expand_path("../../#{dir}", __FILE__)
end

require 'minitest'
require 'minitest/autorun'

require 'sqlite3'
require 'active_record'
require 'oj'

#Oj.mimic_JSON()
Oj.default_options = {mode: :compat, indent: 2}

#ActiveRecord::Base.logger = Logger.new(STDERR)

ActiveRecord::Base.establish_connection(
  :adapter => "sqlite3",
  :database => ":memory:"
)

ActiveRecord::Schema.define do
  create_table :users do |table|
    table.column :first_name, :string
    table.column :last_name, :string
    table.column :email, :string
  end
end

class User < ActiveRecord::Base
end

class ActiveTest < Minitest::Test

  def test_active
    User.find_or_create_by(first_name: "John", last_name: "Smith", email: "john@example.com")
    User.find_or_create_by(first_name: "Joan", last_name: "Smith", email: "joan@example.com")

    # Single instance.
    assert_equal(%|{
  "id":1,
  "first_name":"John",
  "last_name":"Smith",
  "email":"john@example.com"
}
|, Oj.dump(User.first))

    # Array of instances.
    assert_equal(%|[
  {
    "id":1,
    "first_name":"John",
    "last_name":"Smith",
    "email":"john@example.com"
  },
  {
    "id":2,
    "first_name":"Joan",
    "last_name":"Smith",
    "email":"joan@example.com"
  }
]
|, Oj.dump(User.all))

    # Single instance as json. (not Oj)
    assert_equal(%|{"id":1,"first_name":"John","last_name":"Smith","email":"john@example.com"}|, User.first.to_json)

    # Array of instances as json. (not Oj)
    assert_equal(%|[{"id":1,"first_name":"John","last_name":"Smith","email":"john@example.com"},{"id":2,"first_name":"Joan","last_name":"Smith","email":"joan@example.com"}]|, User.all.to_json)

  end
end

Version data entries

168 entries across 168 versions & 3 rubygems

Version Path
devcycle-ruby-server-sdk-2.0.0 vendor/bundle/ruby/3.0.0/gems/oj-3.13.2/test/_test_active.rb
oj-3.14.2 test/_test_active.rb
oj-3.14.1 test/_test_active.rb
oj-3.14.0 test/_test_active.rb
oj-3.13.23 test/_test_active.rb
oj-3.13.22 test/_test_active.rb
oj-3.13.21 test/_test_active.rb
oj-3.13.20 test/_test_active.rb
oj-3.13.19 test/_test_active.rb
oj-3.13.18 test/_test_active.rb
oj-3.13.17 test/_test_active.rb
oj-3.13.16 test/_test_active.rb
oj-3.13.15 test/_test_active.rb
oj-3.13.14 test/_test_active.rb
oj-3.13.13 test/_test_active.rb
oj-3.13.12 test/_test_active.rb
oj-3.13.11 test/_test_active.rb
oj-3.13.10 test/_test_active.rb
oj-3.13.9 test/_test_active.rb
oj-3.13.8 test/_test_active.rb