Skip to content

Commit

Permalink
Tests, fixtures, and annotations
Browse files Browse the repository at this point in the history
  • Loading branch information
matt-bernhardt committed Sep 10, 2024
1 parent c9a372b commit fce4417
Show file tree
Hide file tree
Showing 8 changed files with 160 additions and 2 deletions.
2 changes: 0 additions & 2 deletions app/models/detector.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@
# created_at :datetime not null
# updated_at :datetime not null
#
# Detectors are classes that implement various algorithms that allow us to identify patterns
# within search terms.
class Detector < ApplicationRecord
has_many :detector_categories, dependent: :destroy
has_many :categories, through: :detector_categories
Expand Down
2 changes: 2 additions & 0 deletions db/migrate/20240909183413_create_categories.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@ def change

t.timestamps
end
add_index :detectors, :name, unique: true

create_table :categories do |t|
t.string :name
t.text :description

t.timestamps
end
add_index :categories, :name, unique: true

create_table :detector_categories do |t|
t.belongs_to :detector, null: false, foreign_key: true
Expand Down
2 changes: 2 additions & 0 deletions db/schema.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 21 additions & 0 deletions test/fixtures/categories.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# == Schema Information
#
# Table name: categories
#
# id :integer not null, primary key
# name :string
# description :text
# created_at :datetime not null
# updated_at :datetime not null
#
informational:
name: 'Informational'
description: '...'

navigational:
name: 'Navigational'
description: '...'

transactional:
name: 'Transactional'
description: '...'
18 changes: 18 additions & 0 deletions test/fixtures/detector_categories.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# == Schema Information
#
# Table name: detector_categories
#
# id :integer not null, primary key
# detector_id :integer not null
# category_id :integer not null
# confidence :float
# created_at :datetime not null
# updated_at :datetime not null
#
one:
detector: doi
category: transactional

two:
detector: isbn
category: transactional
21 changes: 21 additions & 0 deletions test/fixtures/detectors.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# == Schema Information
#
# Table name: detectors
#
# id :integer not null, primary key
# name :string
# confidence :float
# created_at :datetime not null
# updated_at :datetime not null
#
doi:
name: 'DOI'
confidence: 0.95

isbn:
name: 'ISBN'
confidence: 0.8

issn:
name: 'ISSN'
confidence: 0.6
48 changes: 48 additions & 0 deletions test/models/category_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# frozen_string_literal: true

# == Schema Information
#
# Table name: categories
#
# id :integer not null, primary key
# name :string
# description :text
# created_at :datetime not null
# updated_at :datetime not null
#
require 'test_helper'

class CategoryTest < ActiveSupport::TestCase
test 'duplicate Categories are not allowed' do
initial_count = Category.count
Category.create!(name: 'Example')
assert_equal(initial_count + 1, Category.count)

assert_raises(ActiveRecord::RecordNotUnique) do
Category.create!(name: 'Example')
end
end

test 'destroying a Category will delete associated DetectorCategories' do
category_count = Category.count
link_count = DetectorCategory.count
record = categories('transactional')
link_category = record.detector_categories.count

record.destroy

assert_equal(category_count - 1, Category.count)
assert_equal(link_count - link_category, DetectorCategory.count)
end

test 'destroying a Category will not delete associated Detectors' do
category_count = Category.count
detector_count = Detector.count
record = categories('transactional')

record.destroy

assert_equal(category_count - 1, Category.count)
assert_equal(detector_count, Detector.count)
end
end
48 changes: 48 additions & 0 deletions test/models/detector_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# frozen_string_literal: true

# == Schema Information
#
# Table name: detectors
#
# id :integer not null, primary key
# name :string
# confidence :float
# created_at :datetime not null
# updated_at :datetime not null
#
require 'test_helper'

class DetectorTest < ActiveSupport::TestCase
test 'duplicate Detectors are not allowed' do
detector_count = Detector.count
Detector.create!(name: 'Example')
assert_equal(detector_count + 1, Detector.count)

assert_raises(ActiveRecord::RecordNotUnique) do
Detector.create!(name: 'Example')
end
end

test 'destroying a Detector will delete associated DetectorCategories' do
detector_count = Detector.count
link_count = DetectorCategory.count
record = detectors('doi')
link_detector = record.detector_categories.count

record.destroy

assert_equal(detector_count - 1, Detector.count)
assert_equal(link_count - link_detector, DetectorCategory.count)
end

test 'destroying a Detector will not delete associated Categories' do
detector_count = Detector.count
category_count = Category.count
record = detectors('doi')

record.destroy

assert_equal(detector_count - 1, Detector.count)
assert_equal(category_count, Category.count)
end
end

0 comments on commit fce4417

Please sign in to comment.