Detect all changes, not only the first

webui
aeris 5 years ago
parent 9ad0e92bb7
commit 82e99fb5eb
  1. 4
      app/models/check.rb
  2. 4
      app/models/site.rb
  3. 4
      db/migrate/20180510000002_create_sites.rb
  4. 4
      db/migrate/20180510000004_create_checks.rb
  5. 22
      spec/models/site_spec.rb

@ -5,7 +5,7 @@ class Check < ApplicationRecord
def reference!(content)
target = self.target
reference = target.extract content
self.update! reference: reference, content: nil, checked_at: Time.now, changed_at: nil, last_error: nil
self.update! reference: reference, content: reference, checked_at: Time.now, changed_at: nil, last_error: nil
end
def diff!(content, debug: false)
@ -16,7 +16,7 @@ class Check < ApplicationRecord
begin
target = self.target
reference = Utils.utf8! self.reference
reference = Utils.utf8! self.content
content = target.extract content
changed = reference != content
if changed

@ -46,7 +46,7 @@ class Site < ApplicationRecord
end
def reference!(content)
self.update! reference: content, content: nil, checked_at: Time.now, changed_at: nil, last_error: nil
self.update! reference: content, content: content, checked_at: Time.now, changed_at: nil, last_error: nil
self.checks.each { |c| c.reference! content }
end
@ -63,7 +63,7 @@ class Site < ApplicationRecord
state = :unchanged
begin
reference = Utils.utf8! self.reference
reference = Utils.utf8! self.content
checks = self.checks
if checks.empty?
if reference != content

@ -4,8 +4,8 @@ class CreateSites < ActiveRecord::Migration[5.1]
t.string :url, null: false
t.string :name, index: true
t.binary :reference
t.binary :content
t.binary :reference, null: false
t.binary :content, null: false
t.belongs_to :group, index: true, foreign_key: true
t.belongs_to :template, index: true, foreign_key: true

@ -1,8 +1,8 @@
class CreateChecks < ActiveRecord::Migration[5.1]
def change
create_table :checks do |t|
t.binary :reference
t.binary :content
t.binary :reference, null: false
t.binary :content, null: false
t.belongs_to :target, index: true, foreign_key: true
t.belongs_to :site, index: true, foreign_key: true

@ -1,8 +1,10 @@
RSpec.describe Site, type: :model do
REFERENCE = '<html><body>foo <div id="content">bar</div></body></html>'
REFERENCE_TARGET = '<div id="content">bar</div>'
REFERENCE = "<html><body>foo #{REFERENCE_TARGET}</body></html>"
CHANGE_OUTSIDE_TARGET = '<html><body>baz <div id="content">bar</div></body></html>'
CHANGE_INSIDE_TARGET = '<html><body>foo <div id="content">baz</div></body></html>'
CHANGE_TARGET = '<div id="content">baz</div>'
CHANGE_INSIDE_TARGET = "<html><body>foo #{CHANGE_TARGET}</body></html>"
let :site do
Site.create! url: 'http://localhost/'
@ -32,7 +34,7 @@ RSpec.describe Site, type: :model do
expect(status).to be :unchanged
expect(site.changed_at).to be_nil
expect(site.content).to be_nil
expect(site.content).to eq REFERENCE
end
it 'must not change if no change with checks' do
@ -42,10 +44,10 @@ RSpec.describe Site, type: :model do
expect(status).to be :unchanged
expect(site.changed_at).to be_nil
expect(site.content).to be_nil
expect(site.content).to eq REFERENCE
expect(check.changed_at).to be_nil
expect(check.content).to be_nil
expect(check.content).to eq REFERENCE_TARGET
end
it 'must change if change with no check' do
@ -53,7 +55,7 @@ RSpec.describe Site, type: :model do
expect(status).to be :changed
expect(site.changed_at).not_to be_nil
expect(site.content).not_to be_nil
expect(site.content).not_to eq REFERENCE
end
it 'must not change if change but no check changed' do
@ -62,10 +64,10 @@ RSpec.describe Site, type: :model do
expect(status).to be :unchanged
expect(site.changed_at).to be_nil
expect(site.content).to be_nil
expect(site.content).to be REFERENCE
expect(check.changed_at).to be_nil
expect(check.content).to be_nil
expect(check.content).to eq REFERENCE_TARGET
end
it 'must change if check changed' do
@ -74,9 +76,9 @@ RSpec.describe Site, type: :model do
expect(status).to be :changed
expect(site.changed_at).not_to be_nil
expect(site.content).not_to be_nil
expect(site.content).to eq CHANGE_INSIDE_TARGET
expect(check.changed_at).not_to be_nil
expect(check.content).not_to be_nil
expect(check.content).to eq CHANGE_TARGET
end
end

Loading…
Cancel
Save