Example #1
0
    def test_market_monitor_registration(self):
        monitor = utils.create_market_monitor()
        province = utils.create_province("province")
        rpiarea = utils.create_rpiarea("rpiarea")
        rpiarea.provinces.add(province)
        district = utils.create_district("district", province)
        market = utils.create_market("market", district)

        crop = utils.create_crop("potatoes")
        unit = utils.create_crop_unit("boxes")
        price_floor = 150
        price_ceiling = 200

        offer = monitor.register_offer(market, crop, unit, price_floor,
                                       price_ceiling)
        self.assertTrue(monitor.is_monitoring(market))
        self.assertIn(market, monitor.markets.all())
        for rpiarea in market.rpiareas():
            self.assertIn(rpiarea, monitor.rpiareas.all())
        self.assertEquals(offer.price_floor, 150.0)
        self.assertEquals(offer.price_ceiling, 200.0)
        self.assertEquals(offer.crop, crop)
        self.assertEquals(offer.unit, unit)
        self.assertEquals(offer.market, market)
        self.assertAlmostEqual(offer.created_at, datetime.now(),
                               delta=timedelta(seconds=2))
Example #2
0
    def test_crops(self):
        farmer = utils.create_farmer()
        market = utils.create_market("market", farmer.districts.all()[0])
        agent = utils.create_agent()

        crop = utils.create_crop("potatoes")
        unit = utils.create_crop_unit("boxes")
        amount = 10

        receipt = agent.take_in_crop(market, farmer, amount, unit, crop)

        self.assertEquals(list(market.crops()), [crop])
Example #3
0
    def test_farmer_agent_link(self):
        farmer = utils.create_farmer()
        market = utils.create_market("market", farmer.districts.all()[0])
        agent = utils.create_agent()

        crop = utils.create_crop("potatoes")
        unit = utils.create_crop_unit("boxes")
        amount = 10

        agent.take_in_crop(market, farmer, amount, unit, crop)

        self.assertTrue(agent.is_selling_for(farmer, market))
        self.assertIn(market, agent.markets.all())
        self.assertIn(farmer, agent.farmers.all())
        self.assertIn(market, farmer.markets.all())
Example #4
0
    def test_price_history(self):
        farmer = utils.create_farmer()
        market = utils.create_market("market", farmer.districts.all()[0])
        agent = utils.create_agent()

        crop = utils.create_crop("potatoes")
        unit = utils.create_crop_unit("boxes")
        price = 20
        amount = 10

        for i in range(100):
            receipt = agent.take_in_crop(market, farmer, amount, unit, crop)
            transaction = agent.register_sale(receipt, amount, price)

        price_history = Transaction.price_history_for(market, crop, unit)
        self.assertEquals(list(price_history), [20.0] * 100)
Example #5
0
    def create_highest_markets(self, prices):
        farmer = utils.create_farmer()
        markets = [
            utils.create_market("market %d" % i, farmer.districts.all()[0])
            for i in range(len(prices))]
        crop = utils.create_crop("potatoes")
        unit = utils.create_crop_unit("boxes")
        agent = utils.create_agent()
        amount = 10

        for market, price in zip(markets, prices):
            for i in range(10):
                receipt = agent.take_in_crop(market, farmer, amount, unit,
                                             crop)
                agent.register_sale(receipt, amount, price)

        return crop, markets
Example #6
0
    def test_agent_sale(self):
        farmer = utils.create_farmer()
        market = utils.create_market("market", farmer.districts.all()[0])
        agent = utils.create_agent()

        crop = utils.create_crop("potatoes")
        unit = utils.create_crop_unit("boxes")
        price = 20
        amount = 10

        # create inventory
        receipt = agent.take_in_crop(market, farmer, amount, unit, crop)
        self.assertTrue(receipt.remaining_inventory(), 10)
        transaction = agent.register_sale(receipt, amount, price)

        self.assertTrue(agent.is_selling_for(farmer, market))
        self.assertIn(market, agent.markets.all())
        self.assertIn(farmer, agent.farmers.all())
        # test the transaction aspect
        self.assertEquals(transaction.total, 200.0)
        self.assertEquals(transaction.price, price)
        self.assertEquals(transaction.amount, amount)
        # test the selling out of the inventory
        crop_receipt = transaction.crop_receipt
        self.assertEquals(crop_receipt.crop, crop)
        self.assertEquals(crop_receipt.unit, unit)
        self.assertEquals(crop_receipt.agent, agent)
        self.assertEquals(crop_receipt.farmer, farmer)
        self.assertEquals(crop_receipt.market, market)

        # had 20 crops in inventory, inventory should be reconciled
        # and the calculated stock count should reflect this
        self.assertEquals(crop_receipt.amount, 10)
        self.assertTrue(crop_receipt.reconciled)
        self.assertEquals(crop_receipt.remaining_inventory(), 0)

        self.assertAlmostEqual(transaction.created_at, datetime.now(),
                               delta=timedelta(seconds=2))
        self.assertIn(transaction, farmer.transactions())
        self.assertTrue(farmer.is_growing_crop(crop))
        self.assertIn(transaction, agent.sales_for(farmer))