|
|
@@ -461,6 +461,159 @@ class DateTimeAttrTestCase(TestCase):
|
|
|
self.assertEqual(context['spec'], 'minutes')
|
|
|
|
|
|
|
|
|
+class WeightAttrTestCase(TestCase):
|
|
|
+
|
|
|
+ def _ctx(self, system=''):
|
|
|
+ return {'name': 'weight', 'preferences': {'ui.measurement_system': system}}
|
|
|
+
|
|
|
+ def _obj(self, weight, unit, abs_g, display=None):
|
|
|
+ display_fn = (lambda: display) if display else (lambda: unit)
|
|
|
+ return SimpleNamespace(
|
|
|
+ weight=weight,
|
|
|
+ weight_unit=unit,
|
|
|
+ _abs_weight=abs_g,
|
|
|
+ get_weight_unit_display=display_fn,
|
|
|
+ )
|
|
|
+
|
|
|
+ def test_none_returns_placeholder(self):
|
|
|
+ attr = attrs.WeightAttr('weight')
|
|
|
+ obj = SimpleNamespace(weight=None)
|
|
|
+ self.assertEqual(attr.render(obj, self._ctx()), attr.placeholder)
|
|
|
+
|
|
|
+ def test_inherit_shows_stored_value(self):
|
|
|
+ attr = attrs.WeightAttr('weight')
|
|
|
+ obj = self._obj(5, 'kg', 5000, 'Kilograms')
|
|
|
+ result = attr.render(obj, self._ctx(system=''))
|
|
|
+ self.assertIn('5', result)
|
|
|
+ self.assertIn('kilograms', result)
|
|
|
+
|
|
|
+ def test_metric_converts_lbs_to_kg(self):
|
|
|
+ # 10 lb = 4535.92 g → 4535.92 / 1000 = 4.54 kg
|
|
|
+ attr = attrs.WeightAttr('weight')
|
|
|
+ obj = self._obj(10, 'lb', 4535.92, 'Pounds')
|
|
|
+ result = attr.render(obj, self._ctx(system='metric'))
|
|
|
+ self.assertIn('4.54', result)
|
|
|
+ self.assertIn('kg', result)
|
|
|
+
|
|
|
+ def test_metric_no_conversion_for_metric_unit(self):
|
|
|
+ attr = attrs.WeightAttr('weight')
|
|
|
+ obj = self._obj(5, 'kg', 5000, 'Kilograms')
|
|
|
+ result = attr.render(obj, self._ctx(system='metric'))
|
|
|
+ self.assertIn('5', result)
|
|
|
+ self.assertIn('kilograms', result)
|
|
|
+
|
|
|
+ def test_imperial_converts_kg_to_lbs(self):
|
|
|
+ # 1 kg = 1000 g → 1000 / 453.592 = 2.2 lbs
|
|
|
+ attr = attrs.WeightAttr('weight')
|
|
|
+ obj = self._obj(1, 'kg', 1000, 'Kilograms')
|
|
|
+ result = attr.render(obj, self._ctx(system='imperial'))
|
|
|
+ self.assertIn('2.2', result)
|
|
|
+ self.assertIn('lbs', result)
|
|
|
+
|
|
|
+ def test_imperial_no_conversion_for_imperial_unit(self):
|
|
|
+ attr = attrs.WeightAttr('weight')
|
|
|
+ obj = self._obj(10, 'lb', 4535.92, 'Pounds')
|
|
|
+ result = attr.render(obj, self._ctx(system='imperial'))
|
|
|
+ self.assertIn('10', result)
|
|
|
+ self.assertIn('pounds', result)
|
|
|
+
|
|
|
+ def test_metric_no_conversion_when_abs_weight_is_none(self):
|
|
|
+ # abs_weight=None → falsy → falls through to stored value
|
|
|
+ attr = attrs.WeightAttr('weight')
|
|
|
+ obj = SimpleNamespace(
|
|
|
+ weight=10, weight_unit='lb', _abs_weight=None,
|
|
|
+ get_weight_unit_display=lambda: 'Pounds',
|
|
|
+ )
|
|
|
+ result = attr.render(obj, self._ctx(system='metric'))
|
|
|
+ self.assertIn('10', result)
|
|
|
+ self.assertIn('pounds', result)
|
|
|
+
|
|
|
+
|
|
|
+class DistanceAttrTestCase(TestCase):
|
|
|
+
|
|
|
+ def _ctx(self, system=''):
|
|
|
+ return {'name': 'distance', 'preferences': {'ui.measurement_system': system}}
|
|
|
+
|
|
|
+ def _obj(self, distance, unit, abs_m, display=None):
|
|
|
+ display_fn = (lambda: display) if display else (lambda: unit)
|
|
|
+ return SimpleNamespace(
|
|
|
+ distance=distance,
|
|
|
+ distance_unit=unit,
|
|
|
+ _abs_distance=abs_m,
|
|
|
+ get_distance_unit_display=display_fn,
|
|
|
+ )
|
|
|
+
|
|
|
+ def test_none_returns_placeholder(self):
|
|
|
+ attr = attrs.DistanceAttr('distance')
|
|
|
+ obj = SimpleNamespace(distance=None)
|
|
|
+ self.assertEqual(attr.render(obj, self._ctx()), attr.placeholder)
|
|
|
+
|
|
|
+ def test_inherit_shows_stored_value(self):
|
|
|
+ attr = attrs.DistanceAttr('distance')
|
|
|
+ obj = self._obj(10, 'km', 10000, 'Kilometers')
|
|
|
+ result = attr.render(obj, self._ctx(system=''))
|
|
|
+ self.assertIn('10', result)
|
|
|
+ self.assertIn('kilometers', result)
|
|
|
+
|
|
|
+ def test_metric_converts_ft_to_m_under_threshold(self):
|
|
|
+ # 500 ft = 152.4 m (< 1000) → display in m
|
|
|
+ attr = attrs.DistanceAttr('distance')
|
|
|
+ obj = self._obj(500, 'ft', 152.4, 'Feet')
|
|
|
+ result = attr.render(obj, self._ctx(system='metric'))
|
|
|
+ self.assertIn('152.4', result)
|
|
|
+ self.assertNotIn('km', result)
|
|
|
+ self.assertIn('m', result)
|
|
|
+
|
|
|
+ def test_metric_converts_mi_to_km_over_threshold(self):
|
|
|
+ # 5 mi = 8046.72 m (>= 1000) → 8046.72 / 1000 = 8.05 km
|
|
|
+ attr = attrs.DistanceAttr('distance')
|
|
|
+ obj = self._obj(5, 'mi', 8046.72, 'Miles')
|
|
|
+ result = attr.render(obj, self._ctx(system='metric'))
|
|
|
+ self.assertIn('8.05', result)
|
|
|
+ self.assertIn('km', result)
|
|
|
+
|
|
|
+ def test_imperial_converts_m_to_ft_under_threshold(self):
|
|
|
+ # 500 m (< 1609.344) → 500 / 0.3048 = 1640.42 ft
|
|
|
+ attr = attrs.DistanceAttr('distance')
|
|
|
+ obj = self._obj(500, 'm', 500, 'Meters')
|
|
|
+ result = attr.render(obj, self._ctx(system='imperial'))
|
|
|
+ self.assertIn('1640.42', result)
|
|
|
+ self.assertIn('ft', result)
|
|
|
+
|
|
|
+ def test_imperial_converts_km_to_mi_over_threshold(self):
|
|
|
+ # 10 km = 10000 m (>= 1609.344) → 10000 / 1609.344 = 6.21 mi
|
|
|
+ attr = attrs.DistanceAttr('distance')
|
|
|
+ obj = self._obj(10, 'km', 10000, 'Kilometers')
|
|
|
+ result = attr.render(obj, self._ctx(system='imperial'))
|
|
|
+ self.assertIn('6.21', result)
|
|
|
+ self.assertIn('mi', result)
|
|
|
+
|
|
|
+ def test_metric_no_conversion_for_metric_unit(self):
|
|
|
+ attr = attrs.DistanceAttr('distance')
|
|
|
+ obj = self._obj(10, 'km', 10000, 'Kilometers')
|
|
|
+ result = attr.render(obj, self._ctx(system='metric'))
|
|
|
+ self.assertIn('10', result)
|
|
|
+ self.assertIn('kilometers', result)
|
|
|
+
|
|
|
+ def test_imperial_no_conversion_for_imperial_unit(self):
|
|
|
+ attr = attrs.DistanceAttr('distance')
|
|
|
+ obj = self._obj(10, 'mi', 16093.44, 'Miles')
|
|
|
+ result = attr.render(obj, self._ctx(system='imperial'))
|
|
|
+ self.assertIn('10', result)
|
|
|
+ self.assertIn('miles', result)
|
|
|
+
|
|
|
+ def test_metric_no_conversion_when_abs_distance_is_none(self):
|
|
|
+ # abs_distance=None → falls through to stored value
|
|
|
+ attr = attrs.DistanceAttr('distance')
|
|
|
+ obj = SimpleNamespace(
|
|
|
+ distance=10, distance_unit='ft', _abs_distance=None,
|
|
|
+ get_distance_unit_display=lambda: 'Feet',
|
|
|
+ )
|
|
|
+ result = attr.render(obj, self._ctx(system='metric'))
|
|
|
+ self.assertIn('10', result)
|
|
|
+ self.assertIn('feet', result)
|
|
|
+
|
|
|
+
|
|
|
class ObjectsTablePanelTestCase(TestCase):
|
|
|
"""
|
|
|
Verify that ObjectsTablePanel.should_render() hides the panel when
|