]> git.stg.codes - stg.git/blob - tests/test_fee_charge_rules.cpp
657a609ae036c9264e64201ea0d61957b4d198d1
[stg.git] / tests / test_fee_charge_rules.cpp
1 #include "tut/tut.hpp"
2
3 #include "stg/user_property.h"
4 #include "user_impl.h"
5
6 #include "testsettings.h"
7 #include "testtariffs.h"
8 #include "testadmin.h"
9 #include "teststore.h"
10
11 const volatile time_t stgTime = 0;
12
13 namespace tut
14 {
15     struct fee_charge_rules_data {
16     };
17
18     typedef test_group<fee_charge_rules_data> tg;
19     tg fee_charge_rules_test_group("Fee charge rules tests group");
20
21     typedef tg::object testobject;
22
23     class TEST_SETTINGS_LOCAL : public TEST_SETTINGS {
24         public:
25             TEST_SETTINGS_LOCAL(unsigned _feeChargeType)
26                 : feeChargeType(_feeChargeType)
27             {}
28
29             unsigned GetFeeChargeType() const { return feeChargeType; }
30
31         private:
32             unsigned feeChargeType;
33     };
34
35     template<>
36     template<>
37     void testobject::test<1>()
38     {
39         set_test_name("Check classic rules");
40
41         TEST_SETTINGS_LOCAL settings(0);
42         TEST_TARIFFS tariffs;
43         TEST_ADMIN admin;
44         TEST_STORE store;
45         USER_IMPL user(&settings, &store, &tariffs, &admin, NULL);
46
47         USER_PROPERTY<double> & cash(user.GetProperty().cash);
48         USER_PROPERTY<std::string> & tariffName(user.GetProperty().tariffName);
49
50         ensure_equals("user.cash == 0", user.GetProperty().cash, 0);
51         cash = 100;
52         ensure_equals("user.cash == 100", user.GetProperty().cash, 100);
53
54         tariffs.SetFee(50);
55         tariffName = "test";
56         ensure_equals("user.tariffName == 'test'", user.GetProperty().tariffName.ConstData(), "test");
57         user.ProcessDayFee();
58         ensure_equals("user.cash == 50", user.GetProperty().cash, 50);
59         user.ProcessDayFee();
60         ensure_equals("user.cash == 0", user.GetProperty().cash, 0);
61         user.ProcessDayFee();
62         ensure_equals("user.cash == -50", user.GetProperty().cash, -50);
63     }
64
65     template<>
66     template<>
67     void testobject::test<2>()
68     {
69         set_test_name("Check second rules (allow fee if cash value is positive)");
70
71         TEST_SETTINGS_LOCAL settings(1);
72         TEST_TARIFFS tariffs;
73         TEST_ADMIN admin;
74         TEST_STORE store;
75         USER_IMPL user(&settings, &store, &tariffs, &admin, NULL);
76
77         USER_PROPERTY<double> & cash(user.GetProperty().cash);
78         USER_PROPERTY<std::string> & tariffName(user.GetProperty().tariffName);
79
80         ensure_equals("user.cash == 0", user.GetProperty().cash, 0);
81         cash = 100;
82         ensure_equals("user.cash == 100", user.GetProperty().cash, 100);
83
84         tariffs.SetFee(50);
85         tariffName = "test";
86         ensure_equals("user.tariffName == 'test'", user.GetProperty().tariffName.ConstData(), "test");
87         user.ProcessDayFee();
88         ensure_equals("user.cash == 50", user.GetProperty().cash, 50);
89         user.ProcessDayFee();
90         ensure_equals("user.cash == 0", user.GetProperty().cash, 0);
91         user.ProcessDayFee();
92         ensure_equals("user.cash == 0", user.GetProperty().cash, 0);
93         cash = 49;
94         ensure_equals("user.cash == 49", user.GetProperty().cash, 49);
95         user.ProcessDayFee();
96         ensure_equals("user.cash == -1", user.GetProperty().cash, -1);
97     }
98
99     template<>
100     template<>
101     void testobject::test<3>()
102     {
103         set_test_name("Check third rules (allow fee if cash value is greater than fee)");
104
105         TEST_SETTINGS_LOCAL settings(2);
106         TEST_TARIFFS tariffs;
107         TEST_ADMIN admin;
108         TEST_STORE store;
109         USER_IMPL user(&settings, &store, &tariffs, &admin, NULL);
110
111         USER_PROPERTY<double> & cash(user.GetProperty().cash);
112         USER_PROPERTY<std::string> & tariffName(user.GetProperty().tariffName);
113
114         ensure_equals("user.cash == 0", user.GetProperty().cash, 0);
115         cash = 100;
116         ensure_equals("user.cash == 100", user.GetProperty().cash, 100);
117
118         tariffs.SetFee(50);
119         tariffName = "test";
120         ensure_equals("user.tariffName == 'test'", user.GetProperty().tariffName.ConstData(), "test");
121         user.ProcessDayFee();
122         ensure_equals("user.cash == 50", user.GetProperty().cash, 50);
123         user.ProcessDayFee();
124         ensure_equals("user.cash == 50", user.GetProperty().cash, 50);
125         tariffs.SetFee(49);
126         user.ProcessDayFee();
127         ensure_equals("user.cash == 1", user.GetProperty().cash, 1);
128         cash = 0;
129         ensure_equals("user.cash == 0", user.GetProperty().cash, 0);
130         user.ProcessDayFee();
131         ensure_equals("user.cash == 0", user.GetProperty().cash, 0);
132     }
133 }