X-Git-Url: https://git.stg.codes/stg.git/blobdiff_plain/e9ae1f101b5418c0ba2e6c9d86b23c12f0140982..72797a0baa15ce75663a8dc662a132a6dd1a0de1:/tests/test_fee_charge_rules.cpp diff --git a/tests/test_fee_charge_rules.cpp b/tests/test_fee_charge_rules.cpp index b2a2790e..9dac1575 100644 --- a/tests/test_fee_charge_rules.cpp +++ b/tests/test_fee_charge_rules.cpp @@ -1,158 +1,155 @@ -#include "tut/tut.hpp" +#define BOOST_TEST_MODULE STGFeeChargeRules +#include "stg/admin.h" #include "stg/user_property.h" #include "user_impl.h" #include "testsettings.h" #include "testtariffs.h" -#include "testadmin.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 +#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& cash = user.GetProperties().cash; + STG::UserProperty& 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 tg; - tg fee_charge_rules_test_group("Fee charge rules tests group"); - - typedef tg::object testobject; - - class TEST_SETTINGS_LOCAL : public TEST_SETTINGS { - public: - TEST_SETTINGS_LOCAL(unsigned _feeChargeType) - : feeChargeType(_feeChargeType) - {} - - unsigned GetFeeChargeType() const { return feeChargeType; } - - private: - unsigned feeChargeType; - }; - - template<> - template<> - void testobject::test<1>() - { - set_test_name("Check classic rules"); - - TEST_SETTINGS_LOCAL settings(0); - TEST_TARIFFS tariffs; - TEST_ADMIN admin; - TEST_STORE store; - TEST_SERVICES services; - STG::UserImpl user(&settings, &store, &tariffs, &admin, NULL, services); - - STG::UserProperty & cash(user.GetProperties().cash); - STG::UserProperty & tariffName(user.GetProperties().tariffName); - - ensure_equals("user.cash == 0 (initial value)", user.GetProperties().cash, 0); - cash = 100; - ensure_equals("user.cash == 100 (explicitly set)", user.GetProperties().cash, 100); - - tariffs.SetFee(50); - tariffName = "test"; - ensure_equals("user.tariffName == 'test' (explicitly set)", user.GetProperties().tariffName.ConstData(), "test"); - user.ProcessDayFee(); - ensure_equals("user.cash == 50 (first fee charge)", user.GetProperties().cash, 50); - user.ProcessDayFee(); - ensure_equals("user.cash == 0 (second fee charge)", user.GetProperties().cash, 0); - user.ProcessDayFee(); - ensure_equals("user.cash == -50 (third fee charge)", user.GetProperties().cash, -50); - user.ProcessDayFee(); - ensure_equals("user.cash == -100 (fourth fee charge)", user.GetProperties().cash, -100); - } - - 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; - TEST_SERVICES services; - STG::UserImpl user(&settings, &store, &tariffs, &admin, NULL, services); - - STG::UserProperty & cash(user.GetProperties().cash); - STG::UserProperty & credit(user.GetProperties().credit); - STG::UserProperty & tariffName(user.GetProperties().tariffName); - - ensure_equals("user.cash == 0 (initial value)", user.GetProperties().cash, 0); - ensure_equals("user.credit == 0 (initial value)", user.GetProperties().credit, 0); - cash = 100; - ensure_equals("user.cash == 100 (explicitly set)", user.GetProperties().cash, 100); - - tariffs.SetFee(50); - tariffName = "test"; - ensure_equals("user.tariffName == 'test' (explicitly set)", user.GetProperties().tariffName.ConstData(), "test"); - user.ProcessDayFee(); - ensure_equals("user.cash == 50 (first fee charge)", user.GetProperties().cash, 50); - user.ProcessDayFee(); - ensure_equals("user.cash == 0 (second fee charge)", user.GetProperties().cash, 0); - user.ProcessDayFee(); - ensure_equals("user.cash == -50 (third fee charge)", user.GetProperties().cash, -50); - user.ProcessDayFee(); - ensure_equals("user.cash == -50 (not charging `cause value is negative)", user.GetProperties().cash, -50); - cash = 49; - ensure_equals("user.cash == 49 (explicitly set)", user.GetProperties().cash, 49); - user.ProcessDayFee(); - ensure_equals("user.cash == -1 (charge to negative value)", user.GetProperties().cash, -1); - user.ProcessDayFee(); - ensure_equals("user.cash == -1 (not charging `cause value is negative)", user.GetProperties().cash, -1); - credit = 50; - ensure_equals("user.credit == 50 (explicitly set)", user.GetProperties().credit, 50); - user.ProcessDayFee(); - ensure_equals("user.cash == -51 (charging `cause value + credit gives us a positive value)", user.GetProperties().cash, -51); - user.ProcessDayFee(); - ensure_equals("user.cash == -51 (not charging `cause credit now is not enoght)", user.GetProperties().cash, -51); - } - - 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; - TEST_SERVICES services; - STG::UserImpl user(&settings, &store, &tariffs, &admin, NULL, services); - - STG::UserProperty & cash(user.GetProperties().cash); - STG::UserProperty & credit(user.GetProperties().credit); - STG::UserProperty & tariffName(user.GetProperties().tariffName); - - ensure_equals("user.cash == 0 (initial value)", user.GetProperties().cash, 0); - cash = 100; - ensure_equals("user.cash == 100 (explicitly set)", user.GetProperties().cash, 100); - - tariffs.SetFee(50); - tariffName = "test"; - ensure_equals("user.tariffName == 'test' (explicitly set)", user.GetProperties().tariffName.ConstData(), "test"); - user.ProcessDayFee(); - ensure_equals("user.cash == 50 (first fee charge)", user.GetProperties().cash, 50); - user.ProcessDayFee(); - ensure_equals("user.cash == 0 (second fee charge)", user.GetProperties().cash, 0); - user.ProcessDayFee(); - ensure_equals("user.cash == 0 (not charging `cause value is lower than fee)", user.GetProperties().cash, 0); - cash = 50; - ensure_equals("user.cash == 50 (explicitly set)", user.GetProperties().cash, 50); - tariffs.SetFee(51); - user.ProcessDayFee(); - ensure_equals("user.cash == 50 (not charging `cause value is lower than fee)", user.GetProperties().cash, 50); - cash = 0; - ensure_equals("user.cash == 0 (explicitly set)", user.GetProperties().cash, 0); - credit = 51; - ensure_equals("user.credit == 51 (explicitly set)", user.GetProperties().credit, 51); - user.ProcessDayFee(); - ensure_equals("user.cash == -51 (charging `cause value + credit gives us a value greater than fee)", user.GetProperties().cash, -51); - user.ProcessDayFee(); - ensure_equals("user.cash == -51 (not charging `cause credit now is not enought)", user.GetProperties().cash, -51); - } + 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 & cash(user.GetProperties().cash); + STG::UserProperty & credit(user.GetProperties().credit); + STG::UserProperty & 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 & cash(user.GetProperties().cash); + STG::UserProperty & credit(user.GetProperties().credit); + STG::UserProperty & 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()