mirror of
https://github.com/lloeki/rebel.git
synced 2025-12-06 01:54:40 +01:00
Tests
This commit is contained in:
parent
3415cf8ad4
commit
2155aa8d43
5 changed files with 75 additions and 5 deletions
1
Gemfile
1
Gemfile
|
|
@ -6,3 +6,4 @@ gem 'minitest'
|
||||||
gem 'pry'
|
gem 'pry'
|
||||||
gem 'rake'
|
gem 'rake'
|
||||||
gem 'rubocop'
|
gem 'rubocop'
|
||||||
|
gem 'sqlite3'
|
||||||
|
|
|
||||||
|
|
@ -27,6 +27,7 @@ GEM
|
||||||
unicode-display_width (~> 1.0, >= 1.0.1)
|
unicode-display_width (~> 1.0, >= 1.0.1)
|
||||||
ruby-progressbar (1.8.1)
|
ruby-progressbar (1.8.1)
|
||||||
slop (3.6.0)
|
slop (3.6.0)
|
||||||
|
sqlite3 (1.3.13)
|
||||||
unicode-display_width (1.1.3)
|
unicode-display_width (1.1.3)
|
||||||
|
|
||||||
PLATFORMS
|
PLATFORMS
|
||||||
|
|
@ -38,6 +39,7 @@ DEPENDENCIES
|
||||||
rake
|
rake
|
||||||
rebel!
|
rebel!
|
||||||
rubocop
|
rubocop
|
||||||
|
sqlite3
|
||||||
|
|
||||||
BUNDLED WITH
|
BUNDLED WITH
|
||||||
1.14.5
|
1.14.5
|
||||||
|
|
|
||||||
|
|
@ -9,13 +9,17 @@ module Rebel::SQL
|
||||||
exec(Rebel::SQL.create_table(table_name, desc))
|
exec(Rebel::SQL.create_table(table_name, desc))
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def drop_table(table_name)
|
||||||
|
exec(Rebel::SQL.drop_table(table_name))
|
||||||
|
end
|
||||||
|
|
||||||
def select(*fields, from: nil, where: nil, inner: nil, left: nil, right: nil)
|
def select(*fields, from: nil, where: nil, inner: nil, left: nil, right: nil)
|
||||||
exec(Rebel::SQL.select(*fields,
|
exec(Rebel::SQL.select(*fields,
|
||||||
from: from,
|
from: from,
|
||||||
where: where,
|
where: where,
|
||||||
inner: inner,
|
inner: inner,
|
||||||
left: left,
|
left: left,
|
||||||
right: right))
|
right: right))
|
||||||
end
|
end
|
||||||
|
|
||||||
def insert_into(table_name, *rows)
|
def insert_into(table_name, *rows)
|
||||||
|
|
@ -77,6 +81,12 @@ module Rebel::SQL
|
||||||
SQL
|
SQL
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def drop_table(table_name)
|
||||||
|
<<-SQL
|
||||||
|
DROP TABLE #{Rebel::SQL.name(table_name)};
|
||||||
|
SQL
|
||||||
|
end
|
||||||
|
|
||||||
def select(*fields, from: nil, where: nil, inner: nil, left: nil, right: nil)
|
def select(*fields, from: nil, where: nil, inner: nil, left: nil, right: nil)
|
||||||
<<-SQL
|
<<-SQL
|
||||||
SELECT #{names(*fields)} FROM #{name(from)}
|
SELECT #{names(*fields)} FROM #{name(from)}
|
||||||
|
|
|
||||||
8
test/helper.rb
Normal file
8
test/helper.rb
Normal file
|
|
@ -0,0 +1,8 @@
|
||||||
|
require 'pry'
|
||||||
|
require 'sqlite3'
|
||||||
|
|
||||||
|
def memdb
|
||||||
|
SQLite3::Database.new(':memory:').tap do |db|
|
||||||
|
db.class.instance_eval { alias_method :exec, :execute }
|
||||||
|
end
|
||||||
|
end
|
||||||
49
test/test_exec.rb
Normal file
49
test/test_exec.rb
Normal file
|
|
@ -0,0 +1,49 @@
|
||||||
|
require 'minitest/autorun'
|
||||||
|
require 'helper'
|
||||||
|
require 'rebel'
|
||||||
|
|
||||||
|
class TestExec < Minitest::Test
|
||||||
|
include Rebel::SQL
|
||||||
|
|
||||||
|
def setup
|
||||||
|
@conn = memdb
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_create_table
|
||||||
|
assert_raises(SQLite3::SQLException) { conn.execute('SELECT * FROM foo') }
|
||||||
|
create_table :foo, id: 'INT', col: 'VARCHAR(255)'
|
||||||
|
assert_equal(conn.execute('SELECT * FROM foo'), [])
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_drop_table
|
||||||
|
create_table :foo, id: 'INT', col: 'VARCHAR(255)'
|
||||||
|
assert_equal(conn.execute('SELECT * FROM foo'), [])
|
||||||
|
drop_table :foo
|
||||||
|
assert_raises(SQLite3::SQLException) { conn.execute('SELECT * FROM foo') }
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_insert_into
|
||||||
|
create_table :foo, id: 'INT', col: 'VARCHAR(255)'
|
||||||
|
insert_into :foo, id: 1, col: 'whatevs'
|
||||||
|
assert_equal(conn.execute('SELECT * FROM foo'), [[1, 'whatevs']])
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_insert_into_with_many_values
|
||||||
|
create_table :foo, id: 'INT', col: 'VARCHAR(255)'
|
||||||
|
insert_into :foo,
|
||||||
|
{ id: 1, col: 'more' },
|
||||||
|
{ id: 2, col: 'rows' },
|
||||||
|
{ id: 3, col: 'for the win' }
|
||||||
|
assert_equal(conn.execute('SELECT * FROM foo'), [
|
||||||
|
[1, 'more'],
|
||||||
|
[2, 'rows'],
|
||||||
|
[3, 'for the win'],
|
||||||
|
])
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_select
|
||||||
|
create_table :foo, id: 'INT', col: 'VARCHAR(255)'
|
||||||
|
insert_into :foo, id: 1, col: 'whatevs'
|
||||||
|
assert_equal(select('*', from: :foo), [[1, 'whatevs']])
|
||||||
|
end
|
||||||
|
end
|
||||||
Loading…
Add table
Add a link
Reference in a new issue