File: schema_spec.rb

package info (click to toggle)
libsequel-ruby 3.13.0-1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 3,720 kB
  • ctags: 3,038
  • sloc: ruby: 43,327; makefile: 8
file content (127 lines) | stat: -rw-r--r-- 3,580 bytes parent folder | download
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
require File.join(File.dirname(File.expand_path(__FILE__)), "spec_helper")

describe Sequel::Model, "dataset & schema" do
  before do
    @model = Class.new(Sequel::Model(:items))
  end 

  specify "sets schema with implicit table name" do
    @model.set_schema do
      primary_key :ssn, :string
    end
    @model.primary_key.should == :ssn
    @model.table_name.should == :items
  end

  specify "sets schema with explicit table name" do
    @model.set_schema :foo do
      primary_key :id
    end
    @model.primary_key.should == :id
    @model.table_name.should == :foo
  end
end

describe Sequel::Model, "table_exists?" do

  before(:each) do
    MODEL_DB.reset
    @model = Class.new(Sequel::Model(:items))
  end

  it "should get the table name and question the model's db if table_exists?" do
    @model.should_receive(:table_name).and_return(:items)
    @model.db.should_receive(:table_exists?)
    @model.table_exists?
  end
end

describe Sequel::Model, "create_table and schema" do

  before(:each) do
    MODEL_DB.reset
    @model = Class.new(Sequel::Model) do
      set_schema(:items) do
        text :name
        float :price, :null => false
      end
    end
  end

  it "should get the create table SQL list from the db and execute it line by line" do
    @model.create_table
    MODEL_DB.sqls.should == ['CREATE TABLE items (name text, price float NOT NULL)']
  end

  it "should reload the schema from the database" do
    schem = {:name=>{:type=>:string}, :price=>{:type=>:float}}
    @model.db.should_receive(:schema).with(:items, :reload=>true).and_return(schem.to_a.sort_by{|x| x[0].to_s})
    @model.create_table
    @model.db_schema.should == schem
    @model.instance_variable_get(:@columns).should == [:name, :price]
  end

  it "should return the schema generator via schema" do
    @model.schema.should be_a_kind_of(Sequel::Schema::Generator)
  end

  it "should use the superclasses schema if it exists" do
    @submodel = Class.new(@model)
    @submodel.schema.should be_a_kind_of(Sequel::Schema::Generator)
  end

  it "should return nil if no schema is present" do
    @model = Class.new(Sequel::Model)
    @model.schema.should == nil
    @submodel = Class.new(@model)
    @submodel.schema.should == nil
  end
end

describe Sequel::Model, "drop_table" do

  before(:each) do
    MODEL_DB.reset
    @model = Class.new(Sequel::Model(:items))
  end

  it "should get the drop table SQL for the associated table and then execute the SQL." do
    @model.should_receive(:table_name).and_return(:items)
    @model.db.should_receive(:drop_table_sql).with(:items)
    @model.db.should_receive(:execute).and_return(:true)
    @model.drop_table
  end

end

describe Sequel::Model, "create_table!" do
  before do
    MODEL_DB.reset
    @model = Class.new(Sequel::Model(:items))
  end
  
  it "should drop table if it exists and then create the table" do
    @model.should_receive(:drop_table).and_return(true)
    @model.should_receive(:create_table).and_return(true)
    @model.create_table!
  end
end

describe Sequel::Model, "create_table?" do
  before do
    MODEL_DB.reset
    @model = Class.new(Sequel::Model(:items))
  end
  
  it "should not create the table if it already exists" do
    @model.should_receive(:table_exists?).and_return(true)
    @model.should_not_receive(:create_table)
    @model.create_table?.should == nil
  end

  it "should create the table if it doesn't exist" do
    @model.should_receive(:table_exists?).and_return(false)
    @model.should_receive(:create_table).and_return(3)
    @model.create_table?.should == 3
  end
end