Skip to content

Commit

Permalink
Add sqlite3 stuff - much closer to being database-neutral
Browse files Browse the repository at this point in the history
  • Loading branch information
Nathan Long committed Jan 15, 2013
1 parent 08dbee6 commit 671d64c
Show file tree
Hide file tree
Showing 18 changed files with 244 additions and 69 deletions.
4 changes: 4 additions & 0 deletions README.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -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 -;`
Expand Down
Empty file added dossier_test
Empty file.
40 changes: 40 additions & 0 deletions lib/dossier/adapter/sqlite3.rb
Original file line number Diff line number Diff line change
@@ -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
12 changes: 9 additions & 3 deletions lib/dossier/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
78 changes: 78 additions & 0 deletions spec/dossier/adapter/sqlite3_spec.rb
Original file line number Diff line number Diff line change
@@ -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
13 changes: 0 additions & 13 deletions spec/dummy/db/migrate/20130110141950_create_employees.rb

This file was deleted.

This file was deleted.

Empty file added spec/dummy/dossier_test
Empty file.
8 changes: 4 additions & 4 deletions spec/fixtures/customized_employee_report.html
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
2 changes: 0 additions & 2 deletions spec/fixtures/db/sqlite3.yml.example
Original file line number Diff line number Diff line change
@@ -1,4 +1,2 @@
adapter: sqlite3
database: path/to/test.sqlite3
pool: 5
timeout: 5000
Binary file added spec/fixtures/db/test.sqlite3
Binary file not shown.
8 changes: 4 additions & 4 deletions spec/fixtures/employee_report.csv
Original file line number Diff line number Diff line change
@@ -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
16 changes: 8 additions & 8 deletions spec/fixtures/employee_report.html
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
16 changes: 8 additions & 8 deletions spec/fixtures/employee_report_with_footer.html
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
4 changes: 0 additions & 4 deletions spec/requests/employee_report_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 9 additions & 4 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading

0 comments on commit 671d64c

Please sign in to comment.