PageRenderTime 42ms CodeModel.GetById 29ms app.highlight 3ms RepoModel.GetById 1ms app.codeStats 0ms

/src-extra/chaos/sql/chaos/server/actions/TestSupport.sql

http://github.com/JakeWheat/hssqlppp
SQL | 67 lines | 36 code | 6 blank | 25 comment | 2 complexity | 620ade95ffb82137c62ba65539196ed7 MD5 | raw file
Possible License(s): BSD-3-Clause
 1/*
 2Action test support
 3===================
 4
 5for testing purposes sometimes want to make a given nondeterministic
 6action always fail or always succeed.
 7
 8The categories are:
 9castle disappear
10gooey blob spread
11attack
12ranged attack
13resist: decree, lightning, subversion
14cast spell
15
16you have to set the override each time you want to override something
17
18*/
19
20select module('Chaos.Server.Actions.TestSupport');
21
22create domain random_test text check (value in
23       ('disappear', 'spread', 'attack',
24        'ranged_attack', 'resist', 'cast',
25        'bonus','break_engaged'));
26
27create table test_action_overrides (
28  override random_test unique,
29  setting bool
30);
31select set_relvar_type('test_action_overrides', 'data');
32
33create function action_rig_action_success(poverride random_test,
34       psetting boolean) returns void as $$
35begin
36  insert into test_action_overrides (override, setting)
37    values (poverride, psetting);
38end;
39$$ language plpgsql volatile;
40
41/*
42random numbers
43
44run all random tests through this, so that we can hook into them
45during testing.
46
47*/
48create function check_random_success(t random_test, successPercentage int)
49  returns boolean as $$
50declare
51  o boolean;
52begin
53  o := (select setting from test_action_overrides
54       where override = t);
55  if o is null then --normal random
56    return (random() * 100) < successPercentage;
57  else --overriden
58    delete from test_action_overrides
59      where override = t;
60    return o;
61  end if;
62end;
63$$ language plpgsql volatile;
64
65create function limit_chance(integer) returns integer as $$
66  select greatest(10, least($1, 100));
67$$ language sql immutable;