-
-
Notifications
You must be signed in to change notification settings - Fork 14
/
deep_pluck_test.rb
44 lines (37 loc) · 1.56 KB
/
deep_pluck_test.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
require 'test_helper'
class DeepPluckTest < Minitest::Test
def setup
end
def test_that_it_has_a_version_number
refute_nil ::DeepPluck::VERSION
end
def test_1_level_deep
assert_equal [
{'name' => 'John'},
{'name' => 'Pearl'},
], User.where(:name => %w(John Pearl)).deep_pluck(:name)
end
def test_2_level_deep
assert_equal [
{'name' => 'Pearl' , :posts => [{'name' => "post4"}, {'name' => "post5"}]},
{'name' => 'Kathenrie', :posts => [{'name' => "post6"}]},
], User.where(:name => %w(Pearl Kathenrie)).deep_pluck(:name, :posts => [:name])
assert_equal [
{'name' => 'John' , :contact => {'address' => "John's Home"}},
{'name' => 'Pearl', :contact => {'address' => "Pearl's Home"}},
], User.where(:name => %w(John Pearl)).deep_pluck(:name, :contact => :address)
end
def test_two_associations
assert_equal [
{'name' => 'Pearl' , :posts => [{'name' => "post4"}, {'name' => "post5"}], :contact => {'address' => "Pearl's Home"}},
{'name' => 'Kathenrie', :posts => [{'name' => "post6"}], :contact => {'address' => "Kathenrie's Home"}},
], User.where(:name => %w(Pearl Kathenrie)).deep_pluck(:name, :contact => :address, :posts => :name)
end
def test_2_level_deep_and_reverse_association
assert_equal [
{'name' => 'post4', :user => {'name' => "Pearl"}},
{'name' => 'post5', :user => {'name' => "Pearl"}},
{'name' => 'post6', :user => {'name' => "Kathenrie"}},
], Post.where(:name => %w(post4 post5 post6)).deep_pluck(:name, :user => [:name])
end
end