Permalink
Browse files

Added testing

  • Loading branch information...
1 parent e573a76 commit 2ea32f9303688e62e312e569c4c66e09cc181f03 Bruce Williams committed Nov 16, 2006
Showing with 57 additions and 11 deletions.
  1. +2 −2 Rakefile
  2. +0 −1 lib/data_element.rb
  3. +11 −7 lib/twfy.rb
  4. +44 −1 test/test_twfy.rb
View
@@ -2,7 +2,7 @@
require 'rubygems'
require 'hoe'
-require './lib/twfy.rb'
+require 'lib/twfy.rb'
Hoe.new('twfy', Twfy::VERSION) do |p|
p.rubyforge_name = 'twfy'
@@ -14,7 +14,7 @@ Hoe.new('twfy', Twfy::VERSION) do |p|
EOD
p.url = "http://twfy.rubyforge.org"
p.changes = p.paragraphs_of('History.txt', 0..1).join("\n\n")
- p.add_dependency('json')
+ p.extra_deps = ['json']
p.email = %q{bruce@codefluency.com}
p.author = ["Bruce Williams"]
end
View
@@ -35,7 +35,6 @@ def initialize(client, data={})
def convert(field, value)
if conversion = @@conversions[field.to_sym]
- puts "Calling conversion of #{field}"
args = [value]
args.unshift self if conversion.arity == 2
conversion.call(*args)
View
@@ -28,7 +28,9 @@ def log(message, level=:info)
end
def convert_url(params={})
- service :convertURL, validate(params, :require => :url)
+ service :convertURL, validate(params, :require => :url) do |value|
+ URI.parse(value['url'])
+ end
end
def constituency(params={})
@@ -92,7 +94,7 @@ def comments(params={})
service :getComments, validate(params, :allow => [:date, :search, :user_id, :pid, :page, :num])
end
- def service(name, params={}, klass=OpenStruct)
+ def service(name, params={}, target=OpenStruct, &block)
log "Calling service #{name}"
url = BASE + name.to_s
url.query = build_query(params)
@@ -104,7 +106,7 @@ def service(name, params={}, klass=OpenStruct)
if result.kind_of?(Hash) && result['error']
raise APIError, result['error'].to_s
else
- structure result, klass
+ structure result, block || target
end
end
@@ -144,15 +146,17 @@ def validate(params, against)
params
end
- def structure(value, klass)
+ def structure(value, target)
case value
when Array
- value.map{|r| structure(r, klass) }
+ value.map{|r| structure(r, target) }
when Hash
- if klass == Hash
+ if target.kind_of?(Proc)
+ target.call(value)
+ elsif target == Hash
value
else
- klass.ancestors.include?(DataElement) ? klass.new(self,value) : klass.new(value)
+ target.ancestors.include?(DataElement) ? target.new(self,value) : target.new(value)
end
else
result
View
@@ -1 +1,44 @@
-# ok, so testing is pretty dismal at the moment...
+require 'test/unit'
+require File.join(File.dirname(__FILE__), '../lib/twfy')
+
+class BasicReturnedDataTest < Test::Unit::TestCase
+
+ def setup
+ @client = Twfy::Client.new
+ end
+
+ def test_convert_url
+ uri = @client.convert_url(:url=>'http://www.publications.parliament.uk/pa/cm200506/cmhansrd/cm061018/debtext/61018-0002.htm#06101834000471')
+ assert_kind_of URI::HTTP, uri
+ assert_equal "www.theyworkforyou.com", uri.host
+ end
+
+ def test_mp_and_mp_info
+ mp = @client.mp(:postcode=>'IP6 9PN')
+ assert_kind_of Twfy::MP, mp
+ assert_kind_of OpenStruct, @client.mp_info(:id=>mp.person_id)
+ end
+
+ def test_mps
+ mps = @client.mps
+ assert_kind_of Array, mps
+ mps.each do |mp|
+ assert_kind_of Twfy::MP, mp
+ end
+ end
+
+ def test_constituency_and_geometry
+ c = @client.constituency(:postcode => 'IP6 9PN')
+ assert_kind_of Twfy::Constituency, c
+ assert_kind_of Twfy::Geometry, @client.geometry(:name=>c.name)
+ end
+
+ def test_constituencies
+ cs = @client.constituencies
+ assert_kind_of Array, cs
+ cs.each do |c|
+ assert_kind_of Twfy::Constituency, c
+ end
+ end
+
+end

0 comments on commit 2ea32f9

Please sign in to comment.