spec/base_spec.rb in logworm-0.7.4 vs spec/base_spec.rb in logworm-0.7.5

- old
+ new

@@ -1,5 +1,10 @@ +require 'rubygems' +require 'webmock' + +require File.dirname(__FILE__) + '/spec_helper' + $: << File.dirname(__FILE__) + '/../lib' require 'logworm.rb' describe Logworm::DB, " initialization" do it "should only accept proper URLs" do @@ -26,27 +31,66 @@ end describe Logworm::DB, " functioning" do + host = "http://localhost:9401" + before(:all) do @db = Logworm::DB.new('logworm://a:b@localhost:9401/c/d/') end - it "should support a call to get the list of tables" do - @db.should have(0).tables - log_details = {:value => 10, :_ts => Time.now.utc.strftime("%Y-%m-%dT%H:%M:%SZ"), :_ts_utc => (Time.now.utc.to_f * 1000).to_i} - @db.batch_log([["log", log_details]]) - @db.should have(1).tables + it "should offer a call to get the list of tables --> /" do + @db.should_receive(:db_call).with(:get, "#{host}/") + @db.tables end - it "should support a call to start a query" + it "should just parse and return the results of the call to get tables" do + return_body = [ + {"tablename" => "table1", "url" => "/table1", "last_write" => "2010-03-20 18:10:22", "rows" => 50}, + {"tablename" => "table2", "url" => "/table1", "last_write" => "2010-03-20 18:10:22", "rows" => 50}] + stub_request(:get, "#{host}/").to_return(:body => return_body.to_json) + @db.tables.should == return_body + end - it "should support a call to get the results of a query" + it "should support a call to start a query --> POST /queries" do + @db.should_receive(:db_call).with(:post, "#{host}/queries", {:table => "tbl1", :query => "a good query"}) + @db.query("tbl1", "a good query") + end - it "should raise DFGD if 404" + it "should just parse and return the results of the call to query" do + return_body = {"id" => 10, "query" => "q", "self_uri" => "/queries/10", "results_uri" => "/queries/10/results"} + stub_request(:post, "#{host}/queries").with(:body => "table=table1&query=q").to_return(:body => return_body.to_json) + @db.query("table1", "q").should == return_body + end - it "should raise DFGD if 403" + it "should support a call to retrieve the results of a query --> GET /queries/10/results" do + @db.should_receive(:db_call).with(:get, "#{host}/queries/10/results") + @db.results("#{host}/queries/10/results") rescue Exception # Returns an error when trying to parse results + end + + it "should just parse and return the results of the call to retrieve results, but also add results field" do + results = [{"a" => 10, "b" => "2"}, {"a" => "x"}] + return_body = {"id" => 10, "execution_time" => "5", + "query_url" => "#{host}/queries/10", "results_url" => "#{host}/queries/10/results", + "results" => results.to_json} + stub_request(:get, "#{host}/queries/10/results").to_return(:body => return_body.to_json) + @db.results("#{host}/queries/10/results").should == return_body.merge("results" => results) + end + + it "should raise ForbiddenAccessException if 403" do + stub_request(:get, "#{host}/").to_return(:status => 403) + lambda {@db.tables}.should raise_exception(Logworm::ForbiddenAccessException) + end - it "should raise DFGD if 400" + it "should raise InvalidQueryException if query is not valid" do + stub_request(:post, "#{host}/queries").to_return(:status => 400, :body => "Query error") + lambda {@db.query("tbl1", "bad query")}.should raise_exception(Logworm::InvalidQueryException) + end + it "should raise DatabaseException if response from server is not JSON" do + stub_request(:get, "#{host}/").to_return(:body => "blah") + lambda {@db.tables}.should raise_exception(Logworm::DatabaseException) + end + + end