· 8 years ago · Mar 09, 2018, 03:20 PM
1##Â dm-is-list/spec/integration/constrained_spec.rb
2
3## NB!! These tests fail.
4# They are incorporated just to show the problem. IF you know how to fix this issue, please do so.
5
6
7require 'pathname'
8require Pathname(__FILE__).dirname.expand_path.parent + 'spec_helper'
9
10if HAS_SQLITE3 || HAS_MYSQL || HAS_POSTGRES
11
12 # supported_by :all do
13
14 describe 'DataMapper::Is::List' do
15
16 class User
17 include DataMapper::Resource
18
19 property :id, Serial
20 property :name, String
21
22 has n, :todos
23 end
24
25 class Todo
26 include DataMapper::Resource
27
28 property :id, Serial
29 property :title, String
30 property :position, Integer, :nullable => false, :unique_index => :position
31 property :user_id, Integer, :unique_index => :position
32
33 belongs_to :user
34
35 is :list, :scope => [:user_id]
36 end
37
38 before :each do
39 User.auto_migrate!
40 Todo.auto_migrate!
41
42 @u1 = User.create(:name => 'Johnny')
43 @u2 = User.create(:name => 'Freddy')
44 end
45
46 describe "Todo" do
47
48 before(:each) do
49 @loop = 10
50 @loop.times do |n|
51 Todo.create(:user => @u1, :title => "Todo #{n+1}" )
52 end
53 end
54
55 describe "should handle :unique_index => :position" do
56
57 it "should generate all in the correct order" do
58 DataMapper.repository(:default) do |repos|
59 Todo.all.map{ |a| [a.id, a.position] }.should == (1..@loop).map { |n| [n,n] }
60 end
61 end
62
63 it "should move items :higher in list" do
64 # DataMapper.logger.debug "should move Todo items higher =================="
65 DataMapper.repository(:default) do |repos|
66 Todo.get(2).move(:higher).should == true
67 Todo.all.map{ |a| [a.id, a.position] }.should == [ [1, 2], [2, 1] ] + (3..@loop).map { |n| [n,n] }
68 end
69 # DataMapper.logger.debug "/should move Todo items higher =================="
70 end
71
72 it "should move items :lower in list" do
73 DataMapper.repository(:default) do |repos|
74 Todo.get(9).move(:lower).should == true
75 Todo.all.map{ |a| [a.id, a.position] }.should == (1..8).map { |n| [n,n] } + [ [9, 10], [10, 9] ] + (11..@loop).map { |n| [n,n] }
76 end
77 end
78
79 end #/ should handle :unique_index => :position
80
81 end #/ Todo
82
83 end
84
85 # end #/supported_by
86
87end
88
89##################################################
90
91
92## ERROR
93
94# $ spec -cfs spec/integration/constrained_spec.rb
95
96# DataMapper::Is::List Todo should handle :unique_index => :position
97# - should generate all in the correct order
98# - should move items :higher in list (FAILED - 1)
99# - should move items :lower in list (FAILED - 2)
100
101# 1) DataObjects::IntegrityError in 'DataMapper::Is::List Todo should handle :unique_index => :position should move items :higher in list'
102
103# columns position, user_id are not unique
104#
105# /Library/Ruby/Gems/1.8/gems/dm-core-0.10.0/lib/dm-core/adapters/data_objects_adapter.rb:159:in `execute_non_query'
106# /Library/Ruby/Gems/1.8/gems/dm-core-0.10.0/lib/dm-core/adapters/data_objects_adapter.rb:159:in `execute'
107# /Library/Ruby/Gems/1.8/gems/dm-core-0.10.0/lib/dm-core/adapters/data_objects_adapter.rb:263:in `with_connection'
108# /Library/Ruby/Gems/1.8/gems/dm-core-0.10.0/lib/dm-core/adapters/data_objects_adapter.rb:157:in `execute'
109# /Users/kematzy/Code/GitHub/dm-more/dm-adjust/lib/dm-adjust/adapters/data_objects_adapter.rb:22:in `adjust'
110# /Users/kematzy/Code/GitHub/dm-more/dm-adjust/lib/dm-adjust/repository.rb:4:in `adjust'
111# /Users/kematzy/Code/GitHub/dm-more/dm-adjust/lib/dm-adjust/collection.rb:54:in `adjust!'
112# /Users/kematzy/Code/GitHub/dm-more/dm-is-list/lib/dm-is-list/is/list.rb:381:in `move_without_saving'
113# /Users/kematzy/Code/GitHub/dm-more/dm-is-list/lib/dm-is-list/is/list.rb:292:in `move'
114# /Library/Ruby/Gems/1.8/gems/dm-core-0.10.0/lib/dm-core/transaction.rb:503:in `transaction'
115# /Library/Ruby/Gems/1.8/gems/dm-core-0.10.0/lib/dm-core/transaction.rb:489:in `transaction'
116# /Library/Ruby/Gems/1.8/gems/dm-core-0.10.0/lib/dm-core/transaction.rb:114:in `commit'
117# /Library/Ruby/Gems/1.8/gems/dm-core-0.10.0/lib/dm-core/transaction.rb:176:in `within'
118# /Library/Ruby/Gems/1.8/gems/dm-core-0.10.0/lib/dm-core/transaction.rb:114:in `commit'
119# /Library/Ruby/Gems/1.8/gems/dm-core-0.10.0/lib/dm-core/transaction.rb:489:in `transaction'
120# /Library/Ruby/Gems/1.8/gems/dm-core-0.10.0/lib/dm-core/transaction.rb:503:in `transaction'
121# /Users/kematzy/Code/GitHub/dm-more/dm-is-list/lib/dm-is-list/is/list.rb:291:in `move'
122# ./spec/integration/constrained_spec.rb:64:
123# /Library/Ruby/Gems/1.8/gems/dm-core-0.10.0/lib/dm-core.rb:205:in `repository'
124# /Library/Ruby/Gems/1.8/gems/dm-core-0.10.0/lib/dm-core/repository.rb:110:in `scope'
125# /Library/Ruby/Gems/1.8/gems/dm-core-0.10.0/lib/dm-core.rb:205:in `repository'
126# ./spec/integration/constrained_spec.rb:63:
127#
128# 157 with_connection do |connection|
129# 158 command = connection.create_command(statement)
130# 159 command.execute_non_query(*bind_values)
131# 160 end
132# 161 end
133#
134# 2) DataObjects::IntegrityError in 'DataMapper::Is::List Todo should handle :unique_index => :position should move items :lower in list'
135#
136# columns position, user_id are not unique
137#
138# /Library/Ruby/Gems/1.8/gems/dm-core-0.10.0/lib/dm-core/adapters/data_objects_adapter.rb:159:in `execute_non_query'
139# /Library/Ruby/Gems/1.8/gems/dm-core-0.10.0/lib/dm-core/adapters/data_objects_adapter.rb:159:in `execute'
140# /Library/Ruby/Gems/1.8/gems/dm-core-0.10.0/lib/dm-core/adapters/data_objects_adapter.rb:263:in `with_connection'
141# /Library/Ruby/Gems/1.8/gems/dm-core-0.10.0/lib/dm-core/adapters/data_objects_adapter.rb:157:in `execute'
142# /Users/kematzy/Code/GitHub/dm-more/dm-adjust/lib/dm-adjust/adapters/data_objects_adapter.rb:22:in `adjust'
143# /Users/kematzy/Code/GitHub/dm-more/dm-adjust/lib/dm-adjust/repository.rb:4:in `adjust'
144# /Users/kematzy/Code/GitHub/dm-more/dm-adjust/lib/dm-adjust/collection.rb:54:in `adjust!'
145# /Users/kematzy/Code/GitHub/dm-more/dm-is-list/lib/dm-is-list/is/list.rb:379:in `move_without_saving'
146# /Users/kematzy/Code/GitHub/dm-more/dm-is-list/lib/dm-is-list/is/list.rb:292:in `move'
147# /Library/Ruby/Gems/1.8/gems/dm-core-0.10.0/lib/dm-core/transaction.rb:503:in `transaction'
148# /Library/Ruby/Gems/1.8/gems/dm-core-0.10.0/lib/dm-core/transaction.rb:489:in `transaction'
149# /Library/Ruby/Gems/1.8/gems/dm-core-0.10.0/lib/dm-core/transaction.rb:114:in `commit'
150# /Library/Ruby/Gems/1.8/gems/dm-core-0.10.0/lib/dm-core/transaction.rb:176:in `within'
151# /Library/Ruby/Gems/1.8/gems/dm-core-0.10.0/lib/dm-core/transaction.rb:114:in `commit'
152# /Library/Ruby/Gems/1.8/gems/dm-core-0.10.0/lib/dm-core/transaction.rb:489:in `transaction'
153# /Library/Ruby/Gems/1.8/gems/dm-core-0.10.0/lib/dm-core/transaction.rb:503:in `transaction'
154# /Users/kematzy/Code/GitHub/dm-more/dm-is-list/lib/dm-is-list/is/list.rb:291:in `move'
155# ./spec/integration/constrained_spec.rb:72:
156# /Library/Ruby/Gems/1.8/gems/dm-core-0.10.0/lib/dm-core.rb:205:in `repository'
157# /Library/Ruby/Gems/1.8/gems/dm-core-0.10.0/lib/dm-core/repository.rb:110:in `scope'
158# /Library/Ruby/Gems/1.8/gems/dm-core-0.10.0/lib/dm-core.rb:205:in `repository'
159# ./spec/integration/constrained_spec.rb:71:
160#
161# 157 with_connection do |connection|
162# 158 command = connection.create_command(statement)
163# 159 command.execute_non_query(*bind_values)
164# 160 end
165# 161 end
166#
167#
168# Finished in 0.153781 seconds
169#
170# 3 examples, 2 failures
171#
172#
173# ## SQL DEBUG
174#
175#
176# ## BOOTSTRAP
177# ~ (0.000117) DROP TABLE IF EXISTS "users"
178# ~ (0.000009) PRAGMA table_info("users")
179# ~ (0.000109) CREATE TABLE "users" ("id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, "name" VARCHAR(50))
180# ~ (0.000078) DROP TABLE IF EXISTS "todos"
181# ~ (0.000007) PRAGMA table_info("todos")
182# ~ (0.000410) CREATE TABLE "todos" ("id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, "title" VARCHAR(50), "position" INTEGER NOT NULL, "user_id" INTEGER)
183# ~ (0.000141) CREATE UNIQUE INDEX "unique_todos_position" ON "todos" ("position", "user_id")
184# ~ (0.000033) INSERT INTO "users" ("name") VALUES ('Johnny')
185# ~ (0.000032) INSERT INTO "users" ("name") VALUES ('Freddy')
186# ~ (0.000053) SELECT "id", "title", "position", "user_id" FROM "todos" WHERE "user_id" = 1 ORDER BY "position" DESC LIMIT 1
187# ~ (0.000038) UPDATE "todos" SET "position" = "position" + 1 WHERE "position" >= 1 AND "user_id" = 1
188# ~ (0.000039) INSERT INTO "todos" ("title", "position", "user_id") VALUES ('Todo 1', 1, 1)
189# ~ (0.000024) SELECT "id", "title", "position", "user_id" FROM "todos" WHERE "user_id" = 1 ORDER BY "position" DESC LIMIT 1
190# ~ (0.000031) UPDATE "todos" SET "position" = "position" + 1 WHERE "position" >= 2 AND "user_id" = 1
191# ~ (0.000034) INSERT INTO "todos" ("title", "position", "user_id") VALUES ('Todo 2', 2, 1)
192# ~ (0.000025) SELECT "id", "title", "position", "user_id" FROM "todos" WHERE "user_id" = 1 ORDER BY "position" DESC LIMIT 1
193# ~ (0.000031) UPDATE "todos" SET "position" = "position" + 1 WHERE "position" >= 3 AND "user_id" = 1
194# ~ (0.000034) INSERT INTO "todos" ("title", "position", "user_id") VALUES ('Todo 3', 3, 1)
195# ~ (0.000024) SELECT "id", "title", "position", "user_id" FROM "todos" WHERE "user_id" = 1 ORDER BY "position" DESC LIMIT 1
196# ~ (0.000032) UPDATE "todos" SET "position" = "position" + 1 WHERE "position" >= 4 AND "user_id" = 1
197# ~ (0.000049) INSERT INTO "todos" ("title", "position", "user_id") VALUES ('Todo 4', 4, 1)
198# ~ (0.000025) SELECT "id", "title", "position", "user_id" FROM "todos" WHERE "user_id" = 1 ORDER BY "position" DESC LIMIT 1
199# ~ (0.000063) UPDATE "todos" SET "position" = "position" + 1 WHERE "position" >= 5 AND "user_id" = 1
200# ~ (0.000038) INSERT INTO "todos" ("title", "position", "user_id") VALUES ('Todo 5', 5, 1)
201# ~ (0.000026) SELECT "id", "title", "position", "user_id" FROM "todos" WHERE "user_id" = 1 ORDER BY "position" DESC LIMIT 1
202# ~ (0.000031) UPDATE "todos" SET "position" = "position" + 1 WHERE "position" >= 6 AND "user_id" = 1
203# ~ (0.000033) INSERT INTO "todos" ("title", "position", "user_id") VALUES ('Todo 6', 6, 1)
204# ~ (0.000025) SELECT "id", "title", "position", "user_id" FROM "todos" WHERE "user_id" = 1 ORDER BY "position" DESC LIMIT 1
205# ~ (0.000031) UPDATE "todos" SET "position" = "position" + 1 WHERE "position" >= 7 AND "user_id" = 1
206# ~ (0.000032) INSERT INTO "todos" ("title", "position", "user_id") VALUES ('Todo 7', 7, 1)
207# ~ (0.000024) SELECT "id", "title", "position", "user_id" FROM "todos" WHERE "user_id" = 1 ORDER BY "position" DESC LIMIT 1
208# ~ (0.000031) UPDATE "todos" SET "position" = "position" + 1 WHERE "position" >= 8 AND "user_id" = 1
209# ~ (0.000033) INSERT INTO "todos" ("title", "position", "user_id") VALUES ('Todo 8', 8, 1)
210# ~ (0.000025) SELECT "id", "title", "position", "user_id" FROM "todos" WHERE "user_id" = 1 ORDER BY "position" DESC LIMIT 1
211# ~ (0.000032) UPDATE "todos" SET "position" = "position" + 1 WHERE "position" >= 9 AND "user_id" = 1
212# ~ (0.000033) INSERT INTO "todos" ("title", "position", "user_id") VALUES ('Todo 9', 9, 1)
213# ~ (0.000025) SELECT "id", "title", "position", "user_id" FROM "todos" WHERE "user_id" = 1 ORDER BY "position" DESC LIMIT 1
214# ~ (0.000032) UPDATE "todos" SET "position" = "position" + 1 WHERE "position" >= 10 AND "user_id" = 1
215# ~ (0.000034) INSERT INTO "todos" ("title", "position", "user_id") VALUES ('Todo 10', 10, 1)
216
217# ## /END BOOTSTRAP
218
219
220# 1)
221# ~ (0.000019) SELECT "id", "title", "position", "user_id" FROM "todos" WHERE "id" = 2
222# ~ sqlite3: begin
223# ~ (0.000007) BEGIN
224# ~ (0.000024) SELECT "id", "title", "position", "user_id" FROM "todos" WHERE "user_id" = 1 ORDER BY "position" DESC LIMIT 1
225# ~ (0.000036) SELECT "id" FROM "todos" WHERE "position" IN (1, 2) AND "id" IN (10, 2) AND "user_id" = 1 ORDER BY "position"
226# ~ columns position, user_id are not unique (code: 19, sql state: , query: UPDATE "todos" SET "position" = "position" + 1 WHERE "position" IN (1, 2) AND "user_id" = 1, uri: sqlite3://:memory:)
227# ~ sqlite3: rollback
228# ~ (0.000008) ROLLBACK
229
230
231# 2)
232# ~ (0.000019) SELECT "id", "title", "position", "user_id" FROM "todos" WHERE "id" = 9
233# ~ sqlite3: begin
234# ~ (0.000007) BEGIN
235# ~ (0.000024) SELECT "id", "title", "position", "user_id" FROM "todos" WHERE "user_id" = 1 ORDER BY "position" DESC LIMIT 1
236# ~ (0.000053) SELECT "id" FROM "todos" WHERE "position" IN (9, 10) AND "id" IN (10, 9) AND "user_id" = 1 ORDER BY "position"
237# ~ columns position, user_id are not unique (code: 19, sql state: , query: UPDATE "todos" SET "position" = "position" + -1 WHERE "position" IN (9, 10) AND "user_id" = 1, uri: sqlite3://:memory:)
238# ~ sqlite3: rollback
239# ~ (0.000009) ROLLBACK