From c492ce07afae75b638659ca84ea9782844e79bf2 Mon Sep 17 00:00:00 2001 From: Jeremy Prevost Date: Tue, 19 Nov 2024 16:14:52 -0500 Subject: [PATCH] Create SearchLogger model Why are these changes being introduced: * This will simplify logic in GraphQL while making it easier to expand the logic necessary to add more complex logic to preprocess terms for each source or do any other normalization to incoming data. Relevant ticket(s): * Pre-work for: https://mitlibraries.atlassian.net/browse/TCO-113 --- app/graphql/types/query_type.rb | 4 +--- app/models/search_logger.rb | 13 +++++++++++ test/models/search_logger_test.rb | 38 +++++++++++++++++++++++++++++++ 3 files changed, 52 insertions(+), 3 deletions(-) create mode 100644 app/models/search_logger.rb create mode 100644 test/models/search_logger_test.rb diff --git a/app/graphql/types/query_type.rb b/app/graphql/types/query_type.rb index 83d400f..8c81a7a 100644 --- a/app/graphql/types/query_type.rb +++ b/app/graphql/types/query_type.rb @@ -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:) diff --git a/app/models/search_logger.rb b/app/models/search_logger.rb new file mode 100644 index 0000000..c6ca4bc --- /dev/null +++ b/app/models/search_logger.rb @@ -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 diff --git a/test/models/search_logger_test.rb b/test/models/search_logger_test.rb new file mode 100644 index 0000000..2b03c03 --- /dev/null +++ b/test/models/search_logger_test.rb @@ -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