Best Python code snippet using fMBT_python
test_stock_availability.py
Source:test_stock_availability.py
1import pytest2from ...core.exceptions import InsufficientStock3from ..availability import (4 _get_available_quantity,5 check_stock_quantity,6 check_stock_quantity_bulk,7)8COUNTRY_CODE = "US"9def test_check_stock_quantity(variant_with_many_stocks, channel_USD):10 assert (11 check_stock_quantity(12 variant_with_many_stocks, COUNTRY_CODE, channel_USD.slug, 713 )14 is None15 )16def test_check_stock_quantity_out_of_stock(variant_with_many_stocks, channel_USD):17 with pytest.raises(InsufficientStock):18 check_stock_quantity(19 variant_with_many_stocks, COUNTRY_CODE, channel_USD.slug, 820 )21def test_check_stock_quantity_with_allocations(22 variant_with_many_stocks,23 order_line_with_allocation_in_many_stocks,24 order_line_with_one_allocation,25 channel_USD,26):27 assert (28 check_stock_quantity(29 variant_with_many_stocks, COUNTRY_CODE, channel_USD.slug, 330 )31 is None32 )33def test_check_stock_quantity_with_allocations_out_of_stock(34 variant_with_many_stocks, order_line_with_allocation_in_many_stocks, channel_USD35):36 with pytest.raises(InsufficientStock):37 check_stock_quantity(38 variant_with_many_stocks, COUNTRY_CODE, channel_USD.slug, 539 )40def test_check_stock_quantity_without_stocks(variant_with_many_stocks, channel_USD):41 variant_with_many_stocks.stocks.all().delete()42 with pytest.raises(InsufficientStock):43 check_stock_quantity(44 variant_with_many_stocks, COUNTRY_CODE, channel_USD.slug, 145 )46def test_check_stock_quantity_without_one_stock(variant_with_many_stocks, channel_USD):47 variant_with_many_stocks.stocks.get(quantity=3).delete()48 assert (49 check_stock_quantity(50 variant_with_many_stocks, COUNTRY_CODE, channel_USD.slug, 451 )52 is None53 )54def test_check_stock_quantity_bulk(variant_with_many_stocks, channel_USD):55 variant = variant_with_many_stocks56 country_code = "US"57 available_quantity = _get_available_quantity(variant.stocks.all())58 # test that it doesn't raise error for available quantity59 assert (60 check_stock_quantity_bulk(61 [variant_with_many_stocks], country_code, [available_quantity], channel_USD62 )63 is None64 )65 # test that it raises an error for exceeded quantity66 with pytest.raises(InsufficientStock):67 check_stock_quantity_bulk(68 [variant_with_many_stocks],69 country_code,70 [available_quantity + 1],71 channel_USD,72 )73 # test that it raises an error if no stocks are found74 variant.stocks.all().delete()75 with pytest.raises(InsufficientStock):76 check_stock_quantity_bulk(77 [variant_with_many_stocks], country_code, [available_quantity], channel_USD78 )79def test_check_stock_quantity_bulk_no_channel_shipping_zones(80 variant_with_many_stocks, channel_USD81):82 variant = variant_with_many_stocks83 country_code = "US"84 available_quantity = _get_available_quantity(variant.stocks.all())85 channel_USD.shipping_zones.clear()86 with pytest.raises(InsufficientStock):87 check_stock_quantity_bulk(88 [variant_with_many_stocks], country_code, [available_quantity], channel_USD...
fields.py
Source:fields.py
1from functools import partial2from django.db import models3from django.db.models.fields.related import (4 RECURSIVE_RELATIONSHIP_CONSTANT, ManyToManyDescriptor, RelatedField,5 create_many_to_many_intermediary_model,6)7class CustomManyToManyField(RelatedField):8 """9 Ticket #24104 - Need to have a custom ManyToManyField,10 which is not an inheritor of ManyToManyField.11 """12 many_to_many = True13 def __init__(self, to, db_constraint=True, swappable=True, related_name=None, related_query_name=None,14 limit_choices_to=None, symmetrical=None, through=None, through_fields=None, db_table=None, **kwargs):15 try:16 to._meta17 except AttributeError:18 to = str(to)19 kwargs['rel'] = models.ManyToManyRel(20 self, to,21 related_name=related_name,22 related_query_name=related_query_name,23 limit_choices_to=limit_choices_to,24 symmetrical=symmetrical if symmetrical is not None else (to == RECURSIVE_RELATIONSHIP_CONSTANT),25 through=through,26 through_fields=through_fields,27 db_constraint=db_constraint,28 )29 self.swappable = swappable30 self.db_table = db_table31 if kwargs['rel'].through is not None:32 assert self.db_table is None, "Cannot specify a db_table if an intermediary model is used."33 super().__init__(**kwargs)34 def contribute_to_class(self, cls, name, **kwargs):35 if self.remote_field.symmetrical and (36 self.remote_field.model == "self" or self.remote_field.model == cls._meta.object_name):37 self.remote_field.related_name = "%s_rel_+" % name38 super().contribute_to_class(cls, name, **kwargs)39 if not self.remote_field.through and not cls._meta.abstract and not cls._meta.swapped:40 self.remote_field.through = create_many_to_many_intermediary_model(self, cls)41 setattr(cls, self.name, ManyToManyDescriptor(self.remote_field))42 self.m2m_db_table = partial(self._get_m2m_db_table, cls._meta)43 def get_internal_type(self):44 return 'ManyToManyField'45 # Copy those methods from ManyToManyField because they don't call super() internally46 contribute_to_related_class = models.ManyToManyField.__dict__['contribute_to_related_class']47 _get_m2m_attr = models.ManyToManyField.__dict__['_get_m2m_attr']48 _get_m2m_reverse_attr = models.ManyToManyField.__dict__['_get_m2m_reverse_attr']49 _get_m2m_db_table = models.ManyToManyField.__dict__['_get_m2m_db_table']50class InheritedManyToManyField(models.ManyToManyField):51 pass52class MediumBlobField(models.BinaryField):53 """54 A MySQL BinaryField that uses a different blob size.55 """56 def db_type(self, connection):...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!