github.com/tooploox/oya@v0.0.21-0.20230524103240-1cda1861aad6/pkg/mvs/mvs_test.go (about) 1 package mvs_test 2 3 import ( 4 "sort" 5 "testing" 6 7 "github.com/tooploox/oya/pkg/mvs" 8 "github.com/tooploox/oya/pkg/pack" 9 tu "github.com/tooploox/oya/testutil" 10 ) 11 12 type MockReqs map[string][]pack.Pack 13 14 func (r *MockReqs) Reqs(pack pack.Pack) ([]pack.Pack, error) { 15 reqs, ok := (*r)[mvs.Hash(pack)] 16 if !ok { 17 return nil, nil 18 } else { 19 return reqs, nil 20 } 21 } 22 23 var ( 24 pack1_100 = tu.MustMakeMockPack("pack1", "v1.0.0") 25 pack1_110 = tu.MustMakeMockPack("pack1", "v1.1.0") 26 pack1_111 = tu.MustMakeMockPack("pack1", "v1.1.1") 27 pack2_100 = tu.MustMakeMockPack("pack2", "v1.0.0") 28 pack2_110 = tu.MustMakeMockPack("pack2", "v1.1.0") 29 pack2_111 = tu.MustMakeMockPack("pack2", "v1.1.1") 30 pack3_100 = tu.MustMakeMockPack("pack3", "v1.0.0") 31 pack3_110 = tu.MustMakeMockPack("pack3", "v1.1.0") 32 pack3_111 = tu.MustMakeMockPack("pack3", "v1.1.1") 33 ) 34 35 func TestNoDependencies(t *testing.T) { 36 reqs := &MockReqs{} 37 list, err := mvs.List(nil, reqs) 38 tu.AssertNoErr(t, err, "Error creating dependency list") 39 tu.AssertEqualMsg(t, 0, len(list), "Incorrect dependency list length") 40 } 41 42 func TestOneDependency(t *testing.T) { 43 reqs := &MockReqs{} 44 list, err := mvs.List([]pack.Pack{pack1_100}, reqs) 45 tu.AssertNoErr(t, err, "Error creating dependency list") 46 tu.AssertEqualMsg(t, 1, len(list), "Incorrect dependency list length") 47 assertSamePacks(t, []pack.Pack{pack1_100}, list) 48 } 49 50 func TestTwoLevels(t *testing.T) { 51 reqs := &MockReqs{ 52 mvs.Hash(pack1_100): []pack.Pack{pack2_100, pack3_100}, 53 mvs.Hash(pack2_100): []pack.Pack{pack3_100}, 54 } 55 list, err := mvs.List([]pack.Pack{pack1_100, pack2_100}, reqs) 56 tu.AssertNoErr(t, err, "Error creating dependency list") 57 tu.AssertEqualMsg(t, 3, len(list), "Incorrect dependency list length") 58 assertSamePacks(t, []pack.Pack{pack1_100, pack2_100, pack3_100}, list) 59 } 60 61 func TestCycles(t *testing.T) { 62 reqs := &MockReqs{ 63 mvs.Hash(pack1_100): []pack.Pack{pack2_100, pack3_100}, 64 mvs.Hash(pack2_100): []pack.Pack{pack3_100, pack1_100}, 65 } 66 list, err := mvs.List([]pack.Pack{pack1_100, pack2_100}, reqs) 67 tu.AssertNoErr(t, err, "Error creating dependency list") 68 tu.AssertEqualMsg(t, 3, len(list), "Incorrect dependency list length") 69 assertSamePacks(t, []pack.Pack{pack1_100, pack2_100, pack3_100}, list) 70 } 71 72 func TestPreferHigherVersions(t *testing.T) { 73 reqs := &MockReqs{ 74 mvs.Hash(pack1_100): []pack.Pack{pack2_110, pack3_110}, 75 mvs.Hash(pack2_100): []pack.Pack{pack3_111}, 76 } 77 list, err := mvs.List([]pack.Pack{pack1_100, pack2_100, pack3_100}, reqs) 78 tu.AssertNoErr(t, err, "Error creating dependency list") 79 tu.AssertEqualMsg(t, 3, len(list), "Incorrect dependency list length") 80 assertSamePacks(t, []pack.Pack{pack1_100, pack2_110, pack3_111}, list) 81 } 82 83 func sortPacks(packs []pack.Pack) { 84 sort.Slice(packs, func(i, j int) bool { 85 return mvs.Hash(packs[i]) < mvs.Hash(packs[j]) 86 }) 87 } 88 89 func assertSamePacks(t *testing.T, lhs, rhs []pack.Pack) { 90 sortPacks(lhs) 91 sortPacks(rhs) 92 tu.AssertObjectsEqual(t, lhs, rhs) 93 }