]> git.stg.codes - stg.git/blobdiff - tests/test_fee_charge_rules.cpp
Replace some unsigned types with size_t
[stg.git] / tests / test_fee_charge_rules.cpp
index 93b7dd6052fd33cfdd4deab78dab9262e9cb189b..0b55e5de5f2b96dc20f8619483cc28163bb787b3 100644 (file)
@@ -1,10 +1,12 @@
 #include "tut/tut.hpp"
 
 #include "tut/tut.hpp"
 
-#include "stg/settings.h"
 #include "stg/user_property.h"
 #include "user_impl.h"
 
 #include "stg/user_property.h"
 #include "user_impl.h"
 
-const volatile time_t stgTime = 0;
+#include "testsettings.h"
+#include "testtariffs.h"
+#include "testadmin.h"
+#include "teststore.h"
 
 namespace tut
 {
 
 namespace tut
 {
@@ -16,32 +18,15 @@ namespace tut
 
     typedef tg::object testobject;
 
 
     typedef tg::object testobject;
 
-    class TEST_SETTINGS : public SETTINGS {
+    class TEST_SETTINGS_LOCAL : public TEST_SETTINGS {
         public:
         public:
-            TEST_SETTINGS(unsigned _feeChargeType)
-                : feeChargeType(_feeChargeType) {}
-
-            const std::string & GetDirName(size_t) const { return dirName; }
-            const std::string & GetScriptsDir() const { return scriptsDir; }
-            unsigned            GetDetailStatWritePeriod() const { return 10; }
-            unsigned            GetStatWritePeriod() const { return 10; }
-            unsigned            GetDayFee() const { return 0; }
-            bool                GetFullFee() const { return false; }
-            unsigned            GetDayResetTraff() const { return 0; }
-            bool                GetSpreadFee() const { return false; }
-            bool                GetFreeMbAllowInet() const { return false; }
-            bool                GetDayFeeIsLastDay() const { return false; }
-            bool                GetWriteFreeMbTraffCost() const { return false; }
-            bool                GetShowFeeInCash() const { return false; }
-            unsigned            GetMessageTimeout() const { return 0; }
-            unsigned            GetFeeChargeType() const { return feeChargeType; }
-            const std::string & GetMonitorDir() const { return monitorDir; }
-            bool                GetMonitoring() const { return false; }
+            TEST_SETTINGS_LOCAL(unsigned _feeChargeType)
+                : feeChargeType(_feeChargeType)
+            {}
+
+            unsigned GetFeeChargeType() const { return feeChargeType; }
 
         private:
 
         private:
-            std::string dirName;
-            std::string scriptsDir;
-            std::string monitorDir;
             unsigned feeChargeType;
     };
 
             unsigned feeChargeType;
     };
 
@@ -51,13 +36,96 @@ namespace tut
     {
         set_test_name("Check classic rules");
 
     {
         set_test_name("Check classic rules");
 
-        TEST_SETTINGS settings(0);
-        USER_IMPL user(&settings, NULL, NULL, NULL, NULL);
+        TEST_SETTINGS_LOCAL settings(0);
+        TEST_TARIFFS tariffs;
+        TEST_ADMIN admin;
+        TEST_STORE store;
+        USER_IMPL user(&settings, &store, &tariffs, &admin, NULL);
+
+        USER_PROPERTY<double> & cash(user.GetProperty().cash);
+        USER_PROPERTY<std::string> & tariffName(user.GetProperty().tariffName);
+
+        ensure_equals("user.cash == 0", user.GetProperty().cash, 0);
+        cash = 100;
+        ensure_equals("user.cash == 100", user.GetProperty().cash, 100);
+
+        tariffs.SetFee(50);
+        tariffName = "test";
+        ensure_equals("user.tariffName == 'test'", user.GetProperty().tariffName.ConstData(), "test");
+        user.ProcessDayFee();
+        ensure_equals("user.cash == 50", user.GetProperty().cash, 50);
+        user.ProcessDayFee();
+        ensure_equals("user.cash == 0", user.GetProperty().cash, 0);
+        user.ProcessDayFee();
+        ensure_equals("user.cash == -50", user.GetProperty().cash, -50);
+    }
+
+    template<>
+    template<>
+    void testobject::test<2>()
+    {
+        set_test_name("Check second rules (allow fee if cash value is positive)");
+
+        TEST_SETTINGS_LOCAL settings(1);
+        TEST_TARIFFS tariffs;
+        TEST_ADMIN admin;
+        TEST_STORE store;
+        USER_IMPL user(&settings, &store, &tariffs, &admin, NULL);
 
         USER_PROPERTY<double> & cash(user.GetProperty().cash);
 
         USER_PROPERTY<double> & cash(user.GetProperty().cash);
+        USER_PROPERTY<std::string> & tariffName(user.GetProperty().tariffName);
 
         ensure_equals("user.cash == 0", user.GetProperty().cash, 0);
         cash = 100;
 
         ensure_equals("user.cash == 0", user.GetProperty().cash, 0);
         cash = 100;
-        ensure_equals("user.cash == 0", user.GetProperty().cash, 100);
+        ensure_equals("user.cash == 100", user.GetProperty().cash, 100);
+
+        tariffs.SetFee(50);
+        tariffName = "test";
+        ensure_equals("user.tariffName == 'test'", user.GetProperty().tariffName.ConstData(), "test");
+        user.ProcessDayFee();
+        ensure_equals("user.cash == 50", user.GetProperty().cash, 50);
+        user.ProcessDayFee();
+        ensure_equals("user.cash == 0", user.GetProperty().cash, 0);
+        user.ProcessDayFee();
+        ensure_equals("user.cash == 0", user.GetProperty().cash, 0);
+        cash = 49;
+        ensure_equals("user.cash == 49", user.GetProperty().cash, 49);
+        user.ProcessDayFee();
+        ensure_equals("user.cash == -1", user.GetProperty().cash, -1);
+    }
+
+    template<>
+    template<>
+    void testobject::test<3>()
+    {
+        set_test_name("Check third rules (allow fee if cash value is greater than fee)");
+
+        TEST_SETTINGS_LOCAL settings(2);
+        TEST_TARIFFS tariffs;
+        TEST_ADMIN admin;
+        TEST_STORE store;
+        USER_IMPL user(&settings, &store, &tariffs, &admin, NULL);
+
+        USER_PROPERTY<double> & cash(user.GetProperty().cash);
+        USER_PROPERTY<std::string> & tariffName(user.GetProperty().tariffName);
+
+        ensure_equals("user.cash == 0", user.GetProperty().cash, 0);
+        cash = 100;
+        ensure_equals("user.cash == 100", user.GetProperty().cash, 100);
+
+        tariffs.SetFee(50);
+        tariffName = "test";
+        ensure_equals("user.tariffName == 'test'", user.GetProperty().tariffName.ConstData(), "test");
+        user.ProcessDayFee();
+        ensure_equals("user.cash == 50", user.GetProperty().cash, 50);
+        user.ProcessDayFee();
+        ensure_equals("user.cash == 50", user.GetProperty().cash, 50);
+        tariffs.SetFee(49);
+        user.ProcessDayFee();
+        ensure_equals("user.cash == 1", user.GetProperty().cash, 1);
+        cash = 0;
+        ensure_equals("user.cash == 0", user.GetProperty().cash, 0);
+        user.ProcessDayFee();
+        ensure_equals("user.cash == 0", user.GetProperty().cash, 0);
     }
 }
     }
 }