Ejemplo n.º 1
0
    def test_post(self):
        """ Test that the form submits """
        client = Client()

        gardener = GardenerFactory()
        plot = gardener.get_plots().first()
        client.force_login(gardener)

        # Add user to multiple gardens
        # https://github.com/HarvestHub/GardenHub/issues/88
        plot.garden.managers.add(gardener)
        GardenFactory(managers=[gardener])

        response = client.post(
            reverse('plot-update', args=[plot.id]), {
                'garden': plot.garden.id,
                'title': "New title",
                'gardener_emails': gardener.email,
                'crops': CropFactory().id
            }
        )

        # Ensure the correct response
        self.assertRedirects(response, reverse('plot-list'))

        # Check that the model updated
        plot.refresh_from_db()
        self.assertEqual(plot.title, "New title")
Ejemplo n.º 2
0
 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))
Ejemplo n.º 3
0
    def test_is_picker(self):
        """ user.is_picker() """

        # Test a normal user
        self.assertFalse(ActiveUserFactory().is_anything())

        # Create garden and assign a picker
        garden = GardenFactory()
        picker = ActiveUserFactory()
        garden.pickers.add(picker)

        # Test that the user is a picker
        self.assertTrue(picker.is_picker())
Ejemplo n.º 4
0
    def test_can_edit_garden(self):
        """ user.can_edit_garden() """

        # Garden manager can edit the garden
        manager = GardenManagerFactory()
        garden = manager.get_gardens().first()
        self.assertTrue(manager.can_edit_garden(garden))

        # Gardener can't edit the garden
        gardener = GardenerFactory()
        garden = gardener.get_plots().first().garden
        self.assertFalse(gardener.can_edit_garden(garden))

        # Normal user can't edit the garden
        self.assertFalse(ActiveUserFactory().can_edit_garden(GardenFactory()))
Ejemplo n.º 5
0
    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()))
Ejemplo n.º 6
0
    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()))
Ejemplo n.º 7
0
    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([]))
Ejemplo n.º 8
0
    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())
Ejemplo n.º 9
0
 def test_set_pickers(self):
     """ Test setting pickers """
     pickers = [ActiveUserFactory()]
     garden = GardenFactory(pickers=pickers)
     self.assertEqual(set(pickers), set(garden.pickers.all()))
Ejemplo n.º 10
0
 def test_set_managers(self):
     """ Test setting managers """
     managers = [ActiveUserFactory()]
     garden = GardenFactory(managers=managers)
     self.assertEqual(set(managers), set(garden.managers.all()))
Ejemplo n.º 11
0
 def test_create(self):
     """ Test creation """
     self.assertTrue(GardenFactory())
Ejemplo n.º 12
0
 def test_set_gardens(self):
     """ Make sure we can set gardens """
     gardens = GardenFactory.create_batch(5)
     manager = GardenManagerFactory(gardens=gardens)
     for garden in gardens:
         self.assertIn(manager, garden.managers.all())
Ejemplo n.º 13
0
 def test_garden_str(self):
     """
     Test the __str__ method of Garden.
     """
     garden = GardenFactory(title="Test Garden")
     self.assertEqual(str(garden), "Test Garden")
Ejemplo n.º 14
0
 def test_create_garden(self):
     """
     Ensure that an Garden can be created, saved, and retrieved.
     """
     garden = GardenFactory()
     self.assertIn(garden, Garden.objects.all())
Ejemplo n.º 15
0
 def test_get_picker_gardens(self):
     """ user.get_picker_gardens() """
     picker = ActiveUserFactory()
     gardens = GardenFactory.create_batch(4, pickers=[picker])
     GardenFactory.create_batch(3)
     self.assertEqual(set(picker.get_picker_gardens()), set(gardens))
Ejemplo n.º 16
0
 def test_get_gardens(self):
     """ user.get_gardens() """
     user = ActiveUserFactory()
     gardens = GardenFactory.create_batch(4, managers=[user])
     self.assertEqual(set(user.get_gardens()), set(gardens))