Ejemplo n.º 1
0
 def test_farmer_market_setting(self):
     farmer = utils.create_farmer()
     market1 = utils.create_market("market 1", farmer.districts.all()[0])
     market2 = utils.create_market("market 2", farmer.districts.all()[0])
     market3 = utils.create_market("market 3", farmer.districts.all()[0])
     # prime the farmer with 1 market
     farmer.markets.add(market1)
     # test the destructive set
     farmer.operates_at_markets_exclusively([market2, market3])
     self.assertNotIn(market1, farmer.markets.all())
     self.assertIn(market2, farmer.markets.all())
     self.assertIn(market3, farmer.markets.all())
Ejemplo n.º 2
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))
Ejemplo n.º 3
0
    def setUp(self):
        self.province = utils.create_province('test province')
        self.district = utils.create_district('test district', self.province)
        self.ward = utils.create_ward('test ward', self.district)
        self.market = utils.create_market('test market', self.district)

        self.officer = utils.create_extension_officer()
        self.client.login(username=self.officer.actor.user.username, password=utils.PASSWORD)
Ejemplo n.º 4
0
    def test_farmer_districts(self):
        province = utils.create_province("province")
        district1 = utils.create_district("district 1", province)
        market1 = utils.create_market("market 1", district1)

        district2 = utils.create_district("district 2", province)
        market2 = utils.create_market("market 2", district2)

        farmer = utils.create_farmer()
        agent1 = utils.create_agent("agent 1")
        agent2 = utils.create_agent("agent 2")

        farmer.operates_at(market1, agent1)
        farmer.operates_at(market2, agent2)

        self.assertEquals(farmer.market_districts().count(), 2)
        self.assertIn(district1, farmer.market_districts())
        self.assertIn(district2, farmer.market_districts())
Ejemplo n.º 5
0
 def test_agent_crop_receipt_inventory(self):
     farmer1 = utils.create_farmer(msisdn="27700000000")
     farmer2 = utils.create_farmer(msisdn="27700000001")
     market = utils.create_market("market", farmer1.districts.all()[0])
     agent = utils.create_agent()
     receipt1 = utils.take_in(market, agent, farmer1, 10, 'box', 'tomato')
     receipt2 = utils.take_in(market, agent, farmer2, 10, 'box', 'onion')
     self.assertEqual([receipt1],
                      list(agent.cropreceipts_available_for(farmer1)))
     self.assertEqual([receipt2],
                      list(agent.cropreceipts_available_for(farmer2)))
Ejemplo n.º 6
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])
    def setUp(self):
        self.user_1, _ = User.objects.get_or_create(username="******",
                                                    first_name="user_first_1",
                                                    last_name="user_last_1")

        self.user_2, _ = User.objects.get_or_create(username="******",
                                                    first_name="user_first_2",
                                                    last_name="user_last_2")

        self.province = utils.create_province("province_name")
        self.district_1 = utils.create_district("district_1_name", self.province)
        self.district_2 = utils.create_district("district_2_name", self.province)
        self.market_1 = utils.create_market("market_1_name", self.district_1)
        self.market_2 = utils.create_market("market_1_name", self.district_2)
        self.farmer_1, _ = Farmer.objects.get_or_create(actor=self.user_1.get_profile())
        self.farmer_1.markets.add(self.market_1)
        self.farmer_1.markets.add(self.market_2)

        self.farmer_2, _ = Farmer.objects.get_or_create(actor=self.user_2.get_profile())
        self.farmer_2.markets.add(self.market_1)
        self.farmer_2.markets.add(self.market_2)
        self.farmer_2.districts.add(self.district_2)
Ejemplo n.º 8
0
    def setUp(self):
        self.province = utils.create_province('test province')
        self.district = utils.create_district('test district', self.province)
        self.ward = utils.create_ward('test ward', self.district)
        self.market = utils.create_market('test market', self.district)

        self.agent = utils.create_agent()

        self.farmers = list(create_random_farmers(10, self.agent, self.market))
        self.farmer = self.farmers[0]

        self.officer = utils.create_extension_officer()
        self.client.login(username=self.officer.actor.user.username, password=utils.PASSWORD)
Ejemplo n.º 9
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())
Ejemplo n.º 10
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)
Ejemplo n.º 11
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
Ejemplo n.º 12
0
    def setUp(self):
        self.client = Client()
        self.pin = '1234'
        self.province = utils.create_province('test province')
        self.district = utils.create_district('test district', self.province)
        self.ward = utils.create_ward('test ward', self.district)
        self.market = utils.create_market('test market', self.district)

        self.agent = utils.create_agent()
        self.msisdn = self.agent.actor.user.username

        identity = self.agent.actor.get_identity(self.msisdn)
        identity.set_pin(self.pin)
        identity.save()

        self.login_url = '%s?next=%s' % (reverse('login'), reverse('fncs:home'))
        self.farmers = list(create_random_farmers(10, self.agent, self.market))
        self.farmer = self.farmers[0]
Ejemplo n.º 13
0
 def handle_row(self, row):
     username = row['Mobile_Number_1']
     password = username[-4:]
     
     if User.objects.filter(username=username).exists():
         print 'User with username %s already exists' % username
         return
     
     user = User.objects.create_user(username, '*****@*****.**' % username,
         password)
     user.first_name = row['Agent_F_Name']
     user.last_name = row['Agent_S_Name']
     user.save()
     
     actor = user.get_profile()
     agent, _ = Agent.objects.get_or_create(actor=actor)
     province = utils.create_province("Unspecified Province")
     district = utils.create_district("Unspecified District", province)
     market = utils.create_market(row['Market1'], district)
     agent.markets.add(market)
Ejemplo n.º 14
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))
    def generate_farmers(self, msisdns, agent):
        for msisdn in msisdns:
            # create a district
            district = utils.random_district()
            farmergroup_name = "%s Farmer Group" % (district.name,)

            # create the farmer
            farmer = utils.create_farmer(msisdn=str(msisdn),
                                         name=utils.random_name(),
                                         surname=utils.random_surname(),
                                         )

            # cultivates two types of crops
            farmer.grows_crop(utils.random_crop())
            farmer.grows_crop(utils.random_crop())

            # create a market in the district
            market_name = '%s Market' % district.name
            market = utils.create_market(market_name, district)

            # have the farmer sells crops at that market through the agent
            farmer.operates_at(market, agent)