github.com/mendersoftware/go-lib-micro@v0.0.0-20240304135804-e8e39c59b148/mongo/migrate/version_test.go (about)

     1  // Copyright 2023 Northern.tech AS
     2  //
     3  //	Licensed under the Apache License, Version 2.0 (the "License");
     4  //	you may not use this file except in compliance with the License.
     5  //	You may obtain a copy of the License at
     6  //
     7  //	    http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  //	Unless required by applicable law or agreed to in writing, software
    10  //	distributed under the License is distributed on an "AS IS" BASIS,
    11  //	WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  //	See the License for the specific language governing permissions and
    13  //	limitations under the License.
    14  package migrate
    15  
    16  import (
    17  	"testing"
    18  
    19  	"github.com/stretchr/testify/assert"
    20  )
    21  
    22  func TestNewVersion(t *testing.T) {
    23  	testCases := map[string]struct {
    24  		input string
    25  
    26  		output Version
    27  		err    string
    28  	}{
    29  		"1.0.0": {
    30  			input:  "1.0.0",
    31  			output: Version{Major: 1, Minor: 0, Patch: 0},
    32  			err:    "",
    33  		},
    34  		"1.0": {
    35  			input:  "1.0",
    36  			output: Version{Major: 1, Minor: 0, Patch: 0},
    37  			err:    "failed to parse Version: unexpected EOF",
    38  		},
    39  		"1_0_0": {
    40  			input:  "1_0_0",
    41  			output: Version{Major: 1, Minor: 0, Patch: 0},
    42  			err:    "failed to parse Version: input does not match format",
    43  		},
    44  	}
    45  	for name, tc := range testCases {
    46  		t.Logf("test case: %s", name)
    47  		ver, err := NewVersion(tc.input)
    48  		if tc.err != "" {
    49  			assert.EqualError(t, err, tc.err)
    50  		} else {
    51  			assert.NoError(t, err)
    52  			assert.Equal(t, tc.output, *ver)
    53  		}
    54  	}
    55  }
    56  
    57  func TestVersionString(t *testing.T) {
    58  	testCases := map[string]struct {
    59  		input Version
    60  
    61  		output string
    62  	}{
    63  		"1.0.0": {
    64  			input:  Version{Major: 1, Minor: 0, Patch: 0},
    65  			output: "1.0.0",
    66  		},
    67  		"4.2.5": {
    68  			input:  Version{Major: 4, Minor: 2, Patch: 5},
    69  			output: "4.2.5",
    70  		},
    71  	}
    72  	for name, tc := range testCases {
    73  		t.Logf("test case: %s", name)
    74  		str := tc.input.String()
    75  		assert.Equal(t, tc.output, str)
    76  	}
    77  }
    78  
    79  func TestVersionIsLess(t *testing.T) {
    80  	assert.True(t, VersionIsLess(Version{0, 0, 0}, Version{1, 1, 0}))
    81  	assert.True(t, VersionIsLess(Version{1, 0, 0}, Version{1, 1, 0}))
    82  	assert.True(t, VersionIsLess(Version{1, 0, 0}, Version{1, 0, 1}))
    83  	assert.False(t, VersionIsLess(Version{1, 0, 0}, Version{0, 1, 0}))
    84  	assert.False(t, VersionIsLess(Version{1, 1, 0}, Version{1, 0, 1}))
    85  	assert.False(t, VersionIsLess(Version{1, 1, 0}, Version{1, 1, 0}))
    86  }