def test_init(self): try: time = Time(timezone="+05:00") self.assertEqual(time.timezone, "+05:00") except CraftAiTimeError as e: self.fail(e) try: Time() except CraftAiTimeError as e: self.fail(e)
def test_rebuild_context(): configuration = { "context": { "car": { "type": "enum" }, "speed": { "type": "continuous" }, "day_of_week": { "type": "day_of_week", "is_generated": False }, "month_of_year": { "type": "month_of_year" }, "timezone": { "type": "timezone" } }, "output": ["speed"], "time_quantum": 500 } #pylint: disable=W0212 # Case 1: # - don't provide a Time object while properties in configuration need to be generated from it # - don't provide those properties directly in the context state = {"car": "Renault", "day_of_week": 2} assert_raises( craft_err.CraftAiDecisionError, Interpreter._rebuild_context, configuration, state) # Case 2: # - provide none of the properties that should be generated state = {"car": "Renault", "day_of_week": 2} time = Time(1489998174, "+01:00") rebuilt_context = Interpreter._rebuild_context(configuration, state, time) expected_context = { "car": "Renault", "day_of_week": 2, "month_of_year": 3, "timezone": "+01:00" } #pylint: enable=W0212 for output in expected_context: assert_equal(rebuilt_context[output], expected_context[output])
def check_expectation(tree, expectation): exp_context = expectation["context"] timestamp = None exp_time = expectation.get("time") time = Time(exp_time["t"], exp_time["tz"]) if exp_time else {} if expectation.get("error"): assert_raises( craft_err.CraftAiDecisionError, CLIENT.decide, tree, exp_context, timestamp) else: expected_decision = expectation["output"] decision = CLIENT.decide(tree, exp_context, time) assert_equal(decision, expected_decision)
def test_rebuild_context(): configuration = { "context": { "car": { "type": "enum" }, "speed": { "type": "continuous" }, "day_of_week": { "type": "day_of_week", "is_generated": False }, "month_of_year": { "type": "month_of_year" }, "timezone": { "type": "timezone" } }, "output": ["speed"], "time_quantum": 500 } #pylint: disable=W0212 # Case 2: # - provide none of the properties that should be generated state = {"car": "Renault", "day_of_week": 2, "timezone": "+01:00"} time = Time(1489998174, "+01:00") rebuilt_context = Interpreter._rebuild_context(configuration, state, time)["context"] expected_context = { "car": "Renault", "day_of_week": 2, "month_of_year": 3, "timezone": "+01:00" } #pylint: enable=W0212 for output in expected_context: assert_equal(rebuilt_context[output], expected_context[output])
def check_expectation(tree, expectation): exp_context = expectation["context"] timestamp = None exp_time = expectation.get("time") time = Time(exp_time["t"], exp_time["tz"]) if exp_time else {} configuration = expectation.get("configuration") if configuration: tree["configuration"].update(configuration) if expectation.get("error"): with assert_raises(craft_err.CraftAiDecisionError) as context_manager: CLIENT.decide(tree, exp_context, timestamp) exception = context_manager.exception expected_message = "" if isinstance(expectation["error"]["message"], str): expected_message = expectation["error"]["message"] else: expected_message = expectation["error"]["message"].encode("utf8") assert_equal(exception.message, expected_message) else: expected_decision = expectation["output"] decision = CLIENT.decide(tree, exp_context, time) assert_equal(decision, expected_decision)
def test_timezone_format(self): self.assertEqual(Time(timezone="+01:00").timezone, "+01:00")
def test_timezone_format(self): self.assertEqual(Time(timezone="+01:00").timezone, "+01:00") self.assertEqual(Time(timezone="CET").timezone, "+01:00") self.assertEqual(Time(1356998400, timezone="+0100").timezone, "+01:00") self.assertEqual(Time(1356998400, timezone="+01").timezone, "+01:00") self.assertEqual(Time(1356998400, timezone="-0600").timezone, "-06:00") self.assertEqual(Time(1356998400, timezone="CST").timezone, "-06:00") self.assertEqual(Time("1977-04-22T01:00:00-0300").timezone, "-03:00") self.assertEqual(Time("1977-04-22T01:00:00+1000").timezone, "+10:00") self.assertEqual(Time("2011-04-22 01:00:00+0900").timezone, "+09:00") self.assertEqual(Time(t="2017-01-01 00:00:00", timezone="-03:00").timezone, "-03:00") self.assertEqual(Time(t="2017-01-01 03:00:00", timezone="+02:00").timezone, "+02:00") self.assertEqual(Time(t=datetime(2011, 1, 1, 0, 0), timezone="+02:00").timezone, "+02:00") self.assertEqual(Time(t="2017-01-01 03:00:00", timezone=120).timezone, "+02:00") self.assertEqual(Time(t="2017-01-01 03:00:00", timezone=-120).timezone, "-02:00") self.assertEqual(Time(t="2017-01-01 03:00:00", timezone=-2).timezone, "-02:00") self.assertEqual(Time(t=datetime(2012, 9, 12, 6, 0, 0, tzinfo=pytz.utc)).timezone, "+00:00") self.assertEqual(Time().timezone, Time().timezone)