Example #1
0
    def test_q_method_use_prop(self):
        order = Order(price=100)
        order.delivered_time = timezone.now()
        order.save()

        pizza = Pizza(diameter=12, order=order, created=timezone.now())
        pizza.save()

        self.assertTrue(pizza.is_delivered)
Example #2
0
    def test_q_method_cant_use_self(self):
        order = Order(price=100)
        order.save()

        pizza = Pizza(diameter=12, order=order, created=timezone.now())
        pizza.save()

        with self.assertRaisesRegexp(Exception, 'q_method'):
            Pizza.objects.is_delivered_using_self()
Example #3
0
    def test_q_method_use_self(self):
        order = Order(price=100)
        order.delivered_time = timezone.now()
        order.save()

        pizza = Pizza(diameter=12, order=order, created=timezone.now())
        pizza.save()

        self.assertEqual(1, Pizza.objects.is_delivered_using_self().count())
Example #4
0
def read():
    """

    :return:
    """

    with open(os.path.join(settings.BASE_DIR, 'main', 'utils', 'from_dagrasso.txt')) as f:
        fl = f.readlines()
        pairs = int(len(fl) / 2)
        for pair_index in range(pairs):
            first_line = fl[pair_index * 2].replace('\n','')
            second_line = fl[pair_index * 2 + 1].replace('\n','')
            web_id = int(first_line.split(' ', 1)[0])
            name = first_line.split(' ', 1)[1]
            topping_list = [x.strip(' ') for x in second_line.split(',')]

            # Pizza create or update
            pizza_obj = Pizza.objects.filter(name=name).first()
            if not pizza_obj:
                pizza_obj = Pizza()
                pizza_obj.web_id = web_id
                pizza_obj.name = name
                pizza_obj.save()
                pizza_votes_obj = PizzaVotes()
                pizza_votes_obj.pizza = pizza_obj
                pizza_votes_obj.save()

            topics_id_list = []
            for topping_one in topping_list:
                # Topping create or get
                topping_obj = Topping.objects.filter(name=topping_one).first()
                if not topping_obj:
                    topping_obj = Topping()
                    topping_obj.name = topping_one
                    topping_obj.pizzas_ids_json = json.dumps([pizza_obj.id])
                    topping_votes_obj = ToppingVotes()
                    topping_votes_obj.topping = topping_obj
                    topping_votes_obj.save()
                else:
                    pizzas_ids = json.loads(topping_obj.pizzas_ids_json)
                    if pizza_obj.id not in pizzas_ids:
                        pizzas_ids.append(pizza_obj.id)

                    topping_obj.pizzas_ids_json = json.dumps(pizzas_ids)
                topping_obj.save()
                topics_id_list.append(topping_obj.id)
            pizza_obj.toppings_ids_json = json.dumps(topics_id_list)
            pizza_obj.save()

            logger.debug('%s %s --> %s', web_id, name, topping_list)
Example #5
0
    def test_example(self):
        order = Order(price=100, name_on_order='Bob')
        order.save()

        pizza = Pizza(diameter=12, order=order, created=timezone.now())
        pizza.save()

        self.assertEqual(0, Pizza.objects.is_delivered().count())
        self.assertEqual(0, Order.objects.is_delivered().count())
        self.assertFalse(pizza.is_delivered)

        order.delivered_time = timezone.now()
        order.save()

        self.assertEqual(1, Order.objects.is_delivered().count())
        self.assertEqual(1, Pizza.objects.is_delivered().count())
        self.assertTrue(pizza.is_delivered)
Example #6
0
    def test_example(self):
        order = Order(price=100, name_on_order="Bob")
        order.save()

        pizza = Pizza(diameter=12, order=order, created=timezone.now())
        pizza.save()

        self.assertEqual(0, Pizza.objects.is_delivered().count())
        self.assertEqual(0, Order.objects.is_delivered().count())
        self.assertFalse(pizza.is_delivered)

        order.delivered_time = timezone.now()
        order.save()

        self.assertEqual(1, Order.objects.is_delivered().count())
        self.assertEqual(1, Pizza.objects.is_delivered().count())
        self.assertTrue(pizza.is_delivered)
Example #7
0
    def test_q_method_used_from_other_model(self):
        """
        Use a @q_method from a different model in a queryset

        This test is a good example of a plausible use case for this.
        """

        order = Order(price=100)
        order.save()

        pizza = Pizza(diameter=12, order=order, created=timezone.now())
        pizza.save()

        self.assertEqual(0, Pizza.objects.is_delivered().count())

        order.delivered_time = timezone.now()
        order.save()

        self.assertEqual(1, Pizza.objects.is_delivered().count())
Example #8
0
    def test_q_method_used_from_other_model(self):
        """
        Use a @q_method from a different model in a queryset

        This test is a good example of a plausible use case for this.
        """

        order = Order(price=100)
        order.save()

        pizza = Pizza(diameter=12, order=order, created=timezone.now())
        pizza.save()

        self.assertEqual(0, Pizza.objects.is_delivered().count())

        order.delivered_time = timezone.now()
        order.save()

        self.assertEqual(1, Pizza.objects.is_delivered().count())