From fc3753fc573366897c4f073c11e1c2b041da4587 Mon Sep 17 00:00:00 2001 From: Andrew Kane Date: Sat, 20 Jun 2020 05:11:58 -0700 Subject: [PATCH] Moved tests to new files [skip ci] --- test/load_test.rb | 29 +++++++++++++++++++++++++++++ test/search_options_test.rb | 92 -------------------------------------------------------------------------------------------- test/select_test.rb | 65 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 94 insertions(+), 92 deletions(-) create mode 100644 test/load_test.rb create mode 100644 test/select_test.rb diff --git a/test/load_test.rb b/test/load_test.rb new file mode 100644 index 0000000..b45bd13 --- /dev/null +++ b/test/load_test.rb @@ -0,0 +1,29 @@ +require_relative "test_helper" + +class LoadTest < Minitest::Test + def test_load_default + store_names ["Product A"] + assert_kind_of Product, Product.search("product").first + end + + def test_load_false + store_names ["Product A"] + assert_kind_of Hash, Product.search("product", load: false).first + end + + def test_load_false_methods + store_names ["Product A"] + assert_equal "Product A", Product.search("product", load: false).first.name + end + + def test_load_false_with_includes + store_names ["Product A"] + assert_kind_of Hash, Product.search("product", load: false, includes: [:store]).first + end + + def test_load_false_nested_object + aisle = {"id" => 1, "name" => "Frozen"} + store [{name: "Product A", aisle: aisle}] + assert_equal aisle, Product.search("product", load: false).first.aisle.to_hash + end +end diff --git a/test/search_options_test.rb b/test/search_options_test.rb index 0f19f81..4398a11 100644 --- a/test/search_options_test.rb +++ b/test/search_options_test.rb @@ -59,98 +59,6 @@ class SearchOptionsTest < Minitest::Test assert_equal 1.0, query.body[:min_score] end - # load - - def test_load_default - store_names ["Product A"] - assert_kind_of Product, Product.search("product").first - end - - def test_load_false - store_names ["Product A"] - assert_kind_of Hash, Product.search("product", load: false).first - end - - def test_load_false_methods - store_names ["Product A"] - assert_equal "Product A", Product.search("product", load: false).first.name - end - - def test_load_false_with_includes - store_names ["Product A"] - assert_kind_of Hash, Product.search("product", load: false, includes: [:store]).first - end - - def test_load_false_nested_object - aisle = {"id" => 1, "name" => "Frozen"} - store [{name: "Product A", aisle: aisle}] - assert_equal aisle, Product.search("product", load: false).first.aisle.to_hash - end - - # select - - def test_select - store [{name: "Product A", store_id: 1}] - result = Product.search("product", load: false, select: [:name, :store_id]).first - assert_equal %w(id name store_id), result.keys.reject { |k| k.start_with?("_") }.sort - assert_equal "Product A", result.name - assert_equal 1, result.store_id - end - - def test_select_array - store [{name: "Product A", user_ids: [1, 2]}] - result = Product.search("product", load: false, select: [:user_ids]).first - assert_equal [1, 2], result.user_ids - end - - def test_select_single_field - store [{name: "Product A", store_id: 1}] - result = Product.search("product", load: false, select: :name).first - assert_equal %w(id name), result.keys.reject { |k| k.start_with?("_") }.sort - assert_equal "Product A", result.name - assert_nil result.store_id - end - - def test_select_all - store [{name: "Product A", user_ids: [1, 2]}] - hit = Product.search("product", select: true).hits.first - assert_equal hit["_source"]["name"], "Product A" - assert_equal hit["_source"]["user_ids"], [1, 2] - end - - def test_select_none - store [{name: "Product A", user_ids: [1, 2]}] - hit = Product.search("product", select: []).hits.first - assert_nil hit["_source"] - hit = Product.search("product", select: false).hits.first - assert_nil hit["_source"] - end - - def test_select_includes - store [{name: "Product A", user_ids: [1, 2]}] - result = Product.search("product", load: false, select: {includes: [:name]}).first - assert_equal %w(id name), result.keys.reject { |k| k.start_with?("_") }.sort - assert_equal "Product A", result.name - assert_nil result.store_id - end - - def test_select_excludes - store [{name: "Product A", user_ids: [1, 2], store_id: 1}] - result = Product.search("product", load: false, select: {excludes: [:name]}).first - assert_nil result.name - assert_equal [1, 2], result.user_ids - assert_equal 1, result.store_id - end - - def test_select_include_and_excludes - # let's take this to the next level - store [{name: "Product A", user_ids: [1, 2], store_id: 1}] - result = Product.search("product", load: false, select: {includes: [:store_id], excludes: [:name]}).first - assert_equal 1, result.store_id - assert_nil result.name - assert_nil result.user_ids - end - # nested def test_nested_search diff --git a/test/select_test.rb b/test/select_test.rb new file mode 100644 index 0000000..5035ac7 --- /dev/null +++ b/test/select_test.rb @@ -0,0 +1,65 @@ +require_relative "test_helper" + +class SelectTest < Minitest::Test + def test_basic + store [{name: "Product A", store_id: 1}] + result = Product.search("product", load: false, select: [:name, :store_id]).first + assert_equal %w(id name store_id), result.keys.reject { |k| k.start_with?("_") }.sort + assert_equal "Product A", result.name + assert_equal 1, result.store_id + end + + def test_array + store [{name: "Product A", user_ids: [1, 2]}] + result = Product.search("product", load: false, select: [:user_ids]).first + assert_equal [1, 2], result.user_ids + end + + def test_single_field + store [{name: "Product A", store_id: 1}] + result = Product.search("product", load: false, select: :name).first + assert_equal %w(id name), result.keys.reject { |k| k.start_with?("_") }.sort + assert_equal "Product A", result.name + assert_nil result.store_id + end + + def test_all + store [{name: "Product A", user_ids: [1, 2]}] + hit = Product.search("product", select: true).hits.first + assert_equal hit["_source"]["name"], "Product A" + assert_equal hit["_source"]["user_ids"], [1, 2] + end + + def test_none + store [{name: "Product A", user_ids: [1, 2]}] + hit = Product.search("product", select: []).hits.first + assert_nil hit["_source"] + hit = Product.search("product", select: false).hits.first + assert_nil hit["_source"] + end + + def test_includes + store [{name: "Product A", user_ids: [1, 2]}] + result = Product.search("product", load: false, select: {includes: [:name]}).first + assert_equal %w(id name), result.keys.reject { |k| k.start_with?("_") }.sort + assert_equal "Product A", result.name + assert_nil result.store_id + end + + def test_excludes + store [{name: "Product A", user_ids: [1, 2], store_id: 1}] + result = Product.search("product", load: false, select: {excludes: [:name]}).first + assert_nil result.name + assert_equal [1, 2], result.user_ids + assert_equal 1, result.store_id + end + + def test_include_and_excludes + # let's take this to the next level + store [{name: "Product A", user_ids: [1, 2], store_id: 1}] + result = Product.search("product", load: false, select: {includes: [:store_id], excludes: [:name]}).first + assert_equal 1, result.store_id + assert_nil result.name + assert_nil result.user_ids + end +end -- libgit2 0.21.0