OSDN Git Service

Applied r247 changes for the test cases (spliting hte file).
[shogi-server/shogi-server.git] / test / TC_league.rb
1 $:.unshift File.join(File.dirname(__FILE__), "..")
2 require 'test/unit'
3 require 'fileutils'
4 require 'shogi_server'
5 require 'shogi_server/player'
6 require 'shogi_server/league'
7
8 class TestPersistent < Test::Unit::TestCase
9   def setup
10     @filename = File.join(".", "test.yaml")
11     if File.exists?(@filename)
12       FileUtils.rm(@filename)
13     end
14     @persistent = ShogiServer::League::Persistent.new(@filename)
15     @p = ShogiServer::BasicPlayer.new
16     @p.name = "gps_normal"
17     @p.player_id = "gps_normal_dummy_id"
18     @p.last_game_win = true
19   end
20
21   def test_save_player
22     @persistent.save(@p)
23
24     p2 = ShogiServer::BasicPlayer.new
25     p2.player_id = @p.player_id
26
27     @persistent.load_player(p2)
28     assert_equal(p2.last_game_win, false)
29   end
30
31   def test_empty_yaml
32     count = 0
33     @persistent.each_group do |group, players|
34       count += 1
35     end
36     assert_equal(count, 0)
37     FileUtils.rm(@filename)
38     count = 0
39     @persistent.each_group do |group, players|
40       count += 1
41     end
42     assert_equal(count, 0)
43   end
44
45   def test_load_player
46     filename = File.join(".", "players.yaml")
47     persistent = ShogiServer::League::Persistent.new(filename)
48     p = ShogiServer::BasicPlayer.new
49     p.player_id = "gps_normal+e293220e3f8a3e59f79f6b0efffaa931"
50     persistent.load_player(p)
51
52     assert_equal(p.name, "gps_normal")
53     assert_in_delta(p.rate, -1752.0, 0.1)
54     assert_equal(p.modified_at.to_s, "Thu May 08 23:50:54 +0900 2008")
55     assert_equal(p.rating_group, 0)
56     assert_in_delta(p.win, 3384.04877829976,  0.00001)
57     assert_in_delta(p.loss, 906.949084230512, 0.00001)
58   end
59
60   def test_get_players
61     filename = File.join(".", "players.yaml")
62     persistent = ShogiServer::League::Persistent.new(filename)
63     players = persistent.get_players
64     assert_equal(players.size, 295)
65   end
66 end
67
68
69 class TestLeague < Test::Unit::TestCase
70   def setup
71     @league = ShogiServer::League.new
72     @league.dir = "."
73     @league.setup_players_database
74
75     @p = ShogiServer::BasicPlayer.new
76     @p.name = "test_name"
77   end
78
79   def teardown
80     @league.shutdown
81   end
82
83   def test_add_player
84     assert(!@league.find(@p.name))
85     @league.add(@p)
86     assert(@league.find(@p.name))
87     @league.delete(@p)
88     assert(!@league.find(@p.name))
89   end
90
91   def test_reload
92     @league.reload
93     assert(true)
94   end
95 end