<commit_before>-module(search_resource_test).

-include_lib("eunit/include/eunit.hrl").
-include("../src/chef_rest_search_resource.hrl").

make_state() ->
    #state{start_time = now(),
           resource = "chef_rest_search_resource",
           batch_size = 5,
           estatsd_server = "127.0.0.1",
           estatsd_port = 3365,
           hostname = net_adm:localhost(),
           organization_name = "mock-org",
           request_type = "search.get"}.
%% /FIXME

malformed_request_test_() ->
    {setup,
     fun() ->
             application:start(crypto),
             application:set_env(chef_rest, reqid_header_name, "X-Request-Id")
     end,
     fun(_) ->
             stopping
     end,
     malformed_request_tests()}.

malformed_request_tests() ->
    {foreach,
     fun() ->
             meck:new(wrq),
             meck:new(fast_log),
             meck:expect(fast_log, info, fun(_, _, _) -> ok end),
             meck:expect(fast_log, info, fun(_, _, _, _) -> ok end),
             application:set_env(chef_rest, reqid_header_name, "X-Request-Id")
     end,
     fun(_) ->
             meck:unload()
     end,
    [
     {"properly formed",
      fun() ->
              SignedHeadersBin = get_signed_headers(),
              meck:expect(wrq, get_req_header,
                          fun(HName, req_mock) -> proplists:get_value(HName, SignedHeadersBin) end),
              meck:expect(wrq, path_info, fun(object_type, req_mock) ->
                                                  "node";
                                             (organization_id, req_mock) ->
                                                  "testorg"
                                          end),
              meck:expect(wrq, get_qs_value, fun("q", req_mock) ->
                                                     "myquery";
                                                ("start", req_mock) ->
                                                     "0";
                                                ("rows", req_mock) ->
                                                     "20";
                                                ("sort", req_mock) ->
                                                     "sort should be ignored."
                                             end),
              {IsMalformed, _Req1, State} =
                  chef_rest_search_resource:malformed_request(req_mock, make_state()),
              ?assertEqual(false, IsMalformed),
              ?assertEqual("testorg", State#state.organization_name),
              SolrQuery = State#state.solr_query,
              %% FIXME: this is a record defined in chef_solr
              ?assertEqual({chef_solr_query, "myquery", "+X_CHEF_type_CHEF_X:node",
                            0, 20, "X_CHEF_id_CHEF_X asc"}, SolrQuery),
              ?assert(meck:validate(wrq))
      end},

     {"q param defaults to '*:*'",
      fun() ->
              SignedHeadersBin = get_signed_headers(),
              meck:expect(wrq, get_req_header,
                          fun(HName, req_mock) -> proplists:get_value(HName, SignedHeadersBin) end),
              meck:expect(wrq, path_info, fun(object_type, req_mock) ->
                                                  "node";
                                             (organization_id, req_mock) ->
                                                  "testorg"
                                          end),
              meck:expect(wrq, get_qs_value, fun("q", req_mock) ->
                                                     undefined;
                                                ("start", req_mock) ->
                                                     "0";
                                                ("rows", req_mock) ->
                                                     "20";
                                                ("sort", req_mock) ->
                                                     "X_CHEF_id_CHEF_X+asc"
                                             end),
              {IsMalformed, _Req1, State} =
                  chef_rest_search_resource:malformed_request(req_mock, make_state()),
              ?assertEqual(false, IsMalformed),
              %% FIXME: this is a record defined in chef_solr
              SolrQuery = State#state.solr_query,
              ?assertEqual({chef_solr_query, "*:*", "+X_CHEF_type_CHEF_X:node",
                            0, 20, "X_CHEF_id_CHEF_X asc"}, SolrQuery),<commit_msg>Fix chef_rest tests to use chef_solr.hrl<commit_after>-module(search_resource_test).

-include_lib("eunit/include/eunit.hrl").
-include("../src/chef_rest_search_resource.hrl").
-include("../../chef_common/src/chef_solr.hrl").

make_state() ->
    #state{start_time = now(),
           resource = "chef_rest_search_resource",
           batch_size = 5,
           estatsd_server = "127.0.0.1",
           estatsd_port = 3365,
           hostname = net_adm:localhost(),
           organization_name = "mock-org",
           request_type = "search.get"}.
%% /FIXME

malformed_request_test_() ->
    {setup,
     fun() ->
             application:start(crypto),
             application:set_env(chef_rest, reqid_header_name, "X-Request-Id")
     end,
     fun(_) ->
             stopping
     end,
     malformed_request_tests()}.

malformed_request_tests() ->
    {foreach,
     fun() ->
             meck:new(wrq),
             meck:new(fast_log),
             meck:expect(fast_log, info, fun(_, _, _) -> ok end),
             meck:expect(fast_log, info, fun(_, _, _, _) -> ok end),
             application:set_env(chef_rest, reqid_header_name, "X-Request-Id")
     end,
     fun(_) ->
             meck:unload()
     end,
    [
     {"properly formed",
      fun() ->
              SignedHeadersBin = get_signed_headers(),
              meck:expect(wrq, get_req_header,
                          fun(HName, req_mock) -> proplists:get_value(HName, SignedHeadersBin) end),
              meck:expect(wrq, path_info, fun(object_type, req_mock) ->
                                                  "node";
                                             (organization_id, req_mock) ->
                                                  "testorg"
                                          end),
              meck:expect(wrq, get_qs_value, fun("q", req_mock) ->
                                                     "myquery";
                                                ("start", req_mock) ->
                                                     "0";
                                                ("rows", req_mock) ->
                                                     "20";
                                                ("sort", req_mock) ->
                                                     "sort should be ignored."
                                             end),
              {IsMalformed, _Req1, State} =
                  chef_rest_search_resource:malformed_request(req_mock, make_state()),
              ?assertEqual(false, IsMalformed),
              ?assertEqual("testorg", State#state.organization_name),
              SolrQuery = State#state.solr_query,
              %% FIXME: this is a record defined in chef_solr
              ?assertEqual(#chef_solr_query{
                              query_string = "myquery",
                              filter_query = "+X_CHEF_type_CHEF_X:node",
                              start = 0, rows = 20,
                              sort = "X_CHEF_id_CHEF_X asc",
                              index = node},
                           SolrQuery),
              ?assert(meck:validate(wrq))
      end},

     {"q param defaults to '*:*'",
      fun() ->
              SignedHeadersBin = get_signed_headers(),
              meck:expect(wrq, get_req_header,
                          fun(HName, req_mock) -> proplists:get_value(HName, SignedHeadersBin) end),
              meck:expect(wrq, path_info, fun(object_type, req_mock) ->
                                                  "node";
                                             (organization_id, req_mock) ->
                                                  "testorg"
                                          end),
              meck:expect(wrq, get_qs_value, fun("q", req_mock) ->
                                                     undefined;
                                                ("start", req_mock) ->
                                                     "0";
                                                ("rows", req_mock) ->
                                                     "20";
                                                ("sort", req_mock) ->
                                                     "X_CHEF_id_CHEF_X+asc"
                                             end),
              {IsMalformed, _Req1, State} =
                  chef_rest_search_resource:malformed_request(req_mock, make_state()),
              ?assertEqual(false, IsMalformed),
              %% FIXME: this is a record defined in chef_solr
              SolrQuery = State#state.solr_query,
              ?assertEqual(#chef_solr_query{
                              query_string = "*:*",
                              filter_query = "+X_CHEF_type_CHEF_X:node",
                              start = 0, rows = 20,
                              sort = "X_CHEF_id_CHEF_X asc",
                              index = node}, SolrQuery),
{'fraction_non_alphanumeric': 0.10202997130407057, 'fraction_numerical': 0.005314061005420342, 'mean_word_length': 1.0149892933618843, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 53, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 98, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '403564', 'n_tokens_mistral': 2340, 'n_tokens_neox': 2281, 'n_words': 389}