Example #1
0
    def test_evaluate_and_resolve_expression(self):
        expression = Expression()

        util = DatabaseUtilities()
        util.delete_lookups()
        util.insert_lookups('somevalue', 0, 2)
        util.insert_lookups('m.monthval', 201703, 4)

        monthly = DataStructure()
        monthly.ProdMonth = 201703
        monthly.ProdVol = 100
        monthly.SalesVol = 90
        monthly.GJ = 1000
        monthly.Heat = 65.01

        s = "asdf =(prod + sales + gj + heat + somevalue + m.monthval) more stuff in here"

        self.assertEqual(1196 + 65.01,
                         expression.evaluate_expression(s, monthly))
        self.assertEqual(
            "asdf =(100 + 90 + 1000 + 65.01 + 2 + 4) more stuff in here",
            expression.resolve_expression(s, monthly))

        s = "asdf =(prod + sales + gj + notfoundvalue) more stuff in here"
        self.assertRaises(AppError, expression.evaluate_expression, s, monthly)
Example #2
0
    def test_string_is_formula(self):
        expression = Expression()

        s = "prod + sales + gj"
        monthly = DataStructure()
        monthly.ProdVol = 100
        monthly.SalesVol = 90
        monthly.GJ = 1000

        self.assertEqual(1190, expression.evaluate_expression(s, monthly))
        self.assertEqual("100 + 90 + 1000",
                         expression.resolve_expression(s, monthly))
Example #3
0
    def test_lookup_vars(self):
        expression = Expression()

        util = DatabaseUtilities()
        util.delete_lookups()
        util.insert_lookups('somevalue', 0, 1234)
        util.insert_lookups('m.monthval', 201703, 5678)

        monthly = DataStructure()
        monthly.ProdMonth = 201703
        monthly.ProdVol = 100
        monthly.SalesVol = 90
        monthly.GJ = 1000
        monthly.Heat = 65.01
        monthly.SalesPrice = 50.00
        monthly.TransRate = 10.00
        monthly.ProcessingRate = 5.00
        monthly.GCARate = 3.00
        calc = DataStructure()
        calc.RoyaltyPrice = 27.12

        rv = expression.lookup_vars(
            {
                "prod", "sales", "gj", "heat", "price", "trans", "processing",
                "gca", "royalty_price", "somevalue", "m.monthval"
            }, monthly, calc)

        self.assertEqual(100, rv["prod"])
        self.assertEqual(90, rv["sales"])
        self.assertEqual(1000, rv["gj"])
        self.assertEqual(65.01, rv["heat"])
        self.assertEqual(50, rv["price"])
        self.assertEqual(10, rv["trans"])
        self.assertEqual(5, rv["processing"])
        self.assertEqual(3, rv["gca"])
        self.assertEqual(27.12, rv["royalty_price"])
        self.assertEqual(1234, rv["somevalue"])
        self.assertEqual(5678, rv["m.monthval"])

        self.assertRaises(AppError, expression.lookup_vars, {"notfooundvalue"},
                          monthly)

        monthly.GJ = None
        self.assertRaises(AppError, expression.lookup_vars, {"gj", "sales"},
                          monthly)