· 8 years ago · May 08, 2018, 08:20 AM
1#!/usr/bin/env ruby
2#
3# A one file test to show uniqueness on 1.0
4require 'rubygems'
5require 'dm-core'
6require 'dm-migrations'
7require 'dm-validations'
8
9
10# setup the logger
11DataMapper::Logger.new($stdout, :debug)
12
13# connect to the DB
14DataMapper.setup(:default, 'sqlite3::memory:')
15
16class TestModel
17 include DataMapper::Resource
18
19 # properties
20 property :id, Serial
21 property :name, String, :unique => [:category]
22 property :category, String
23
24end
25
26class BothUnique
27 include DataMapper::Resource
28
29 # properties
30 property :id, Serial
31 property :name, String, :unique => [:category]
32 property :category, String, :unique => [:category]
33end
34
35class BothUniqueOnBoth
36 include DataMapper::Resource
37
38 # properties
39 property :id, Serial
40 property :name, String, :unique => [:name, :category]
41 property :category, String, :unique => [:name, :category]
42end
43
44class SharedName
45 include DataMapper::Resource
46
47 property :id, Serial
48 property :name, String, :unique => [:name_category]
49 property :category, String, :unique => [:name_category]
50end
51
52DataMapper.finalize.auto_migrate!
53
54@t = TestModel.new(:name => 'foo', :category => 'bar')
55puts "Trying to save #{@t.inspect}"
56puts "#=> #{@t.save}"
57puts
58
59
60@t = TestModel.new(:name => 'foo', :category => 'bar')
61puts "Trying to save #{@t.inspect}"
62puts "#=> #{@t.save}"
63puts "Good, we failed!"
64puts
65
66
67@t = TestModel.new(:name => 'foo', :category => 'baz')
68puts "Trying to save #{@t.inspect}"
69begin
70 puts "#=> #{@t.save}"
71 puts "Good, we succeeded!"
72rescue Exception => e
73 puts "Ooops, we exploded because we were only unique on name in the DB"
74end
75puts
76
77
78@t = BothUnique.new(:name => 'foo', :category => 'bar')
79puts "Trying to save #{@t.inspect}"
80puts "#=> #{@t.save}"
81puts
82
83
84@t = BothUnique.new(:name => 'foo', :category => 'bar')
85puts "Trying to save #{@t.inspect}"
86puts "#=> #{@t.save}"
87puts "Good, we failed!"
88puts
89
90
91@t = BothUnique.new(:name => 'foo', :category => 'baz')
92puts "Trying to save #{@t.inspect}"
93begin
94 puts "#=> #{@t.save}"
95 puts "Good, we succeeded!"
96rescue Exception => e
97 puts "Ooops, we exploded because we were only unique on name in the DB"
98end
99puts
100
101@t = BothUnique.new(:name => 'random', :category => 'bar')
102puts "Trying to save #{@t.inspect}"
103puts "#=> #{@t.save}"
104puts "Ooops we failed, as category validator is only on the category"
105puts
106
107
108@t = BothUniqueOnBoth.new(:name => 'foo', :category => 'bar')
109puts "Trying to save #{@t.inspect}"
110puts "#=> #{@t.save}"
111puts
112
113
114@t = BothUniqueOnBoth.new(:name => 'foo', :category => 'bar')
115puts "Trying to save #{@t.inspect}"
116puts "#=> #{@t.save}"
117puts "Good, we failed!"
118puts
119
120
121@t = BothUniqueOnBoth.new(:name => 'foo', :category => 'baz')
122puts "Trying to save #{@t.inspect}"
123begin
124 puts "#=> #{@t.save}"
125 puts "Good, we succeeded!"
126rescue Exception => e
127 puts "Ooops, we exploded because we were only unique on name in the DB"
128end
129puts
130
131@t = BothUniqueOnBoth.new(:name => 'random', :category => 'bar')
132puts "Trying to save #{@t.inspect}"
133puts "#=> #{@t.save}"
134puts "We succeeded, yay"
135puts "But note: We end up with two unique indexes on the table, AND two validations. With three properties, that's 3 unique indexes and 3x validations"
136puts
137
138@t = SharedName.new(:name => 'foo', :category => 'bar')
139puts "Trying to save #{@t.inspect}"
140begin
141 puts "#=> #{@t.save}"
142 puts "We succeeded, yay"
143rescue NoMethodError => e
144 puts "Oooops, we blew up. With a NoMethodError!"
145 puts e.message
146 puts "But we do only get one index"
147end
148
149__END__
150 ~ (0.000104) SELECT sqlite_version(*)
151 ~ (0.000190) DROP TABLE IF EXISTS "test_models"
152 ~ (0.000025) PRAGMA table_info("test_models")
153 ~ (0.000564) CREATE TABLE "test_models" ("id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, "name" VARCHAR(50), "category" VARCHAR(50))
154 ~ (0.000172) CREATE UNIQUE INDEX "unique_test_models_category" ON "test_models" ("name")
155 ~ (0.000027) DROP TABLE IF EXISTS "both_uniques"
156 ~ (0.000016) PRAGMA table_info("both_uniques")
157 ~ (0.000238) CREATE TABLE "both_uniques" ("id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, "name" VARCHAR(50), "category" VARCHAR(50))
158 ~ (0.000122) CREATE UNIQUE INDEX "unique_both_uniques_category" ON "both_uniques" ("name", "category")
159 ~ (0.000030) DROP TABLE IF EXISTS "both_unique_on_boths"
160 ~ (0.000017) PRAGMA table_info("both_unique_on_boths")
161 ~ (0.000227) CREATE TABLE "both_unique_on_boths" ("id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, "name" VARCHAR(50), "category" VARCHAR(50))
162 ~ (0.000151) CREATE UNIQUE INDEX "unique_both_unique_on_boths_name" ON "both_unique_on_boths" ("name", "category")
163 ~ (0.000114) CREATE UNIQUE INDEX "unique_both_unique_on_boths_category" ON "both_unique_on_boths" ("name", "category")
164 ~ (0.000023) DROP TABLE IF EXISTS "shared_names"
165 ~ (0.000016) PRAGMA table_info("shared_names")
166 ~ (0.000326) CREATE TABLE "shared_names" ("id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, "name" VARCHAR(50), "category" VARCHAR(50))
167 ~ (0.000134) CREATE UNIQUE INDEX "unique_shared_names_name_category" ON "shared_names" ("name", "category")
168Trying to save #<TestModel @id=nil @name="foo" @category="bar">
169 ~ (0.000100) SELECT "id" FROM "test_models" WHERE ("name" = 'foo' AND "category" = 'bar') ORDER BY "id" LIMIT 1
170 ~ (0.000096) INSERT INTO "test_models" ("name", "category") VALUES ('foo', 'bar')
171#=> true
172
173Trying to save #<TestModel @id=nil @name="foo" @category="bar">
174 ~ (0.000097) SELECT "id" FROM "test_models" WHERE ("name" = 'foo' AND "category" = 'bar') ORDER BY "id" LIMIT 1
175#=> false
176Good, we failed!
177
178Trying to save #<TestModel @id=nil @name="foo" @category="baz">
179 ~ (0.000095) SELECT "id" FROM "test_models" WHERE ("name" = 'foo' AND "category" = 'baz') ORDER BY "id" LIMIT 1
180 ~ column name is not unique (code: 19, sql state: , query: INSERT INTO "test_models" ("name", "category") VALUES ('foo', 'baz'), uri: sqlite3://:memory:)
181Ooops, we exploded because we were only unique on name in the DB
182
183Trying to save #<BothUnique @id=nil @name="foo" @category="bar">
184 ~ (0.000095) SELECT "id" FROM "both_uniques" WHERE ("name" = 'foo' AND "category" = 'bar') ORDER BY "id" LIMIT 1
185 ~ (0.000081) SELECT "id" FROM "both_uniques" WHERE "category" = 'bar' ORDER BY "id" LIMIT 1
186 ~ (0.000101) INSERT INTO "both_uniques" ("name", "category") VALUES ('foo', 'bar')
187#=> true
188
189Trying to save #<BothUnique @id=nil @name="foo" @category="bar">
190 ~ (0.000092) SELECT "id" FROM "both_uniques" WHERE ("name" = 'foo' AND "category" = 'bar') ORDER BY "id" LIMIT 1
191 ~ (0.000153) SELECT "id" FROM "both_uniques" WHERE "category" = 'bar' ORDER BY "id" LIMIT 1
192#=> false
193Good, we failed!
194
195Trying to save #<BothUnique @id=nil @name="foo" @category="baz">
196 ~ (0.000095) SELECT "id" FROM "both_uniques" WHERE ("name" = 'foo' AND "category" = 'baz') ORDER BY "id" LIMIT 1
197 ~ (0.000082) SELECT "id" FROM "both_uniques" WHERE "category" = 'baz' ORDER BY "id" LIMIT 1
198 ~ (0.000099) INSERT INTO "both_uniques" ("name", "category") VALUES ('foo', 'baz')
199#=> true
200Good, we succeeded!
201
202Trying to save #<BothUnique @id=nil @name="random" @category="bar">
203 ~ (0.000090) SELECT "id" FROM "both_uniques" WHERE ("name" = 'random' AND "category" = 'bar') ORDER BY "id" LIMIT 1
204 ~ (0.000079) SELECT "id" FROM "both_uniques" WHERE "category" = 'bar' ORDER BY "id" LIMIT 1
205#=> false
206Ooops we failed, as category validator is only on the category
207
208Trying to save #<BothUniqueOnBoth @id=nil @name="foo" @category="bar">
209 ~ (0.000093) SELECT "id" FROM "both_unique_on_boths" WHERE ("name" = 'foo' AND "category" = 'bar') ORDER BY "id" LIMIT 1
210 ~ (0.000083) SELECT "id" FROM "both_unique_on_boths" WHERE ("category" = 'bar' AND "name" = 'foo') ORDER BY "id" LIMIT 1
211 ~ (0.000112) INSERT INTO "both_unique_on_boths" ("name", "category") VALUES ('foo', 'bar')
212#=> true
213
214Trying to save #<BothUniqueOnBoth @id=nil @name="foo" @category="bar">
215 ~ (0.000093) SELECT "id" FROM "both_unique_on_boths" WHERE ("name" = 'foo' AND "category" = 'bar') ORDER BY "id" LIMIT 1
216 ~ (0.000088) SELECT "id" FROM "both_unique_on_boths" WHERE ("category" = 'bar' AND "name" = 'foo') ORDER BY "id" LIMIT 1
217#=> false
218Good, we failed!
219
220Trying to save #<BothUniqueOnBoth @id=nil @name="foo" @category="baz">
221 ~ (0.000091) SELECT "id" FROM "both_unique_on_boths" WHERE ("name" = 'foo' AND "category" = 'baz') ORDER BY "id" LIMIT 1
222 ~ (0.000083) SELECT "id" FROM "both_unique_on_boths" WHERE ("category" = 'baz' AND "name" = 'foo') ORDER BY "id" LIMIT 1
223 ~ (0.000102) INSERT INTO "both_unique_on_boths" ("name", "category") VALUES ('foo', 'baz')
224#=> true
225Good, we succeeded!
226
227Trying to save #<BothUniqueOnBoth @id=nil @name="random" @category="bar">
228 ~ (0.000091) SELECT "id" FROM "both_unique_on_boths" WHERE ("name" = 'random' AND "category" = 'bar') ORDER BY "id" LIMIT 1
229 ~ (0.000083) SELECT "id" FROM "both_unique_on_boths" WHERE ("category" = 'bar' AND "name" = 'random') ORDER BY "id" LIMIT 1
230 ~ (0.000100) INSERT INTO "both_unique_on_boths" ("name", "category") VALUES ('random', 'bar')
231#=> true
232We succeeded, yay
233But note: We end up with two unique indexes on the table, AND two validations. With three properties, that's 3 unique indexes and 3x validations
234
235Trying to save #<SharedName @id=nil @name="foo" @category="bar">
236Oooops, we blew up. With a NoMethodError!
237undefined method `name_category' for #<SharedName @id=nil @name="foo" @category="bar">
238But we do only get one index