Skip to content

Commit

Permalink
refacored and backfilled tests, removed some lenses we weren't using
Browse files Browse the repository at this point in the history
  • Loading branch information
bmaddy committed Apr 22, 2014
1 parent e13d3b6 commit 0aabbfa
Show file tree
Hide file tree
Showing 4 changed files with 177 additions and 100 deletions.
4 changes: 2 additions & 2 deletions lib/fedora_lens.rb
Original file line number Diff line number Diff line change
Expand Up @@ -152,8 +152,8 @@ def create(data)

def orm_to_hash
if @orm_to_hash.nil?
aggregate_lens = attributes_as_lenses.inject({}) do |acc, (name, path)|
lens = path.inject {|outer, inner| Lenses.compose(outer, inner)}
aggregate_lens = attributes_as_lenses.reduce({}) do |acc, (name, path)|
lens = path.reduce {|outer, inner| Lenses.compose(outer, inner)}
acc.merge(name => lens)
end
@orm_to_hash = Lenses.orm_to_hash(aggregate_lens)
Expand Down
89 changes: 50 additions & 39 deletions lib/fedora_lens/lens_tests.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,36 @@ module LensTests
# See page 6 of the manual for the Harmony language for a description
# on how lenses work
# http://www.seas.upenn.edu/~harmony/manual.pdf

class LensLawError < StandardError
def initialize(actual, expected)
@actual = actual
@expected = expected
end
def message(desc)
"Lens must be well-behaved (#{desc})
expected: #{@expected.inspect}
got: #{@actual.inspect}"
end
end
class GetPutError < LensLawError
def message
super("GetPut: lens.put(source, lens.get(source)) == source")
end
end
class PutGetError < LensLawError
def message
super("PutGet: lens.get(lens.put(source, value)) == value")
end
end
class CreateGetError < LensLawError
def message
super("CreateGet: lens.get(lens.create(value)) == value")
end
end

# @example testing a lens that converts xml to a dom and back
# test_lens(lens, Nokogiri::XML("<a/>"), Nokogiri::XML("<b/>") do |v|
# check_laws(lens, Nokogiri::XML("<a/>"), Nokogiri::XML("<b/>") do |v|
# v.to_xml
# end
# @param [lens]
Expand All @@ -13,48 +41,31 @@ module LensTests
# the value to test with (when calling put)
# @param [source]
# the source document this lens operates on
# @yield [converter]
# a block that converts the value from the lens to something that can be
# compared with #== (defaults to the identity function)
# @yieldparam [value or source]
# a value that will be compared
def test_lens(lens, source, value, &block)
test_lens_get_put(lens, source, &block)
test_lens_put_get(lens, source, value, &block)
test_lens_create_get(lens, value, &block)
end

def test_lens_get_put(lens, source)
it "is well-behaved (GetPut: put(source, get(source)) == source)" do
converted = lens.put(source, lens.get(source))
if block_given?
yield(converted).should eq yield(source)
else
converted.should eq source
end
# @yield [actual, expected]
# a block that returns true if the supplied values are equal
def check_laws(lens, source, value, &block)
if block_given?
equality_test = block
else
equality_test = lambda { |a, b| a == b }
end
end

def test_lens_put_get(lens, source, value)
it "is well-behaved (PutGet: get(put(source, value)) == value)" do
converted = lens.get(lens.put(source, value))
if block_given?
yield(converted).should eq yield(value)
else
converted.should eq value
end
end
actual, expected = check_get_put(lens, source)
raise GetPutError.new(actual, expected) unless equality_test.call(actual, expected)
actual, expected = check_put_get(lens, source, value)
raise PutGetError.new(actual, expected) unless equality_test.call(actual, expected)
actual, expected = check_create_get(lens, value)
raise CreateGetError.new(actual, expected) unless equality_test.call(actual, expected)
end

def test_lens_create_get(lens, value)
it "is well-behaved (CreateGet: get(create(value)) == value)" do
created = lens.get(lens.create(value))
if block_given?
yield(created).should eq yield(value)
else
created.should eq value
end
end
def check_get_put(lens, source)
[lens.put(source, lens.get(source)), source]
end
def check_put_get(lens, source, value)
[lens.get(lens.put(source, value)), value]
end
def check_create_get(lens, value)
[lens.get(lens.create(value)), value]
end
end
end
74 changes: 28 additions & 46 deletions lib/fedora_lens/lenses.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ def literal_to_string
else
RDF::Literal.new(value)
end
end
end,
create: lambda {|value| RDF::Literal.new(value)}
]
end

Expand All @@ -43,6 +44,11 @@ def literals_to_strings
Array(values).map do |value|
RDF::Literal.new(value)
end
end,
create: lambda do |values|
Array(values).map do |v|
RDF::Literal.new(v)
end
end
]
end
Expand All @@ -56,25 +62,34 @@ def uris_to_ids
Array(values).compact.map do |value|
RDF::URI.new(HOST + value)
end
end,
create: lambda do |values|
Array(values).compact.map do |value|
RDF::URI.new(HOST + value)
end
end
]
end

def hash_update
Lens[
get: lambda {|hash| hash[key]},
put: lambda {|hash, pair| hash.merge(pair[0] => pair[1])}
]
end

def orm_to_hash(name_to_lens)
Lens[
get: lambda do |orm|
name_to_lens.inject({}) do |hash, (key, lens)|
name_to_lens.reduce({}) do |hash, (key, lens)|
hash.merge(key => lens.get(orm))
end
end,
put: lambda do |orm, hash|
unexpected = hash.keys - name_to_lens.keys
raise ArgumentError.new("Unexpected keys for put: #{unexpected}") if unexpected.present?
name_to_lens.each do |(key, lens)|
lens.put(orm, hash[key])
end
orm
end,
create: lambda do |hash|
unexpected = hash.keys - name_to_lens.keys
raise ArgumentError.new("Unexpected keys for put: #{unexpected}") if unexpected.present?
orm = Ldp::Orm.new(Ldp::Resource::RdfSource.new(FedoraLens.connection, '', RDF::Graph.new))
name_to_lens.each do |(key, lens)|
lens.put(orm, hash[key])
end
Expand All @@ -95,8 +110,7 @@ def as_dom
def at_css(selector)
Lens[
get: lambda {|doc| doc.at_css(selector).content},
put: lambda {|doc, value|
doc.at_css(selector).content = value; doc},
put: lambda {|doc, value| doc.at_css(selector).content = value; doc},
# can't do a css create because we don't know the structure
]
end
Expand Down Expand Up @@ -148,51 +162,19 @@ def get_predicate(predicate, opts = {})
]
end

def load_model(klass)
Lens[
get: lambda do |id|
klass.find(id)
end,
put: lambda do |id, model|
model.save
id
end
]
end

def load_or_build_orm
Lens[
get: lambda do |uri|
if uri.present?
Ldp::Orm.new(Ldp::Resource::RdfSource.new(FedoraLens.connection, uri.to_s))
else
Ldp::Orm.new(Ldp::Resource::RdfSource.new(FedoraLens.connection, nil, RDF::Graph.new))
end
end,
put: lambda do |uri, orm|
if orm.resource.subject.present?
orm.save
else
orm.resource.create
end
orm.resource.subject_uri
end
]
end

def compose(outer, inner)
Lens[
get: lambda do |source|
inner.get(outer.get(source))
end,
put: lambda do |source, value|
outer.put(source, inner.put(outer.get(source), value))
end,
create: lambda do |value|
outer.create(inner.create(value))
end
]
end

def zip(first, second)
end
end
end
end
Loading

0 comments on commit 0aabbfa

Please sign in to comment.