X-Git-Url: https://git.stg.codes/stg.git/blobdiff_plain/9d0859536c6590903c7348250847ef608bcba14a..581728513ab4c81fd2943f3ee6f43d8f8682e4e7:/tests/test_fee_charge_rules.cpp?ds=sidebyside diff --git a/tests/test_fee_charge_rules.cpp b/tests/test_fee_charge_rules.cpp index e233ae34..825c2b36 100644 --- a/tests/test_fee_charge_rules.cpp +++ b/tests/test_fee_charge_rules.cpp @@ -1,10 +1,12 @@ #include "tut/tut.hpp" -#include "stg/settings.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 { @@ -16,69 +18,137 @@ namespace tut typedef tg::object testobject; - class TEST_SETTINGS : public SETTINGS { + class TEST_SETTINGS_LOCAL : public TEST_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; } + TEST_SETTINGS_LOCAL(unsigned _feeChargeType) + : feeChargeType(_feeChargeType) + {} + + unsigned GetFeeChargeType() const { return feeChargeType; } private: - std::string dirName; - std::string scriptsDir; - std::string monitorDir; unsigned feeChargeType; }; - class TEST_TARIFFS : public TARIFFS { - public: - TEST_TARIFFS() {} + template<> + template<> + void testobject::test<1>() + { + set_test_name("Check classic rules"); - 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; } + TEST_SETTINGS_LOCAL settings(0); + TEST_TARIFFS tariffs; + TEST_ADMIN admin; + TEST_STORE store; + USER_IMPL user(&settings, &store, &tariffs, &admin, NULL); - void GetTariffsData(std::list * tdl) {} + USER_PROPERTY & cash(user.GetProperty().cash); + USER_PROPERTY & tariffName(user.GetProperty().tariffName); - const std::string & GetStrError() const { return strError; } + ensure_equals("user.cash == 0 (initial value)", user.GetProperty().cash, 0); + cash = 100; + ensure_equals("user.cash == 100 (explicitly set)", user.GetProperty().cash, 100); + + tariffs.SetFee(50); + tariffName = "test"; + ensure_equals("user.tariffName == 'test' (explicitly set)", user.GetProperty().tariffName.ConstData(), "test"); + user.ProcessDayFee(); + ensure_equals("user.cash == 50 (first fee charge)", user.GetProperty().cash, 50); + user.ProcessDayFee(); + ensure_equals("user.cash == 0 (second fee charge)", user.GetProperty().cash, 0); + user.ProcessDayFee(); + ensure_equals("user.cash == -50 (third fee charge)", user.GetProperty().cash, -50); + user.ProcessDayFee(); + ensure_equals("user.cash == -100 (fourth fee charge)", user.GetProperty().cash, -100); + } - private: - std::string strError; - }; + 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 & cash(user.GetProperty().cash); + USER_PROPERTY & credit(user.GetProperty().credit); + USER_PROPERTY & tariffName(user.GetProperty().tariffName); + + ensure_equals("user.cash == 0 (initial value)", user.GetProperty().cash, 0); + ensure_equals("user.credit == 0 (initial value)", user.GetProperty().credit, 0); + cash = 100; + ensure_equals("user.cash == 100 (explicitly set)", user.GetProperty().cash, 100); + + tariffs.SetFee(50); + tariffName = "test"; + ensure_equals("user.tariffName == 'test' (explicitly set)", user.GetProperty().tariffName.ConstData(), "test"); + user.ProcessDayFee(); + ensure_equals("user.cash == 50 (first fee charge)", user.GetProperty().cash, 50); + user.ProcessDayFee(); + ensure_equals("user.cash == 0 (second fee charge)", user.GetProperty().cash, 0); + user.ProcessDayFee(); + ensure_equals("user.cash == -50 (third fee charge)", user.GetProperty().cash, -50); + user.ProcessDayFee(); + ensure_equals("user.cash == -50 (not charging `cause value is negative)", user.GetProperty().cash, -50); + cash = 49; + ensure_equals("user.cash == 49 (explicitly set)", user.GetProperty().cash, 49); + user.ProcessDayFee(); + ensure_equals("user.cash == -1 (charge to negative value)", user.GetProperty().cash, -1); + user.ProcessDayFee(); + ensure_equals("user.cash == -1 (not charging `cause value is negative)", user.GetProperty().cash, -1); + credit = 50; + ensure_equals("user.credit == 50 (explicitly set)", user.GetProperty().credit, 50); + user.ProcessDayFee(); + ensure_equals("user.cash == -51 (charging `cause value + credit gives us a positive value)", user.GetProperty().cash, -51); + user.ProcessDayFee(); + ensure_equals("user.cash == -51 (not charging `cause credit now is not enoght)", user.GetProperty().cash, -51); + } template<> template<> - void testobject::test<1>() + void testobject::test<3>() { - set_test_name("Check classic rules"); + set_test_name("Check third rules (allow fee if cash value is greater than fee)"); - TEST_SETTINGS settings(0); + TEST_SETTINGS_LOCAL settings(2); TEST_TARIFFS tariffs; - USER_IMPL user(&settings, NULL, &tariffs, NULL, NULL); + TEST_ADMIN admin; + TEST_STORE store; + USER_IMPL user(&settings, &store, &tariffs, &admin, NULL); USER_PROPERTY & cash(user.GetProperty().cash); + USER_PROPERTY & credit(user.GetProperty().credit); + USER_PROPERTY & tariffName(user.GetProperty().tariffName); - ensure_equals("user.cash == 0", user.GetProperty().cash, 0); + ensure_equals("user.cash == 0 (initial value)", user.GetProperty().cash, 0); cash = 100; - ensure_equals("user.cash == 0", user.GetProperty().cash, 100); + ensure_equals("user.cash == 100 (explicitly set)", user.GetProperty().cash, 100); + + tariffs.SetFee(50); + tariffName = "test"; + ensure_equals("user.tariffName == 'test' (explicitly set)", user.GetProperty().tariffName.ConstData(), "test"); + user.ProcessDayFee(); + ensure_equals("user.cash == 50 (first fee charge)", user.GetProperty().cash, 50); + user.ProcessDayFee(); + ensure_equals("user.cash == 0 (second fee charge)", user.GetProperty().cash, 0); + user.ProcessDayFee(); + ensure_equals("user.cash == 0 (not charging `cause value is lower than fee)", user.GetProperty().cash, 0); + cash = 50; + ensure_equals("user.cash == 50 (explicitly set)", user.GetProperty().cash, 50); + tariffs.SetFee(51); + user.ProcessDayFee(); + ensure_equals("user.cash == 50 (not charging `cause value is lower than fee)", user.GetProperty().cash, 50); + cash = 0; + ensure_equals("user.cash == 0 (explicitly set)", user.GetProperty().cash, 0); + credit = 51; + ensure_equals("user.credit == 51 (explicitly set)", user.GetProperty().credit, 51); + user.ProcessDayFee(); + ensure_equals("user.cash == -51 (charging `cause value + credit gives us a value greater than fee)", user.GetProperty().cash, -51); + user.ProcessDayFee(); + ensure_equals("user.cash == -51 (not charging `cause credit now is not enought)", user.GetProperty().cash, -51); } }