Skip to content

Commit

Permalink
Add tests to String and Translation refactoring, close #85
Browse files Browse the repository at this point in the history
  • Loading branch information
edouard committed Apr 17, 2012
1 parent c65e1b4 commit a25ef91
Show file tree
Hide file tree
Showing 9 changed files with 166 additions and 162 deletions.
5 changes: 5 additions & 0 deletions Guardfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
guard 'rspec', :cli => "--color --format progress" do
watch(%r{^spec/.+_spec\.rb$})
watch(%r{^lib/(.+)\.rb$}) { |m| "spec/lib/#{m[1]}_spec.rb" }
watch('spec/spec_helper.rb') { "spec" }
end
17 changes: 8 additions & 9 deletions lib/web_translate_it/string.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ def initialize(api_key, params = {})

def self.find_all(http_connection, api_key, params = {})
url = "/api/projects/#{api_key}/strings.yaml"
url += '?' + HashUtil.to_param({ :filters => params }) unless params.blank?
url += '?' + HashUtil.to_params("filters" => params) unless params.empty?

request = Net::HTTP::Get.new(url)
request.add_field("X-Client-Name", "web_translate_it")
Expand Down Expand Up @@ -95,8 +95,9 @@ def self.find(http_connection, api_key, id)
request.add_field("X-Client-Version", WebTranslateIt::Util.version)

begin
response = Util.handle_response(http_connection.request(request), true)
string = WebTranslateIt::String.new(api_key, YAML.load(response))
response = http_connection.request(request)
return nil if response.code.to_i == 404
string = WebTranslateIt::String.new(api_key, YAML.load(response.body))
string.new_record = false
return string
rescue Timeout::Error
Expand Down Expand Up @@ -160,12 +161,9 @@ def translation_for(http_connection, locale)
begin
response = Util.handle_response(http_connection.request(request), true)
hash = YAML.load(response)
unless hash.empty?
translation = WebTranslateIt::Translation.new(api_key, hash)
translation.new_record = false
return translation
end
return nil
return nil if hash.empty?
translation = WebTranslateIt::Translation.new(api_key, hash)
return translation

rescue Timeout::Error
puts "The request timed out. The service may be overloaded. We will retry in 5 seconds."
Expand Down Expand Up @@ -215,6 +213,7 @@ def create(http_connection)
begin
response = YAML.load(Util.handle_response(http_connection.request(request), true))
self.id = response["id"]
self.new_record = false
return true
rescue Timeout::Error
puts "The request timed out. The service may be overloaded. We will retry in 5 seconds."
Expand Down
28 changes: 24 additions & 4 deletions lib/web_translate_it/util/hash_util.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,27 @@
class HashUtil
def self.to_param(hash, namespace = nil) # taken from Rails
hash.collect do |key, value|
value.to_query(namespace ? "#{namespace}[#{key}]" : key)
end.sort * '&'
def self.to_params(hash)
params = ''
stack = []

hash.each do |k, v|
if v.is_a?(Hash)
stack << [k,v]
else
params << "#{k}=#{v}&"
end
end

stack.each do |parent, hash|
hash.each do |k, v|
if v.is_a?(Hash)
stack << ["#{parent}[#{k}]", v]
else
params << "#{parent}[#{k}]=#{v}&"
end
end
end

params.chop! # trailing &
params
end
end
60 changes: 0 additions & 60 deletions spec/web_translate_it/configuration_spec.rb

This file was deleted.

127 changes: 127 additions & 0 deletions spec/web_translate_it/string_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
require 'spec_helper'

describe WebTranslateIt::String do

let(:api_key) { "8b669bfc326eff878e2532b67dd001ad55998963" }

describe "#initialize" do
it "should assign api_key and many parameters" do
string = WebTranslateIt::String.new(api_key, { "id" => 1234, "key" => "bacon"})
string.api_key.should == api_key
string.id.should == 1234
string.key.should == "bacon"
end
end

describe "#save" do
it "should create a new String" do
WebTranslateIt::Util.http_connection do |connection|
string = WebTranslateIt::String.new(api_key, { "key" => "bacon" })
string.save(connection)
string.id.should_not be_nil
string_fetched = WebTranslateIt::String.find(connection, api_key, string.id)
string_fetched.should_not be_nil
string_fetched.should be_a(WebTranslateIt::String)
string_fetched.id.should == string.id
string.delete(connection)
end
end

it "should update an existing String" do
WebTranslateIt::Util.http_connection do |connection|
string = WebTranslateIt::String.new(api_key, { "key" => "bacony" })
string.save(connection)
string.key = "bacon changed"
string.save(connection)
string_fetched = WebTranslateIt::String.find(connection, api_key, string.id)
string_fetched.key.should == "bacon changed"
string.delete(connection)
end
end

it "should create a new String with Translation" do
translation1 = WebTranslateIt::Translation.new(api_key, { "locale" => "en", "text" => "Hello" })
translation2 = WebTranslateIt::Translation.new(api_key, { "locale" => "fr", "text" => "Bonjour" })

string = WebTranslateIt::String.new(api_key, { "key" => "bacon", "translations" => [translation1, translation2] })
WebTranslateIt::Util.http_connection do |connection|
string.save(connection)
string_fetched = WebTranslateIt::String.find(connection, api_key, string.id)
string_fetched.translation_for(connection, "en").should_not be_nil
string_fetched.translation_for(connection, "en").text.should == "Hello"
string_fetched.translation_for(connection, "fr").should_not be_nil
string_fetched.translation_for(connection, "fr").text.should == "Bonjour"
string_fetched.translation_for(connection, "es").should be_nil
string.delete(connection)
end
end

it "should update a String and save its Translation" do
translation1 = WebTranslateIt::Translation.new(api_key, { "locale" => "en", "text" => "Hello" })
translation2 = WebTranslateIt::Translation.new(api_key, { "locale" => "fr", "text" => "Bonjour" })

string = WebTranslateIt::String.new(api_key, { "key" => "bacon" })
WebTranslateIt::Util.http_connection do |connection|
string.save(connection)
string_fetched = WebTranslateIt::String.find(connection, api_key, string.id)
string_fetched.translation_for(connection, "fr").should be_nil

string_fetched.translations = [translation1, translation2]
string_fetched.save(connection)

string_fetched = WebTranslateIt::String.find(connection, api_key, string.id)
string_fetched.translation_for(connection, "en").should_not be_nil
string_fetched.translation_for(connection, "en").text.should == "Hello"
string_fetched.translation_for(connection, "fr").should_not be_nil
string_fetched.translation_for(connection, "fr").text.should == "Bonjour"
string.delete(connection)
end
end
end

describe "#delete" do
it "should delete a String" do
string = WebTranslateIt::String.new(api_key, { "key" => "bacon" })
WebTranslateIt::Util.http_connection do |connection|
string.save(connection)
string_fetched = WebTranslateIt::String.find(connection, api_key, string.id)
string_fetched.should_not be_nil

string_fetched.delete(connection)
string_fetched = WebTranslateIt::String.find(connection, api_key, string.id)
string_fetched.should be_nil
end
end
end

describe "#find_all" do
it "should find many strings" do
WebTranslateIt::Util.http_connection do |connection|
string1 = WebTranslateIt::String.new(api_key, { "key" => "bacon" })
string1.save(connection)
string2 = WebTranslateIt::String.new(api_key, { "key" => "tart" })
string2.save(connection)
string3 = WebTranslateIt::String.new(api_key, { "key" => "bacon tart" })
string3.save(connection)

strings = WebTranslateIt::String.find_all(connection, api_key, { "key" => "bacon" })
strings.count.should == 2
strings[0].key.should == "bacon"
strings[1].key.should == "bacon tart"

strings = WebTranslateIt::String.find_all(connection, api_key, { "key" => "tart" })
strings.count.should == 2
strings[0].key.should == "tart"
strings[1].key.should == "bacon tart"

strings = WebTranslateIt::String.find_all(connection, api_key, { "key" => "bacon tart" })
strings.count.should == 1
strings[0].key.should == "bacon tart"

string1.delete(connection)
string2.delete(connection)
string3.delete(connection)
end
end
end
end
52 changes: 0 additions & 52 deletions spec/web_translate_it/translation_file_spec.rb

This file was deleted.

10 changes: 0 additions & 10 deletions spec/web_translate_it/util_spec.rb

This file was deleted.

26 changes: 0 additions & 26 deletions spec/web_translate_it_spec.rb

This file was deleted.

3 changes: 2 additions & 1 deletion web_translate_it.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@ Gem::Specification.new do |s|
s.add_dependency "multipart-post", "~> 1.1.3"
s.add_dependency "trollop", "~> 1.16.2"
s.add_dependency "json"

s.add_development_dependency "rspec", ">= 2.6.0"

s.add_development_dependency "guard-rspec"
s.has_rdoc = true
s.rdoc_options = ["--main", "readme.md"]
s.extra_rdoc_files = ["history.md", "readme.md"]
Expand Down

0 comments on commit a25ef91

Please sign in to comment.