Repository: wearewhys/magnivore

Issue:
Add possibility to specify whether to round up or down in factor

Discussion:

Patch:
diff --git a/magnivore/Lexicon.py b/magnivore/Lexicon.py
index 84ae7a2..acf038b 100644
--- a/magnivore/Lexicon.py
+++ b/magnivore/Lexicon.py
@@ -2,6 +2,7 @@
 import re
 from decimal import Decimal
 from functools import reduce
+from math import ceil, floor
 
 from .Tracker import Tracker
 
@@ -40,7 +41,12 @@ class Lexicon:
         """
         value = cls._dot_reduce(rule['from'], target)
         original_type = type(value)
-        return original_type(Decimal(value) * Decimal(rule['factor']))
+        result = Decimal(value) * Decimal(rule['factor'])
+        if 'round' in rule:
+            if rule['round'] == 'up':
+                return original_type(ceil(result))
+            return original_type(floor(result))
+        return original_type(result)
 
     @classmethod
     def format(cls, rule, target):


Test patch:
diff --git a/tests/unit/Lexicon.py b/tests/unit/Lexicon.py
index f1c85d6..3af833c 100644
--- a/tests/unit/Lexicon.py
+++ b/tests/unit/Lexicon.py
@@ -65,6 +65,19 @@ def test_lexicon_factor(from_data, target, expected):
     assert type(result) == type(expected)
 
 
+@mark.parametrize('rounding, expected', [
+    ('down', 47),
+    ('up', 48)
+])
+def test_lexicon_factor_round(rounding, expected):
+    rule = {
+        'from': 'value',
+        'round': rounding,
+        'factor': 0.5
+    }
+    assert Lexicon.factor(rule, MagicMock(value=95)) == expected
+
+
 @mark.parametrize('from_data, format, expected', [
     ('birthyear', '{}-0-0', '1992-0-0'),
     (['birthyear', 'birthmonth'], '{}-{}-0', '1992-9-0')