test_dumps_loads.py 文件源码

python
阅读 18 收藏 0 点赞 0 评论 0

项目:morejson 作者: shaypal5 项目源码 文件源码
def test_dumps_datetime_with_zone(self):
        """Testing dumps and loads of timezone-aware datetime types, as well
        as standalone  timezone objects """

        try:
            import pytz
            import tzlocal
        except ImportError:
            # These packages aren't available - can't test
            raise unittest.SkipTest(
                "pytz or tzlocal not available in this test run; skipping"
                "zone-aware DT tests.")

        local_tz = tzlocal.get_localzone()
        pytz_est = pytz.timezone("US/Eastern")
        pytz_pst = pytz.timezone("US/Pacific")
        pytz_utc = pytz.timezone("UTC")
        pytz_fixed = pytz.FixedOffset(-120)

        original_allow_pickle = morejson.CONFIG.get("allow_pickle", False)
        morejson.CONFIG["allow_pickle"] = True

        dicti = {
            'datetime-no-tz': datetime.datetime.now(),
            'datetime-with-utc': datetime.datetime.now(tz=pytz_utc),
            'datetime-with-est': datetime.datetime.now(tz=pytz_est),
            'datetime-with-tzlocal': datetime.datetime.now(tz=local_tz),
            'datetime-with-pst': datetime.datetime.now(tz=pytz_pst),
            'datetime-with-fixedoffset': datetime.datetime.now(tz=pytz_fixed),
            'eastern-tzone': pytz_est,
            'pacific-tzone': pytz_pst,
            'array': [
                1, 2, 3, pytz_utc, pytz_est, local_tz, pytz_pst, pytz_fixed],
            'string': 'trololo',
            'null': None
        }
        out_str = morejson.dumps(dicti)
        actual_obj = morejson.loads(out_str)
        self.assertEqual(dicti, actual_obj)
        morejson.CONFIG["allow_pickle"] = original_allow_pickle
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号