Skip to content

Commit

Permalink
Remove Fingerprint calculation from migration
Browse files Browse the repository at this point in the history
This comments out the initial calculation of Fingerprint records from
the migrations in this PR. A following commit will add a rake task that
will focus on this work.

A side effect of this work is that we need to change the fixtures used
for our test suite, to match the state the migrations will produce. This
in turn causes some tests to change, because some tests will require
additional setup work.

More complex tests now have code comments to indicate this sort of
sequencing.
  • Loading branch information
matt-bernhardt committed Dec 12, 2024
1 parent 0930853 commit 76da216
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 41 deletions.
6 changes: 3 additions & 3 deletions db/migrate/20241211195504_add_fingerprint_to_terms.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ def up
add_reference :terms, :fingerprint, foreign_key: true

# Seed the relationship between Terms and Fingerprints
Term.all.each do |t|
t.save
end
# Term.all.each do |t|
# t.save
# end
end

def down
Expand Down
12 changes: 0 additions & 12 deletions test/fixtures/terms.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,48 +12,36 @@

cool:
phrase: Super cool search
fingerprint: cool

cool_cluster:
phrase: Super. Cool. Search.
fingerprint: cool

hi:
phrase: hello world
fingerprint: hi

pmid_38908367:
phrase: 'TERT activation targets DNA methylation and multiple aging hallmarks. Shim HS, et al. Cell. 2024. PMID: 38908367'
fingerprint: pmid_38908367

lcsh:
phrase: 'Geology -- Massachusetts'
fingerprint: lcsh

issn_1075_8623:
phrase: 1075-8623
fingerprint: issn_1075_8623

doi:
phrase: '10.1016/j.physio.2010.12.004'
fingerprint: doi

isbn_9781319145446:
phrase: 'Sadava, D. E., D. M. Hillis, et al. Life The Science of Biology. 11th ed. W. H. Freeman, 2016. ISBN: 9781319145446'
fingerprint: isbn_9781319145446

journal_nature_medicine:
phrase: 'nature medicine'
fingerprint: journal_nature_medicine

suggested_resource_jstor:
phrase: 'jstor'
fingerprint: suggested_resource_jstor

multiple_detections:
phrase: 'Environmental and Health Impacts of Air Pollution: A Review. Frontiers in Public Health. PMID: 32154200. DOI: 10.3389/fpubh.2020.00014'
fingerprint: multiple_detections

citation:
phrase: "A. Altun, "Understanding hypertext in the context of reading on the web: Language learners' experience," Current Issues in Education, vol. 6, no. 12, July, 2005. [Online serial]. Available: http://cie.ed.asu.edu/volume6/number12/. [Accessed Dec. 2, 2007]."
fingerprint: citation
12 changes: 9 additions & 3 deletions test/models/fingerprint_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,23 @@ class FingerprintTest < ActiveSupport::TestCase
end

test 'deleting a Fingerprint does not delete its Term, which is still valid' do
# Setup
target_term = Term.last
target_term.save
target_term.reload
target = target_term.fingerprint

# Initial condition
term_count = Term.count
fingerprint_count = Fingerprint.count

target = Fingerprint.last
target_term = target.terms.last

assert_operator 0, :<, target.terms.count

# Change
target.destroy
target_term.reload

# Verify impact
assert_equal term_count, Term.count
assert_equal fingerprint_count - 1, Fingerprint.count
assert_nil target_term.fingerprint_value
Expand Down
76 changes: 53 additions & 23 deletions test/models/term_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,12 @@ class TermTest < ActiveSupport::TestCase
end

test 'destroying a Term will delete its Fingerprint if no cluster exists' do
term = terms('hi')
term.save
term.reload

term_pre_count = Term.count
fingerprints_pre_count = Fingerprint.count
term = terms('hi')

assert_equal 1, term.fingerprint.terms.count

Expand All @@ -110,16 +113,25 @@ class TermTest < ActiveSupport::TestCase
end

test 'destroying a Term will not delete its Fingerprint if other terms exist in that cluster' do
# Setup
term = terms('cool')
term_cluster = terms('cool_cluster')
term.save
term_cluster.save
term.reload

# Initial conditions
term_pre_count = Term.count
fingerprints_pre_count = Fingerprint.count
term = terms('cool')
fingerprint = term.fingerprint
cluster_size = fingerprint.terms.count

assert_operator 1, :<, cluster_size

# Change
term.destroy

# Verify impact
assert_equal term_pre_count - 1, Term.count
assert_equal fingerprints_pre_count, Fingerprint.count
assert_equal cluster_size - 1, fingerprint.terms.count
Expand All @@ -128,32 +140,23 @@ class TermTest < ActiveSupport::TestCase
# This test, and maybe the actual dynamic, may need to be refactored. For right now I'm confirming this behavior via
# the test here.
test 'a Term without a Fingerprint is valid (but regenerates on next save)' do
term_count = Term.count
fingerprint_count = Fingerprint.count

target_fingerprint = fingerprints('hi')
target_term = target_fingerprint.terms.last

assert_operator 0, :<, target_fingerprint.terms.count

target_fingerprint.destroy
target_term.reload
target_term = terms('hi')

assert_nil target_term.fingerprint
assert_predicate target_term, :valid?
assert_equal fingerprint_count - 1, Fingerprint.count

target_term.save
target_term.reload

assert_equal term_count, Term.count
assert_equal fingerprint_count, Fingerprint.count
assert_not_nil target_term.fingerprint
assert_predicate target_term, :valid?
end

test 'deleting a Term without a Fingerprint succeeds without problem' do
term_count = Term.count

target = fingerprints('hi')
target_term = target.terms.last
target_term = terms('hi')

target.destroy
target_term.reload
Expand Down Expand Up @@ -355,15 +358,27 @@ class TermTest < ActiveSupport::TestCase

test 'Term.fingerprint_value is a delegate of the Fingerprint method' do
t = terms('cool')
t.save
t.reload

tf = t.fingerprint

assert_equal t.fingerprint_value, tf.fingerprint
end

test 'Term.fingerprint returns nil of there is no fingerprint' do
# Terms without fingerprints are a temporary condition, but one that might occur for brief periods
target_fingerprint = fingerprints('hi')
target_term = target_fingerprint.terms.last
# Terms without fingerprints are a legacy condition - fingerprints are generated when saving the term - but one that
# we still need to confirm is stable.
target_term = terms('hi')

assert_nil target_term.fingerprint

target_term.save
target_term.reload

assert_not_nil target_term.fingerprint

target_fingerprint = target_term.fingerprint

target_fingerprint.destroy
target_term.reload
Expand All @@ -373,13 +388,19 @@ class TermTest < ActiveSupport::TestCase

test 'Term.cluster returns empty array if no related record exists' do
t = terms('hi')
t.save
t.reload

assert_equal 1, t.fingerprint.terms.count
assert_empty t.cluster
end

test 'Term.cluster returns array of terms if other terms share a fingerprint' do
t = terms('cool')
t.save
t2 = terms('cool_cluster')
t2.save
t.reload

assert_operator 1, :<, t.fingerprint.terms.count
assert_instance_of Array, t.cluster
Expand All @@ -389,16 +410,25 @@ class TermTest < ActiveSupport::TestCase
end

test 'Term.cluster returns nil if there is no fingerprint' do
target_fingerprint = fingerprints('hi')
target_term = target_fingerprint.terms.last
# Setup
target_term = terms('hi')

# Initial condition
assert_nil target_term.cluster

# Changes
target_term.save
target_term.reload
target_fingerprint = target_term.fingerprint

# Before we delete the fingerprint, the cluster method returns an array
# Verify impact
assert_instance_of Array, target_term.cluster

# Delete the fingerprint, leave the term - this will only ever be a temporary condition, but one that might exist.
target_fingerprint.destroy
target_term.reload

assert_instance_of NilClass, target_term.cluster
# Verify impact
assert_nil target_term.cluster
end
end

0 comments on commit 76da216

Please sign in to comment.