301 Days

A year of gamedev experiments.

Day 76b - Hook Cleanup

| Comments

In which we clean the SQL out of our hooks.

Tackle a TODO every other day

No database-specific code in hooks.rb

Our @db_cleanup hook is still the offending piece:

features/lib/hooks.rblink
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
After('@db_cleanup') do |scenario|
  client = connect_to_db(@server_database)
  result = client.execute("DELETE
    FROM [#{@server_database}].[dbo].[Account]
    WHERE account LIKE 'test%'
    AND email LIKE '%301days.com'")
  debug_msg "Rows deleted from [Account]: #{result.do}"
  player_tables = ["PlayerBelt","PlayerEffects","PlayerFlags","PlayerHeld","PlayerLocker",
    "PlayerQuests","PlayerRings","PlayerSack","PlayerSettings","PlayerSkills","PlayerSpells",
    "PlayerWearing","Player"]
  get_all_test_player_ids().each do |player_to_delete|
    debug_msg "Deleting player #{player_to_delete}"
    player_tables.each do |table|
      result = client.execute("DELETE
      FROM [#{@server_database}].[dbo].[#{table}]
      WHERE playerID = '#{player_to_delete}'")
      debug_msg "Rows deleted from [#{table}]: #{result.do}"
    end
  end
    result = client.execute("DELETE
    FROM [#{@server_database}].[dbo].[Log]")
  debug_msg "Rows deleted from [Log]: #{result.do}"
end

Easy enough to push the SQL-specific code out:

features/lib/db/account.rblink
2
3
4
5
6
7
8
9
def delete_all_test_accounts()
  client = connect_to_db(@server_database)
  result = client.execute("DELETE
    FROM [#{@server_database}].[dbo].[Account]
    WHERE account LIKE 'test%'
    AND email LIKE '%301days.com'")
  debug_msg "Rows deleted from [Account]: #{result.do}"
end
features/lib/db/player.rblink
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
def delete_all_test_players()
  client = connect_to_db(@server_database)
  player_tables = ["PlayerBelt","PlayerEffects","PlayerFlags","PlayerHeld","PlayerLocker",
    "PlayerQuests","PlayerRings","PlayerSack","PlayerSettings","PlayerSkills","PlayerSpells",
    "PlayerWearing","Player"]
  get_all_test_player_ids().each do |player_to_delete|
    debug_msg "Deleting player #{player_to_delete}"
    player_tables.each do |table|
      result = client.execute("DELETE
      FROM [#{@server_database}].[dbo].[#{table}]
      WHERE playerID = '#{player_to_delete}'")
      debug_msg "Rows deleted from [#{table}]: #{result.do}"
    end
  end
end
def get_all_test_player_ids()
  id_list = []
  client = connect_to_db(@server_database)
  query = "SELECT [playerID] FROM [#{@server_database}].[dbo].[Player] \
           WHERE account LIKE 'test%' \
           AND name LIKE 'Dude.%'"
  result = client.execute(query)
  result.each(:symbolize_keys => true) do |rowset|
    id_list << rowset[:playerID]
  end
  id_list
end
features/lib/db/log.rblink
27
28
29
30
31
32
def clear_log()
  client = connect_to_db(@server_database)
  result = client.execute("DELETE
    FROM [#{@server_database}].[dbo].[Log]")
  debug_msg "Rows deleted from [Log]: #{result.do}"
end
features/lib/hooks.rblink
43
44
45
46
47
After('@db_cleanup') do |scenario|
  delete_all_test_accounts()
  delete_all_test_players()
  clear_log()
end

Not very exciting, but will be necessary pretty soon. Let’s move on to something more exciting. Tomorrow.


Useful Stuff


Day 76b code - tests

Comments