Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 96 lines (79 sloc) 2.25 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
require 'spec_helper'
require 'minisql/database'
require 'minisql/record'

describe MiniSQL::Record do

  include SpecHelperMethods

  before :each do
    init_db
    create_sample_table
    @catalog = @db.catalog
    @buffer = new_buffer "#{tmp_file}/buffer"
    @record = MiniSQL::Record.new(@catalog.table_info(:tbl), @buffer,
      lambda do |new_table_info|
        @catalog.drop_table :tbl
        @catalog.create_table new_table_info
      end)
  end

  after :each do
    @buffer.close
    clean_db
  end

  it 'can be initialized and closed' do
  end

  it 'can serialize and deserialize item' do
    serialized = @record.serialize(sample_data)
    serialized.size.should == @record.record_size

    deserialized = @record.deserialize(serialized)
    record_equal deserialized
  end

  it 'can insert and retrieve records' do
    insert_records
    (0...100).to_a.shuffle.each do |i|
      data = sample_data
      data[0] = i
      record_equal @record.read_record(i), data
    end
  end

  it 'can delete records' do
    insert_records
    @record.delete_record 7
    @record.size.should == 99
  end

  it 'can delete records' do
    insert_records
    func = lambda {|i| i[0]%3==0}
    @record.delete_records func
    @record.select_records(func).size.should == 0
    @record.size.should == 66
  end

  it 'can select records' do
    insert_records
    meta_func = lambda {|i| lambda{|x| x[0]%3==i}}
    @record.select_records(meta_func[0]).size.should == 34
    @record.select_records(meta_func[1]).size.should == 33
    @record.select_records(meta_func[2]).size.should == 33
  end

  it 'can update record' do
    insert_records
    @record.update_record(7,sample_data)
    record_equal @record.read_record(7), sample_data
  end

  it 'can update records' do
    insert_records
    func = lambda {|i| i[0]%3==0}
    @record.update_records func, lambda{|i| i[0]=9875321; i}
    @record.size.should == 100
    @record.select_records(func).should == []
    @record.select_records(lambda{|i| i[0]==9875321}).size.should == 34
  end

  def insert_records data_gen=lambda{|x| x}
    (0...100).each do |i|
      data = sample_data
      data[0] = data_gen[i]
      @record.insert_record data
    end
  end

  def block_size
    123
  end

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