def test_is_gardener(self): """ user.is_gardener() """ # Test a gardener of a single plot self.assertTrue(GardenerFactory().is_gardener()) # Test that a garden manager is also considered a gardener # if the garden has plots manager = GardenManagerFactory() garden = manager.get_gardens().first() PlotFactory.create_batch(3, garden=garden) self.assertTrue(manager.is_gardener()) # Create and test a *not* gardener self.assertFalse(ActiveUserFactory().is_gardener())
def test_is_order_picker(self): """ user.is_order_picker() """ picker = PickerFactory() plot = PlotFactory(garden=Garden.objects.get(pickers__id=picker.id)) order = OrderFactory(plot=plot) self.assertTrue(picker.is_order_picker(order)) self.assertFalse(ActiveUserFactory().is_order_picker(order))
def test_plot_str(self): """ Test the __str__ method of Plot. """ garden = GardenFactory() plot = PlotFactory(garden=garden) self.assertEqual(str(plot), "{} [{}]".format(garden.title, plot.title))
def test_can_edit_plot(self): """ user.can_edit_plot() """ # Gardener can edit the plot gardener = GardenerFactory() plot = gardener.get_plots().first() self.assertTrue(gardener.can_edit_plot(plot)) # Garden manager can edit the plot manager = GardenManagerFactory() garden = manager.get_gardens().first() plot = PlotFactory(garden=garden) self.assertTrue(manager.can_edit_plot(plot)) # Normal user can't edit the plot self.assertFalse(ActiveUserFactory().can_edit_plot(PlotFactory()))
def test_get_peers(self): """ user.get_peers() """ # Create test Gardens gardens = GardenFactory.create_batch(4) # Create test Plots plots = [ PlotFactory(garden=gardens[0]), PlotFactory(garden=gardens[2]), PlotFactory(garden=gardens[3]), PlotFactory(garden=gardens[3]) ] # Create test Users users = ActiveUserFactory.create_batch(10) # Garden with 2 managers and 1 plot with 1 gardener gardens[0].managers.add(users[0], users[1]) plots[0].gardeners.add(users[2]) self.assertEqual(set(users[0].get_peers()), set([users[1], users[2]])) self.assertEqual(set(users[1].get_peers()), set([users[0], users[2]])) self.assertEqual(set(users[2].get_peers()), set([])) # Garden with 2 managers and no plots gardens[1].managers.add(users[3], users[4]) self.assertEqual(set(users[3].get_peers()), set([users[4]])) self.assertEqual(set(users[4].get_peers()), set([users[3]])) # Garden with 0 managers and 1 plot with 2 gardeners gardens[2].managers.add(users[5], users[6]) self.assertEqual(set(users[5].get_peers()), set([users[6]])) self.assertEqual(set(users[6].get_peers()), set([users[5]])) # Garden with 1 manager and 2 plots, each with 1 gardener gardens[3].managers.add(users[7]) plots[2].gardeners.add(users[8]) plots[3].gardeners.add(users[9]) self.assertEqual(set(users[7].get_peers()), set([users[8], users[9]])) self.assertEqual(set(users[8].get_peers()), set([])) self.assertEqual(set(users[9].get_peers()), set([]))
def test_get_plots(self): """ user.get_plots() """ # Exclusively a gardener on a plot gardener = ActiveUserFactory() plot = PlotFactory(gardeners=[gardener]) self.assertEqual(set([plot]), set(gardener.get_plots())) # Exclusively a manager on a garden manager = ActiveUserFactory() garden = GardenFactory(managers=[manager]) plots = PlotFactory.create_batch(5, garden=garden) self.assertEqual(set(plots), set(manager.get_plots())) # Neither a gardener on a plot nor a manager on a garden nobody = ActiveUserFactory() self.assertEqual(nobody.get_plots().count(), 0) # Both a gardener on a plot and a manager on a garden godlike = ActiveUserFactory() garden = GardenFactory(managers=[godlike]) plots = PlotFactory.create_batch(5, garden=garden) plot = PlotFactory(gardeners=[godlike]) self.assertEqual(set(plots + [plot]), set(godlike.get_plots()))
def test_get_picker_orders(self): """ user.get_picker_orders() """ picker = PickerFactory() plot = PlotFactory(garden=picker.get_picker_gardens().first()) # Create orders orders = OrderFactory.create_batch( 3, plot=plot, start_date=localdate(2017, 1, 1), end_date=localdate(2017, 1, 5), requester=ActiveUserFactory() ) self.assertEqual(set(picker.get_picker_orders()), set(orders)) self.assertEqual(picker.get_picker_orders().count(), 3)
def test_can_edit_order(self): """ user.can_edit_order() """ # Gardener can edit the order gardener = GardenerFactory() order = OrderFactory(plot=gardener.get_plots().first()) self.assertTrue(gardener.can_edit_order(order)) # Garden manager can edit the order manager = GardenManagerFactory() garden = manager.get_gardens().first() plot = PlotFactory(garden=garden) order = OrderFactory(plot=plot) self.assertTrue(manager.can_edit_order(order)) # Normal user can't edit the order self.assertFalse(ActiveUserFactory().can_edit_order(OrderFactory()))
def test_inquirers(self): """ pick.inquirers() """ picker = ActiveUserFactory() requester = ActiveUserFactory() gardeners = ActiveUserFactory.create_batch(2) garden = GardenFactory() plot = PlotFactory(garden=garden) plot.gardeners.add(gardeners[0]) plot.gardeners.add(gardeners[1]) Order.objects.create( plot=plot, start_date=today()-timedelta(days=5), end_date=today()+timedelta(days=5), requester=requester ) pick = Pick.objects.create(picker=picker, plot=plot) self.assertIn(gardeners[0], list(pick.inquirers())) self.assertIn(gardeners[1], list(pick.inquirers())) self.assertIn(requester, list(pick.inquirers()))
def test_has_orders(self): """ user.has_orders() """ # Create plot garden = GardenFactory() plot = PlotFactory(garden=garden) # Create orders requester = ActiveUserFactory() OrderFactory.create_batch( 5, plot=plot, requester=requester ) # Create user and assign it to the plot user = ActiveUserFactory() plot.gardeners.add(user) # Test orders self.assertTrue(user.has_orders()) self.assertFalse(ActiveUserFactory().has_orders())
def test_set_plots(self): """ Make sure we can set plots """ plots = PlotFactory.create_batch(5) gardener = GardenerFactory(plots=plots) for plot in plots: self.assertIn(gardener, plot.gardeners.all())
def test_set_gardeners(self): """ Test setting gardeners """ gardeners = [ActiveUserFactory()] plot = PlotFactory(gardeners=gardeners) self.assertEqual(set(gardeners), set(plot.gardeners.all()))
def test_crop_count(self): """ Passing crop_count generates the given number of crops """ plot = PlotFactory(crop_count=5) self.assertEqual(plot.crops.count(), 5)
def test_no_crops(self): """ Make sure we can set no crops """ plot = PlotFactory(crops=[]) self.assertEqual(plot.crops.count(), 0)
def test_zero_default(self): """ By default, no crops are generated """ self.assertEqual(PlotFactory().crops.count(), 0)
def test_set_crops(self): """ Test setting crops """ crops = CropFactory.create_batch(5) plot = PlotFactory(crops=crops) self.assertEqual(set(crops), set(plot.crops.all()))
def test_create(self): """ Test creation """ self.assertTrue(PlotFactory())
def test_create_plot(self): """ Ensure that a Plot can be created, saved, and retrieved. """ plot = PlotFactory() self.assertIn(plot, Plot.objects.all())