コード例 #1
0
ファイル: prototype.py プロジェクト: cloudnative/cruddy
 def check(self, item, operation, response):
     for key in self.prototype:
         value = self.prototype[key]
         cv = CalculatedValue.check(value)
         if cv:
             if cv.operation == operation:
                 item[key] = cv.value
             elif cv.operation == "update" and operation == "create":
                 item[key] = cv.value
         else:
             # a Python object was provided as the prototype value
             # so if the item has a value for that attribute name
             # check to see if it is the right type
             if key in item:
                 if type(item[key]) != type(value):
                     response.status = "error"
                     response.error_type = "InvalidType"
                     msg = "Attribute {} must be of type {}".format(key, type(value))
                     response.error_message = msg
                     return False
             else:
                 item[key] = value
     return True
コード例 #2
0
 def check(self, item, operation, response):
     for key in self.prototype:
         value = self.prototype[key]
         cv = CalculatedValue.check(value)
         if cv:
             if cv.operation == operation:
                 item[key] = cv.value
             elif cv.operation == 'update' and operation == 'create':
                 item[key] = cv.value
         else:
             # a Python object was provided as the prototype value
             # so if the item has a value for that attribute name
             # check to see if it is the right type
             if key in item:
                 if type(item[key]) != type(value):
                     response.status = 'error'
                     response.error_type = 'InvalidType'
                     msg = 'Attribute {} must be of type {}'.format(
                         key, type(value))
                     response.error_message = msg
                     return False
             else:
                 item[key] = value
     return True
コード例 #3
0
 def test_bad_token(self):
     cv = CalculatedValue.check('<on-create:foobar>')
     self.assertIsNone(cv)
コード例 #4
0
 def test_bad_operation(self):
     cv = CalculatedValue.check('<on-foobar:uuid>')
     self.assertIsNone(cv)
コード例 #5
0
 def test_ts_create(self):
     cv = CalculatedValue.check('<on-create:timestamp>')
     before = int(time.time() * 1000)
     self.assertEqual(cv.operation, 'create')
     self.assertGreaterEqual(cv.value, before)
     self.assertLessEqual(cv.value, int(time.time() * 1000))
コード例 #6
0
 def test_uuid_update(self):
     cv = CalculatedValue.check('<on-update:uuid>')
     self.assertEqual(cv.operation, 'update')
     self.assertTrue(self.uuid_re.match(cv.value))
コード例 #7
0
 def test_bad_token(self):
     cv = CalculatedValue.check("<on-create:foobar>")
     self.assertIsNone(cv)
コード例 #8
0
 def test_bad_operation(self):
     cv = CalculatedValue.check("<on-foobar:uuid>")
     self.assertIsNone(cv)
コード例 #9
0
 def test_ts_create(self):
     cv = CalculatedValue.check("<on-create:timestamp>")
     before = int(time.time() * 1000)
     self.assertEqual(cv.operation, "create")
     self.assertGreaterEqual(cv.value, before)
     self.assertLessEqual(cv.value, int(time.time() * 1000))
コード例 #10
0
 def test_uuid_update(self):
     cv = CalculatedValue.check("<on-update:uuid>")
     self.assertEqual(cv.operation, "update")
     self.assertTrue(self.uuid_re.match(cv.value))