def test_field_descriptor_at_first_level(self):
        schema = self._get_schema_descriptor()

        self.assertEqual(
            schema.get_field_descriptor('field_1'),
            bigquery_schema_descriptor.FieldDescriptor(
                type=Consts.TYPE_STRING, mode=Consts.MODE_NULLABLE))
        self.assertEqual(
            schema.get_field_descriptor('field_2'),
            bigquery_schema_descriptor.FieldDescriptor(
                type=Consts.TYPE_INTEGER, mode=Consts.MODE_REPEATED))
    def test_field_descriptor_at_second_level(self):
        main_schema = self._get_schema_descriptor()
        record_schema = main_schema.get_record_schema_descriptor('record_1')

        self.assertEqual(
            record_schema.get_field_descriptor('record_1_field_1'),
            bigquery_schema_descriptor.FieldDescriptor(
                type=Consts.TYPE_BOOLEAN, mode=Consts.MODE_NULLABLE))
        self.assertEqual(
            record_schema.get_field_descriptor('record_1_field_2'),
            bigquery_schema_descriptor.FieldDescriptor(
                type=Consts.TYPE_FLOAT, mode=Consts.MODE_REPEATED))
 def _run_resolve_schema_conflict_tests(self, test_data_configs):
     for config in test_data_configs:
         self.assertEqual(
             self._resolver.resolve_schema_conflict(
                 bigquery_schema_descriptor.FieldDescriptor(
                     type=config.schema_type, mode=config.schema_mode),
                 config.field_data), config.expected_resolved_field_data)
    def test_field_descriptor_at_third_level(self):
        main_schema = self._get_schema_descriptor()
        parent_record_schema = main_schema.get_record_schema_descriptor(
            'record_1')
        child_record_schema = parent_record_schema.get_record_schema_descriptor(
            'record_1-record_2')

        self.assertEqual(
            child_record_schema.get_field_descriptor(
                'record_1-record_2_field_1'),
            bigquery_schema_descriptor.FieldDescriptor(
                type=Consts.TYPE_BOOLEAN, mode=Consts.MODE_NULLABLE))
  def test_resolving_schema_conflict_type(self):
    test_data_configs = [
        SchemaTestConfig(schema_type=TableFieldConstants.TYPE_BOOLEAN,
                         schema_mode=TableFieldConstants.MODE_NULLABLE,
                         field_data=1,
                         expected_resolved_field_data=True),
        SchemaTestConfig(schema_type=TableFieldConstants.TYPE_BOOLEAN,
                         schema_mode=TableFieldConstants.MODE_REPEATED,
                         field_data=['1', '2'],
                         expected_resolved_field_data=[True, True]),
        SchemaTestConfig(schema_type=TableFieldConstants.TYPE_INTEGER,
                         schema_mode=TableFieldConstants.MODE_NULLABLE,
                         field_data='1',
                         expected_resolved_field_data=1),
        SchemaTestConfig(schema_type=TableFieldConstants.TYPE_INTEGER,
                         schema_mode=TableFieldConstants.MODE_REPEATED,
                         field_data=['1', '2'],
                         expected_resolved_field_data=[1, 2]),
        SchemaTestConfig(schema_type=TableFieldConstants.TYPE_FLOAT,
                         schema_mode=TableFieldConstants.MODE_NULLABLE,
                         field_data=1,
                         expected_resolved_field_data=float(1)),
        SchemaTestConfig(schema_type=TableFieldConstants.TYPE_FLOAT,
                         schema_mode=TableFieldConstants.MODE_REPEATED,
                         field_data=[1, 2],
                         expected_resolved_field_data=[float(1), float(2)]),
        SchemaTestConfig(schema_type=TableFieldConstants.TYPE_STRING,
                         schema_mode=TableFieldConstants.MODE_NULLABLE,
                         field_data=1,
                         expected_resolved_field_data='1'),
        SchemaTestConfig(schema_type=TableFieldConstants.TYPE_STRING,
                         schema_mode=TableFieldConstants.MODE_REPEATED,
                         field_data=[1, 2],
                         expected_resolved_field_data=['1', '2']),
        SchemaTestConfig(schema_type=TableFieldConstants.TYPE_BOOLEAN,
                         schema_mode=TableFieldConstants.MODE_NULLABLE,
                         field_data='',
                         expected_resolved_field_data=False),
        SchemaTestConfig(schema_type=TableFieldConstants.TYPE_BOOLEAN,
                         schema_mode=TableFieldConstants.MODE_REPEATED,
                         field_data=['', ''],
                         expected_resolved_field_data=[False, False]),
        SchemaTestConfig(schema_type=TableFieldConstants.TYPE_STRING,
                         schema_mode=TableFieldConstants.MODE_NULLABLE,
                         field_data=[],
                         expected_resolved_field_data=None),
        SchemaTestConfig(schema_type=TableFieldConstants.TYPE_STRING,
                         schema_mode=TableFieldConstants.MODE_REPEATED,
                         field_data=[],
                         expected_resolved_field_data=[]),
        SchemaTestConfig(schema_type=TableFieldConstants.TYPE_BOOLEAN,
                         schema_mode=TableFieldConstants.MODE_NULLABLE,
                         field_data=[],
                         expected_resolved_field_data=False),
        SchemaTestConfig(schema_type=TableFieldConstants.TYPE_BOOLEAN,
                         schema_mode=TableFieldConstants.MODE_REPEATED,
                         field_data=[],
                         expected_resolved_field_data=[]),
    ]

    self._run_resolve_schema_conflict_tests(test_data_configs)

    with self.assertRaises(ValueError):
      self._resolver.resolve_schema_conflict(
          bigquery_schema_descriptor.FieldDescriptor(
              type=TableFieldConstants.TYPE_INTEGER,
              mode=TableFieldConstants.MODE_NULLABLE),
          'foo')
      self.fail('Should raise exception for converting str to int')