コード例 #1
0
ファイル: tests.py プロジェクト: ccnmtl/mvsim
 def setUp(self):
     coeffs = StubCoeffs(birth_rate=BIRTH_RATE, death_rate=DEATH_RATE,
                         starting_year=2007,
                         avg_family_size=4,
                         recovery_rate0=1.0,
                         recovery_rate1=1.0,
                         recovery_rate2=1.0,
                         recovery_rate3=1.0,
                         mortality0=1.0,
                         mortality1=1.0,
                         base_infection_rate=1.0,
                         mortality2=1,
                         bednet_infection_modifier=1.0,
                         precipitation_infection_modifier=0,
                         avg_precipitation=200,
                         enable_NGO_offers=True,
                         road_subsidy_year=5,
                         other_subsidy_year=10,
                         enable_epidemic=True,
                         no_epidemics_before=10,
                         enable_free_bednets=True,
                         free_bednet_year=30,
                         fish_stock_warn_threshold=0.33,
                         wood_stock_warn_threshold=0.33,
                         available_improvements=['a', 'b'],
                         microfinance_base_interest=0.02,
                         microfinance_min_balance=100.0,
                         microfinance_repay_period=8,
                         microfinance_drought_effect=1,
                         microfinance_epidemic_effect=1,
                         max_precipitation=10,
                         drought_threshold=1,
                         enable_drought=True,
                         no_droughts_before=5,
                         )
     state = StubState(village_population=VILLAGE_POPULATION,
                       population=POPULATION,
                       clinic=False, irrigation=False, road=False,
                       sanitation=False, water_pump=False,
                       meals=False, electricity=False,
                       precipitation=PRECIPITATION,
                       health=[100],
                       year=2007,
                       season=False,
                       village_infected_pop=0,
                       owned_items=['bednet'],
                       epidemic=False,
                       tax_rate=0,
                       user_messages=[],
                       subsidy_offers=[],
                       microfinance_balance=0.0,
                       microfinance_borrow=0.0,
                       microfinance_current_interest_rate=0.0,
                       microfinance_interest_rate=0.0,
                       microfinance_amount_due=0.0,
                       microfinance_amount_paid=0.0,
                       microfinance_max_borrow=0.0,
                       )
     tc = StubTC()
     self.village = Village(state, coeffs, tc)
コード例 #2
0
ファイル: tests.py プロジェクト: ccnmtl/mvsim
class TestVillage(unittest.TestCase):
    def setUp(self):
        coeffs = StubCoeffs(birth_rate=BIRTH_RATE, death_rate=DEATH_RATE,
                            starting_year=2007,
                            avg_family_size=4,
                            recovery_rate0=1.0,
                            recovery_rate1=1.0,
                            recovery_rate2=1.0,
                            recovery_rate3=1.0,
                            mortality0=1.0,
                            mortality1=1.0,
                            base_infection_rate=1.0,
                            mortality2=1,
                            bednet_infection_modifier=1.0,
                            precipitation_infection_modifier=0,
                            avg_precipitation=200,
                            enable_NGO_offers=True,
                            road_subsidy_year=5,
                            other_subsidy_year=10,
                            enable_epidemic=True,
                            no_epidemics_before=10,
                            enable_free_bednets=True,
                            free_bednet_year=30,
                            fish_stock_warn_threshold=0.33,
                            wood_stock_warn_threshold=0.33,
                            available_improvements=['a', 'b'],
                            microfinance_base_interest=0.02,
                            microfinance_min_balance=100.0,
                            microfinance_repay_period=8,
                            microfinance_drought_effect=1,
                            microfinance_epidemic_effect=1,
                            max_precipitation=10,
                            drought_threshold=1,
                            enable_drought=True,
                            no_droughts_before=5,
                            )
        state = StubState(village_population=VILLAGE_POPULATION,
                          population=POPULATION,
                          clinic=False, irrigation=False, road=False,
                          sanitation=False, water_pump=False,
                          meals=False, electricity=False,
                          precipitation=PRECIPITATION,
                          health=[100],
                          year=2007,
                          season=False,
                          village_infected_pop=0,
                          owned_items=['bednet'],
                          epidemic=False,
                          tax_rate=0,
                          user_messages=[],
                          subsidy_offers=[],
                          microfinance_balance=0.0,
                          microfinance_borrow=0.0,
                          microfinance_current_interest_rate=0.0,
                          microfinance_interest_rate=0.0,
                          microfinance_amount_due=0.0,
                          microfinance_amount_paid=0.0,
                          microfinance_max_borrow=0.0,
                          )
        tc = StubTC()
        self.village = Village(state, coeffs, tc)

    def tearDown(self):
        self.village = None

    def test_update_population(self):
        self.village.update_population()
        print(self.village.state.village_population)
        assert self.village.state.village_population == VILLAGE_POPULATION

    def test_check_epidemic(self):
        self.village.check_epidemic()
        assert not self.village.state.epidemic
        self.village.state.village_infected_pop = 5.0
        self.village.state.village_population = 10.0
        self.village.check_epidemic()
        assert self.village.state.epidemic

    def test_avg_family_health(self):
        assert self.village.avg_family_health() == 100

    def test_village_health(self):
        assert self.village.village_health() == 1.0

    def test_family_bednets(self):
        assert self.village.family_bednets() == 1

    def test_bednet_modifier(self):
        assert self.village.bednet_modifier() == 2
        self.village.state.population = 0
        assert self.village.bednet_modifier() == 0

    def test_mortality(self):
        print(self.village.mortality())
        assert self.village.mortality() == 2.0

    def test_recovery_rate(self):
        assert self.village.recovery_rate() == 1

    def test_susceptible_pop(self):
        assert (self.village.susceptible_pop() ==
                self.village.state.village_population)

    def test_recovered(self):
        assert self.village.recovered() == 0

    def test_sickened(self):
        assert self.village.sickened() == 0

    def test_improvements_count(self):
        assert self.village.improvements_count() == 0

    def test_family_taxes(self):
        self.village.state.tax_rate = 10
        self.village.state.fund = 0
        assert self.village.family_taxes(100.0) == 10
        self.village.state.tax_rate = 0
        assert self.village.family_taxes(100.0) == 0

    def test_check_improvement_price(self):
        self.village.coeffs.available_improvements = ['a', 'b']
        self.village.coeffs.improvement_prices = [1, 2]
        assert self.village.check_improvement_price('a') == 1
        assert self.village.check_improvement_price('b') == 2

    def test_buy_improvements(self):
        self.village.coeffs.available_improvements = ['a', 'b']
        self.village.coeffs.improvement_prices = [1, 200]
        self.village.coeffs.subsidy_price_reduction = 1
        self.village.state.fund = 100
        self.village.state.improvements = ['a']
        self.village.buy_improvements()
        assert self.village.state.fund == 99
        assert self.village.state.a

        # try to buy one we can't afford
        self.village.state.improvements = ['b']
        self.village.buy_improvements()
        assert self.village.state.fund == 99
        assert not hasattr(self.village.state, 'b')

        self.village.state.subsidy_offers = ['b']
        self.village.coeffs.subsidy_price_reduction = .05
        self.village.state.fund = 150
        self.village.buy_improvements()
        print(self.village.state.fund)
        assert self.village.state.fund == 140

    def test_update_subsidy_offers(self):
        self.village.state.year = 2013
        self.village.state.road = False
        self.village.coeffs.enable_NGO_offers = True
        self.village.coeffs.road_subsidy_year = 2
        self.village.update_subsidy_offers()
        assert 'road' in self.village.state.subsidy_offers

        self.village.state.road = True
        self.village.state.year = 2030
        self.village.coeffs.other_subsidy_year = 4
        self.village.update_subsidy_offers()

    def test_calc_fish_stock(self):
        self.village.state.amount_fish = 5.0
        self.village.state.fish_stock = 5000.0
        self.village.coeffs.fish_growth_rate = 1.2
        self.village.coeffs.fish_k = 7500.0

        self.village.state.fish_stock = 4000000
        self.village.coeffs.fish_k = 4000000
        self.village.state.amount_fish = 917
        self.village.state.village_population = 251 * 4

        stock = self.village.calc_fish_stock()
        assert stock > 3792849 and stock < 3792850

    def test_update_fish_stock(self):
        self.village.state.amount_fish = 5.0
        self.village.state.fish_stock = 5000.0
        self.village.coeffs.fish_growth_rate = 1.2
        self.village.coeffs.fish_k = 7500.0
        self.village.update_fish_stock()

    def test_calc_wood_stock(self):
        self.village.state.amount_wood = 5.0
        self.village.state.wood_stock = 5000
        self.village.coeffs.forest_growth_rate = 1.2
        self.village.coeffs.wood_k = 7500.0

    def test_update_wood_stock(self):
        self.village.state.amount_wood = 5.0
        self.village.state.wood_stock = 5000
        self.village.coeffs.forest_growth_rate = 1.2
        self.village.coeffs.wood_k = 7500.0
        self.village.update_wood_stock()

    def test_calculate_taxes(self):
        self.village.state.avg_family_size = 4
        assert self.village.calculate_taxes(10.0) == 0
        self.village.state.tax_rate = 1.0

        # make sure that a zero population doesn't give us a divide by zero
        self.village.state.population = 0
        self.village.calculate_taxes(10.0)

    def test_update_precipitation(self):
        self.village.update_precipitation()