def test_collect_model_messages(): collected_messages = {} def mock_update_messages(locale, t9n_list=None): print("locale: {}".format(locale)) print("t9n_list: {}".format(t9n_list)) messages = collected_messages.get(locale, []) messages += t9n_list collected_messages[locale] = messages # mock the update_messages with this test version to see which messages are all collected makemessages.update_messages = mock_update_messages TransTestModel.objects.create(trans_field=('mechanical device', 'spring'), other_trans_field='hurry') TransTestModel.objects.create(trans_field=('one', )) _reload(TransTestModel.objects.create(trans_field=('one', ))) assert collected_messages == {}, "nothing collected yet, should still be empty." makemessages.Command().collect_model_messages({'locale': ['nl']}) assert len(collected_messages.keys()) == 1 and 'nl' in collected_messages.keys(), \ "Only 'nl' should be present in the collected messages keys" for t9n in [ T9N('spring', msgctxt='mechanical device'), T9N('hurry'), T9N('one'), T9N('one') ]: assert t9n in collected_messages['nl'], "{} not found in collected messages." \ " Are msgid({}), msgctxt({}) and plural({}) the same?".\ format(t9n, "'{}'".format(t9n.msgid) if t9n.msgid else '', "'{}'".format(t9n.msgctxt) if t9n.msgctxt else '', "'{}'".format(t9n.plural) if t9n.plural else '', )
def test_distance(l10n_nl): activate_l10n(l10n_nl) units = MeasuresTestModel.objects.create(area=24, height=43, temp=18) units = _reload(units) assert units.height.m == 43 assert units.height.meter == 43 assert units.height.cm == 4300 assert abs(units.height.ft - 141.076) < 0.001 assert abs(units.height.foot - 141.076) < 0.001 units.height = Distance(m=11) units.save() units = _reload(units) assert units.height.m == 11 assert units.height.metre == 11
def test_trans_field_with_context(): # we need to reload the instance from the db, so django converts the varchar value to a I18N object t9n_spring_device = _reload( TransTestModel.objects.create(trans_field=('mechanical device', 'spring'))) t9n_spring_season = _reload( TransTestModel.objects.create(trans_field=('season', 'spring'))) activate('en') assert str(t9n_spring_device.trans_field) == 'spring' assert str(t9n_spring_season.trans_field) == 'spring' deactivate() activate('nl') assert str(t9n_spring_device.trans_field) == 'veer' assert str(t9n_spring_season.trans_field) == 'lente' deactivate()
def test_store(): units = MeasuresTestModel.objects.create(area=Area(ha=1)) units = _reload(units) assert units.area.ha == 1 assert units.area.sq_km == 0.01 units.area = 20000 units.save() units = _reload(units) assert units.area.ha == 2 assert units.area.sq_km == 0.02 units.area = Area(ha=3) units.save() units = _reload(units) assert units.area.ha == 3 assert units.area.sq_km == 0.03
def test_precipitation(l10n_nl, l10n_eg, l10n_us): activate_l10n(l10n_nl) units = MeasuresTestModel.objects.create(precipitation=10) units = _reload(units) assert units.precipitation.mm == 10 assert units.precipitation.inch == 10 / 25.4 assert l10n_nl.unit_precipitation == 'mm' assert l10n_eg.unit_precipitation == 'mm' assert l10n_us.unit_precipitation == 'in' deactivate_l10n()
def test_area(l10n_nl): activate_l10n(l10n_nl) units = MeasuresTestModel.objects.create(area=240000, height=43, temp=18) units = _reload(units) assert units.area.sq_m == 240000 assert units.area.ha == 24 assert units.area.hectare == 24 assert units.area.l10n == '240000.0 sq_m' assert units.area.as_l10n(decimal_pos=0) == '240000 sq_m' assert units.area.default_value == 240000 assert units.height.default_value == 43
def test_windspeed(l10n_nl, l10n_us): activate_l10n(l10n_nl) units = MeasuresTestModel.objects.create(windspeed=10) units = _reload( units) # needed to convert int 10 to windspeed(10) in the units object assert units.windspeed.mps == 10 assert units.windspeed.kmh == 36 assert abs(units.windspeed.mph - 22.36936) < 0.00001 assert l10n_nl.unit_windspeed, 'mps' assert l10n_us.unit_windspeed, 'mph' deactivate_l10n()
def test_trans_field(): # we need to reload the instance from the db, so django converts the varchar value to a I18N object t9n_model = _reload(TransTestModel.objects.create(trans_field='edit')) activate('en') assert str(t9n_model.trans_field) == 'edit' assert "In a string '{}'".format( t9n_model.trans_field) == "In a string 'edit'" assert t9n_model.trans_field.msgid == 'edit' deactivate() activate('nl') assert str(t9n_model.trans_field) == 'bewerken' assert t9n_model.trans_field.msgid == 'edit' deactivate()
def test_temperature(l10n_nl, l10n_us): activate_l10n(l10n_nl) units = MeasuresTestModel.objects.create(area=Area(ha=24), height=43, temp=18) units = _reload(units) assert units.temp.C == 18.0 assert units.temp.F == 64.4 # change language, punctuation should alter units.temp = 0 units.save() units = _reload(units) assert units.temp.Celsius == 0 assert units.temp.F == 32 activate_l10n(l10n_us) units.temp = 8 # 8 °F units.save() units = _reload(units) assert abs(units.temp.F - 8.00) < 0.01 assert abs(units.temp.Celsius - -13.33) < 0.01 deactivate_l10n()
def test_store_l10n(l10n_us): # set country to US, so values are stored in non default units. activate_l10n(l10n_us) # store values in the currently active unit. units = MeasuresTestModel.objects.create(area=1045462.7681142587, height=43, temp=0) units = _reload(units) assert abs(units.area.acre - 24) < 0.0001 assert abs(units.area.ha - 9.7127) < 0.0001 assert units.height.yard == 43 assert abs(units.height.m - 39.3192) < 0.001 assert abs(units.temp.F - 0.00) < 0.01 assert abs(units.temp.C - -17.78) < 0.01 # default values should return the value of the default unit. assert abs(units.area.default_value - 97126.66936) < 0.0001 assert abs(units.height.default_value - 39.3192) < 0.01 assert abs(units.temp.default_value - -17.78) < 0.01 deactivate_l10n()
def test_trans_field_plural(): # we need to reload the instance from the db, so django converts the varchar value to a I18N object t9n_spring_device = _reload( TransTestModel.objects.create(trans_field={ 'msgid': 'car', 'plural': 'cars' })) activate('en') assert t9n_spring_device.trans_field.trans(0) == 'cars' assert t9n_spring_device.trans_field.trans(1) == 'car' assert t9n_spring_device.trans_field.trans(2) == 'cars' activate('nl') assert t9n_spring_device.trans_field.trans(0) == "auto's" assert t9n_spring_device.trans_field.trans(1) == "auto" assert t9n_spring_device.trans_field.trans(2) == "auto's" activate('fr') assert t9n_spring_device.trans_field.trans(0) == "voiture" assert t9n_spring_device.trans_field.trans(1) == "voiture" assert t9n_spring_device.trans_field.trans(2) == "voitures"
def test_trans_field_plural_with_context(): # we need to reload the instance from the db, so django converts the varchar value to a I18N object t9n_spring_device = _reload( TransTestModel.objects.create(trans_field={ 'i': 'tree', 'p': 'trees', 'c': 'in a forest' })) activate('en') assert t9n_spring_device.trans_field.trans(0) == 'trees' assert t9n_spring_device.trans_field.trans(1) == 'tree' assert t9n_spring_device.trans_field.trans(2) == 'trees' activate('nl') assert t9n_spring_device.trans_field.trans(0) == "bomen" assert t9n_spring_device.trans_field.trans(1) == "boom" assert t9n_spring_device.trans_field.trans(2) == "bomen" activate('fr') assert t9n_spring_device.trans_field.trans(0) == "arbre" assert t9n_spring_device.trans_field.trans(1) == "arbre" assert t9n_spring_device.trans_field.trans(2) == "arbres"
def test_trans_field_not_assigned(): # we need to reload the instance from the db, so django converts the varchar value to a I18N object t9n_model = _reload(TransTestModel.objects.create()) assert t9n_model.trans_field == ''