github.com/grailbio/base@v0.0.11/fileio/names_test.go (about)

     1  // Copyright 2017 GRAIL, Inc. All rights reserved.
     2  // Use of this source code is governed by the Apache-2.0
     3  // license that can be found in the LICENSE file.
     4  
     5  package fileio_test
     6  
     7  import (
     8  	"testing"
     9  
    10  	"github.com/grailbio/base/fileio"
    11  )
    12  
    13  func TestNames(t *testing.T) {
    14  	if got, want := fileio.DetermineType("xx"), fileio.Other; got != want {
    15  		t.Errorf("got %v, want %v", got, want)
    16  	}
    17  
    18  	if got, want := fileio.DetermineType("xx.bar"), fileio.Other; got != want {
    19  		t.Errorf("got %v, want %v", got, want)
    20  	}
    21  
    22  	if got, want := fileio.DetermineType("xx.grail-rpk"), fileio.GrailRIOPacked; got != want {
    23  		t.Errorf("got %v, want %v", got, want)
    24  	}
    25  
    26  	if got, want := fileio.FileSuffix(fileio.GrailRIOPackedEncrypted), ".grail-rpk-kd"; got != want {
    27  		t.Errorf("got %v, want %v", got, want)
    28  	}
    29  
    30  	if got, want := fileio.FileSuffix(fileio.Other), ""; got != want {
    31  		t.Errorf("got %v, want %v", got, want)
    32  	}
    33  
    34  	if got, want := fileio.DetermineAPI("xx.bar"), fileio.LocalAPI; got != want {
    35  		t.Errorf("got %v, want %v", got, want)
    36  	}
    37  	if got, want := fileio.DetermineAPI("s3://"), fileio.S3API; got != want {
    38  		t.Errorf("got %v, want %v", got, want)
    39  	}
    40  }
    41  
    42  func TestS3Spelling(t *testing.T) {
    43  	for _, tc := range []struct {
    44  		input     string
    45  		api       fileio.StorageAPI
    46  		corrected bool
    47  		fixed     string
    48  	}{
    49  		{"s3://ok", fileio.S3API, false, "s3://ok"},
    50  		{"s3://ok/a", fileio.S3API, false, "s3://ok/a"},
    51  		{"s3://", fileio.S3API, false, "s3://"},
    52  		{"s3:/", fileio.S3API, true, "s3://"},
    53  		{"s3:/1", fileio.S3API, true, "s3://1"},
    54  		{"s3:///2", fileio.S3API, true, "s3://2"},
    55  		{"s3:///2/x", fileio.S3API, true, "s3://2/x"},
    56  		{"s3:4", fileio.S3API, true, "s3://4"},
    57  		{"s://5", fileio.S3API, true, "s3://5"},
    58  		{"s:/6", fileio.S3API, true, "s3://6"},
    59  		{"s33", fileio.LocalAPI, false, "s33"},
    60  	} {
    61  		api, corrected, fixed := fileio.SpellCorrectS3(tc.input)
    62  		if got, want := api, tc.api; got != want {
    63  			t.Errorf("%v: got %v, want %v", tc.input, got, want)
    64  		}
    65  		if got, want := corrected, tc.corrected; got != want {
    66  			t.Errorf("%v: got %v, want %v", tc.input, got, want)
    67  		}
    68  		if got, want := fixed, tc.fixed; got != want {
    69  			t.Errorf("%v: got %v, want %v", tc.input, got, want)
    70  		}
    71  	}
    72  }