ribs 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,25 +1,27 @@
1
1
  require File.join(File.dirname(__FILE__), 'test_helper')
2
2
 
3
3
  class Artist
4
- Ribs!
4
+ Ribs! :identity_map => false
5
+ attr_accessor :id
6
+ attr_accessor :name
5
7
  end
6
8
 
7
9
  describe Artist do
8
10
  it "should be able to find all artists" do
9
- Artist.find(:all).length.should == 3
11
+ R(Artist).all.length.should == 3
10
12
  end
11
13
 
12
14
  it "should return correct value and type for id property" do
13
- Artist.find(:all).map { |a| a.id }.sort.should == [1,2,3]
15
+ R(Artist).all.map { |a| a.id }.sort.should == [1,2,3]
14
16
  end
15
17
 
16
18
  it "should return correct value and type for name property" do
17
- Artist.find(:all).map { |a| a.name }.sort.should == ["David Bowie","New Model Army","Public Image Ltd"]
19
+ R(Artist).all.map { |a| a.name }.sort.should == ["David Bowie","New Model Army","Public Image Ltd"]
18
20
  end
19
21
 
20
22
  it "should only have the appropriate methods defined" do
21
23
  methods = (Artist.instance_methods - Object.instance_methods).sort
22
- methods.should == ['__ribs_meat', 'destroy!', 'id=', 'name', 'name=', 'save']
24
+ methods.should == ['id=', 'name', 'name=']
23
25
  end
24
26
 
25
27
  it "should be possible to create a new instance by setting properties" do
@@ -27,8 +29,8 @@ describe Artist do
27
29
  artist = Artist.new
28
30
  artist.id = 4
29
31
  artist.name = "Assemblage 23"
30
- artist.save
31
- artist2 = Artist.find(4)
32
+ R(artist).save
33
+ artist2 = R(Artist).get(4)
32
34
  artist2.should_not be_nil
33
35
  artist2.id.should == 4
34
36
  artist2.name.should == "Assemblage 23"
@@ -39,9 +41,9 @@ describe Artist do
39
41
 
40
42
  it "should be possible to create a new instance by giving properties to new" do
41
43
  begin
42
- artist = Artist.new :id => 4, :name => "Assemblage 23"
43
- artist.save
44
- artist2 = Artist.find(4)
44
+ artist = R(Artist).new :id => 4, :name => "Assemblage 23"
45
+ R(artist).save
46
+ artist2 = R(Artist).get(4)
45
47
  artist2.should_not be_nil
46
48
  artist2.id.should == 4
47
49
  artist2.name.should == "Assemblage 23"
@@ -52,8 +54,8 @@ describe Artist do
52
54
 
53
55
  it "should be possible to create a new instance by using create" do
54
56
  begin
55
- Artist.create :id => 4, :name => "Assemblage 23"
56
- artist = Artist.find(4)
57
+ R(Artist).create :id => 4, :name => "Assemblage 23"
58
+ artist = R(Artist).get(4)
57
59
  artist.should_not be_nil
58
60
  artist.id.should == 4
59
61
  artist.name.should == "Assemblage 23"
@@ -64,11 +66,11 @@ describe Artist do
64
66
 
65
67
  it "should be possible to update name property on existing bean" do
66
68
  begin
67
- artist = Artist.find(2)
69
+ artist = R(Artist).get(2)
68
70
  artist.name = "U2"
69
- Artist.find(2).name.should == "New Model Army"
70
- artist.save
71
- Artist.find(2).name.should == "U2"
71
+ R(Artist).get(2).name.should == "New Model Army"
72
+ R(artist).save
73
+ R(Artist).get(2).name.should == "U2"
72
74
  ensure
73
75
  reset_database!
74
76
  end
@@ -76,8 +78,8 @@ describe Artist do
76
78
 
77
79
  it "should be possible to delete existing bean" do
78
80
  begin
79
- Artist.find(2).destroy!
80
- Artist.find(2).should be_nil
81
+ R(R(Artist).get(2)).destroy!
82
+ R(Artist).get(2).should be_nil
81
83
  ensure
82
84
  reset_database!
83
85
  end
@@ -85,8 +87,8 @@ describe Artist do
85
87
 
86
88
  it "should be possible to delete existing bean by id" do
87
89
  begin
88
- Artist.destroy(2)
89
- Artist.find(2).should be_nil
90
+ R(Artist).destroy(2)
91
+ R(Artist).get(2).should be_nil
90
92
  ensure
91
93
  reset_database!
92
94
  end
@@ -0,0 +1,27 @@
1
+ require File.join(File.dirname(__FILE__), 'test_helper')
2
+
3
+ class Person;end
4
+
5
+ R(Person).define_accessors
6
+
7
+ describe "define_accessors" do
8
+ it "should define id accessors" do
9
+ Person.instance_methods.should include("id")
10
+ Person.instance_methods.should include("id=")
11
+ end
12
+
13
+ it "should define given_name accessors" do
14
+ Person.instance_methods.should include("given_name")
15
+ Person.instance_methods.should include("given_name=")
16
+ end
17
+
18
+ it "should define sur_name accessors" do
19
+ Person.instance_methods.should include("sur_name")
20
+ Person.instance_methods.should include("sur_name=")
21
+ end
22
+
23
+ it "should define age accessors" do
24
+ Person.instance_methods.should include("age")
25
+ Person.instance_methods.should include("age=")
26
+ end
27
+ end
@@ -0,0 +1,41 @@
1
+ require File.join(File.dirname(__FILE__), 'test_helper')
2
+
3
+ Ribs::define_model :Address do |rib|
4
+ rib.zip_code :column => :zip
5
+ end
6
+
7
+ describe "define_model" do
8
+ it "should create a class" do
9
+ Address.class.should == Class
10
+ end
11
+
12
+ it "should create all the needed properties" do
13
+ Address.instance_methods.should include("id")
14
+ Address.instance_methods.should include("id=")
15
+ Address.instance_methods.should include("street")
16
+ Address.instance_methods.should include("street=")
17
+ Address.instance_methods.should include("postal")
18
+ Address.instance_methods.should include("postal=")
19
+ Address.instance_methods.should include("zip_code")
20
+ Address.instance_methods.should include("zip_code=")
21
+ Address.instance_methods.should_not include("zip")
22
+ Address.instance_methods.should_not include("zip=")
23
+ Address.instance_methods.should include("country")
24
+ Address.instance_methods.should include("country=")
25
+ end
26
+
27
+ it "should be a working Ribs class" do
28
+ R(Address).create :id => 1, :street => "foobar 42"
29
+ all = R(Address).all
30
+ all.length.should == 1
31
+ all[0].id.should == 1
32
+ all[0].street.should == "foobar 42"
33
+ all[0].zip_code.should be_nil
34
+ R(Address).get(1).street.should == all[0].street
35
+ all[0].street = "foobar 43"
36
+ R(all[0]).save
37
+ R(Address).get(1).street.should == "foobar 43"
38
+ R(Address).destroy(1)
39
+ R(Address).all.should be_empty
40
+ end
41
+ end
@@ -0,0 +1,63 @@
1
+ require File.join(File.dirname(__FILE__), 'test_helper')
2
+
3
+ class IdentityMapAddress
4
+ Ribs! :table => :address
5
+ end
6
+
7
+ R(IdentityMapAddress).define_accessors
8
+
9
+ class IdentityMapPerson
10
+ Ribs! :identity_map => false, :table => :person
11
+ end
12
+
13
+ R(IdentityMapPerson).define_accessors
14
+
15
+ describe "Identity map" do
16
+ describe "(disabled)" do
17
+ before :all do
18
+ R(IdentityMapPerson).create :id => 1, :sur_name => "DeWhite", :age => 23
19
+ end
20
+
21
+ after :all do
22
+ R(IdentityMapPerson).destroy 1
23
+ end
24
+
25
+ it "should return different objects with get" do
26
+ R(IdentityMapPerson).get(1).should_not == R(IdentityMapPerson).get(1)
27
+ end
28
+ it "should return different objects with all" do
29
+ R(IdentityMapPerson).all[0].should_not == R(IdentityMapPerson).all[0]
30
+ end
31
+ it "should return different objects with a combination of get and all" do
32
+ R(IdentityMapPerson).all[0].should_not == R(IdentityMapPerson).get(1)
33
+ end
34
+ end
35
+
36
+ describe "(enabled)" do
37
+ before :each do
38
+ @@id ||= 0
39
+ @@id += 1
40
+ R(IdentityMapAddress).create :id => @@id
41
+ end
42
+
43
+ after :each do
44
+ R(IdentityMapAddress).destroy @@id
45
+ end
46
+
47
+ it "should return same objects with get" do
48
+ p [:BLARG, @@id]
49
+ R(IdentityMapAddress).get(@@id).should == R(IdentityMapAddress).get(@@id)
50
+ end
51
+ it "should return same objects with all" do
52
+ R(IdentityMapAddress).all[0].should == R(IdentityMapAddress).all[0]
53
+ end
54
+ it "should return same objects with a combination of get -> all" do
55
+ obj = R(IdentityMapAddress).all[0]
56
+ obj.should == R(IdentityMapAddress).get(@@id)
57
+ end
58
+ it "should return same objects with a combination of all -> get" do
59
+ obj = R(IdentityMapAddress).get(@@id)
60
+ R(IdentityMapAddress).all[0].should == obj
61
+ end
62
+ end
63
+ end
@@ -0,0 +1,79 @@
1
+ require File.join(File.dirname(__FILE__), 'test_helper')
2
+
3
+ class FakeModel;end
4
+ class FakeModel::FakeSecondModel;end
5
+
6
+ describe Ribs::Repository do
7
+ before :all do
8
+ R(FakeModel)
9
+ R(FakeModel::FakeSecondModel)
10
+
11
+ R(FakeModel, :flarg)
12
+ R(FakeModel::FakeSecondModel, :flurg)
13
+ end
14
+
15
+ it 'should create a Repository class for a model with default db' do
16
+ Ribs::Repository::DB_default.constants.should include("FakeModel")
17
+ Ribs::Repository::DB_default::FakeModel.class.should == Class
18
+ Ribs::Repository::DB_default::FakeModel.ancestors.should include(Ribs::Repository)
19
+ Ribs::Repository::DB_default::FakeModel.ancestors.should include(Ribs::Repository::InstanceMethods)
20
+ Ribs::Repository::DB_default::FakeModel.ancestors.should include(Ribs::Repository::FakeModel)
21
+ (class << Ribs::Repository::DB_default::FakeModel; self; end).ancestors.should include(Ribs::Repository)
22
+ (class << Ribs::Repository::DB_default::FakeModel; self; end).ancestors.should include(Ribs::Repository::ClassMethods)
23
+ (class << Ribs::Repository::DB_default::FakeModel; self; end).ancestors.should include(Ribs::Repository::FakeModel::ClassMethods)
24
+ end
25
+
26
+ it 'should create a Repository class for a model with another db' do
27
+ Ribs::Repository::DB_flarg.constants.should include("FakeModel")
28
+ Ribs::Repository::DB_flarg::FakeModel.class.should == Class
29
+ Ribs::Repository::DB_flarg::FakeModel.ancestors.should include(Ribs::Repository)
30
+ Ribs::Repository::DB_flarg::FakeModel.ancestors.should include(Ribs::Repository::InstanceMethods)
31
+ Ribs::Repository::DB_flarg::FakeModel.ancestors.should include(Ribs::Repository::FakeModel)
32
+ (class << Ribs::Repository::DB_flarg::FakeModel; self; end).ancestors.should include(Ribs::Repository)
33
+ (class << Ribs::Repository::DB_flarg::FakeModel; self; end).ancestors.should include(Ribs::Repository::ClassMethods)
34
+ (class << Ribs::Repository::DB_flarg::FakeModel; self; end).ancestors.should include(Ribs::Repository::FakeModel::ClassMethods)
35
+ end
36
+
37
+ it 'should create a DB place the first time asked for it' do
38
+ Ribs::Repository::DB_flux
39
+ Ribs::Repository::DB_flux.class.should == Module
40
+ end
41
+
42
+ it 'should only create a db if prefix with the DB_' do
43
+ proc do
44
+ Ribs::Repository::Noxic
45
+ end.should raise_error(NameError)
46
+ end
47
+
48
+ it 'should return a Repository class for a model' do
49
+ R(FakeModel).should == Ribs::Repository::DB_default::FakeModel
50
+ R(FakeModel).model.should == ::FakeModel
51
+ R(FakeModel).database.should == :default
52
+ end
53
+
54
+ it 'should return an instance of the Repository class for a model instance' do
55
+ ff = FakeModel.new
56
+ R(ff).should be_kind_of(Ribs::Repository::DB_default::FakeModel)
57
+ R(ff).model.should == ff
58
+ R(ff).database.should == :default
59
+ end
60
+
61
+ it 'should return a Repository class for a model with another db' do
62
+ R(FakeModel, :flarg).should == Ribs::Repository::DB_flarg::FakeModel
63
+ R(FakeModel, :flarg).model.should == ::FakeModel
64
+ R(FakeModel, :flarg).database.should == :flarg
65
+ end
66
+
67
+ it 'should return an instance of the Repository class for a model instance with another db' do
68
+ ff = FakeModel.new
69
+ R(ff, :flarg).should be_kind_of(Ribs::Repository::DB_flarg::FakeModel)
70
+ R(ff, :flarg).model.should == ff
71
+ R(ff, :flarg).database.should == :flarg
72
+ end
73
+
74
+ it 'should handle submodules correctly' do
75
+ R(FakeModel::FakeSecondModel).should == Ribs::Repository::DB_default::FakeModel_FakeSecondModel
76
+ R(FakeModel::FakeSecondModel).model.should == ::FakeModel::FakeSecondModel
77
+ R(FakeModel::FakeSecondModel).database.should == :default
78
+ end
79
+ end
@@ -0,0 +1,73 @@
1
+ require File.join(File.dirname(__FILE__), 'test_helper')
2
+
3
+ describe Ribs::Rib do
4
+ it "should not have any methods defined except for method_missing" do
5
+ Ribs::Rib.instance_methods.sort.should ==
6
+ %w(Ribs! __send__ __id__ rspec_reset
7
+ rspec_verify should_receive
8
+ should_not_receive R received_message?
9
+ stub! __column_data__ method_missing).sort
10
+ end
11
+
12
+ it "should be possible to define a primary key with a method call" do
13
+ r = Ribs::Rib.new
14
+ r.track_id.primary_key!
15
+ r.__column_data__.primary_keys.should == %w(track_id)
16
+ end
17
+
18
+ it "should be possible to define a primary key with a hash" do
19
+ r = Ribs::Rib.new
20
+ r.track_id :primary_key => true
21
+ r.__column_data__.primary_keys.should == %w(track_id)
22
+ end
23
+
24
+ it "should be possible to define a primary key with a simple symbol" do
25
+ r = Ribs::Rib.new
26
+ r.track_id :primary_key
27
+ r.__column_data__.primary_keys.should == %w(track_id)
28
+ end
29
+
30
+ it "should be possible to define more than one primary key" do
31
+ r = Ribs::Rib.new
32
+ r.track_id.primary_key!
33
+ r.fox_id.primary_key!
34
+ r.__column_data__.primary_keys.should == %w(track_id fox_id)
35
+ end
36
+
37
+ it "should handle methods with capital letters" do
38
+ r = Ribs::Rib.new
39
+ r.TRACK_ID.primary_key!
40
+ r.__column_data__.primary_keys.should == %w(TRACK_ID)
41
+ end
42
+
43
+ it "should be possible to define a simple columns mapping with a hash" do
44
+ r = Ribs::Rib.new
45
+ r.track_id :column => :TRUCK_ID
46
+ r.__column_data__.columns.should == { 'track_id' => ['TRUCK_ID', {:column => :TRUCK_ID}]}
47
+ end
48
+
49
+ it "should be possible to define a simple columns mapping with a method call" do
50
+ r = Ribs::Rib.new
51
+ r.track_id.column = :TRUCK_ID
52
+ r.__column_data__.columns.should == { 'track_id' => ['TRUCK_ID', {}]}
53
+ end
54
+
55
+ it "should be possible to define something to avoid with a method call" do
56
+ r = Ribs::Rib.new
57
+ r.track_id.avoid!
58
+ r.TRUCK_ID.avoid!
59
+ r.__column_data__.to_avoid.should == %w(track_id truck_id)
60
+ end
61
+
62
+ it "should be possible to define something to avoid with a hash" do
63
+ r = Ribs::Rib.new
64
+ r.track_id :avoid => true
65
+ r.__column_data__.to_avoid.should == %w(track_id)
66
+ end
67
+
68
+ it "should be possible to define something to avoid with a simple symbol" do
69
+ r = Ribs::Rib.new
70
+ r.track_id :avoid
71
+ r.__column_data__.to_avoid.should == %w(track_id)
72
+ end
73
+ end
@@ -2,38 +2,38 @@ require File.join(File.dirname(__FILE__), 'test_helper')
2
2
 
3
3
  describe "Simple select" do
4
4
  it "should return correct data for ints" do
5
- Ribs.with_session do |s|
6
- s.select("SELECT TRACK_ID, volume FROM DB_TRACK").should == [[1, 13], [2, 13]]
5
+ Ribs.with_handle do |h|
6
+ h.select("SELECT TRACK_ID, volume FROM DB_TRACK").should == [[1, 13], [2, 13]]
7
7
  end
8
8
  end
9
9
 
10
10
  it "should return correct data for strings" do
11
- Ribs.with_session do |s|
12
- s.select("SELECT title, filePath FROM DB_TRACK").should == [["foobar", "c:/abc/cde/foo.mp3"], ["flux", "d:/abc/cde/flax.mp3"]]
11
+ Ribs.with_handle do |h|
12
+ h.select("SELECT title, filePath FROM DB_TRACK").should == [["foobar", "c:/abc/cde/foo.mp3"], ["flux", "d:/abc/cde/flax.mp3"]]
13
13
  end
14
14
  end
15
15
 
16
16
  it "should return correct data for times" do
17
- Ribs.with_session do |s|
18
- s.select("SELECT playTime FROM DB_TRACK").should == [[Time.time_at(14,50,0)], [Time.time_at(16,23,0)]]
17
+ Ribs.with_handle do |h|
18
+ h.select("SELECT playTime FROM DB_TRACK").should == [[Time.time_at(14,50,0)], [Time.time_at(16,23,0)]]
19
19
  end
20
20
  end
21
21
 
22
22
  it "should return correct data for dates" do
23
- Ribs.with_session do |s|
24
- s.select("SELECT added FROM DB_TRACK").should == [[Time.local(1984, 12, 13, 0, 0, 0)], [Time.local(1983, 12, 13, 0, 0, 0)]]
23
+ Ribs.with_handle do |h|
24
+ h.select("SELECT added FROM DB_TRACK").should == [[Time.local(1984, 12, 13, 0, 0, 0)], [Time.local(1983, 12, 13, 0, 0, 0)]]
25
25
  end
26
26
  end
27
27
 
28
28
  it "should return correct data for timestamp" do
29
- Ribs.with_session do |s|
30
- s.select("SELECT lastPlayed FROM DB_TRACK").should == [[Time.local(1984, 12, 14, 12,3,11)], [Time.local(1982, 5, 3, 13,3,7)]]
29
+ Ribs.with_handle do |h|
30
+ h.select("SELECT lastPlayed FROM DB_TRACK").should == [[Time.local(1984, 12, 14, 12,3,11)], [Time.local(1982, 5, 3, 13,3,7)]]
31
31
  end
32
32
  end
33
33
 
34
34
  it "should return correct data for floats" do
35
- result = Ribs.with_session do |s|
36
- s.select("SELECT fraction FROM DB_TRACK")
35
+ result = Ribs.with_handle do |h|
36
+ h.select("SELECT fraction FROM DB_TRACK")
37
37
  end
38
38
  result[0].length.should == 1
39
39
  result[0][0].should be_close(3.4, 0.00001)
@@ -43,8 +43,8 @@ describe "Simple select" do
43
43
  end
44
44
 
45
45
  it "should return correct data for doubles" do
46
- result = Ribs.with_session do |s|
47
- s.select("SELECT otherFraction FROM DB_TRACK")
46
+ result = Ribs.with_handle do |h|
47
+ h.select("SELECT otherFraction FROM DB_TRACK")
48
48
  end
49
49
  result[0].length.should == 1
50
50
  result[0][0].should be_close(5.7, 0.00001)
@@ -53,27 +53,27 @@ describe "Simple select" do
53
53
  end
54
54
 
55
55
  it "should return correct data for blobs" do
56
- Ribs.with_session do |s|
57
- s.select("SELECT data FROM DB_TRACK").should == [["abc"], ["mumsi"]]
56
+ Ribs.with_handle do |h|
57
+ h.select("SELECT data FROM DB_TRACK").should == [["abc"], ["mumsi"]]
58
58
  end
59
59
  end
60
60
 
61
61
  it "should return correct data for clobs" do
62
- Ribs.with_session do |s|
63
- s.select("SELECT description FROM DB_TRACK").should == [["foobar"], ["maxi"]]
62
+ Ribs.with_handle do |h|
63
+ h.select("SELECT description FROM DB_TRACK").should == [["foobar"], ["maxi"]]
64
64
  end
65
65
  end
66
66
 
67
67
  it "should return correct data for booleans" do
68
- Ribs.with_session do |s|
68
+ Ribs.with_handle do |h|
69
69
  # Not strictly correct - an artifact of a lack in Derby
70
- s.select("SELECT good FROM DB_TRACK").should == [[1], [0]]
70
+ h.select("SELECT good FROM DB_TRACK").should == [[1], [0]]
71
71
  end
72
72
  end
73
73
 
74
74
  it "should return correct data for decimal" do
75
- Ribs.with_session do |s|
76
- s.select("SELECT price FROM DB_TRACK").should == [[BigDecimal.new("13134.11")], [BigDecimal.new("55454.33")]]
75
+ Ribs.with_handle do |h|
76
+ h.select("SELECT price FROM DB_TRACK").should == [[BigDecimal.new("13134.11")], [BigDecimal.new("55454.33")]]
77
77
  end
78
78
  end
79
79
  end