github.com/dannyzhou2015/migrate/v4@v4.15.2/source/parse_test.go (about)

     1  package source
     2  
     3  import (
     4  	"testing"
     5  )
     6  
     7  func TestParse(t *testing.T) {
     8  	tt := []struct {
     9  		name            string
    10  		expectErr       error
    11  		expectMigration *Migration
    12  	}{
    13  		{
    14  			name:      "1_foobar.up.sql",
    15  			expectErr: nil,
    16  			expectMigration: &Migration{
    17  				Version:    1,
    18  				Identifier: "foobar",
    19  				Direction:  Up,
    20  				Raw:        "1_foobar.up.sql",
    21  			},
    22  		},
    23  		{
    24  			name:      "1_foobar.down.sql",
    25  			expectErr: nil,
    26  			expectMigration: &Migration{
    27  				Version:    1,
    28  				Identifier: "foobar",
    29  				Direction:  Down,
    30  				Raw:        "1_foobar.down.sql",
    31  			},
    32  		},
    33  		{
    34  			name:      "1_f-o_ob+ar.up.sql",
    35  			expectErr: nil,
    36  			expectMigration: &Migration{
    37  				Version:    1,
    38  				Identifier: "f-o_ob+ar",
    39  				Direction:  Up,
    40  				Raw:        "1_f-o_ob+ar.up.sql",
    41  			},
    42  		},
    43  		{
    44  			name:      "1485385885_foobar.up.sql",
    45  			expectErr: nil,
    46  			expectMigration: &Migration{
    47  				Version:    1485385885,
    48  				Identifier: "foobar",
    49  				Direction:  Up,
    50  				Raw:        "1485385885_foobar.up.sql",
    51  			},
    52  		},
    53  		{
    54  			name:      "20170412214116_date_foobar.up.sql",
    55  			expectErr: nil,
    56  			expectMigration: &Migration{
    57  				Version:    20170412214116,
    58  				Identifier: "date_foobar",
    59  				Direction:  Up,
    60  				Raw:        "20170412214116_date_foobar.up.sql",
    61  			},
    62  		},
    63  		{
    64  			name:            "-1_foobar.up.sql",
    65  			expectErr:       ErrParse,
    66  			expectMigration: nil,
    67  		},
    68  		{
    69  			name:            "foobar.up.sql",
    70  			expectErr:       ErrParse,
    71  			expectMigration: nil,
    72  		},
    73  		{
    74  			name:            "1.up.sql",
    75  			expectErr:       ErrParse,
    76  			expectMigration: nil,
    77  		},
    78  		{
    79  			name:            "1_foobar.sql",
    80  			expectErr:       ErrParse,
    81  			expectMigration: nil,
    82  		},
    83  		{
    84  			name:            "1_foobar.up",
    85  			expectErr:       ErrParse,
    86  			expectMigration: nil,
    87  		},
    88  		{
    89  			name:            "1_foobar.down",
    90  			expectErr:       ErrParse,
    91  			expectMigration: nil,
    92  		},
    93  	}
    94  
    95  	for i, v := range tt {
    96  		f, err := Parse(v.name)
    97  
    98  		if err != v.expectErr {
    99  			t.Errorf("expected %v, got %v, in %v", v.expectErr, err, i)
   100  		}
   101  
   102  		if v.expectMigration != nil && *f != *v.expectMigration {
   103  			t.Errorf("expected %+v, got %+v, in %v", *v.expectMigration, *f, i)
   104  		}
   105  	}
   106  }