From 00615b2b6b7a63a8ddec16778881aeba327221a8 Mon Sep 17 00:00:00 2001 From: Ahmed Ismail Date: Tue, 7 Nov 2023 23:40:00 +0500 Subject: [PATCH 1/2] Fix search by company name and search by industry name --- lib/companies/helper.ex | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/lib/companies/helper.ex b/lib/companies/helper.ex index b7bbbaf4..daf1a948 100644 --- a/lib/companies/helper.ex +++ b/lib/companies/helper.ex @@ -5,8 +5,18 @@ defmodule Companies.Helpers do @default_page_size "16" - def searched_list(list, _params) do - list + def searched_list(list, params) do + search_param = params["search"]["text"] + + if search_param do + list + |> Enum.filter( + &(String.contains?(String.downcase(&1.name), String.downcase(search_param)) or + String.contains?(String.downcase(&1.industry), String.downcase(search_param))) + ) + else + list + end end def sorted_list(list, params) do From 69626b3e7858107a4f9f27794a242f2c4f52d030 Mon Sep 17 00:00:00 2001 From: Ahmed Ismail Date: Thu, 21 Dec 2023 21:09:53 +0500 Subject: [PATCH 2/2] Add testsuite for searched_list/2 action of helper.ex --- test/companies/helper_test.exs | 77 ++++++++++++++++++++++++++++++++++ 1 file changed, 77 insertions(+) create mode 100644 test/companies/helper_test.exs diff --git a/test/companies/helper_test.exs b/test/companies/helper_test.exs new file mode 100644 index 00000000..528ecf09 --- /dev/null +++ b/test/companies/helper_test.exs @@ -0,0 +1,77 @@ +defmodule Companies.HelperTest do + use ExUnit.Case + + alias Companies.Helpers + + describe "searched_list/2" do + test "returns the original list if search_param is not provided" do + list = [ + %{name: "Company A", industry: "Tech"}, + %{name: "Company B", industry: "Health"} + ] + + params = %{"search" => %{"text" => nil}} + + assert Helpers.searched_list(list, params) == list + end + + test "filters the list based on search_param" do + list = [ + %{name: "Company A", industry: "Tech"}, + %{name: "Company B", industry: "Health"} + ] + + params = %{"search" => %{"text" => "tech"}} + + result = Helpers.searched_list(list, params) + + assert length(result) == 1 + assert Enum.at(result, 0) == %{name: "Company A", industry: "Tech"} + end + + test "handles case-insensitive search" do + list = [ + %{name: "Company A", industry: "Tech"}, + %{name: "Company B", industry: "Health"} + ] + + params = %{"search" => %{"text" => "COMPANY"}} + + result = Helpers.searched_list(list, params) + + assert length(result) == 2 + assert Enum.at(result, 0) == %{name: "Company A", industry: "Tech"} + assert Enum.at(result, 1) == %{name: "Company B", industry: "Health"} + end + + test "handles an empty list" do + list = [] + + params = %{"search" => %{"text" => "tech"}} + + assert Helpers.searched_list(list, params) == [] + end + + test "handles an empty search_param" do + list = [ + %{name: "Company A", industry: "Tech"}, + %{name: "Company B", industry: "Health"} + ] + + params = %{"search" => %{"text" => ""}} + + assert Helpers.searched_list(list, params) == list + end + + test "handles a non-matching search_param" do + list = [ + %{name: "Company A", industry: "Tech"}, + %{name: "Company B", industry: "Health"} + ] + + params = %{"search" => %{"text" => "Finance"}} + + assert Helpers.searched_list(list, params) == [] + end + end +end