ohm 0.0.38 → 0.1.0.rc1

Sign up to get free protection for your applications and to get access to all the features.
@@ -40,7 +40,7 @@ class Event < Ohm::Model
40
40
  end
41
41
  end
42
42
 
43
- class TestRedis < Test::Unit::TestCase
43
+ class ModelTest < Test::Unit::TestCase
44
44
  setup do
45
45
  Ohm.flush
46
46
  end
@@ -167,7 +167,7 @@ class TestRedis < Test::Unit::TestCase
167
167
  context "Finding an event" do
168
168
  setup do
169
169
  Ohm.redis.sadd("Event:all", 1)
170
- Ohm.redis.set("Event:1:name", "Concert")
170
+ Ohm.redis.hset("Event:1", "name", "Concert")
171
171
  end
172
172
 
173
173
  should "return an instance of Event" do
@@ -180,7 +180,7 @@ class TestRedis < Test::Unit::TestCase
180
180
  context "Finding a user" do
181
181
  setup do
182
182
  Ohm.redis.sadd("User:all", 1)
183
- Ohm.redis.set("User:1:email", "albert@example.com")
183
+ Ohm.redis.hset("User:1", "email", "albert@example.com")
184
184
  end
185
185
 
186
186
  should "return an instance of User" do
@@ -277,8 +277,8 @@ class TestRedis < Test::Unit::TestCase
277
277
 
278
278
  assert_nil Ohm.redis.get(ModelToBeDeleted.key(id))
279
279
  assert_nil Ohm.redis.get(ModelToBeDeleted.key(id, :name))
280
- assert !Ohm.redis.exists(ModelToBeDeleted.key(id, :foos))
281
- assert !Ohm.redis.exists(ModelToBeDeleted.key(id, :bars))
280
+ assert_equal Array.new, Ohm.redis.smembers(ModelToBeDeleted.key(id, :foos))
281
+ assert_equal Array.new, Ohm.redis.lrange(ModelToBeDeleted.key(id, :bars), 0, -1)
282
282
 
283
283
  assert ModelToBeDeleted.all.empty?
284
284
  end
@@ -293,16 +293,11 @@ class TestRedis < Test::Unit::TestCase
293
293
 
294
294
  Foo.create(:name => "Bar")
295
295
 
296
- assert_equal ["Foo:1:_indices", "Foo:1:name", "Foo:all", "Foo:id", "Foo:name:QmFy"], Ohm.redis.keys("*").sort
296
+ assert_equal ["Foo:1:_indices", "Foo:1", "Foo:all", "Foo:id", "Foo:name:QmFy"].sort, Ohm.redis.keys("*").sort
297
297
 
298
298
  Foo[1].delete
299
299
 
300
- keys = Ohm.redis.keys("*")
301
-
302
- assert !keys.include?("Foo:1:_indices")
303
- assert !keys.include?("Foo:1:name")
304
-
305
- assert_equal "1", Ohm.redis.get("Foo:id")
300
+ assert_equal ["Foo:id"], Ohm.redis.keys("*")
306
301
  end
307
302
  end
308
303
 
@@ -602,6 +597,7 @@ class TestRedis < Test::Unit::TestCase
602
597
 
603
598
  class ::Appointment < Ohm::Model
604
599
  attribute :text
600
+ reference :subscriber, lambda { |id| MyActiveRecordModel.find(id) }
605
601
  end
606
602
 
607
603
  setup do
@@ -620,6 +616,11 @@ class TestRedis < Test::Unit::TestCase
620
616
  assert_equal [MyActiveRecordModel.find(1)], @calendar.subscribers.all
621
617
  end
622
618
 
619
+ should "allow lambdas in references" do
620
+ appointment = Appointment.create(:subscriber => MyActiveRecordModel.find(1))
621
+ assert_equal MyActiveRecordModel.find(1), appointment.subscriber
622
+ end
623
+
623
624
  should "work with models too" do
624
625
  @calendar.appointments.add(Appointment.create(:text => "Meet with Bertrand"))
625
626
 
@@ -697,11 +698,17 @@ class TestRedis < Test::Unit::TestCase
697
698
  should "be able to increment a counter" do
698
699
  @event.incr(:votes)
699
700
  assert_equal 1, @event.votes
701
+
702
+ @event.incr(:votes, 2)
703
+ assert_equal 3, @event.votes
700
704
  end
701
705
 
702
706
  should "be able to decrement a counter" do
703
707
  @event.decr(:votes)
704
708
  assert_equal -1, @event.votes
709
+
710
+ @event.decr(:votes, 2)
711
+ assert_equal -3, @event.votes
705
712
  end
706
713
  end
707
714
 
@@ -910,7 +917,7 @@ class TestRedis < Test::Unit::TestCase
910
917
  end
911
918
 
912
919
  setup do
913
- Car.connect(:port => 6381, :db => 2)
920
+ Car.connect(:port => 6379, :db => 14)
914
921
  end
915
922
 
916
923
  teardown do
@@ -921,8 +928,11 @@ class TestRedis < Test::Unit::TestCase
921
928
  car = Car.create(:name => "Twingo")
922
929
  make = Make.create(:name => "Renault")
923
930
 
924
- assert_equal 1, Make.db.client.instance_variable_get("@db")
925
- assert_equal 2, Car.db.client.instance_variable_get("@db")
931
+ assert_equal ["1"], Redis.new(:db => 15).smembers("Make:all")
932
+ assert_equal [], Redis.new(:db => 15).smembers("Car:all")
933
+
934
+ assert_equal ["1"], Redis.new(:db => 14).smembers("Car:all")
935
+ assert_equal [], Redis.new(:db => 14).smembers("Make:all")
926
936
 
927
937
  assert_equal car, Car[1]
928
938
  assert_equal make, Make[1]
@@ -933,4 +943,20 @@ class TestRedis < Test::Unit::TestCase
933
943
  assert_nil Make[1]
934
944
  end
935
945
  end
946
+
947
+ context "Persistence" do
948
+ should "persist attributes to a hash" do
949
+ event = Event.create(:name => "Redis Meetup")
950
+ event.incr(:votes)
951
+
952
+ assert_equal "hash", Ohm.redis.type("Event:1")
953
+
954
+ assert_equal [
955
+ "Event:1", "Event:all", "Event:id"
956
+ ].sort, Ohm.redis.keys("Event:*").sort
957
+
958
+ assert_equal "Redis Meetup", Event[1].name
959
+ assert_equal 1, Event[1].votes
960
+ end
961
+ end
936
962
  end
@@ -1,6 +1,6 @@
1
1
  dir ./test/db
2
2
  pidfile ./redis.pid
3
- port 6381
3
+ port 6379
4
4
  timeout 300
5
5
  loglevel debug
6
6
  logfile stdout
@@ -1,4 +1,4 @@
1
- require "rubygems"
1
+ $LOAD_PATH.unshift(File.expand_path(File.dirname(__FILE__) + "/../lib"))
2
2
 
3
3
  begin
4
4
  require "ruby-debug"
@@ -6,7 +6,7 @@ rescue LoadError
6
6
  end
7
7
 
8
8
  require "contest"
9
- require File.dirname(__FILE__) + "/../lib/ohm"
9
+ require "ohm"
10
10
 
11
- Ohm.connect(:port => 6381, :db => 1, :timeout => 3)
11
+ Ohm.connect(:port => 6379, :db => 15, :timeout => 3)
12
12
  Ohm.flush
@@ -0,0 +1,68 @@
1
+ # encoding: UTF-8
2
+
3
+ require File.join(File.dirname(__FILE__), "test_helper")
4
+
5
+ require "ohm/utils/upgrade"
6
+
7
+ # class User < Ohm::Model
8
+ # attribute :name
9
+ # attribute :email
10
+ #
11
+ # counter :views
12
+ #
13
+ # index :email
14
+ #
15
+ # set :posts
16
+ # list :comments
17
+ # end
18
+
19
+ class UpgradeScriptTest < Test::Unit::TestCase
20
+ def redis
21
+ Ohm.redis
22
+ end
23
+
24
+ setup do
25
+ redis.flushdb
26
+
27
+ @users = Ohm::Key[:User]
28
+
29
+ 10.times do
30
+ @id = redis.incr(@users[:id])
31
+ @user = @users[@id]
32
+
33
+ redis.sadd @users[:all], @id
34
+
35
+ redis.set @user[:name], "Albert"
36
+ redis.set @user[:email], "albert-#{@id}@example.com"
37
+ redis.incr @user[:views]
38
+
39
+ redis.sadd @user[:posts], 1
40
+ redis.sadd @user[:posts], 2
41
+
42
+ redis.lpush @user[:comments], 3
43
+ redis.lpush @user[:comments], 4
44
+
45
+ redis.sadd @user[:_indices], @users[:email][Ohm::Model.encode "albert-#{@id}@example.com"]
46
+ redis.sadd @users[:email][Ohm::Model.encode "albert-#{@id}@example.com"], @id
47
+ end
48
+ end
49
+
50
+ should "upgrade to hashes" do
51
+ Ohm::Utils::Upgrade.new([:User]).run
52
+
53
+ @user = @users[1]
54
+
55
+ assert_nil redis.get(@user[:name])
56
+ assert_nil redis.get(@user[:email])
57
+ assert_nil redis.get(@user[:views])
58
+
59
+ assert_equal ["1", "2"], redis.smembers(@user[:posts])
60
+
61
+ assert_equal [@users[:email][Ohm::Model.encode "albert-1@example.com"]], redis.smembers(@user[:_indices])
62
+ assert_equal ["1"], redis.smembers(@users[:email][Ohm::Model.encode "albert-1@example.com"])
63
+
64
+ assert_equal "Albert", redis.hget(@user, :name)
65
+ assert_equal "albert-1@example.com", redis.hget(@user, :email)
66
+ assert_equal "1", redis.hget(@user, :views)
67
+ end
68
+ end
metadata CHANGED
@@ -1,12 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ohm
3
3
  version: !ruby/object:Gem::Version
4
- prerelease: false
4
+ hash: 176182720
5
+ prerelease: true
5
6
  segments:
6
7
  - 0
8
+ - 1
7
9
  - 0
8
- - 38
9
- version: 0.0.38
10
+ - rc1
11
+ version: 0.1.0.rc1
10
12
  platform: ruby
11
13
  authors:
12
14
  - Michel Martens
@@ -15,7 +17,7 @@ autorequire:
15
17
  bindir: bin
16
18
  cert_chain: []
17
19
 
18
- date: 2010-07-12 00:00:00 -03:00
20
+ date: 2010-05-10 00:00:00 -03:00
19
21
  default_executable:
20
22
  dependencies:
21
23
  - !ruby/object:Gem::Dependency
@@ -26,6 +28,7 @@ dependencies:
26
28
  requirements:
27
29
  - - ">="
28
30
  - !ruby/object:Gem::Version
31
+ hash: 31
29
32
  segments:
30
33
  - 1
31
34
  - 0
@@ -41,6 +44,7 @@ dependencies:
41
44
  requirements:
42
45
  - - ~>
43
46
  - !ruby/object:Gem::Version
47
+ hash: 9
44
48
  segments:
45
49
  - 0
46
50
  - 1
@@ -61,6 +65,7 @@ files:
61
65
  - lib/ohm/collection.rb
62
66
  - lib/ohm/compat-1.8.6.rb
63
67
  - lib/ohm/key.rb
68
+ - lib/ohm/utils/upgrade.rb
64
69
  - lib/ohm/validations.rb
65
70
  - lib/ohm.rb
66
71
  - README.markdown
@@ -68,16 +73,15 @@ files:
68
73
  - Rakefile
69
74
  - test/1.8.6_test.rb
70
75
  - test/all_tests.rb
71
- - test/benchmarks.rb
72
- - test/circular_reference_test.rb
73
76
  - test/connection_test.rb
74
77
  - test/errors_test.rb
75
78
  - test/indices_test.rb
79
+ - test/model_module_test.rb
76
80
  - test/model_test.rb
77
81
  - test/mutex_test.rb
78
82
  - test/test_helper.rb
83
+ - test/upgrade_script_test.rb
79
84
  - test/validations_test.rb
80
- - test/wrapper_test.rb
81
85
  - test/test.conf
82
86
  has_rdoc: true
83
87
  homepage: http://github.com/soveran/ohm
@@ -93,21 +97,25 @@ required_ruby_version: !ruby/object:Gem::Requirement
93
97
  requirements:
94
98
  - - ">="
95
99
  - !ruby/object:Gem::Version
100
+ hash: 3
96
101
  segments:
97
102
  - 0
98
103
  version: "0"
99
104
  required_rubygems_version: !ruby/object:Gem::Requirement
100
105
  none: false
101
106
  requirements:
102
- - - ">="
107
+ - - ">"
103
108
  - !ruby/object:Gem::Version
109
+ hash: 25
104
110
  segments:
105
- - 0
106
- version: "0"
111
+ - 1
112
+ - 3
113
+ - 1
114
+ version: 1.3.1
107
115
  requirements: []
108
116
 
109
117
  rubyforge_project: ohm
110
- rubygems_version: 1.3.7
118
+ rubygems_version: 1.3.7.pre.1
111
119
  signing_key:
112
120
  specification_version: 3
113
121
  summary: Object-hash mapping library for Redis.
@@ -1,39 +0,0 @@
1
- require "rubygems"
2
- require "bench"
3
- require File.dirname(__FILE__) + "/../lib/ohm"
4
-
5
- Ohm.connect(:port => 6381)
6
- Ohm.flush
7
-
8
- class Event < Ohm::Model
9
- attribute :name
10
- attribute :location
11
-
12
- index :name
13
- index :location
14
-
15
- def validate
16
- assert_present :name
17
- assert_present :location
18
- end
19
- end
20
-
21
- i = 0
22
-
23
- benchmark "Create Events" do
24
- Event.create(:name => "Redis Meetup #{i}", :location => "London #{i}")
25
- end
26
-
27
- benchmark "Find by indexed attribute" do
28
- Event.find(:name => "Redis Meetup #{i}").first
29
- end
30
-
31
- benchmark "Mass update" do
32
- Event[1].update(:name => "Redis Meetup II")
33
- end
34
-
35
- benchmark "Load events" do
36
- Event[1].name
37
- end
38
-
39
- run 5000
@@ -1,28 +0,0 @@
1
- require File.expand_path("test_helper", File.dirname(__FILE__))
2
-
3
- class Post < Ohm::Model
4
- attribute :title
5
-
6
- list :categories, Category
7
- end
8
-
9
- class Category < Ohm::Model
10
- attribute :name
11
-
12
- set :posts, Post
13
- end
14
-
15
- class CircularReferenceTest < Test::Unit::TestCase
16
- setup do
17
- @post = Post.create(:title => "New post")
18
- @category = Category.create(:name => "Ruby")
19
- end
20
-
21
- test "inspect" do
22
- @post.categories << @category
23
- @category.posts << @post
24
-
25
- assert_equal %Q{#<Post:1 title="New post" categories=#<List (Category): ["1"]>>}, @post.inspect
26
- assert_equal %Q{#<Category:1 name="Ruby" posts=#<Set (Post): ["1"]>>}, @category.inspect
27
- end
28
- end
@@ -1,20 +0,0 @@
1
- require File.expand_path(File.join(File.dirname(__FILE__), "test_helper"))
2
-
3
- $missing_constants = []
4
-
5
- class Object
6
- def self.const_missing(name)
7
- $missing_constants << name
8
- super(name)
9
- end
10
- end
11
-
12
- class Foo < Ohm::Model
13
- set :bars, Bar
14
- end
15
-
16
- class Tests < Test::Unit::TestCase
17
- test "calls other const_missing hooks" do
18
- assert_equal [:Bar], $missing_constants
19
- end
20
- end