Skip to content

Commit

Permalink
Merge pull request #142 from MITLibraries/tco-113-primo-preprocessor
Browse files Browse the repository at this point in the history
Create SearchLogger model
  • Loading branch information
JPrevost authored Nov 20, 2024
2 parents c342c32 + c492ce0 commit 74bd38b
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 3 deletions.
4 changes: 1 addition & 3 deletions app/graphql/types/query_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,7 @@ def nodes(ids:)
end

def log_search_event(search_term:, source_system:)
term = Term.create_or_find_by!(phrase: search_term)
term.calculate_categorizations
term.search_events.create!(source: source_system)
SearchLogger.logevent(search_term, source_system)
end

def lookup_term(search_term:)
Expand Down
13 changes: 13 additions & 0 deletions app/models/search_logger.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# frozen_string_literal: true

# SearchLogger handles logging of search events including coordination of any preprocessing or normalization
# of data.
class SearchLogger
# Receives a phrase and source and creates a search event. Will find or create a term as needed.
# @return [SearchEvent] the newly created SearchEvent
def self.logevent(phrase, source)
term = Term.create_or_find_by!(phrase:)
term.calculate_categorizations
term.search_events.create!(source:)
end
end
38 changes: 38 additions & 0 deletions test/models/search_logger_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# frozen_string_literal: true

#
require 'test_helper'

class SearchLoggerTest < ActiveSupport::TestCase
test 'a term is created if it does not exist' do
orig_term_count = Term.count
phrase = 'a term is created if it does not exist'
SearchLogger.logevent(phrase, 'search logger test')

assert_operator(Term.count, :>, orig_term_count)
end

test 'a term is not created if it already exists' do
orig_term_count = Term.count
phrase = Term.first.phrase
SearchLogger.logevent(phrase, 'search logger test')

assert_equal(orig_term_count, Term.count)
end

test 'a new search event is created for an existing term' do
orig_searchevent_count = SearchEvent.count
phrase = Term.first.phrase
SearchLogger.logevent(phrase, 'search logger test')

assert_equal(orig_searchevent_count + 1, SearchEvent.count)
end

test 'a new search event is created for a new term' do
orig_searchevent_count = SearchEvent.count
phrase = 'a new search event is created for a new term'
SearchLogger.logevent(phrase, 'search logger test')

assert_equal(orig_searchevent_count + 1, SearchEvent.count)
end
end

0 comments on commit 74bd38b

Please sign in to comment.