Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Start of SQL connection implementation, does schema load and :create …

…method
  • Loading branch information...
commit fd5974784e0e5805c1886d60d2aa1e2d3b43e2e2 1 parent 7fceacc
Jorge Alvarez authored
View
70 lib/zuora/sqlite_connector.rb
@@ -0,0 +1,70 @@
+require 'sqlite3'
+
+module Zuora
+ #Sqlite3 in memoroy connector to simulate Zuora in test environments
+ class SqliteConnector
+ cattr_accessor :db
+
+ def initialize(model)
+ @model = model
+ end
+
+ def create
+ table = self.class.table_name(@model.class)
+ hash = @model.to_hash
+ keys = []
+ values = []
+ hash.each do |key, value|
+ keys << key
+ values << value
+ end
+ place_holder = ['?'] * keys.length
+ keys = keys.join(', ')
+ place_holder = place_holder.join(', ')
+ insert = "INSERT into '#{table}'(#{keys}) VALUES(#{place_holder})"
+ db.execute insert, values
+ new_id = db.last_insert_row_id
+ {
+ :create_response => {
+ :success => true,
+ :id => new_id
+ }
+ }
+ end
+
+ def self.build_schema
+ self.db = SQLite3::Database.new ":memory:"
+ self.generate_tables
+ end
+
+ def self.table_name(model)
+ model.name.underscore.gsub '/', '_'
+ end
+
+ protected
+
+ def hash_result_row(row)
+ Hash[row.columns.zip(row.first.to_a)]
+ end
+
+ def self.generate_tables
+ Zuora::Objects::Base.subclasses.each do |model|
+ create_table(model)
+ end
+ end
+
+ def self.create_table(model)
+ table_name = self.table_name(model)
+ attributes = model.attributes - [:id]
+ attributes = attributes.map do |a|
+ "'#{a}' text"
+ end
+ autoid = "'id' integer PRIMARY KEY AUTOINCREMENT"
+ attributes.unshift autoid
+ attributes = attributes.join(", ")
+ schema = "CREATE TABLE 'main'.'#{table_name}' (#{attributes});"
+ db.execute schema
+ end
+
+ end
+end
View
51 spec/zuora/sqlite_connector_spec.rb
@@ -0,0 +1,51 @@
+require 'spec_helper'
+require 'zuora/sqlite_connector'
+
+describe Zuora::SqliteConnector do
+ describe :build_schema do
+ before :each do
+ @models = Zuora::Objects::Base.subclasses
+ described_class.build_schema
+
+ @db = described_class.db
+ end
+
+ it "builds a table schema for all Zuora::Object::Base classes" do
+ result = @db.execute "SELECT t.sql sql FROM 'main'.sqlite_master t WHERE t.type='table'"
+ sqlite_system_tables = 1
+ result.length.should == @models.length + sqlite_system_tables
+ end
+
+ it "creates a column for each attribute" do
+ @models.each do |m|
+ table_name = described_class.table_name(m)
+ table = @db.table_info(table_name)
+ columns = table.map {|t| t["name"].to_sym }
+ (m.attributes - columns).should == []
+ end
+ end
+ end
+
+ describe :create do
+ before :each do
+ described_class.build_schema
+ @model = Zuora::Objects::Product.new
+ @model.name = 'A Product'
+ @instance = described_class.new @model
+ @db = described_class.db
+ end
+
+ it "creates a record in the table" do
+ @instance.create
+ table_name = described_class.table_name(@model.class)
+ result = @db.execute "SELECT * FROM #{table_name}"
+ result.length.should == 1
+ end
+
+ it "returns success and new id in ash" do
+ result = @instance.create[:create_response]
+ result[:success].should be_true
+ result[:id].should_not be_nil
+ end
+ end
+end
View
1  zuora.gemspec
@@ -31,4 +31,5 @@ Gem::Specification.new do |s|
s.add_development_dependency(%q<redcarpet>, ["~> 2.1.0"])
s.add_development_dependency(%q<factory_girl>, ["~> 2.3.2"])
s.add_development_dependency(%q<appraisal>, ["~> 0.4.1"])
+ s.add_development_dependency(%q<sqlite3>, ["~> 1.3.0"])
end
Please sign in to comment.
Something went wrong with that request. Please try again.