/
acts_as_fu_spec.rb
175 lines (140 loc) · 3.89 KB
/
acts_as_fu_spec.rb
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
require File.dirname(__FILE__) + '/spec_helper'
describe ActsAsFu do
include ActsAsFu
def create_models
build_model(:foos) do
string :name
integer :age
def self.awesome?; true end
end
build_model(:bars) do
integer :foo_id
belongs_to :foo
end
end
describe "build_models" do
it "returns the new klass" do
klass = build_model(:people) do
string :name
end
klass.should == Person
end
end
describe "without building a model" do
it "asplodes" do
proc {
Foo
}.should raise_error
end
end
describe "after building a model" do
before(:each) do
create_models
end
it "creates the class" do
proc {
Foo
}.should_not raise_error
end
it "allows access to class" do
Foo.should be_awesome
end
describe "the class" do
it "is a subclass of ActiveRecord::Base" do
Foo.superclass.should == ActsAsFu::Connection
end
it "has specified attributes" do
foo = Foo.create! :name => "The WHIZ", :age => 100
foo.name.should == "The WHIZ"
foo.age.should == 100
end
it "is really real" do
Foo.validates_presence_of :name
proc {
Foo.create! :age => 100
}.should raise_error(ActiveRecord::RecordInvalid)
end
end
describe "rebuilding the class" do
it "clears the table" do
create_models
5.times { Foo.create!(:name => "The WHIZ", :age => 100) }
Foo.count.should == 5
create_models
Foo.count.should == 0
end
it "resets the class" do
create_models
class << Foo; attr_reader :bar end
proc { Foo.bar }.should_not raise_error
create_models
proc { Foo.bar }.should raise_error(NoMethodError)
end
it "resets the attributes" do
build_model :fakers do
string :bar
end
proc { Faker.new :bar => "bar" }.should_not raise_error
proc { Faker.new :foo => "foo" }.should raise_error
build_model :fakers do
string :foo
end
proc { Faker.new :foo => "foo" }.should_not raise_error
proc { Faker.new :bar => "bar" }.should raise_error
end
end
describe "single table inheritance" do
it "allows superclass to be specified" do
build_model(:assets) do
string :type
string :name
if ActiveRecord::VERSION::MAJOR < 3
named_scope :pictures, :conditions => { :type => "Picture" }
else
scope :pictures, :conditions => { :type => "Picture" }
end
end
build_model(:pictures, :superclass => Asset)
proc {
Picture.create!
}.should change(Asset.pictures, :count)
end
end
describe "nested class" do
it "is contained in a class" do
class Foo::SubThing; end
build_model(:sub_things, :contained => Foo) do
string :name
end
proc {
Foo::SubThing.create :name => "foo"
}.should change(Foo::SubThing, :count)
end
end
end
describe "ActsAsFu.report!" do
it "has a log" do
create_models
ActsAsFu::Connection.log.should include("CREATE TABLE")
end
end
describe "custom DB config" do
attr_reader :db
before(:each) do
system("rm -rf #{db}")
@db = "#{File.dirname(__FILE__)}/tmp.sqlite3"
end
it "allows connection to custom DB config" do
ActsAsFu::Connection.connect! \
:adapter => 'sqlite3',
:database => db
build_model(:others) do
string :body
end
File.exists?(db).should be_true
end
after(:each) do
system("rm -rf #{db}")
end
end
end