github.com/ActiveState/cli@v0.0.0-20240508170324-6801f60cd051/pkg/platform/model/vcs_internal_test.go (about)

     1  package model
     2  
     3  import (
     4  	"fmt"
     5  	"reflect"
     6  	"testing"
     7  
     8  	"github.com/ActiveState/cli/pkg/platform/api/mono/mono_models"
     9  	"github.com/go-openapi/strfmt"
    10  )
    11  
    12  func TestIndexedCommits(t *testing.T) {
    13  	t.Run("countBetween", testIndexedCommitsCountBetween)
    14  }
    15  
    16  func testIndexedCommitsCountBetween(t *testing.T) {
    17  	// linked data
    18  	// a-b-c-d-e
    19  	basic := indexedCommits{
    20  		"e": "d",
    21  		"d": "c",
    22  		"c": "b",
    23  		"b": "a",
    24  		"a": "",
    25  	}
    26  	// linked data with split
    27  	// a-b-c
    28  	//  \
    29  	//   d-e
    30  	split := indexedCommits{
    31  		"e": "d",
    32  		"d": "a",
    33  		"c": "b",
    34  		"b": "a",
    35  		"a": "",
    36  	}
    37  
    38  	tests := map[string]struct {
    39  		indexed indexedCommits
    40  		first   string
    41  		last    string
    42  		want    int
    43  		wantErr bool
    44  	}{
    45  		"basic: none to last":     {basic, "", "e", 5, false},
    46  		"basic: first to none":    {basic, "a", "", 0, true},
    47  		"basic: first to last":    {basic, "a", "e", 4, false},
    48  		"basic: first to second":  {basic, "a", "b", 1, false},
    49  		"basic: second to fourth": {basic, "b", "d", 2, false},
    50  		"basic: first to badval":  {basic, "a", "x", 0, true},
    51  		"basic: badval to last":   {basic, "x", "e", 0, true},
    52  		"split: none to last":     {split, "", "e", 3, false},
    53  		"split: first to none":    {split, "a", "", 0, true},
    54  		"split: first to last":    {split, "a", "e", 2, false},
    55  		"split: first to second":  {split, "a", "b", 1, false},
    56  		"split: second to broken": {split, "b", "d", 0, true},
    57  	}
    58  
    59  	for label, test := range tests {
    60  		got, err := test.indexed.countBetween(test.first, test.last)
    61  		gotErr := err != nil
    62  
    63  		if test.wantErr != gotErr {
    64  			t.Errorf("%s: got %v, want %v", label, gotErr, test.wantErr)
    65  		}
    66  
    67  		if got != test.want {
    68  			t.Errorf("%s: got %v, want %v", label, got, test.want)
    69  		}
    70  	}
    71  }
    72  
    73  func cid(id int) strfmt.UUID {
    74  	return strfmt.UUID(fmt.Sprintf("00000000-0000-0000-0000-%011d", id))
    75  }
    76  
    77  func cidp(id int) *strfmt.UUID {
    78  	v := cid(id)
    79  	return &v
    80  }
    81  
    82  func Test_commonParentWithHistory(t *testing.T) {
    83  	type args struct {
    84  		commit1  *strfmt.UUID
    85  		commit2  *strfmt.UUID
    86  		history1 []*mono_models.Commit
    87  		history2 []*mono_models.Commit
    88  	}
    89  	tests := []struct {
    90  		name string
    91  		args args
    92  		want *strfmt.UUID
    93  	}{
    94  		{
    95  			"Same commits",
    96  			args{
    97  				cidp(1), cidp(1), nil, nil,
    98  			},
    99  			cidp(1),
   100  		},
   101  		{
   102  			"One nil",
   103  			args{
   104  				cidp(1), nil, nil, nil,
   105  			},
   106  			nil,
   107  		},
   108  		{
   109  			"Both nil",
   110  			args{
   111  				nil, nil, nil, nil,
   112  			},
   113  			nil,
   114  		},
   115  		{
   116  			"Commit2 is in Commit1 history",
   117  			args{
   118  				cidp(1), cidp(2), []*mono_models.Commit{{CommitID: cid(2)}}, nil,
   119  			},
   120  			cidp(2),
   121  		},
   122  		{
   123  			"Commit1 is in Commit2 history",
   124  			args{
   125  				cidp(1), cidp(2), nil, []*mono_models.Commit{{CommitID: cid(1)}},
   126  			},
   127  			cidp(1),
   128  		},
   129  		{
   130  			"Commit1 and Commit2 have common parent",
   131  			args{
   132  				cidp(1), cidp(2),
   133  				[]*mono_models.Commit{
   134  					{CommitID: cid(11)},
   135  					{CommitID: cid(12)},
   136  					{CommitID: cid(100)},
   137  					{CommitID: cid(13)},
   138  				},
   139  				[]*mono_models.Commit{
   140  					{CommitID: cid(21)},
   141  					{CommitID: cid(22)},
   142  					{CommitID: cid(23)},
   143  					{CommitID: cid(100)},
   144  				},
   145  			},
   146  			cidp(100),
   147  		},
   148  		{
   149  			"Commit1 and Commit2 have no common parent",
   150  			args{
   151  				cidp(1), cidp(2),
   152  				[]*mono_models.Commit{
   153  					{CommitID: cid(11)},
   154  					{CommitID: cid(12)},
   155  					{CommitID: cid(13)},
   156  				},
   157  				[]*mono_models.Commit{
   158  					{CommitID: cid(21)},
   159  					{CommitID: cid(22)},
   160  					{CommitID: cid(23)},
   161  				},
   162  			},
   163  			nil,
   164  		},
   165  	}
   166  	for _, tt := range tests {
   167  		t.Run(tt.name, func(t *testing.T) {
   168  			if got := commonParentWithHistory(tt.args.commit1, tt.args.commit2, tt.args.history1, tt.args.history2); !reflect.DeepEqual(got, tt.want) {
   169  				t.Errorf("commonParentWithHistory() = %v, want %v", got, tt.want)
   170  			}
   171  		})
   172  	}
   173  }