-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #142 from MITLibraries/tco-113-primo-preprocessor
Create SearchLogger model
- Loading branch information
Showing
3 changed files
with
52 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |