def test_farmer_crop_setting(self): farmer = utils.create_farmer() crop1 = utils.create_crop("apples") crop2 = utils.create_crop("oranges") crop3 = utils.create_crop("potatoes") farmer.grows_crop(crop1) self.assertIn(crop1, farmer.crops.all()) farmer.grows_crops_exclusively([crop2, crop3]) self.assertNotIn(crop1, farmer.crops.all()) self.assertIn(crop2, farmer.crops.all()) self.assertIn(crop3, farmer.crops.all())
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))
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 test_farmer_new_sale(self): # create sample crop data crop_names = ["onion", "potato", "apple"] other_crops = [utils.create_crop(crop_name) for crop_name in crop_names] receipt = self.take_in(10, 'boxes', 'tomato') response = self.client.get(self.farmer_url('new_sale')) self.assertContains(response, 'tomato') crop_receipts = self.agent.cropreceipts_available_for(self.farmer) crop_inventory = [cr.crop for cr in crop_receipts] for crop in other_crops: self.assertNotIn(crop, crop_inventory) self.assertNotContains(response, crop.name)
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())
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)
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
def test_farmer_crops(self): random_crops = [utils.create_crop(n) for n in ['tomatoe', 'potato', 'carrot']] receipt1 = self.take_in(10, 'boxes', 'apple') receipt2 = self.take_in(20, 'kilos', 'cabbage') response = self.client.get(self.farmer_url('crops')) self.assertContains(response, receipt1.crop.name) self.assertContains(response, receipt2.crop.name) response = self.client.post(self.farmer_url('crops'), { 'crops': [crop.pk for crop in random_crops] }, follow=True) # this should be a mass override for crop in random_crops: self.assertContains(response, crop.name) # previous selection should have been cleared self.assertNotContains(response, receipt1.crop.name) self.assertNotContains(response, receipt2.crop.name) self.assertRedirects(response, self.farmer_url('sales'))
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))