コード例 #1
0
    def test_unshortened_units_have_pluralized_name_when_appropriate(self):
        am5 = Amount(unit=self.cup_unit, quantity=Decimal('108'))
        am6 = Amount(unit=self.cup_unit, quantity=Decimal('1.000'))
        am7 = Amount(unit=self.cup_unit, quantity=Decimal('0.025'))
        am8 = Amount(unit=self.cup_unit, quantity=Decimal('1'))

        self.assertIn('cups', am5.__str__())
        self.assertIn('cup', am6.__str__())
        self.assertIn('cups', am7.__str__())
        self.assertIn('cup', am8.__str__())
コード例 #2
0
ファイル: test_ingredient.py プロジェクト: Remy-TPP/remy-api
 def setUp(self):
     # TODO: should do this with mock objects?
     dish0 = Dish(name="The bestest dish")
     rec0 = Recipe(dish=dish0, title="Some awesome recipe")
     self.pr0 = Product(name="Harina 0000")
     self.pr1 = Product(name="Manzana")
     self.am0 = Amount(quantity=Decimal('10.01'))
     self.am1 = Amount(quantity=Decimal('5'))
     self.ing0 = Ingredient(product=self.pr0,
                            recipe=rec0,
                            quantity=Decimal('10.01'))
     self.ing1 = Ingredient(product=self.pr1,
                            recipe=rec0,
                            quantity=Decimal('5'))
コード例 #3
0
def cook_recipe(request):
    recipe_id = request.query_params.get('recipe_id')
    place_id = request.query_params.get('place_id')

    if not recipe_id:
        return Response({'message': 'recipe_id must be provided'},
                        status=status.HTTP_400_BAD_REQUEST)

    recipe = get_object_or_404(Recipe, id=recipe_id)
    place = get_place_or_default(request.user.profile, place_id)

    if not place:
        return Response(
            {'message': 'Must have at least one item in your inventory'},
            status=status.HTTP_404_NOT_FOUND,
        )

    interaction_serializer = InteractionSerializer(
        data={
            **request.data, 'recipe_id': recipe_id
        })
    interaction_serializer.is_valid(raise_exception=True)

    with transaction.atomic():
        interaction = interaction_serializer.save(profile=request.user.profile)
        interaction.cook()

        for ingredient in recipe.ingredient_set.all():
            # TODO: en la línea de abajo, que pasa si cocina con algo que no tiene?? sustitutos??
            item = place.inventory.filter(product=ingredient.product).first()
            if item:
                item.reduce_amount(
                    Amount(ingredient.quantity, ingredient.unit.id))

    return Response(interaction_serializer.data, status=status.HTTP_200_OK)
コード例 #4
0
    def test_convert_volume_to_unit(self):
        """ When converting 2L to unit with avg_unit_volume=1L, should return 2"""

        item = InventoryItem.objects.create(place=self.place,
                                            product=self.leche_descremada,
                                            quantity=2,
                                            unit=Unit.objects.get(name='unit'))

        amount = Amount(unit=Unit.objects.get(short_name='L'), quantity=2)

        obj, other = convert_to_correct_unit(item, amount)

        self.assertEqual(round(other.magnitude), 2)
        self.assertEqual(round((obj - other).magnitude), 0)
コード例 #5
0
    def test_convert_mass_to_unit(self):
        """ When converting 2kg to unit with avg_unit_weight=0.252kg, should return ~8"""

        item = InventoryItem.objects.create(place=self.place,
                                            product=self.manzana,
                                            quantity=2,
                                            unit=Unit.objects.get(name='unit'))

        amount = Amount(unit=Unit.objects.get(short_name='kg'), quantity=2)

        obj, other = convert_to_correct_unit(item, amount)

        self.assertEqual(round(other.magnitude), 8)
        self.assertEqual(round((obj - other).magnitude), -6)
コード例 #6
0
    def test_convert_unit_to_mass(self):
        """ When converting 1unit to g with avg_unit_weight=0.252kg, should return 252g"""

        item = InventoryItem.objects.create(
            place=self.place,
            product=self.manzana,
            quantity=1000,
            unit=Unit.objects.get(short_name='g'))

        amount = Amount(unit=Unit.objects.get(name='unit'), quantity=1)

        obj, other = convert_to_correct_unit(item, amount)

        self.assertEqual(round(other.magnitude), 252)
        self.assertEqual(round((obj - other).magnitude), 748)
コード例 #7
0
    def test_convert_volume_with_density_to_mass(self):
        """ When converting 0.4845L to kg with density=1032kg/m**3, should return 0.5kg"""

        item = InventoryItem.objects.create(
            place=self.place,
            product=self.leche,
            quantity=5,
            unit=Unit.objects.get(short_name='kg'))

        amount = Amount(unit=Unit.objects.get(short_name='L'), quantity=0.4845)

        obj, other = convert_to_correct_unit(item, amount)

        self.assertEqual(round(other.magnitude, 1), 0.5)
        self.assertEqual(round((obj - other).magnitude, 1), 4.5)
コード例 #8
0
    def test_convert_mass_with_density_to_volume(self):
        """ When converting 2064g to L with density=1032kg/m**3, should return 2L"""

        item = InventoryItem.objects.create(
            place=self.place,
            product=self.leche,
            quantity=5,
            unit=Unit.objects.get(short_name='L'))

        amount = Amount(unit=Unit.objects.get(short_name='g'), quantity=2064)

        obj, other = convert_to_correct_unit(item, amount)

        self.assertEqual(round(other.magnitude), 2)
        self.assertEqual(round((obj - other).magnitude), 3)
コード例 #9
0
ファイル: serializers.py プロジェクト: Remy-TPP/remy-api
    def create(self, validated_data):
        purchase_items_serializer = PurchaseItemSerializer(
            data=validated_data.pop('items'), many=True)
        if not purchase_items_serializer.is_valid():
            raise TypeError(purchase_items_serializer.errors)

        purchase = Purchase.objects.create()

        items_no_dupes = {}
        for purchase_item in purchase_items_serializer.save(purchase=purchase):
            if (existing_pi := items_no_dupes.get(purchase_item.product.name,
                                                  None)):
                existing_pi.add_amount(
                    Amount(purchase_item.quantity, purchase_item.unit.id))
            else:
                purchase_item.save()
                items_no_dupes[purchase_item.product.name] = purchase_item
コード例 #10
0
ファイル: serializers.py プロジェクト: Remy-TPP/remy-api
    def create(self, validated_data):
        product = validated_data.get('product')
        place = validated_data.get('place')

        if place:
            existing_items = place.inventory.filter(product=product)
            if existing_items.exists():
                item = existing_items.first()
                item.add_amount(
                    Amount(validated_data.get('quantity'),
                           validated_data.get('unit').id))
            else:
                item = InventoryItem.objects.create(**validated_data)
        else:
            place = PlaceSerializer.create(self, {'name': DEFAULT_PLACE_NAME})
            item = InventoryItem.objects.create(**validated_data, place=place)

        return item
コード例 #11
0
    def test_unplain_units_have_its_short_name(self):
        am2 = Amount(unit=self.kg_unit, quantity=Decimal('3.14'))
        am3 = Amount(unit=self.liter_unit, quantity=Decimal('2.718'))

        self.assertIn(self.plain_unit.short_name, am2.__str__())
        self.assertIn(self.liter_unit.short_name, am3.__str__())
コード例 #12
0
    def test_plain_unit_has_empty_short_name(self):
        am1 = Amount(unit=self.plain_unit, quantity=Decimal('3'))

        self.assertEqual(am1.__str__(), '3')