benhoskings-ambitious-activerecord 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,43 @@
1
+ require File.dirname(__FILE__) + '/helper'
2
+
3
+ context "ActiveRecord Adapter" do
4
+ context "Slice" do
5
+ setup do
6
+ @sql = User.select { |m| m.name == 'jon' }
7
+ end
8
+
9
+ specify "first" do
10
+ conditions = { :conditions => "users.name = 'jon'", :limit => 1 }
11
+ User.expects(:find).with(:all, conditions)
12
+ @sql.first
13
+ end
14
+
15
+ specify "first with argument" do
16
+ conditions = { :conditions => "users.name = 'jon'", :limit => 5 }
17
+ User.expects(:find).with(:all, conditions)
18
+ @sql.first(5).entries
19
+ end
20
+
21
+ specify "[] with two elements" do
22
+ conditions = { :conditions => "users.name = 'jon'", :limit => 20, :offset => 10 }
23
+ User.expects(:find).with(:all, conditions)
24
+ @sql[10, 20].entries
25
+
26
+ conditions = { :conditions => "users.name = 'jon'", :limit => 20, :offset => 20 }
27
+ User.expects(:find).with(:all, conditions)
28
+ @sql[20, 20].entries
29
+ end
30
+
31
+ specify "slice is an alias of []" do
32
+ conditions = { :conditions => "users.name = 'jon'", :limit => 20, :offset => 10 }
33
+ User.expects(:find).with(:all, conditions)
34
+ @sql.slice(10, 20).entries
35
+ end
36
+
37
+ specify "[] with range" do
38
+ conditions = { :conditions => "users.name = 'jon'", :limit => 10, :offset => 10 }
39
+ User.expects(:find).with(:all, conditions)
40
+ @sql[11..20].entries
41
+ end
42
+ end
43
+ end
data/test/sort_test.rb ADDED
@@ -0,0 +1,54 @@
1
+ require File.dirname(__FILE__) + '/helper'
2
+
3
+ context "ActiveRecord Adapter" do
4
+ context "Sort" do
5
+ setup do
6
+ @sql = User.select { |m| m.name == 'jon' }
7
+ end
8
+
9
+ specify "simple order" do
10
+ string = @sql.sort_by { |m| m.name }.to_s
11
+ string.should == "SELECT * FROM users WHERE users.name = 'jon' ORDER BY users.name"
12
+ end
13
+
14
+ specify "simple combined order" do
15
+ string = @sql.sort_by { |m| [ m.name, m.age ] }.to_s
16
+ string.should == "SELECT * FROM users WHERE users.name = 'jon' ORDER BY users.name, users.age"
17
+ end
18
+
19
+ specify "simple combined order with single reverse" do
20
+ string = @sql.sort_by { |m| [ m.name, -m.age ] }.to_s
21
+ string.should == "SELECT * FROM users WHERE users.name = 'jon' ORDER BY users.name, users.age DESC"
22
+ end
23
+
24
+ specify "simple combined order with two reverses" do
25
+ string = @sql.sort_by { |m| [ -m.name, -m.age ] }.to_s
26
+ string.should == "SELECT * FROM users WHERE users.name = 'jon' ORDER BY users.name DESC, users.age DESC"
27
+ end
28
+
29
+ specify "reverse order with -" do
30
+ string = @sql.sort_by { |m| -m.age }.to_s
31
+ string.should == "SELECT * FROM users WHERE users.name = 'jon' ORDER BY users.age DESC"
32
+ end
33
+
34
+ xspecify "reverse order with #reverse" do
35
+ # TODO: not implemented
36
+ string = @sql.sort_by { |m| m.age }.reverse.to_s
37
+ string.should == "SELECT * FROM users WHERE users.name = 'jon' ORDER BY users.age DESC"
38
+ end
39
+
40
+ specify "random order" do
41
+ string = @sql.sort_by { rand }.to_s
42
+ string.should == "SELECT * FROM users WHERE users.name = 'jon' ORDER BY RAND()"
43
+ end
44
+
45
+ specify "non-existent method to sort by" do
46
+ should.raise(NoMethodError) { @sql.sort_by { foo }.to_s }
47
+ end
48
+
49
+ specify "Symbol#to_proc" do
50
+ string = User.sort_by(&:name).to_s
51
+ string.should == "SELECT * FROM users ORDER BY users.name"
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,43 @@
1
+ require File.dirname(__FILE__) + '/helper'
2
+ require 'ostruct'
3
+
4
+ context "Setting the ambition_source" do
5
+ setup do
6
+ # @users = [
7
+ # OpenStruct.new(:name => 'Chris', :age => 22),
8
+ # OpenStruct.new(:name => 'PJ', :age => 24),
9
+ # OpenStruct.new(:name => 'Kevin', :age => 23),
10
+ # OpenStruct.new(:name => '_why', :age => 65)
11
+ # ]
12
+ # User.ambition_source = @users
13
+ end
14
+
15
+ teardown do
16
+ # User.ambition_source = nil
17
+ end
18
+
19
+ xspecify "should run all selects / detects against that collection" do
20
+ User.detect { |u| u.name == 'Chris' }.should == @users.first
21
+ end
22
+
23
+ xspecify "should run all sorts against that collection" do
24
+ User.sort_by { |u| -u.age }.entries.should == @users.sort_by { |u| -u.age }
25
+ end
26
+
27
+ xspecify "should chain successfully" do
28
+ User.select { |u| u.age > 22 }.sort_by { |u| -u.age }.entries.should == [ @users[3], @users[1], @users[2] ]
29
+ end
30
+
31
+ xspecify "should be able to revert to normal" do
32
+ block = proc { User.select { |m| m.name == 'PJ' }.first }
33
+
34
+ User.expects(:find).never
35
+ block.call.should == @users[1]
36
+
37
+ conditions = { :conditions => "users.name = 'PJ'", :limit => 1 }
38
+ User.expects(:find).with(:first, conditions)
39
+
40
+ User.ambition_source = nil
41
+ block.call
42
+ end
43
+ end
@@ -0,0 +1,69 @@
1
+ require File.dirname(__FILE__) + '/helper'
2
+
3
+ ##
4
+ # Once dynamically, once hardcoded
5
+ context "Different types" do
6
+ types_hash = {
7
+ 'string' => "'string'",
8
+ :symbol => "'--- :symbol\n'",
9
+ 1 => '1',
10
+ 1.2 => '1.2',
11
+ true => '1',
12
+ false => '0',
13
+ Time.now => "'#{Time.now.to_s(:db)}'",
14
+ DateTime.now => "'#{DateTime.now.to_s(:db)}'",
15
+ Date.today => "'#{Date.today.to_s(:db)}'"
16
+ }
17
+
18
+ types_hash.each do |type, translation|
19
+ specify "simple using #{type}" do
20
+ sql = User.select { |m| m.name == type }.to_s
21
+ sql.should == "SELECT * FROM users WHERE users.name = #{translation}"
22
+ end
23
+ end
24
+
25
+ specify "float" do
26
+ sql = User.select { |m| m.name == 1.2 }.to_s
27
+ sql.should == "SELECT * FROM users WHERE users.name = 1.2"
28
+ end
29
+
30
+ specify "integer" do
31
+ sql = User.select { |m| m.name == 1 }.to_s
32
+ sql.should == "SELECT * FROM users WHERE users.name = 1"
33
+ end
34
+
35
+ specify "true" do
36
+ sql = User.select { |m| m.name == true }.to_s
37
+ sql.should == "SELECT * FROM users WHERE users.name = 1"
38
+ end
39
+
40
+ specify "false" do
41
+ sql = User.select { |m| m.name == false }.to_s
42
+ sql.should == "SELECT * FROM users WHERE users.name = 0"
43
+ end
44
+
45
+ specify "nil" do
46
+ sql = User.select { |m| m.name == nil }.to_s
47
+ sql.should == "SELECT * FROM users WHERE users.name IS NULL"
48
+ end
49
+
50
+ specify "not nil" do
51
+ sql = User.select { |m| m.name != nil }.to_s
52
+ sql.should == "SELECT * FROM users WHERE users.name IS NOT NULL"
53
+ end
54
+
55
+ specify "nil?" do
56
+ sql = User.select { |m| m.name.nil? }.to_s
57
+ sql.should == "SELECT * FROM users WHERE users.name IS NULL"
58
+ end
59
+
60
+ specify "!nil?" do
61
+ sql = User.select { |m| !m.name.nil? }.to_s
62
+ sql.should == "SELECT * FROM users WHERE users.name IS NOT NULL"
63
+ end
64
+
65
+ specify "Time" do
66
+ sql = User.select { |m| m.name == Time.now }.to_s
67
+ sql.should == "SELECT * FROM users WHERE users.name = '#{Time.now.to_s(:db)}'"
68
+ end
69
+ end
metadata ADDED
@@ -0,0 +1,113 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: benhoskings-ambitious-activerecord
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.3
5
+ platform: ruby
6
+ authors:
7
+ - Chris Wanstrath
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2008-04-26 00:00:00 -07:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: activerecord
17
+ type: :runtime
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: 1.15.6
24
+ version:
25
+ - !ruby/object:Gem::Dependency
26
+ name: ambition
27
+ type: :runtime
28
+ version_requirement:
29
+ version_requirements: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: 0.5.3
34
+ version:
35
+ description: An ambitious adapter for ActiveRecord
36
+ email: chris@ozmm.org
37
+ executables: []
38
+
39
+ extensions: []
40
+
41
+ extra_rdoc_files:
42
+ - lib/ambition/adapters/active_record/base.rb
43
+ - lib/ambition/adapters/active_record/query.rb
44
+ - lib/ambition/adapters/active_record/select.rb
45
+ - lib/ambition/adapters/active_record/slice.rb
46
+ - lib/ambition/adapters/active_record/sort.rb
47
+ - lib/ambition/adapters/active_record/statements.rb
48
+ - lib/ambition/adapters/active_record.rb
49
+ files:
50
+ - lib/ambition/adapters/active_record/base.rb
51
+ - lib/ambition/adapters/active_record/query.rb
52
+ - lib/ambition/adapters/active_record/select.rb
53
+ - lib/ambition/adapters/active_record/slice.rb
54
+ - lib/ambition/adapters/active_record/sort.rb
55
+ - lib/ambition/adapters/active_record/statements.rb
56
+ - lib/ambition/adapters/active_record.rb
57
+ - test/benchmark.rb
58
+ - test/chaining_test.rb
59
+ - test/count_test.rb
60
+ - test/enumerable_test.rb
61
+ - test/helper.rb
62
+ - test/join_test.rb
63
+ - test/profiler.rb
64
+ - test/ruby_test.rb
65
+ - test/select_test.rb
66
+ - test/slice_test.rb
67
+ - test/sort_test.rb
68
+ - test/source_test.rb
69
+ - test/types_test.rb
70
+ - Manifest
71
+ - ambitious-activerecord.gemspec
72
+ has_rdoc: true
73
+ homepage: http://ambition.rubyforge.org/
74
+ post_install_message:
75
+ rdoc_options:
76
+ - --line-numbers
77
+ - --inline-source
78
+ - --title
79
+ - Ambitious-activerecord
80
+ - --main
81
+ - README
82
+ require_paths:
83
+ - lib
84
+ required_ruby_version: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - ">="
87
+ - !ruby/object:Gem::Version
88
+ version: "0"
89
+ version:
90
+ required_rubygems_version: !ruby/object:Gem::Requirement
91
+ requirements:
92
+ - - ">="
93
+ - !ruby/object:Gem::Version
94
+ version: "0"
95
+ version:
96
+ requirements: []
97
+
98
+ rubyforge_project: ambition
99
+ rubygems_version: 1.2.0
100
+ signing_key:
101
+ specification_version: 2
102
+ summary: An ambitious adapter for ActiveRecord
103
+ test_files:
104
+ - test/chaining_test.rb
105
+ - test/count_test.rb
106
+ - test/enumerable_test.rb
107
+ - test/join_test.rb
108
+ - test/ruby_test.rb
109
+ - test/select_test.rb
110
+ - test/slice_test.rb
111
+ - test/sort_test.rb
112
+ - test/source_test.rb
113
+ - test/types_test.rb