Ejemplo n.º 1
0
 def test_write_note(self):
     farmer = utils.create_farmer()
     agent = utils.create_agent()
     note = agent.write_note(farmer, 'this is a note about the farmer')
     self.assertIn(note, Note.objects.all())
     self.assertIn(note, farmer.actor.attachednote_set.all())
     self.assertIn(note, agent.notes_for(farmer))
Ejemplo n.º 2
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.º 3
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.º 4
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])
Ejemplo n.º 5
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.º 6
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.º 7
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.º 8
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.º 9
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.º 10
0
 def test_farmer_matching_redirect(self):
     farmer = utils.create_farmer()
     msisdn = farmer.actor.get_msisdns()[0]
     other_agent = utils.create_agent(msisdn='1')
     farmer.operates_at(self.market, other_agent)
     response = self.client.post(reverse('fncs:farmer_new'), {
         'msisdn1': msisdn,
         'name': 'name',
         'surname': 'surname',
         'markets': [self.market.pk],
         'matched_farmer': farmer.pk,
         'gender': 'M',
         })
     self.assertRedirects(response, reverse('fncs:farmer_edit', kwargs={
         'farmer_pk': farmer.pk,
         }))
     farmer = Farmer.objects.get(pk=farmer.pk)
     self.assertEqual(set([other_agent, self.agent]),
                      set(farmer.agent_farmer.all()))
 def handle(self, *args, **options):
     total = options['total']
     username = options['agent']
     farmer = options['farmer']
     if farmer:
         msisdns = [int(farmer)]
     else:
         msisdns = [2776123456 + i for i in range(total)]
     if username:
         actor = Actor.objects.get(user__username=username)
         agent = actor.as_agent()
         self.generate_farmers(msisdns, agent)
     else:
         # create the agent with msisdn offset of the total generated to
         # avoid collissions on usernames
         agent_msisdn = 2776123456 + total + 1
         agent = utils.create_agent(msisdn=str(agent_msisdn),
                     name=utils.random_name(),
                     surname=utils.random_surname())
         self.generate_farmers(msisdns, agent)
Ejemplo n.º 12
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))
Ejemplo n.º 13
0
 def test_send_farmer_message(self):
     farmer = utils.create_farmer()
     agent = utils.create_agent()
     message = agent.actor.send_message_to_farmer(farmer, 'hello world')
     self.assertIn(message, Message.objects.filter(sender=agent.actor,
                                                   recipient=farmer.actor))
Ejemplo n.º 14
0
 def test_actor_as_agent(self):
     agent = utils.create_agent()
     actor = agent.actor
     self.assertEquals(agent, actor.as_agent())
Ejemplo n.º 15
0
 def test_agent_creation(self):
     agent = utils.create_agent()
     self.assertEquals(agent.farmers.count(), 0)
     self.assertEquals(agent.markets.count(), 0)