Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: 39037afba8
Fetching contributors…

Cannot retrieve contributors at this time

file 103 lines (84 sloc) 2.274 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
require 'spec_helper'
require 'minisql'

describe MiniSQL::Database do

  include SpecHelperMethods

  before :each do
    init_db
  end

  after :each do
    clean_db
  end

  it 'can be initialized and closed' do
  end

  it 'can run SQL' do
    @db.eval do
      tables.should == []
      execute 'create table tbl (id int, primary key (id) );'
      tables.should == [:tbl]
      execute 'drop table tbl;'
      tables.should == []
    end
  end

  it 'can create table' do
    create_sample_table
  end

  it 'can get table list' do
    insert_sample_data
    @db.tables.should == [:tbl]
  end

  it 'can drop table' do
    create_sample_table
    @db.eval do
      drop_table :tbl
      tables.should == []
    end
  end

  #XXX: why rspec don't let me use @db.eval and 'should include' in the same time?
  it 'can select * from table' do
    create_sample_table
    @db.select['*'].from(:tbl).to_a.should == []
    @db.select['*'].from(@db.meta_table).to_a[0].should include('tbl')
  end

  it 'can select columns from table' do
    create_sample_table
    @db.select[:int_col, :float_col].from(:tbl).to_a.should == []
    @db.select[:type, :name].from(@db.meta_table).to_a.should include(['table', 'tbl'])
  end

  it 'can select from table where ...' do
    create_sample_table

    @db.select['*'].from(:tbl).where do
      column[:int_col] < 0
      column[:float_col] > 1
      column[:char_col] != 'abc'
    end.to_a.should == []

    @db.select[:type, :name].from(@db.meta_table).where do
      column[:type] == 'table'
      column[:name] == 'tbl'
    end.to_a.should == [['table', 'tbl']]
  end

  it 'can create and drop index on table' do
    create_sample_table
    @db.eval do
      create_index :char_index, :tbl, :char_col
      drop_index :char_index
    end
  end

  it 'can insert values into table' do
    insert_sample_data
  end

  it 'can delete all data from table' do
    insert_sample_data
    @db.eval do
      delete_from :tbl
      select['*'].from(:tbl).to_a.should == []
    end
  end

  it 'can delete specified data from table' do
    insert_sample_data
    insert_sample_data [0, 1.0, 'another one']
    @db.delete_from :tbl do
      column[:int_col] < 1
    end
    @db.select['*'].from(:tbl).to_a.should == [sample_data]
  end

end
Something went wrong with that request. Please try again.