Commit f15ebc40 authored by John E. Vincent's avatar John E. Vincent

more watcher design. test done

parent 1a97bf10
......@@ -11,5 +11,6 @@ require 'yaml'
require 'sinatra/base'
require 'sinatra/namespace'
require File.join(File.dirname(__FILE__), 'noah','validations')
require File.join(File.dirname(__FILE__), 'noah','models')
require File.join(File.dirname(__FILE__), 'noah','app')
......@@ -3,6 +3,7 @@ module Noah
class Watcher < Model #NYI
# Don't trust anything in here yet
# I'm still trying a few things
include WatcherValidations
attribute :pattern
attribute :endpoint
......@@ -15,6 +16,8 @@ module Noah
assert_present :endpoint
assert_present :pattern
assert_unique [:endpoint, :pattern]
assert_not_superset
assert_not_subset
end
def name
......@@ -26,6 +29,22 @@ module Noah
super.merge(h)
end
def self.watch_list
hsh = Hash.new
watch_list = self.all.sort_by(:pattern)
watch_list.each do |watch|
p = watch.pattern.to_sym
e = watch.endpoint
if hsh.has_key?(p)
hsh[p].push(watch.endpoint)
else
hsh[p] = Array.new
hsh[p].push(watch.endpoint)
end
end
hsh
end
private
# Not sure about these next two.
# Could get around patterns changing due to namespace changes
......
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
describe "Using the Watcher Model", :reset_redis => true do
before(:each) do
Ohm.redis.flushdb
@test_endpoint = "http://localhost/webhook"
@test_pattern = "/foo/bar"
Noah::Watcher.create :pattern => @test_pattern, :endpoint => @test_webhook
@test_watch = Noah::Watcher.new :endpoint => "http://localhost/webhook"
end
after(:each) do
#Ohm.redis.flushdb
end
describe "should" do
it "create a new Noah::Watcher" do
@test_watch.pattern = "/snarf"
@test_watch.valid?.should == true
@test_watch.save
a = Noah::Watcher.find(:endpoint => @test_watch.endpoint, :pattern => @test_watch.pattern).first
a.should == @test_watch
end
it "delete an existing Noah::Watcher" do
@test_watch.pattern = "/snarf"
@test_watch.save
@test_watch.delete
Noah::Watcher.find(:endpoint => @test_endpoint, :pattern => @test_pattern).first.should == nil
end
end
describe "should not" do
it "create a new Noah::Watcher with missing endpoint" do
a = Noah::Watcher.create(:pattern => "/foo/bar")
a.valid?.should == false
a.errors.to_s.should == "[[:endpoint, :not_present]]"
end
it "create a new Noah::Watcher with missing pattern" do
a = Noah::Watcher.create(:endpoint => "http://localhost/webhook")
a.valid?.should == false
a.errors.to_s.should == "[[:pattern, :not_present]]"
end
it "create a new Noah::Watcher with subset pattern" do
a = Noah::Watcher.create(:endpoint => "http://localhost.domain.com/webhook", :pattern => "//noah/")
b = Noah::Watcher.create(:endpoint => "http://localhost.domain.com/webhook", :pattern => "//noah/foobar")
b.valid?.should == false
b.errors.to_s.should == "[[:pattern, :already_provided]]"
end
it "create a new Noah::Watcher with superset pattern" do
a = Noah::Watcher.create(:endpoint => "http://localhost.domain.com/webhook", :pattern => "//noah/foobar")
b = Noah::Watcher.create(:endpoint => "http://localhost.domain.com/webhook", :pattern => "//noah")
b.valid?.should == false
b.errors.to_s.should == "[[:pattern, :replaces_existing]]"
end
it "create a duplicate Noah::Watcher" do
a = Noah::Watcher.create(:endpoint => "http://localhost.domain.com/webhook", :pattern => "//noah/foobar")
b = Noah::Watcher.create(:endpoint => "http://localhost.domain.com/webhook", :pattern => "//noah/foobar")
b.valid?.should == false
b.errors.to_s.should == "[[[:endpoint, :pattern], :not_unique]]"
end
end
end
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment