forked from ohler55/oj
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_test_active.rb
executable file
·76 lines (61 loc) · 1.74 KB
/
_test_active.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#!/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: "[email protected]")
User.find_or_create_by(first_name: "Joan", last_name: "Smith", email: "[email protected]")
# Single instance.
assert_equal(%|{
"id":1,
"first_name":"John",
"last_name":"Smith",
"email":"[email protected]"
}
|, Oj.dump(User.first))
# Array of instances.
assert_equal(%|[
{
"id":1,
"first_name":"John",
"last_name":"Smith",
"email":"[email protected]"
},
{
"id":2,
"first_name":"Joan",
"last_name":"Smith",
"email":"[email protected]"
}
]
|, Oj.dump(User.all))
# Single instance as json. (not Oj)
assert_equal(%|{"id":1,"first_name":"John","last_name":"Smith","email":"[email protected]"}|, User.first.to_json)
# Array of instances as json. (not Oj)
assert_equal(%|[{"id":1,"first_name":"John","last_name":"Smith","email":"[email protected]"},{"id":2,"first_name":"Joan","last_name":"Smith","email":"[email protected]"}]|, User.all.to_json)
end
end