<commit_before>require 'spec_helper'

describe UserSearch do

  let(:topic)  { Fabricate :topic }
  let(:topic2) { Fabricate :topic }
  let(:topic3) { Fabricate :topic }
  let(:user1)  { Fabricate :user, username: "mrblonde", name: "<NAME>" }
  let(:user2)  { Fabricate :user, username: "mrblue",   name: "<NAME>" }
  let(:user3)  { Fabricate :user, username: "mrorange", name: "<NAME>" }
  let(:user4)  { Fabricate :user, username: "mrpink",   name: "<NAME>" }
  let(:user5)  { Fabricate :user, username: "mrbrown",  name: "<NAME>" }
  let(:user6)  { Fabricate :user, username: "mrwhite",  name: "<NAME>" }

  before do
    Fabricate :post, user: user1, topic: topic
    Fabricate :post, user: user2, topic: topic2
    Fabricate :post, user: user3, topic: topic
    Fabricate :post, user: user4, topic: topic
    Fabricate :post, user: user5, topic: topic3
    Fabricate :post, user: user6, topic: topic
  end

  context "all user search" do
    it "searches the user's name" do
      results = UserSearch.search user1.name.split(" ").first
      results.size.should == 1
      results.first.should == user1
    end

    it "searches the user's name case insensitive" do
      results = UserSearch.search user1.name.split(" ").first.downcase
      results.size.should == 1
      results.first.should == user1
    end

    it "searches the user's username" do
      results = UserSearch.search user4.username
      results.size.should == 1
      results.first.should == user4
    end

    it "searches the user's username case insensitive" do
      results = UserSearch.search user4.username.upcase
      results.size.should == 1
      results.first.should == user4
    end

    it "searches the user's username substring" do
      results = UserSearch.search "mr"
      results.size.should == 6

      results = UserSearch.search "mrb"
      results.size.should == 3
    end

    it "searches the user's username substring upper case" do
      results = UserSearch.search "MR"
      results.size.should == 6

      results = UserSearch.search "MRB"
      results.size.should == 3
    end
  end

  context "sort order respects users with posts on the topic" do
    it "Mr. Blond is first when searching his topic" do
      results = UserSearch.search "mrb", topic.id
      results.first.should == user1
    end

    it "Mr. Blue is first when searching his topic" do
      results = UserSearch.search "mrb", topic2.id
      results.first.should == user2
    end

    it "<NAME> is first when searching his topic" do
      results = UserSearch.search "mrb", topic3.id
      results.first.should == user5
    end
  end

end<commit_msg>before:<commit_after>require 'spec_helper'

describe UserSearch do

  let(:topic)  { Fabricate :topic }
  let(:topic2) { Fabricate :topic }
  let(:topic3) { Fabricate :topic }
  let(:user1)  { Fabricate :user, username: "mrblonde", name: "<NAME>" }
  let(:user2)  { Fabricate :user, username: "mrblue",   name: "<NAME>" }
  let(:user3)  { Fabricate :user, username: "mrorange", name: "<NAME>" }
  let(:user4)  { Fabricate :user, username: "mrpink",   name: "<NAME>" }
  let(:user5)  { Fabricate :user, username: "mrbrown",  name: "<NAME>" }
  let(:user6)  { Fabricate :user, username: "mrwhite",  name: "<NAME>" }

  before do
    Fabricate :post, user: user1, topic: topic
    Fabricate :post, user: user2, topic: topic2
    Fabricate :post, user: user3, topic: topic
    Fabricate :post, user: user4, topic: topic
    Fabricate :post, user: user5, topic: topic3
    Fabricate :post, user: user6, topic: topic
  end

  # this is a seriously expensive integration test, re-creating this entire test db is too expensive
  # reuse
  it "operates correctly" do
    # normal search
    results = UserSearch.search user1.name.split(" ").first
    results.size.should == 1
    results.first.should == user1

    # lower case
    results = UserSearch.search user1.name.split(" ").first.downcase
    results.size.should == 1
    results.first.should == user1

    #  username
    results = UserSearch.search user4.username
    results.size.should == 1
    results.first.should == user4

    # case insensitive
    results = UserSearch.search user4.username.upcase
    results.size.should == 1
    results.first.should == user4

    # substrings
    results = UserSearch.search "mr"
    results.size.should == 6

    results = UserSearch.search "mrb"
    results.size.should == 3


    results = UserSearch.search "MR"
    results.size.should == 6

    results = UserSearch.search "MRB"
    results.size.should == 3

    # topic priority
    results = UserSearch.search "mrb", topic.id
    results.first.should == user1


    results = UserSearch.search "mrb", topic2.id
    results.first.should == user2

    results = UserSearch.search "mrb", topic3.id
    results.first.should == user5
  end

end
{'fraction_non_alphanumeric': 0.12087912087912088, 'fraction_numerical': 0.015343147418619116, 'mean_word_length': 3.0606060606060606, 'pattern_counts': {'":': 0, '<': 16, '<?xml version=': 0, '>': 16, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6063261', 'n_tokens_mistral': 1599, 'n_tokens_neox': 1539, 'n_words': 458}