github.com/mendersoftware/go-lib-micro@v0.0.0-20240304135804-e8e39c59b148/mongo/migrate/migrator_dummy_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_test
    15  
    16  import (
    17  	"context"
    18  	"errors"
    19  	"testing"
    20  	"time"
    21  
    22  	"github.com/stretchr/testify/assert"
    23  
    24  	. "github.com/mendersoftware/go-lib-micro/mongo/migrate"
    25  	"go.mongodb.org/mongo-driver/bson"
    26  )
    27  
    28  func TestDummyMigratorApply(t *testing.T) {
    29  	if testing.Short() {
    30  		t.Skip("skipping TestDummyMigratorApply in short mode.")
    31  	}
    32  
    33  	testCases := map[string]struct {
    34  		Automigrate     bool
    35  		InputMigrations []*MigrationEntry
    36  		InputVersion    Version
    37  
    38  		OutputVersion Version
    39  		OutputError   error
    40  	}{
    41  		"ok - empty state, automigrate": {
    42  			Automigrate:     true,
    43  			InputMigrations: []*MigrationEntry{},
    44  			InputVersion:    Version{Major: 1, Minor: 0, Patch: 0},
    45  
    46  			OutputVersion: Version{Major: 1, Minor: 0, Patch: 0},
    47  		},
    48  
    49  		"ok - already has version, automigrate": {
    50  			Automigrate: true,
    51  			InputMigrations: []*MigrationEntry{
    52  				&MigrationEntry{
    53  					Version:   Version{Major: 1, Minor: 0, Patch: 0},
    54  					Timestamp: time.Now(),
    55  				},
    56  			},
    57  			InputVersion:  Version{Major: 1, Minor: 0, Patch: 0},
    58  			OutputVersion: Version{Major: 1, Minor: 0, Patch: 0},
    59  		},
    60  
    61  		"ok - empty state, no automigrate": {
    62  			Automigrate:     false,
    63  			InputMigrations: []*MigrationEntry{},
    64  			InputVersion:    Version{Major: 1, Minor: 0, Patch: 0},
    65  
    66  			OutputVersion: Version{Major: 0, Minor: 0, Patch: 0},
    67  			OutputError:   errors.New("db needs migration: test has version 0.0.0, needs version 1.0.0"),
    68  		},
    69  
    70  		"ok - already has version, no automigrate": {
    71  			Automigrate: false,
    72  			InputMigrations: []*MigrationEntry{
    73  				&MigrationEntry{
    74  					Version:   Version{Major: 1, Minor: 0, Patch: 0},
    75  					Timestamp: time.Now(),
    76  				},
    77  			},
    78  			InputVersion:  Version{Major: 1, Minor: 0, Patch: 0},
    79  			OutputVersion: Version{Major: 1, Minor: 0, Patch: 0},
    80  		},
    81  	}
    82  
    83  	for name, tc := range testCases {
    84  		t.Logf("test case: %s", name)
    85  
    86  		//setup
    87  		db.Wipe()
    88  		client := db.Client()
    89  		for _, m := range tc.InputMigrations {
    90  			_, err := client.Database("test").
    91  				Collection(DbMigrationsColl).
    92  				InsertOne(db.CTX(), m)
    93  			assert.NoError(t, err)
    94  		}
    95  
    96  		//test
    97  		m := &DummyMigrator{Client: client, Db: "test", Automigrate: tc.Automigrate}
    98  		m.Apply(context.Background(), tc.InputVersion, nil)
    99  
   100  		//verify
   101  		var out []MigrationEntry
   102  		cursor, _ := client.Database("test").
   103  			Collection(DbMigrationsColl).
   104  			Find(db.CTX(), bson.M{})
   105  
   106  		count := 0
   107  		for cursor.Next(db.CTX()) {
   108  			var res MigrationEntry
   109  			count++
   110  			elem := &bson.D{}
   111  			_ = cursor.Decode(elem)
   112  			bsonBytes, _ := bson.Marshal(elem)
   113  			bson.Unmarshal(bsonBytes, &res)
   114  			out = append(out, res)
   115  		}
   116  
   117  		if tc.Automigrate {
   118  			assert.Len(t, out, 1)
   119  			assert.Equal(t, tc.OutputVersion, out[0].Version)
   120  		} else {
   121  			assert.Len(t, out, len(tc.InputMigrations))
   122  		}
   123  	}
   124  }