github.com/fraugster/parquet-go@v0.12.0/parquetschema/schema_def_test.go (about)

     1  package parquetschema
     2  
     3  import (
     4  	"io/ioutil"
     5  	"os"
     6  	"path/filepath"
     7  	"testing"
     8  
     9  	"github.com/fraugster/parquet-go/parquet"
    10  	"github.com/stretchr/testify/require"
    11  )
    12  
    13  func TestSchemaDefinition(t *testing.T) {
    14  	dir, err := os.Open("schema-files")
    15  	require.NoError(t, err)
    16  
    17  	files, err := dir.Readdirnames(-1)
    18  	require.NoError(t, err)
    19  
    20  	for idx, file := range files {
    21  		fileContent, err := ioutil.ReadFile(filepath.Join("schema-files", file))
    22  		require.NoError(t, err)
    23  
    24  		schemaText := string(fileContent)
    25  
    26  		sd, err := ParseSchemaDefinition(schemaText)
    27  		require.NoError(t, err, "%d. %s: parsing schema definition returned error", idx, file)
    28  
    29  		require.Equal(t, schemaText, sd.String(), "%d. %s: sd.String returned different string representation", idx, file)
    30  	}
    31  }
    32  
    33  func TestNilSchemaDef(t *testing.T) {
    34  	var sd *SchemaDefinition
    35  
    36  	require.Equal(t, "message empty {\n}\n", sd.String())
    37  
    38  	sd = &SchemaDefinition{}
    39  
    40  	require.Equal(t, "message empty {\n}\n", sd.String())
    41  }
    42  
    43  func TestSchemaElement(t *testing.T) {
    44  	var sd *SchemaDefinition
    45  
    46  	require.Nil(t, sd.SchemaElement())
    47  
    48  	sd = &SchemaDefinition{}
    49  	require.Nil(t, sd.SchemaElement())
    50  
    51  	sd, err := ParseSchemaDefinition(`message foo { required int64 bar; }`)
    52  	require.NoError(t, err)
    53  
    54  	require.NotNil(t, sd.SchemaElement())
    55  
    56  	require.Equal(t, &parquet.SchemaElement{
    57  		Name: "foo",
    58  	}, sd.SchemaElement())
    59  }
    60  
    61  func TestInvalidSchema(t *testing.T) {
    62  	_, err := ParseSchemaDefinition("")
    63  	require.Error(t, err)
    64  }
    65  
    66  func TestSchemaDefinitionFromColumnDefinition(t *testing.T) {
    67  	require.Nil(t, SchemaDefinitionFromColumnDefinition(nil))
    68  
    69  	require.NotNil(t, SchemaDefinitionFromColumnDefinition(&ColumnDefinition{}))
    70  }
    71  
    72  func TestParseAndGenerateSchema(t *testing.T) {
    73  	schema := `message msg {
    74    required int64 foo (INT(64, true));
    75    required int32 bar (DECIMAL(5, 3));
    76    required binary baz (JSON);
    77    required binary quux (BSON);
    78    required fixed_len_byte_array(16) bla (UUID);
    79    required binary fasel (ENUM);
    80    required int64 t1 (TIMESTAMP(NANOS, true));
    81    required int64 t2 (TIMESTAMP(MICROS, false));
    82    required int64 t3 (TIMESTAMP(MILLIS, true));
    83    required float f;
    84    required double d;
    85    required binary aa (UTF8);
    86    required int32 bb (TIME_MILLIS);
    87    required int64 cc (TIME_MICROS);
    88    required int64 dd (TIMESTAMP_MILLIS);
    89    required int64 ee (TIMESTAMP_MICROS);
    90    required int32 ff (UINT_8);
    91    required int32 gg (UINT_16);
    92    required int32 hh (UINT_32);
    93    required int64 ii (UINT_64);
    94    required int32 jj (INT_8);
    95    required int32 kk (INT_16);
    96    required int32 ll (INT_32);
    97    required int64 mm (INT_64);
    98    required fixed_len_byte_array(12) nn (INTERVAL);
    99    required int64 tt1 (TIME(NANOS, true));
   100    required int64 tt2 (TIME(MICROS, true));
   101    required int32 tt3 (TIME(MILLIS, true));
   102    required int96 oo;
   103  }
   104  `
   105  
   106  	schemaDef, err := ParseSchemaDefinition(schema)
   107  	require.NoError(t, err, "parsing schema definition failed")
   108  
   109  	require.Equal(t, schema, schemaDef.String(), "expected and actual schema definition does not match")
   110  
   111  	newSchemaDef := schemaDef.Clone()
   112  	require.Equal(t, schemaDef, newSchemaDef, "cloned schema definition doesn't match original")
   113  }
   114  
   115  func TestSubSchema(t *testing.T) {
   116  	schema := `message foo {
   117  		required group bar {
   118  			required int64 baz;
   119  		}
   120  	}`
   121  
   122  	schemaDef, err := ParseSchemaDefinition(schema)
   123  	require.NoError(t, err, "parsing schema definition failed")
   124  
   125  	require.Equal(t, "message bar {\n  required int64 baz;\n}\n", schemaDef.SubSchema("bar").String())
   126  
   127  	require.Nil(t, schemaDef.SubSchema("does-not-exist"))
   128  }