-
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 #107 from MITLibraries/tco-33
Implement Detections model
- Loading branch information
Showing
18 changed files
with
386 additions
and
11 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
DETECTOR_VERSION=1 | ||
LINKRESOLVER_BASEURL=https://mit.primo.exlibrisgroup.com/discovery/openurl?institution=01MIT_INST&rfr_id=info:sid/mit.tacos.api&vid=01MIT_INST:MIT | ||
UNPAYWALL_EMAIL=timdex@mit.edu |
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
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,42 @@ | ||
# frozen_string_literal: true | ||
|
||
# A Detection is a joining record between a Term and a Detector, created when a Detector activates based on some aspect | ||
# of the Term. This is the signal that TACOS found something about this Term. | ||
# | ||
# There is a uniqueness constraint on the combination of term_id, detector_id, and detector_version. | ||
# | ||
# New records can be created by passing a Term and a Detector object. The model will look up the current detector | ||
# version, and include that in the record. | ||
# | ||
# == Schema Information | ||
# | ||
# Table name: detections | ||
# | ||
# id :integer not null, primary key | ||
# term_id :integer not null | ||
# detector_id :integer not null | ||
# detector_version :string | ||
# created_at :datetime not null | ||
# updated_at :datetime not null | ||
# | ||
class Detection < ApplicationRecord | ||
belongs_to :term | ||
belongs_to :detector | ||
|
||
# We use the before_create hook to prevent needing to override the initialize method, which Rails frowns upon. | ||
before_create :set_defaults | ||
|
||
# These scopes allow for easy filtering of Detection records by a single parameter. | ||
scope :current, -> { where(detector_version: ENV.fetch('DETECTOR_VERSION', 'unset')) } | ||
scope :for_detector, ->(detector) { where(detector_id: detector.id) } | ||
scope :for_term, ->(term) { where(term_id: term.id) } | ||
|
||
private | ||
|
||
# This looks up the current Detector Version from the environment, storing the value as part of the record which is | ||
# about to be saved. This prevents the rest of the application from having to worry about this value, while also | ||
# providing a mechanism to prevent duplicate records from being created. | ||
def set_defaults | ||
self.detector_version = ENV.fetch('DETECTOR_VERSION', 'unset') | ||
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
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
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
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 @@ | ||
class CreateDetections < ActiveRecord::Migration[7.1] | ||
def change | ||
create_table :detections do |t| | ||
t.belongs_to :term, null: false, foreign_key: true | ||
t.belongs_to :detector, null: false, foreign_key: true | ||
t.string :detector_version | ||
|
||
t.timestamps | ||
end | ||
add_index :detections, [:term_id, :detector_id, :detector_version], unique: true | ||
add_index :detections, [:detector_id, :term_id, :detector_version], unique: true | ||
end | ||
end |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,25 @@ | ||
# == Schema Information | ||
# | ||
# Table name: detections | ||
# | ||
# id :integer not null, primary key | ||
# term_id :integer not null | ||
# detector_id :integer not null | ||
# detector_version :string | ||
# created_at :datetime not null | ||
# updated_at :datetime not null | ||
# | ||
one: | ||
term: doi | ||
detector: doi | ||
detector_version: 1 | ||
|
||
two: | ||
term: multiple_detections | ||
detector: doi | ||
detector_version: 1 | ||
|
||
three: | ||
term: multiple_detections | ||
detector: pmid | ||
detector_version: 1 |
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,93 @@ | ||
# frozen_string_literal: true | ||
|
||
# == Schema Information | ||
# | ||
# Table name: detections | ||
# | ||
# id :integer not null, primary key | ||
# term_id :integer not null | ||
# detector_id :integer not null | ||
# detector_version :string | ||
# created_at :datetime not null | ||
# updated_at :datetime not null | ||
# | ||
require 'test_helper' | ||
|
||
class DetectionTest < ActiveSupport::TestCase | ||
test 'duplicate detections are not allowed' do | ||
initial_count = Detection.count | ||
|
||
sample = { | ||
term: terms('hi'), | ||
detector: detectors('doi') | ||
} | ||
|
||
Detection.create!(sample) | ||
|
||
post_count = Detection.count | ||
|
||
assert_equal(initial_count + 1, post_count) | ||
|
||
assert_raises(ActiveRecord::RecordNotUnique) do | ||
Detection.create!(sample) | ||
end | ||
|
||
post_duplicate_count = Detection.count | ||
|
||
assert_equal(post_count, post_duplicate_count) | ||
end | ||
|
||
test 'new detections are allowed when detector_version is updated' do | ||
initial_count = Detection.count | ||
|
||
sample = Detection.first | ||
|
||
new_sample = { | ||
term: sample.term, | ||
detector: sample.detector | ||
} | ||
|
||
# A purely duplicate record fails to save... | ||
assert_raises(ActiveRecord::RecordNotUnique) do | ||
Detection.create!(new_sample) | ||
end | ||
|
||
# ...but when we update the DETECTOR_VERSION env, now the same record does save. | ||
new_version = 'updated' | ||
|
||
assert_not_equal(ENV.fetch('DETECTOR_VERSION'), new_version) | ||
|
||
ClimateControl.modify DETECTOR_VERSION: new_version do | ||
Detection.create!(new_sample) | ||
|
||
assert_equal(initial_count + 1, Detection.count) | ||
end | ||
end | ||
|
||
test 'detections are assigned the current DETECTOR_VERSION value from env' do | ||
new_detection = { | ||
term: terms('hi'), | ||
detector: detectors('pmid') | ||
} | ||
|
||
Detection.create!(new_detection) | ||
|
||
confirmation = Detection.last | ||
|
||
assert_equal(confirmation.detector_version, ENV.fetch('DETECTOR_VERSION')) | ||
end | ||
|
||
test 'detector current scope filters on current env value' do | ||
count = Detection.current.count | ||
|
||
new_version = 'updated' | ||
|
||
assert_not_equal(ENV.fetch('DETECTOR_VERSION'), new_version) | ||
|
||
ClimateControl.modify DETECTOR_VERSION: new_version do | ||
updated_count = Detection.current.count | ||
|
||
assert_not_equal(count, updated_count) | ||
end | ||
end | ||
end |
Oops, something went wrong.