Permalink
Browse files

Add sqlite3 stuff - much closer to being database-neutral

  • Loading branch information...
1 parent 08dbee6 commit 671d64cfbc964ac513994f9759f68bc15c8752c9 Nathan Long committed Jan 15, 2013
View
@@ -158,6 +158,10 @@ To see a report with all the bells and whistles, check out `spec/support/reports
## Running the Tests
+Note: when you run the tests, Dossier will make and/or truncate some tables in the `dossier_test` database.
+
+(TODO: discuss how to set this up.)
+
- Run `bundle`
- `cp spec/dummy/config/database.yml{.example,}` and edit it so that it can connect to the test database.
- `cd spec/dummy; rake db:create db:schema:load; cd -;`
View
No changes.
@@ -0,0 +1,40 @@
+require 'sqlite3'
+
+module Dossier
+ module Adapter
+ class SQLite3
+ attr_reader :connection
+
+ def self.connection_class
+ ::SQLite3::Database
+ end
+
+ def initialize(options = {})
+ @connection = self.class.connection_class.new(options.fetch(:database))
+ end
+
+ def execute(sql)
+ Result.new connection.execute(sql)
+ rescue ::SQLite3::Exception => e
+ raise Dossier::ExecuteError.new "#{e.message}\n\n#{sql}"
+ end
+
+ def escape(value)
+ connection.escape(value)
+ end
+
+ class Result
+ attr_accessor :rows
+
+ def initialize(rows)
+ self.rows = rows
+ end
+
+ def headers
+ rows.fields
+ end
+ end
+
+ end
+ end
+end
View
@@ -13,9 +13,15 @@ def initialize(options = {})
private
def adapter_class(adapter_name)
- adapter = "dossier/adapter/#{adapter_name}"
- require adapter
- adapter.classify.constantize
+ require "dossier/adapter/#{adapter_name}"
+ "Dossier::Adapter::#{classname_for(adapter_name)}".constantize
+ end
+
+ def classname_for(adapter_name)
+ {
+ 'mysql2' => 'Mysql2',
+ 'sqlite3' => 'SQLite3'
+ }.fetch(adapter_name)
end
end
@@ -0,0 +1,78 @@
+require 'spec_helper'
+require 'dossier/adapter/sqlite3'
+
+describe Dossier::Adapter::SQLite3 do
+
+ let(:db_config) { DB_CONFIG[:sqlite3] }
+ let(:adapter) { described_class.new(db_config) }
+
+ describe "initialize" do
+ it "instantiates its connection_class" do
+ described_class.connection_class.should_receive(:new).with(db_config[:database])
+ adapter
+ end
+ end
+
+ describe "instance methods" do
+
+ let(:example_query) { 'SELECT * FROM employees' }
+
+ describe "execute" do
+
+ it "calls `execute` on the connection" do
+ adapter.connection.should_receive(:execute).with(example_query)
+ adapter.execute(example_query)
+ end
+
+ it "returns an adapter result" do
+ expect(adapter.execute(example_query)).to be_a(Dossier::Adapter::SQLite3::Result)
+ end
+
+ describe "handling SQL syntax errors" do
+
+ it "raises a Dossier::ExecuteError containing the bad query" do
+ query = "FROM SELECT romeo WHERE FROM art SELECT romeo"
+ expect{adapter.execute(query)}.to raise_error(
+ Dossier::ExecuteError, /#{query}/
+ )
+ end
+
+ end
+
+ end
+
+ describe "escape" do
+
+ it "gets its connection to escape the value" do
+ adapter.connection.should_receive(:escape).with('dAnGeRoUsNeSs!#')
+ adapter.escape('dAnGeRoUsNeSs!#')
+ end
+
+ end
+
+ describe Dossier::Adapter::SQLite3::Result do
+
+ describe "headers" do
+
+ it "calls `fields` on its results" do
+ mock_results = mock(:results, fields: [])
+ results = Dossier::Adapter::SQLite3::Result.new(mock_results)
+ mock_results.should_receive(:fields)
+ results.headers
+ end
+
+ end
+
+ describe "rows" do
+
+ it "responds to `each`" do
+ results = adapter.execute(example_query)
+ expect(results.rows).to respond_to(:each)
+ end
+
+ end
+ end
+
+ end
+
+end
@@ -1,13 +0,0 @@
-class CreateEmployees < ActiveRecord::Migration
- def up
- create_table :employees do |t|
- t.string :name
- t.date :hired_on
- t.boolean :suspended
- end
- end
-
- def down
- drop_table :employees
- end
-end
@@ -1,6 +0,0 @@
-class AddDivisionToEmployees < ActiveRecord::Migration
- def change
- add_column :employees, :division, :string
- add_column :employees, :salary, :integer
- end
-end
View
No changes.
@@ -14,20 +14,20 @@ <h1>Employee Report</h1>
<tr>
<th>Id</th>
<th>Name</th>
-<th>Hired On</th>
-<th>Suspended</th>
<th>Division</th>
<th>Salary</th>
+<th>Suspended</th>
+<th>Hired On</th>
</tr>
</thead>
<tbody>
<tr>
<td>2</td>
<td>Employee Jimmy Jackalope, Jr.</td>
-<td>2013-01-11</td>
-<td>1</td>
<td>Tedious Toiling</td>
<td>$20,000.00</td>
+<td>Yes</td>
+<td>2013-01-11</td>
</tr>
</tbody>
</table>
@@ -1,4 +1,2 @@
adapter: sqlite3
database: path/to/test.sqlite3
-pool: 5
-timeout: 5000
Binary file not shown.
@@ -1,4 +1,4 @@
-Id,Name,Hired On,Suspended,Division,Salary
-3,Elise Elderberry,2013-01-11,0,Corporate Malfeasance,99000
-2,"Jimmy Jackalope, Jr.",2013-01-11,1,Tedious Toiling,20000
-1,Moustafa McMann,2010-10-02,0,Zany Inventions,30000
+Id,Name,Division,Salary,Suspended,Hired On
+3,Elise Elderberry,Corporate Malfeasance,99000,0,2013-01-11
+2,"Jimmy Jackalope, Jr.",Tedious Toiling,20000,1,2013-01-11
+1,Moustafa McMann,Zany Inventions,30000,0,2010-10-02
@@ -14,36 +14,36 @@ <h1>Employee Report</h1>
<tr>
<th>Id</th>
<th>Name</th>
-<th>Hired On</th>
-<th>Suspended</th>
<th>Division</th>
<th>Salary</th>
+<th>Suspended</th>
+<th>Hired On</th>
</tr>
</thead>
<tbody>
<tr>
<td>3</td>
<td>Employee Elise Elderberry</td>
-<td>2013-01-11</td>
-<td>0</td>
<td>Corporate Malfeasance</td>
<td>$99,000.00</td>
+<td>No</td>
+<td>2013-01-11</td>
</tr>
<tr>
<td>2</td>
<td>Employee Jimmy Jackalope, Jr.</td>
-<td>2013-01-11</td>
-<td>1</td>
<td>Tedious Toiling</td>
<td>$20,000.00</td>
+<td>Yes</td>
+<td>2013-01-11</td>
</tr>
<tr>
<td>1</td>
<td>Employee Moustafa McMann</td>
-<td>2010-10-02</td>
-<td>0</td>
<td>Zany Inventions</td>
<td>$30,000.00</td>
+<td>No</td>
+<td>2010-10-02</td>
</tr>
</tbody>
</table>
@@ -14,38 +14,38 @@ <h1>Employee Report</h1>
<tr>
<th>Id</th>
<th>Name</th>
-<th>Hired On</th>
-<th>Suspended</th>
<th>Division</th>
<th>Salary</th>
+<th>Suspended</th>
+<th>Hired On</th>
</tr>
</thead>
<tbody>
<tr>
<td>3</td>
<td>Employee Elise Elderberry</td>
-<td>2013-01-11</td>
-<td>0</td>
<td>Corporate Malfeasance</td>
<td>$99,000.00</td>
+<td>No</td>
+<td>2013-01-11</td>
</tr>
<tr>
<td>2</td>
<td>Employee Jimmy Jackalope, Jr.</td>
-<td>2013-01-11</td>
-<td>1</td>
<td>Tedious Toiling</td>
<td>$20,000.00</td>
+<td>Yes</td>
+<td>2013-01-11</td>
</tr>
</tbody>
<tfoot>
<tr>
<th>1</th>
<th>Employee Moustafa McMann</th>
-<th>2010-10-02</th>
-<th>0</th>
<th>Zany Inventions</th>
<th>$30,000.00</th>
+<th>No</th>
+<th>2010-10-02</th>
</tr>
</tfoot>
</table>
@@ -2,10 +2,6 @@
describe "employee report" do
- before :all do
- insert_employees
- end
-
describe "rendering HTML" do
context "when a custom view exists for the report" do
View
@@ -11,18 +11,23 @@
# Load support files
Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each { |f| require f }
-DB_CONFIG = [:mysql2].reduce({}) do |config, adapter_name|
+DB_CONFIG = [:mysql2, :sqlite3].reduce({}) do |config, adapter_name|
config.tap do |hash|
path = "spec/fixtures/db/#{adapter_name}.yml"
- if File.exist?(path)
- hash[adapter_name] = YAML.load_file(path).symbolize_keys
- end
+ hash[adapter_name] = YAML.load_file(path).symbolize_keys if File.exist?(path)
end
end.freeze
RSpec.configure do |config|
config.mock_with :rspec
+ config.before :all do
+ DB_CONFIG.keys.each do |adapter|
+ Dossier::Factory.send("#{adapter}_create_employees")
+ Dossier::Factory.send("#{adapter}_seed_employees")
+ end
+ end
+
config.after :each do
Dossier.instance_variable_set(:@configuration, nil)
end
Oops, something went wrong.

0 comments on commit 671d64c

Please sign in to comment.