本文整理汇总了Python中index.Index.put方法的典型用法代码示例。如果您正苦于以下问题:Python Index.put方法的具体用法?Python Index.put怎么用?Python Index.put使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类index.Index
的用法示例。
在下文中一共展示了Index.put方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: TestIndexClass
# 需要导入模块: from index import Index [as 别名]
# 或者: from index.Index import put [as 别名]
class TestIndexClass(unittest.TestCase):
def setUp(self):
root_path = os.path.dirname(os.path.realpath(__file__)) + "/../"
try:
os.unlink(root_path + "data/ntp.json")
os.unlink(root_path + "data/ntp.json.backup")
except:
pass
self.index = Index(connection=Mockup())
def tearDown(self):
self.index.stop()
self.index = None
def test_get(self):
result = {
"time": "2015-03-26T16:27:48.611441Z",
"timezone": "+08:00,0",
"ntp": {
"enable": 0,
"server": "pool.ntp.org",
"interval": 7200
}
}
resp = Mock()
with patch("index.SysTime.get_system_time") as get_system_time:
get_system_time.return_value = "2015-03-26T16:27:48.611441Z"
self.index.get(message=None, response=resp, test=True)
resp.assert_called_once_with(data=result)
def test_put(self):
result = {
"time": ANY,
"timezone": ANY,
"ntp": {
"enable": ANY,
"server": ANY,
"interval": ANY
}
}
# case 1: no input parameters
resp = Mock()
msg = Message({"data": {}})
self.index.put(message=msg, response=resp, test=True)
resp.assert_called_once_with(code=400,
data={"message": "No input paramters."})
# case 2: change timezone (Normal)
with patch("index.SysTime.set_system_timezone") as set_system_timezone:
resp = Mock()
set_system_timezone.return_value = True
msg = Message({"data": {"timezone": "+08:00,0"}})
self.index.put(message=msg, response=resp, test=True)
resp.assert_called_once_with(data=result)
# case 3: change timezone (Abnormal 1)
resp = Mock()
set_system_timezone.return_value = False
msg = Message({"data": {"timezone": "+08:00,0"}})
self.index.put(message=msg, response=resp, test=True)
resp.assert_called_once_with(
code=500, data={"message": "Change timezone failed."})
# case 4: change timezone (Abnormal 2)
resp = Mock()
msg = Message({"data": {"timezone": "+13:00,1"}})
self.index.put(message=msg, response=resp, test=True)
resp.assert_called_once_with(
code=400, data={"message": "Timezone string error."})
# case 5: change system time (Normal)
with patch("index.SysTime.set_system_time") as set_system_time:
resp = Mock()
set_system_time.return_value = True
msg = Message({"data": {"time": ""}})
self.index.put(message=msg, response=resp, test=True)
resp.assert_called_once_with(data=result)
# case 6: change system time (Abnormal 1)
resp = Mock()
set_system_time.return_value = False
self.index.put(message=msg, response=resp, test=True)
resp.assert_called_once_with(
code=500, data={"message": "Change system time failed."})
# case 7: change system time (Abnormal 2)
resp = Mock()
msg = Message({"data": {"time": ""}})
self.index.put(message=msg, response=resp, test=True)
resp.assert_called_once_with(
code=400, data={"message": "Time format error."})
# case 8: update ntp settings (Normal)
with patch.object(self.index.ntp, "update") as update:
resp = Mock()
update.return_value = True
msg = Message({
"data": {
#.........这里部分代码省略.........
示例2: TestIndexClass
# 需要导入模块: from index import Index [as 别名]
# 或者: from index.Index import put [as 别名]
class TestIndexClass(unittest.TestCase):
def setUp(self):
root_path = os.path.dirname(os.path.realpath(__file__)) + "/../"
try:
os.unlink(root_path + "data/ntp.json")
os.unlink(root_path + "data/ntp.json.backup")
except:
pass
self.index = Index(connection=Mockup())
def tearDown(self):
self.index.stop()
self.index = None
def test_get(self):
result = {
"time": "2015-03-26T16:27:48.611441Z",
"timezone": "Asia/Taipei",
"ntp": {
"enable": 0,
"server": "pool.ntp.org",
"interval": 7200
}
}
resp = Mock()
with patch("index.SysTime.get_system_time") as get_system_time:
get_system_time.return_value = "2015-03-26T16:27:48.611441Z"
self.index.get(message=None, response=resp, test=True)
resp.assert_called_once_with(data=result)
def test_put_schema__should_pass(self):
# arrange
SUT = {
"time": "2015-03-26T16:27:48.611441Z",
"timezone": "Asia/Taipei",
"ntp": {
"enable": True,
"server": "pool.ntp.org",
"interval": 7200
}
}
# act
data = Index.PUT_SCHEMA(SUT)
# assert
self.assertEqual(SUT, data)
def test_put_schema__time_empty_should_fail(self):
# arrange
SUT = {
"time": ""
}
# act
with self.assertRaises(er.MultipleInvalid):
Index.PUT_SCHEMA(SUT)
def test_put(self):
result = {
"time": ANY,
"timezone": ANY,
"ntp": {
"enable": ANY,
"server": ANY,
"interval": ANY
}
}
# case 1: no input parameters
resp = Mock()
msg = Message({"data": {}})
self.index.put(message=msg, response=resp, test=True)
resp.assert_called_once_with(code=400,
data={"message": "No input paramters."})
# case 2: change timezone (Normal)
with patch("index.SysTime.set_system_timezone") as set_system_timezone:
resp = Mock()
set_system_timezone.return_value = True
msg = Message({"data": {"timezone": "Asia/Taipei"}})
self.index.put(message=msg, response=resp, test=True)
resp.assert_called_once_with(data=result)
# case 3: change timezone (Abnormal 1)
resp = Mock()
set_system_timezone.return_value = False
msg = Message({"data": {"timezone": "Asia/Taipei"}})
self.index.put(message=msg, response=resp, test=True)
resp.assert_called_once_with(
code=500, data={"message": "Change timezone failed."})
# case 4: change timezone (Abnormal 2)
resp = Mock()
msg = Message({"data": {"timezone": "Asia/Taipe"}})
self.index.put(message=msg, response=resp, test=True)
resp.assert_called_once_with(
code=400, data={"message": "Timezone not exist."})
#.........这里部分代码省略.........