]> git.stg.codes - stg.git/blobdiff - tests/test_fee_charge_rules.cpp
Enable binaries and mods in test builds.
[stg.git] / tests / test_fee_charge_rules.cpp
index e233ae34d60514b4e275550f286189c134c97ab1..9dac157587e8408f2d4114fe3498a202cf10ced6 100644 (file)
-#include "tut/tut.hpp"
+#define BOOST_TEST_MODULE STGFeeChargeRules
 
 
-#include "stg/settings.h"
+#include "stg/admin.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 "teststore.h"
+#include "testservices.h"
 
 
-namespace tut
+#pragma GCC diagnostic push
+#pragma GCC diagnostic ignored "-Wold-style-cast"
+#pragma GCC diagnostic ignored "-Wunused-parameter"
+#pragma GCC diagnostic ignored "-Wsign-compare"
+#pragma GCC diagnostic ignored "-Wparentheses"
+#include <boost/test/unit_test.hpp>
+#pragma GCC diagnostic pop
+
+volatile time_t stgTime = 0;
+
+namespace
+{
+
+class Settings : public TestSettings
+{
+    public:
+        Settings(unsigned feeChargeType)
+            : m_feeChargeType(feeChargeType)
+        {}
+
+        unsigned GetFeeChargeType() const { return m_feeChargeType; }
+
+    private:
+        unsigned m_feeChargeType;
+};
+
+}
+
+BOOST_AUTO_TEST_SUITE(FeeChargeRules)
+
+BOOST_AUTO_TEST_CASE(ClassicRules)
+{
+    Settings settings(0);
+    TestTariffs tariffs;
+    STG::Admin admin(STG::Priv(0xFFFF), {}, {});
+    TestStore store;
+    TestServices services;
+    STG::UserImpl user(&settings, &store, &tariffs, &admin, NULL, services);
+
+    STG::UserProperty<double>& cash = user.GetProperties().cash;
+    STG::UserProperty<std::string>& tariffName = user.GetProperties().tariffName;
+
+    BOOST_CHECK_EQUAL(user.GetProperties().cash, 0);
+    cash = 100;
+    BOOST_CHECK_EQUAL(user.GetProperties().cash, 100);
+
+    tariffs.SetFee(50);
+    tariffName = "test";
+    BOOST_CHECK_EQUAL(user.GetProperties().tariffName.ConstData(), "test");
+    user.ProcessDayFee();
+    BOOST_CHECK_EQUAL(user.GetProperties().cash, 50);
+    user.ProcessDayFee();
+    BOOST_CHECK_EQUAL(user.GetProperties().cash, 0);
+    user.ProcessDayFee();
+    BOOST_CHECK_EQUAL(user.GetProperties().cash, -50);
+    user.ProcessDayFee();
+    BOOST_CHECK_EQUAL(user.GetProperties().cash, -100);
+}
+
+BOOST_AUTO_TEST_CASE(PositiveCashRules)
 {
 {
-    struct fee_charge_rules_data {
-    };
-
-    typedef test_group<fee_charge_rules_data> tg;
-    tg fee_charge_rules_test_group("Fee charge rules tests group");
-
-    typedef tg::object testobject;
-
-    class TEST_SETTINGS : public SETTINGS {
-        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; }
-
-        private:
-            std::string dirName;
-            std::string scriptsDir;
-            std::string monitorDir;
-            unsigned feeChargeType;
-    };
-
-    class TEST_TARIFFS : public TARIFFS {
-        public:
-            TEST_TARIFFS() {}
-
-            int            ReadTariffs () { return 0; }
-            const TARIFF * FindByName(const std::string & name) const { return NULL; }
-            const TARIFF * GetNoTariff() const { return NULL; }
-            int            GetTariffsNum() const { return 0; }
-            int            Del(const std::string & name, const ADMIN * admin) { return 0; }
-            int            Add(const std::string & name, const ADMIN * admin) { return 0; }
-            int            Chg(const TARIFF_DATA & td, const ADMIN * admin) { return 0; }
-
-            void           GetTariffsData(std::list<TARIFF_DATA> * tdl) {}
-
-            const std::string & GetStrError() const { return strError; }
-
-        private:
-            std::string strError;
-    };
-
-    template<>
-    template<>
-    void testobject::test<1>()
-    {
-        set_test_name("Check classic rules");
-
-        TEST_SETTINGS settings(0);
-        TEST_TARIFFS tariffs;
-        USER_IMPL user(&settings, NULL, &tariffs, NULL, NULL);
-
-        USER_PROPERTY<double> & cash(user.GetProperty().cash);
-
-        ensure_equals("user.cash == 0", user.GetProperty().cash, 0);
-        cash = 100;
-        ensure_equals("user.cash == 0", user.GetProperty().cash, 100);
-    }
+    Settings settings(1);
+    TestTariffs tariffs;
+    STG::Admin admin(STG::Priv(0xFFFF), {}, {});
+    TestStore store;
+    TestServices services;
+    STG::UserImpl user(&settings, &store, &tariffs, &admin, NULL, services);
+
+    STG::UserProperty<double> & cash(user.GetProperties().cash);
+    STG::UserProperty<double> & credit(user.GetProperties().credit);
+    STG::UserProperty<std::string> & tariffName(user.GetProperties().tariffName);
+
+    BOOST_CHECK_EQUAL(user.GetProperties().cash, 0);
+    BOOST_CHECK_EQUAL(user.GetProperties().credit, 0);
+    cash = 100;
+    BOOST_CHECK_EQUAL(user.GetProperties().cash, 100);
+
+    tariffs.SetFee(50);
+    tariffName = "test";
+    BOOST_CHECK_EQUAL(user.GetProperties().tariffName.ConstData(), "test");
+    user.ProcessDayFee();
+    BOOST_CHECK_EQUAL(user.GetProperties().cash, 50);
+    user.ProcessDayFee();
+    BOOST_CHECK_EQUAL(user.GetProperties().cash, 0);
+    user.ProcessDayFee();
+    BOOST_CHECK_EQUAL(user.GetProperties().cash, -50);
+    user.ProcessDayFee();
+    BOOST_CHECK_EQUAL(user.GetProperties().cash, -50);
+    cash = 49;
+    BOOST_CHECK_EQUAL(user.GetProperties().cash, 49);
+    user.ProcessDayFee();
+    BOOST_CHECK_EQUAL(user.GetProperties().cash, -1);
+    user.ProcessDayFee();
+    BOOST_CHECK_EQUAL(user.GetProperties().cash, -1);
+    credit = 50;
+    BOOST_CHECK_EQUAL(user.GetProperties().credit, 50);
+    user.ProcessDayFee();
+    BOOST_CHECK_EQUAL(user.GetProperties().cash, -51);
+    user.ProcessDayFee();
+    BOOST_CHECK_EQUAL(user.GetProperties().cash, -51);
 }
 }
+
+BOOST_AUTO_TEST_CASE(GreaterThanFeeRules)
+{
+    Settings settings(2);
+    TestTariffs tariffs;
+    STG::Admin admin(STG::Priv(0xFFFF), {}, {});
+    TestStore store;
+    TestServices services;
+    STG::UserImpl user(&settings, &store, &tariffs, &admin, NULL, services);
+
+    STG::UserProperty<double> & cash(user.GetProperties().cash);
+    STG::UserProperty<double> & credit(user.GetProperties().credit);
+    STG::UserProperty<std::string> & tariffName(user.GetProperties().tariffName);
+
+    BOOST_CHECK_EQUAL(user.GetProperties().cash, 0);
+    cash = 100;
+    BOOST_CHECK_EQUAL(user.GetProperties().cash, 100);
+
+    tariffs.SetFee(50);
+    tariffName = "test";
+    BOOST_CHECK_EQUAL(user.GetProperties().tariffName.ConstData(), "test");
+    user.ProcessDayFee();
+    BOOST_CHECK_EQUAL(user.GetProperties().cash, 50);
+    user.ProcessDayFee();
+    BOOST_CHECK_EQUAL(user.GetProperties().cash, 0);
+    user.ProcessDayFee();
+    BOOST_CHECK_EQUAL(user.GetProperties().cash, 0);
+    cash = 50;
+    BOOST_CHECK_EQUAL(user.GetProperties().cash, 50);
+    tariffs.SetFee(51);
+    user.ProcessDayFee();
+    BOOST_CHECK_EQUAL(user.GetProperties().cash, 50);
+    cash = 0;
+    BOOST_CHECK_EQUAL(user.GetProperties().cash, 0);
+    credit = 51;
+    BOOST_CHECK_EQUAL(user.GetProperties().credit, 51);
+    user.ProcessDayFee();
+    BOOST_CHECK_EQUAL(user.GetProperties().cash, -51);
+    user.ProcessDayFee();
+    BOOST_CHECK_EQUAL(user.GetProperties().cash, -51);
+}
+
+BOOST_AUTO_TEST_SUITE_END()