simple_unique 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -18,7 +18,9 @@ module AWS
18
18
  relation = relation.where(scope_item.to_sym => scope_value)
19
19
  end
20
20
 
21
- existing_record = relation.where(attribute_name.to_sym => value).first
21
+ existing_record = SimpleDB.consistent_reads do
22
+ relation.where(attribute_name.to_sym => value).first
23
+ end
22
24
  taken = !existing_record.nil?
23
25
 
24
26
  record.errors.add(attribute_name, message) if taken #blank
@@ -1,3 +1,3 @@
1
1
  module SimpleUnique
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
@@ -24,6 +24,21 @@ describe 'model validations' do
24
24
  widg.errors[:name].should == ['has already been taken']
25
25
  end
26
26
 
27
+ it 'should prevent invalid records from being saved' do
28
+ klass.string_attr :name
29
+ klass.validates_uniqueness_of :name
30
+ widg = klass.new(:name => "Turd Furguson")
31
+ widg.valid?.should be_true
32
+ widg.save
33
+ sleep(2) # Allow a couple of seconds for SimpleDB to catch up
34
+
35
+ widg = klass.new(:name => "Turd Furguson")
36
+ widg.valid?.should be_false
37
+ widg.errors[:name].should == ['has already been taken']
38
+ widg.save
39
+ klass.count.should == 1
40
+ end
41
+
27
42
  it 'should only allow one record with a given attribute value within a given scope' do
28
43
  klass.string_attr :name
29
44
  klass.string_attr :category
@@ -96,6 +111,33 @@ describe 'model validations' do
96
111
 
97
112
  end
98
113
 
114
+
115
+
116
+ describe "validation with SimpleDB.consistent_read" do
117
+ before(:each) do
118
+ klass.string_attr :name
119
+ klass.validates_uniqueness_of :name
120
+ end
121
+ it 'should only allow one record with a given attribute value' do
122
+ 10.times do
123
+ #sleep(2) # make sure the first one doesn't fail....?
124
+ random = rand(1000000000)
125
+ name = "Turd Furguson #{random}"
126
+ widg = klass.new(:name => name)
127
+ valid = widg.valid?
128
+ #puts name
129
+ #puts widg.errors
130
+ valid.should be_true
131
+ widg.save
132
+ #sleep(2) # Allow a couple of seconds for SimpleDB to catch up
133
+
134
+ widg = klass.new(:name => name)
135
+ widg.valid?.should be_false
136
+ widg.errors[:name].should == ['has already been taken']
137
+ end
138
+ end
139
+ end
140
+
99
141
  end
100
142
 
101
143
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: simple_unique
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-01-12 00:00:00.000000000 Z
12
+ date: 2013-01-18 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: aws-sdk