コード例 #1
0
 def testReadandValidateMultipleNestedSchemaFromFile(self):
     infile = os.path.join(self.dirname, 'test_multiple_nested_schema_file')
     f = open(infile, 'wt')
     f.write(test_util.GetJobsSchemaString())
     f.close()
     read_schema = load_lib.ReadSchemaFile(infile)
     load_lib._ValidateExtendedSchema(read_schema)
     expected_schema = json.loads(test_util.GetJobsSchemaString())
     self.assertEquals(expected_schema, read_schema)
     # append some non-json text and check failure.
     f = open(infile, 'at')
     f.write('bogus')
     f.close()
     try:
         load_lib.ReadSchemaFile(infile)
         self.fail()
     except ValueError:
         pass  # success
コード例 #2
0
 def testRewriteSchemaWhenString(self):
     """Regression test on RewriteSchema() sloppy behavior on inputs."""
     schema = test_util.GetJobsSchemaString()
     self.assertTrue(isinstance(schema, types.StringTypes))
     # set up the for loop to wrongfully iterate only once, a single char,
     # instead of supplying the entire string and needing to break the
     # flow after _RewriteField()
     schema = schema[0]
     self.mox.StubOutWithMock(load_lib, '_RewriteField')
     load_lib._RewriteField(schema, []).AndReturn(None)
     self.mox.ReplayAll()
     new_schema = load_lib.RewriteSchema(schema)
     self.mox.VerifyAll()
     self.assertEqual(new_schema, [])
コード例 #3
0
 def testConvertComplexJsonDataFile(self):
     schema = json.loads(test_util.GetJobsSchemaString())
     infile = self._WriteTempJobsJsonFile()
     outfile = os.path.join(self.dirname, 'jobs.enc_data')
     master_key = base64.b64decode(_MASTER_KEY)
     string_hasher = ecrypto.StringHash(
         ecrypto.GenerateStringHashKey(master_key, _TABLE_ID))
     load_lib.ConvertJsonDataFile(schema, master_key, _TABLE_ID, infile,
                                  outfile)
     # validate new data file against new rewritten schema.
     new_schema = json.loads(_JOBS_REWRITTEN_SCHEMA)
     load_lib._ValidateJsonDataFile(new_schema, outfile)
     fout = open(outfile, 'rt')
     for line in fout:
         data = json.loads(line)
         break
     self.assertEqual(data['kind'], 'person')
     self.assertTrue(util.SEARCHWORDS_PREFIX + u'gender' in data)
     (model_iv,
      model_hash) = data[util.SEARCHWORDS_PREFIX + u'gender'].split(' ')
     expected_model_key_hash = string_hasher.GetStringKeyHash(
         util.SEARCHWORDS_PREFIX + u'gender', u'Male'.lower())
     expected_model_hash = base64.b64encode(
         hashlib.sha1(model_iv + expected_model_key_hash).digest()[:8])
     self.assertEquals(expected_model_hash, model_hash)
     self.assertTrue(util.SEARCHWORDS_PREFIX +
                     u'place' in data['citiesLived'][0])
     (model_iv,
      model_hash) = data['citiesLived'][0][util.SEARCHWORDS_PREFIX +
                                           u'place'].split(' ')
     expected_model_key_hash = string_hasher.GetStringKeyHash(
         util.SEARCHWORDS_PREFIX + u'place', u'Seattle'.lower())
     expected_model_hash = base64.b64encode(
         hashlib.sha1(model_iv + expected_model_key_hash).digest()[:8])
     self.assertEquals(expected_model_hash, model_hash)
     self.assertEquals(data['citiesLived'][0]['job'][0]['jobRank'], 1)
     self.assertEquals(data['citiesLived'][1]['job'], [])
     self.assertEquals(
         len(data['citiesLived'][0]['job'][0][util.SEARCHWORDS_PREFIX +
                                              u'manager']), 3)
     self.assertEquals(
         len(data['citiesLived'][0]['job'][0][util.SEARCHWORDS_PREFIX +
                                              u'manager'][0].split(' ')), 4)
     fout.close()
コード例 #4
0
 def testValidateComplexJsonDataFile(self):
     schema = json.loads(test_util.GetJobsSchemaString())
     infile = self._WriteTempJobsJsonFile()
     load_lib._ValidateJsonDataFile(schema, infile)
コード例 #5
0
 def testRewriteSchemaTypeCheck(self):
     schema = json.loads(test_util.GetJobsSchemaString())
     self.assertTrue(isinstance(schema, types.ListType))
     new_schema = load_lib.RewriteSchema(schema)
     self.assertTrue(isinstance(new_schema, schema.__class__))
コード例 #6
0
 def testRewriteMultipleNestedSchemaAsJsonFile(self):
     schema = json.loads(test_util.GetJobsSchemaString())
     new_schema = load_lib.RewriteSchema(schema)
     expected_schema = json.loads(_JOBS_REWRITTEN_SCHEMA)
     self.assertEquals(expected_schema, new_schema)