def test_comparison(self): c0 = Temperature(c=0) c100 = Temperature(c=100) f0 = Temperature(f=32) f100 = Temperature(f=212) self.assertEqual(c0, f0) self.assertEqual(c100, f100) self.assertTrue(c0 < c100) self.assertFalse(c0 > c100) self.assertTrue(c0 < f100) self.assertFalse(c0 > f100)
def test_arrays(self): tc_array = Temperature(celsius=self.c_array) tc_marray = Temperature(celsius=self.c_marray) tf_array = Temperature(f=self.f_array) tf_marray = Temperature(f=self.f_marray) with self.subTest('array as C'): for c_as_c, f_as_c in zip(tc_array.c, tf_array.c): self.assertAlmostEqual(c_as_c, f_as_c, 6) with self.subTest('array as F'): for c_as_f, f_as_f in zip(tc_array.f, tf_array.f): self.assertAlmostEqual(c_as_f, f_as_f, 6)
def test_guess_temperature(self): result = guess(98, 'f') self.assertEqual( result, Temperature(f=98) )
def test_attributes(self): t = Temperature(c=100) expected_c = 100 expected_f = 212 expected_k = 373.15 units = ['celsius', 'f', 'k'] expected_values = [100, 212, 373.15] for u, expected_value in zip(units, expected_values): with self.subTest(output_unit=u, expected_value=expected_value): t.unit = u self.assertEqual(t.value, expected_value) u_out = 'c' u_in = 'f' v_in = 32 v_out = 0 kw = dict(unit_in='f', unit_out='c', value_in=32, expected_output=0) with self.subTest("value setter", **kw): t.unit = kw['unit_in'] t.value = kw['value_in'] t.unit = kw['unit_out'] self.assertEqual(t.value, kw['expected_output'])
def test_sanity(self): fahrenheit = Temperature(fahrenheit=70) celsius = Temperature(celsius=21.1111111) self.assertAlmostEqual(fahrenheit.k, celsius.k)
def test_ensure_that_we_always_output_float(self): kelvin = Temperature(kelvin=10) celsius = kelvin.c self.assertTrue(isinstance(celsius, float))
def test_conversion_to_non_si(self): celsius = Temperature(celsius=21.1111111) expected_farenheit = 70 self.assertAlmostEqual(celsius.f, expected_farenheit)
def load_recipes(user): recipes = get_recipes_json() recipes_objects = [] for recipe in recipes: recipe_data = _clean_data(recipe["fields"]) style = models.Style.objects.filter( name__icontains=recipe_data["style"]) recipe_data["user"] = user if style.count() == 0: print( f"Did not fount a syle '{recipe_data['style']}' for '{recipe_data['name']}'" ) recipe_data["style"] = style[0] recipe_data["expected_beer_volume"] = Volume( **{ recipe_data["expected_beer_volume"].split()[-1].split("(")[0]: float(recipe_data["expected_beer_volume"].split()[0]) }) # print(recipe_data) new_recipe = models.Recipe(**recipe_data) new_recipe.save() fermentables = [] for fermentable in recipe["fermentables"]: data = _clean_data(fermentable) data["recipe"] = new_recipe data["amount"] = Weight(**{data["unit"]: data["amount"]}) data["color"] = BeerColor(ebc=data["color"]) del data["unit"] fermentable_ingredient = models.IngredientFermentable(**data) fermentable_ingredient.save() fermentables.append(fermentables) hops = [] for hop in recipe["hops"]: data = _clean_data(hop) data["recipe"] = new_recipe data["amount"] = Weight(**{data["unit"]: data["amount"]}) del data["unit"] data["time_unit"] = data["time_unit"].split("(")[0] hop_ingredient = models.IngredientHop(**data) hop_ingredient.save() hops.append(hops) yeasts = [] for yeast in recipe["yeasts"]: data = _clean_data(yeast) data["recipe"] = new_recipe data["amount"] = Weight(**{data["unit"]: data["amount"]}) del data["unit"] yeast_ingredient = models.IngredientYeast(**data) yeast_ingredient.save() yeasts.append(yeasts) extras = [] for extra in recipe["extras"]: data = _clean_data(extra) data["recipe"] = new_recipe data["amount"] = Weight(**{data["unit"]: data["amount"]}) del data["unit"] data["time_unit"] = data["time_unit"].split("(")[0] extra_ingredient = models.IngredientExtra(**data) extra_ingredient.save() extras.append(extras) mash_steps = [] for mash in recipe["mashing"]: data = _clean_data(mash) data["recipe"] = new_recipe data["temperature"] = Temperature(c=data["temp"]) del data["temp"] del data["time_unit"] mash_step = models.MashStep(**data) mash_step.save() mash_steps.append(mash_step) new_recipe.save() recipes_objects.append((recipe, new_recipe)) return recipes_objects
from model_bakery.generators import random_gen from measurement.measures import Volume, Mass, Temperature from brivo.utils.measures import BeerColor, BeerGravity from brivo.users.models import User, UserProfile, UserBrewery from brivo.brewery.models import Style from brivo.users.tests.factories import UserFactory baker.generators.add('brivo.brewery.fields.BeerColorField', lambda: BeerColor(srm=10)) baker.generators.add('brivo.brewery.fields.BeerGravityField', lambda: BeerGravity(plato=10)) baker.generators.add('brivo.brewery.fields.VolumeField', lambda: Volume(l=10)) baker.generators.add('brivo.brewery.fields.MassField', lambda: Mass(kg=1)) baker.generators.add('brivo.brewery.fields.TemperatureField', lambda: Temperature(c=10)) baker.generators.add('modelcluster.fields.ParentalKey', random_gen.gen_related) ROOT_DIR = Path(__file__).resolve(strict=True).parent @pytest.fixture(autouse=True) def media_storage(settings, tmpdir): settings.MEDIA_ROOT = tmpdir.strpath @pytest.fixture def user(): user = UserFactory() user.profile = UserProfile(user=user) user.brewery = UserBrewery(user=user)
def test_sanity(self): fahrenheit = Temperature(fahrenheit=70) celsius = Temperature(celsius=21.1111111) assert fahrenheit.K == pytest.approx(celsius.K)
def test_ensure_that_we_always_output_float(self): kelvin = Temperature(kelvin=10) assert isinstance(kelvin.celsius, decimal.Decimal)
def test_conversion_to_non_si(self): celsius = Temperature(celsius=21.1111111) expected_farenheit = decimal.Decimal("70") assert celsius.fahrenheit == pytest.approx(expected_farenheit)
def test_addition_typeerrors(self): t1 = Temperature(c=100) values = [1, 1.5, 0, -1] for value in values: with self.subTest(value=value, value_type=pretty_name(value)): self.assertRaises(TypeError, sum, t1, value)
def test_average(self): t1 = Temperature(c=100) t2 = Temperature(c=300) expected_average = (t1.c + t2.c) / 2 t_avg = Temperature.average([t1, t2]) self.assertAlmostEqual(t_avg.c, expected_average)
def test_guess_temperature(): assert guess(98, "°F") == Temperature(fahrenheit=98)
def handle(self, *args, **options): self.stdout.write("Loading Countries to DB") with open(os.path.join(root, "data/countries.json")) as fin: countries = json.load(fin) i = 0 for kwargs in countries: if not models.Country.objects.filter( code=kwargs["code"]).count(): country = models.Country(**kwargs) country.save() i += 1 else: self.stdout.write( self.style.WARNING( f"Country {kwargs['code']} already in DB")) self.stdout.write( self.style.SUCCESS(f"Successfully loaded {i} countries to DB")) self.stdout.write("Loading Fermentable to DB") with open(os.path.join(root, "data/fermentables.json")) as fin: fermentables = json.load(fin) i = 0 for kwargs in fermentables: kwargs = _clean_data(kwargs) # self.stdout.write(str(kwargs)) if kwargs.get("country"): try: kwargs["country"] = models.Country.objects.get( code=kwargs.get("country")) except Exception as err: if kwargs.get("country", ""): raise self.stdout.write( self.style.WARNING( f"Fermentable {kwargs['name']} has no country") ) del kwargs["country"] # if not models.Fermentable.objects.filter(name=kwargs["name"]).count(): for clr in ["color"]: if kwargs.get(clr): kwargs[clr] = BeerColor(srm=kwargs[clr]) fermentable = models.Fermentable(**kwargs) fermentable.save() i += 1 # self.stdout.write(self.style.WARNING(f"Fermentable {kwargs['name']} already in DB")) self.stdout.write( self.style.SUCCESS( f"Successfully loaded {i} fermentables to DB")) with open(os.path.join(root, "data/styles.json")) as fin: styles = json.load(fin) i = 0 for kwargs in styles: kwargs = _clean_data(kwargs) # self.stdout.write(str(kwargs)) for grv in ["og_min", "og_max", "fg_min", "fg_max"]: if kwargs.get(grv): kwargs[grv] = BeerGravity(sg=kwargs[grv]) for clr in ["color_min", "color_max"]: if kwargs.get(clr): kwargs[clr] = BeerColor(srm=kwargs[clr]) if not models.Style.objects.filter( name=kwargs["name"]).count(): tags = None if kwargs.get("tags"): tags = [ models.Tag(name=s.strip()) for s in kwargs.get("tags", "").split(",") ] for t in tags: t.save() del kwargs["tags"] style = models.Style(**kwargs) style.save() if tags is not None: style.tags.set(tags) style.save() i += 1 self.stdout.write( self.style.SUCCESS(f"Successfully loaded {i} styles to DB")) with open(os.path.join(root, "data/hops.json")) as fin: hops_substitutes = {} hops = json.load(fin) i = 0 for kwargs in hops: kwargs = _clean_data(kwargs) # self.stdout.write(str(kwargs)) if kwargs.get("country"): try: kwargs["country"] = models.Country.objects.get( name=kwargs.get("country")) except Exception as err: if kwargs.get("country", ""): raise self.stdout.write( self.style.WARNING( f"Hop {kwargs['name']} has no country")) del kwargs["country"] if not models.Hop.objects.filter(name=kwargs["name"]).count(): if kwargs.get("substitute"): substitute = [ s.strip() for s in kwargs.get("substitute", "").split(",") ] del kwargs["substitute"] kwargs["alpha_acids"] = (kwargs["alpha_max"] + kwargs["alpha_min"]) / 2.0 hop = models.Hop(**kwargs) hop.save() if len(substitute) > 0: hops_substitutes[hop.id] = substitute i += 1 for pk, subs in hops_substitutes.items(): hop = models.Hop.objects.get(pk=pk) hop_subs = models.Hop.objects.filter(name__in=subs) if hop_subs.count() != len(subs): not_found = set(subs) - set([h.name for h in hop_subs]) self.stdout.write( f"TODO: Could not find all substitue Hops: {pk}, {subs}, {not_found}" ) hop.substitute.set(hop_subs) # self.stdout.write(self.style.WARNING(f"Hop {kwargs['name']} already in DB")) self.stdout.write( self.style.SUCCESS(f"Successfully loaded {i} hops to DB")) with open(os.path.join(root, "data/yeasts.json")) as fin: self.stdout.write(self.style.WARNING(f"Loading {i} yeasts to DB")) # yeasts_styles = {} yeasts = json.load(fin) i = 0 for kwargs in yeasts: kwargs = _clean_data(kwargs) for temp in ["temp_min", "temp_max"]: if kwargs.get(temp): kwargs[temp] = Temperature(celsius=kwargs[temp]) if not models.Yeast.objects.filter( name=kwargs["name"]).count(): # if kwargs.get("styles"): # styles = [s.strip().rstrip("s") for s in kwargs.get("styles", "").split(",")] # del kwargs["styles"] print(kwargs) yeast = models.Yeast(**kwargs) yeast.save() # if len(styles) > 0: # yeasts_styles[yeast.id] = styles i += 1 # for pk, stls in yeasts_styles.items(): # yeast = models.Yeast.objects.get(pk=pk) # yeast_stls = models.Style.objects.filter(name__in=stls) # if yeast_stls.count() != len(stls): # not_found = set(stls) - set([h.name for h in yeast_stls]) # self.stdout.write(f"TODO: Could not find all styles for Yeasts: {pk}, {stls}, {not_found}") # yeast.styles.set(yeast_stls) # self.stdout.write(self.style.WARNING(f"Yeast {kwargs['name']} already in DB")) self.stdout.write( self.style.SUCCESS(f"Successfully loaded {i} yeasts to DB")) with open(os.path.join(root, "data/extras.json")) as fin: extras = json.load(fin) i = 0 for kwargs in extras: kwargs = _clean_data(kwargs) if not models.Extra.objects.filter( name=kwargs["name"]).count(): extra = models.Extra(**kwargs) extra.save() i += 1 self.stdout.write( self.style.SUCCESS(f"Successfully loaded {i} extras to DB"))