· 8 years ago · Jun 13, 2018, 11:32 AM
1#!/usr/bin/env ruby
2
3require 'rubygems'
4
5gem 'dm-core', '>=0.9.11'
6require 'dm-core'
7
8gem 'rspec', '>=1.1.12'
9require 'spec'
10
11DataMapper::Logger.new(STDOUT, :debug)
12DataMapper.setup(:default, 'sqlite3::memory:')
13
14# here come the demo class(es)
15
16class Game
17 include DataMapper::Resource
18 property :id, Serial
19 property :name, String
20 has n, :players
21end
22
23class Player
24 include DataMapper::Resource
25 property :id, Serial
26 property :game_id, Integer, :nullable => false
27 property :name, String
28 belongs_to :game
29end
30
31class Event
32 include DataMapper::Resource
33 property :id, Serial
34 property :game_id, Integer, :nullable => false
35 property :name, String
36 belongs_to :game
37 has n, :players, :through => :game
38end
39
40puts '-' * 80
41
42describe Event do
43
44 before(:all) do
45 DataMapper.auto_migrate!
46 end
47
48 before(:each) do
49 @g = Game.create :name => 'ping pong'
50 @p1 = Player.create :name => 'freakguard', :game => @g
51 @p2 = Player.create :name => 'snusnu', :game => @g
52 @e = Event.create :name => 'championships', :game => @g
53 end
54
55 it "should store a list of players" do
56 @e.respond_to?(:players).should be_true
57 @e.players.should_not be_empty?
58 @e.players.include?(@p1).should be_true
59 @e.players.include?(@p2).should be_true
60 end
61
62end
63
64
65# mungo:Desktop snusnu$ spec -c -f -s events.rb
66# --------------------------------------------------------------------------------
67#
68# Event
69# Wed, 15 Apr 2009 14:49:23 GMT ~ debug ~ (0.000217) DROP TABLE IF EXISTS "events"
70# Wed, 15 Apr 2009 14:49:23 GMT ~ debug ~ (0.000017) DROP TABLE IF EXISTS "players"
71# Wed, 15 Apr 2009 14:49:23 GMT ~ debug ~ (0.000015) DROP TABLE IF EXISTS "games"
72# Wed, 15 Apr 2009 14:49:23 GMT ~ debug ~ (0.000032) PRAGMA table_info('games')
73# Wed, 15 Apr 2009 14:49:23 GMT ~ debug ~ (0.000057) SELECT sqlite_version(*)
74# Wed, 15 Apr 2009 14:49:23 GMT ~ debug ~ (0.000355) CREATE TABLE "games" ("id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, "name" VARCHAR(50))
75# Wed, 15 Apr 2009 14:49:23 GMT ~ debug ~ (0.000009) PRAGMA table_info('players')
76# Wed, 15 Apr 2009 14:49:23 GMT ~ debug ~ (0.000119) CREATE TABLE "players" ("id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, "game_id" INTEGER NOT NULL, "name" VARCHAR(50))
77# Wed, 15 Apr 2009 14:49:23 GMT ~ debug ~ (0.000009) PRAGMA table_info('events')
78# Wed, 15 Apr 2009 14:49:23 GMT ~ debug ~ (0.000126) CREATE TABLE "events" ("id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, "game_id" INTEGER NOT NULL, "name" VARCHAR(50), "event_id" INTEGER)
79# Wed, 15 Apr 2009 14:49:23 GMT ~ debug ~ (0.000049) INSERT INTO "games" ("name") VALUES ('ping pong')
80# Wed, 15 Apr 2009 14:49:23 GMT ~ debug ~ (0.000040) INSERT INTO "players" ("game_id", "name") VALUES (1, 'freakguard')
81# Wed, 15 Apr 2009 14:49:23 GMT ~ debug ~ (0.000040) INSERT INTO "players" ("game_id", "name") VALUES (1, 'snusnu')
82# Wed, 15 Apr 2009 14:49:23 GMT ~ debug ~ (0.000040) INSERT INTO "events" ("game_id", "name") VALUES (1, 'championships')
83# - should store a list of players (FAILED - 1)
84#
85# 1)
86# SystemStackError in 'Event should store a list of players'
87# stack level too deep
88# ./events.rb:57:
89#
90# Finished in 0.198908 seconds
91#
92# 1 example, 1 failure