· 8 years ago · Jul 13, 2018, 11:50 PM
1require 'rubygems'
2require 'dm-core'
3require 'spec'
4
5DataMapper::Logger.new(STDOUT, :debug)
6DataMapper.setup(:default, 'sqlite3::memory:')
7
8class Job
9 include DataMapper::Resource
10 property :id, Serial
11 has n, :agents, :through => Resource
12end
13
14class Agent
15 include DataMapper::Resource
16 property :id, Serial
17 has n, :jobs, :through => Resource
18end
19
20describe "Anonymous m:m relationships" do
21 before :all do
22 DataMapper.auto_migrate!
23 end
24 before :each do
25 @job = Job.create :agents => [ Agent.new ]
26 @agent = @job.agents.first
27 end
28 it "should establish the relationships in both directions" do
29 @job.agents.should == [ @agent ]
30 @agent.jobs.should == [ @job ]
31 end
32 it "should allow to call #destroy on an intermediate resource" do
33 intermediate = @job.agent_jobs.first
34 intermediate.job.should == @job
35 intermediate.agent.should == @agent
36 intermediate.destroy.should be_true
37 AgentJob.all.size.should == 0
38 end
39end
40
41__END__
42
43mungo:snippets snusnu$ spec -cfs holoway.rb
44
45Anonymous m:m relationships
46 ~ (0.000132) SELECT sqlite_version(*)
47 ~ (0.000104) DROP TABLE IF EXISTS "jobs"
48 ~ (0.000016) DROP TABLE IF EXISTS "agents"
49 ~ (0.000030) DROP TABLE IF EXISTS "agent_jobs"
50 ~ (0.000025) PRAGMA table_info("jobs")
51 ~ (0.000378) CREATE TABLE "jobs" ("id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT)
52 ~ (0.000010) PRAGMA table_info("agents")
53 ~ (0.000114) CREATE TABLE "agents" ("id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT)
54 ~ (0.000009) PRAGMA table_info("agent_jobs")
55 ~ (0.000174) CREATE TABLE "agent_jobs" ("job_id" INTEGER NOT NULL, "agent_id" INTEGER NOT NULL, PRIMARY KEY("job_id", "agent_id"))
56 ~ (0.000035) INSERT INTO "jobs" DEFAULT VALUES
57 ~ (0.000034) INSERT INTO "agents" DEFAULT VALUES
58 ~ (0.000049) SELECT "job_id", "agent_id" FROM "agent_jobs" WHERE "agent_id" = 1 AND "job_id" = 1 ORDER BY "job_id", "agent_id" LIMIT 1
59 ~ (0.000051) INSERT INTO "agent_jobs" ("job_id", "agent_id") VALUES (1, 1)
60 ~ (0.000069) SELECT "jobs"."id" FROM "jobs" INNER JOIN "agent_jobs" ON "jobs"."id" = "agent_jobs"."job_id" INNER JOIN "agents" ON "agent_jobs"."agent_id" = "agents"."id" WHERE "agent_jobs"."agent_id" = 1 GROUP BY "jobs"."id" ORDER BY "jobs"."id"
61- should establish the relationships in both directions
62 ~ (0.000037) INSERT INTO "jobs" DEFAULT VALUES
63 ~ (0.000033) INSERT INTO "agents" DEFAULT VALUES
64 ~ (0.000037) SELECT "job_id", "agent_id" FROM "agent_jobs" WHERE "agent_id" = 2 AND "job_id" = 2 ORDER BY "job_id", "agent_id" LIMIT 1
65 ~ (0.000049) INSERT INTO "agent_jobs" ("job_id", "agent_id") VALUES (2, 2)
66 ~ (0.000031) SELECT "job_id", "agent_id" FROM "agent_jobs" WHERE "job_id" = 2 ORDER BY "job_id", "agent_id" LIMIT 1
67 ~ (0.000047) DELETE FROM "agent_jobs" WHERE "job_id" = 2 AND "agent_id" = 2
68 ~ (0.000045) SELECT "job_id", "agent_id" FROM "agent_jobs" ORDER BY "job_id", "agent_id"
69- should allow to call #destroy on an intermediate resource (FAILED - 1)
70
711)
72'Anonymous m:m relationships should allow to call #destroy on an intermediate resource' FAILED
73expected: 0,
74 got: 1 (using ==)
75./holoway.rb:37:
76
77Finished in 0.023077 seconds
78
792 examples, 1 failure