github.com/fr-nvriep/migrate/v4@v4.3.2/internal/cli/commands_test.go (about)

     1  package cli
     2  
     3  import (
     4  	"testing"
     5  )
     6  
     7  func TestNextSeq(t *testing.T) {
     8  	cases := []struct {
     9  		name           string
    10  		matches        []string
    11  		dir            string
    12  		seqDigits      int
    13  		expected       string
    14  		expectedErrStr string
    15  	}{
    16  		{"Bad digits", []string{}, "migrationDir", 0, "", "Digits must be positive"},
    17  		{"Single digit initialize", []string{}, "migrationDir", 1, "1", ""},
    18  		{"Single digit malformed", []string{"bad"}, "migrationDir", 1, "", "Malformed migration filename: bad"},
    19  		{"Single digit no int", []string{"bad_bad"}, "migrationDir", 1, "", "strconv.Atoi: parsing \"bad\": invalid syntax"},
    20  		{"Single digit negative seq", []string{"-5_test"}, "migrationDir", 1, "", "Next sequence number must be positive"},
    21  		{"Single digit increment", []string{"3_test", "4_test"}, "migrationDir", 1, "5", ""},
    22  		{"Single digit overflow", []string{"9_test"}, "migrationDir", 1, "", "Next sequence number 10 too large. At most 1 digits are allowed"},
    23  		{"Zero-pad initialize", []string{}, "migrationDir", 6, "000001", ""},
    24  		{"Zero-pad malformed", []string{"bad"}, "migrationDir", 6, "", "Malformed migration filename: bad"},
    25  		{"Zero-pad no int", []string{"bad_bad"}, "migrationDir", 6, "", "strconv.Atoi: parsing \"bad\": invalid syntax"},
    26  		{"Zero-pad negative seq", []string{"-000005_test"}, "migrationDir", 6, "", "Next sequence number must be positive"},
    27  		{"Zero-pad increment", []string{"000003_test", "000004_test"}, "migrationDir", 6, "000005", ""},
    28  		{"Zero-pad overflow", []string{"999999_test"}, "migrationDir", 6, "", "Next sequence number 1000000 too large. At most 6 digits are allowed"},
    29  	}
    30  	for _, c := range cases {
    31  		t.Run(c.name, func(t *testing.T) {
    32  			nextSeq, err := nextSeq(c.matches, c.dir, c.seqDigits)
    33  			if nextSeq != c.expected {
    34  				t.Error("Incorrect nextSeq: " + nextSeq + " != " + c.expected)
    35  			}
    36  			if err != nil {
    37  				if err.Error() != c.expectedErrStr {
    38  					t.Error("Incorrect error: " + err.Error() + " != " + c.expectedErrStr)
    39  				}
    40  			} else if c.expectedErrStr != "" {
    41  				t.Error("Expected error: " + c.expectedErrStr + " but got nil instead")
    42  			}
    43  		})
    44  	}
    45  }