Commit fc3753fc573366897c4f073c11e1c2b041da4587

Authored by Andrew Kane
1 parent 08a5820c

Moved tests to new files [skip ci]

test/load_test.rb 0 → 100644
... ... @@ -0,0 +1,29 @@
  1 +require_relative "test_helper"
  2 +
  3 +class LoadTest < Minitest::Test
  4 + def test_load_default
  5 + store_names ["Product A"]
  6 + assert_kind_of Product, Product.search("product").first
  7 + end
  8 +
  9 + def test_load_false
  10 + store_names ["Product A"]
  11 + assert_kind_of Hash, Product.search("product", load: false).first
  12 + end
  13 +
  14 + def test_load_false_methods
  15 + store_names ["Product A"]
  16 + assert_equal "Product A", Product.search("product", load: false).first.name
  17 + end
  18 +
  19 + def test_load_false_with_includes
  20 + store_names ["Product A"]
  21 + assert_kind_of Hash, Product.search("product", load: false, includes: [:store]).first
  22 + end
  23 +
  24 + def test_load_false_nested_object
  25 + aisle = {"id" => 1, "name" => "Frozen"}
  26 + store [{name: "Product A", aisle: aisle}]
  27 + assert_equal aisle, Product.search("product", load: false).first.aisle.to_hash
  28 + end
  29 +end
... ...
test/search_options_test.rb
... ... @@ -59,98 +59,6 @@ class SearchOptionsTest &lt; Minitest::Test
59 59 assert_equal 1.0, query.body[:min_score]
60 60 end
61 61  
62   - # load
63   -
64   - def test_load_default
65   - store_names ["Product A"]
66   - assert_kind_of Product, Product.search("product").first
67   - end
68   -
69   - def test_load_false
70   - store_names ["Product A"]
71   - assert_kind_of Hash, Product.search("product", load: false).first
72   - end
73   -
74   - def test_load_false_methods
75   - store_names ["Product A"]
76   - assert_equal "Product A", Product.search("product", load: false).first.name
77   - end
78   -
79   - def test_load_false_with_includes
80   - store_names ["Product A"]
81   - assert_kind_of Hash, Product.search("product", load: false, includes: [:store]).first
82   - end
83   -
84   - def test_load_false_nested_object
85   - aisle = {"id" => 1, "name" => "Frozen"}
86   - store [{name: "Product A", aisle: aisle}]
87   - assert_equal aisle, Product.search("product", load: false).first.aisle.to_hash
88   - end
89   -
90   - # select
91   -
92   - def test_select
93   - store [{name: "Product A", store_id: 1}]
94   - result = Product.search("product", load: false, select: [:name, :store_id]).first
95   - assert_equal %w(id name store_id), result.keys.reject { |k| k.start_with?("_") }.sort
96   - assert_equal "Product A", result.name
97   - assert_equal 1, result.store_id
98   - end
99   -
100   - def test_select_array
101   - store [{name: "Product A", user_ids: [1, 2]}]
102   - result = Product.search("product", load: false, select: [:user_ids]).first
103   - assert_equal [1, 2], result.user_ids
104   - end
105   -
106   - def test_select_single_field
107   - store [{name: "Product A", store_id: 1}]
108   - result = Product.search("product", load: false, select: :name).first
109   - assert_equal %w(id name), result.keys.reject { |k| k.start_with?("_") }.sort
110   - assert_equal "Product A", result.name
111   - assert_nil result.store_id
112   - end
113   -
114   - def test_select_all
115   - store [{name: "Product A", user_ids: [1, 2]}]
116   - hit = Product.search("product", select: true).hits.first
117   - assert_equal hit["_source"]["name"], "Product A"
118   - assert_equal hit["_source"]["user_ids"], [1, 2]
119   - end
120   -
121   - def test_select_none
122   - store [{name: "Product A", user_ids: [1, 2]}]
123   - hit = Product.search("product", select: []).hits.first
124   - assert_nil hit["_source"]
125   - hit = Product.search("product", select: false).hits.first
126   - assert_nil hit["_source"]
127   - end
128   -
129   - def test_select_includes
130   - store [{name: "Product A", user_ids: [1, 2]}]
131   - result = Product.search("product", load: false, select: {includes: [:name]}).first
132   - assert_equal %w(id name), result.keys.reject { |k| k.start_with?("_") }.sort
133   - assert_equal "Product A", result.name
134   - assert_nil result.store_id
135   - end
136   -
137   - def test_select_excludes
138   - store [{name: "Product A", user_ids: [1, 2], store_id: 1}]
139   - result = Product.search("product", load: false, select: {excludes: [:name]}).first
140   - assert_nil result.name
141   - assert_equal [1, 2], result.user_ids
142   - assert_equal 1, result.store_id
143   - end
144   -
145   - def test_select_include_and_excludes
146   - # let's take this to the next level
147   - store [{name: "Product A", user_ids: [1, 2], store_id: 1}]
148   - result = Product.search("product", load: false, select: {includes: [:store_id], excludes: [:name]}).first
149   - assert_equal 1, result.store_id
150   - assert_nil result.name
151   - assert_nil result.user_ids
152   - end
153   -
154 62 # nested
155 63  
156 64 def test_nested_search
... ...
test/select_test.rb 0 → 100644
... ... @@ -0,0 +1,65 @@
  1 +require_relative "test_helper"
  2 +
  3 +class SelectTest < Minitest::Test
  4 + def test_basic
  5 + store [{name: "Product A", store_id: 1}]
  6 + result = Product.search("product", load: false, select: [:name, :store_id]).first
  7 + assert_equal %w(id name store_id), result.keys.reject { |k| k.start_with?("_") }.sort
  8 + assert_equal "Product A", result.name
  9 + assert_equal 1, result.store_id
  10 + end
  11 +
  12 + def test_array
  13 + store [{name: "Product A", user_ids: [1, 2]}]
  14 + result = Product.search("product", load: false, select: [:user_ids]).first
  15 + assert_equal [1, 2], result.user_ids
  16 + end
  17 +
  18 + def test_single_field
  19 + store [{name: "Product A", store_id: 1}]
  20 + result = Product.search("product", load: false, select: :name).first
  21 + assert_equal %w(id name), result.keys.reject { |k| k.start_with?("_") }.sort
  22 + assert_equal "Product A", result.name
  23 + assert_nil result.store_id
  24 + end
  25 +
  26 + def test_all
  27 + store [{name: "Product A", user_ids: [1, 2]}]
  28 + hit = Product.search("product", select: true).hits.first
  29 + assert_equal hit["_source"]["name"], "Product A"
  30 + assert_equal hit["_source"]["user_ids"], [1, 2]
  31 + end
  32 +
  33 + def test_none
  34 + store [{name: "Product A", user_ids: [1, 2]}]
  35 + hit = Product.search("product", select: []).hits.first
  36 + assert_nil hit["_source"]
  37 + hit = Product.search("product", select: false).hits.first
  38 + assert_nil hit["_source"]
  39 + end
  40 +
  41 + def test_includes
  42 + store [{name: "Product A", user_ids: [1, 2]}]
  43 + result = Product.search("product", load: false, select: {includes: [:name]}).first
  44 + assert_equal %w(id name), result.keys.reject { |k| k.start_with?("_") }.sort
  45 + assert_equal "Product A", result.name
  46 + assert_nil result.store_id
  47 + end
  48 +
  49 + def test_excludes
  50 + store [{name: "Product A", user_ids: [1, 2], store_id: 1}]
  51 + result = Product.search("product", load: false, select: {excludes: [:name]}).first
  52 + assert_nil result.name
  53 + assert_equal [1, 2], result.user_ids
  54 + assert_equal 1, result.store_id
  55 + end
  56 +
  57 + def test_include_and_excludes
  58 + # let's take this to the next level
  59 + store [{name: "Product A", user_ids: [1, 2], store_id: 1}]
  60 + result = Product.search("product", load: false, select: {includes: [:store_id], excludes: [:name]}).first
  61 + assert_equal 1, result.store_id
  62 + assert_nil result.name
  63 + assert_nil result.user_ids
  64 + end
  65 +end
... ...