github.com/anchore/syft@v1.4.2-0.20240516191711-1bec1fc5d397/internal/relationship/binary/relationship_index_test.go (about)

     1  package binary
     2  
     3  import (
     4  	"reflect"
     5  	"testing"
     6  
     7  	"github.com/scylladb/go-set/strset"
     8  
     9  	"github.com/anchore/syft/syft/artifact"
    10  )
    11  
    12  func Test_newRelationshipIndex(t *testing.T) {
    13  	from := fakeIdentifiable{id: "from"}
    14  	to := fakeIdentifiable{id: "to"}
    15  	tests := []struct {
    16  		name  string
    17  		given []artifact.Relationship
    18  		want  *relationshipIndex
    19  	}{
    20  		{
    21  			name: "newRelationshipIndex returns an empty index with no existing relationships",
    22  			want: &relationshipIndex{
    23  				typesByFromTo: make(map[artifact.ID]map[artifact.ID]*strset.Set),
    24  				additional:    make([]artifact.Relationship, 0),
    25  			},
    26  		},
    27  		{
    28  			name: "newRelationshipIndex returns an index which tracks existing relationships",
    29  			given: []artifact.Relationship{
    30  				{
    31  					From: from,
    32  					To:   to,
    33  					Type: artifact.EvidentByRelationship,
    34  				},
    35  			},
    36  			want: &relationshipIndex{
    37  				typesByFromTo: map[artifact.ID]map[artifact.ID]*strset.Set{
    38  					"from": {
    39  						"to": strset.New("evident-by"),
    40  					},
    41  				},
    42  				additional: make([]artifact.Relationship, 0),
    43  			},
    44  		},
    45  	}
    46  	for _, tt := range tests {
    47  		t.Run(tt.name, func(t *testing.T) {
    48  			if got := newRelationshipIndex(tt.given...); !reflect.DeepEqual(got, tt.want) {
    49  				t.Errorf("newRelationshipIndex() = %v, want %v", got, tt.want)
    50  			}
    51  		})
    52  	}
    53  }
    54  
    55  func Test_relationshipIndex_track(t *testing.T) {
    56  	from := fakeIdentifiable{id: "from"}
    57  	to := fakeIdentifiable{id: "to"}
    58  	relationship := artifact.Relationship{From: from, To: to, Type: artifact.EvidentByRelationship}
    59  	tests := []struct {
    60  		name     string
    61  		existing []artifact.Relationship
    62  		given    artifact.Relationship
    63  		want     bool
    64  	}{
    65  		{
    66  			name:     "track returns true for a new relationship",
    67  			existing: []artifact.Relationship{},
    68  			given:    relationship,
    69  			want:     true,
    70  		},
    71  		{
    72  			name:     "track returns false for an existing relationship",
    73  			existing: []artifact.Relationship{relationship},
    74  			given:    relationship,
    75  			want:     false,
    76  		},
    77  	}
    78  	for _, tt := range tests {
    79  		t.Run(tt.name, func(t *testing.T) {
    80  			i := newRelationshipIndex(tt.existing...)
    81  			if got := i.track(tt.given); got != tt.want {
    82  				t.Errorf("track() = %v, want %v", got, tt.want)
    83  			}
    84  		})
    85  	}
    86  }
    87  
    88  func Test_relationshipIndex_add(t *testing.T) {
    89  	from := fakeIdentifiable{id: "from"}
    90  	to := fakeIdentifiable{id: "to"}
    91  	relationship := artifact.Relationship{From: from, To: to, Type: artifact.EvidentByRelationship}
    92  	tests := []struct {
    93  		name     string
    94  		existing []artifact.Relationship
    95  		given    artifact.Relationship
    96  		want     bool
    97  	}{
    98  		{
    99  			name:     "add returns true for a new relationship",
   100  			existing: []artifact.Relationship{},
   101  			given:    relationship,
   102  			want:     true,
   103  		},
   104  		{
   105  			name:     "add returns false for an existing relationship",
   106  			existing: []artifact.Relationship{relationship},
   107  			given:    relationship,
   108  			want:     false,
   109  		},
   110  	}
   111  	for _, tt := range tests {
   112  		t.Run(tt.name, func(t *testing.T) {
   113  			i := newRelationshipIndex(tt.existing...)
   114  			if got := i.add(tt.given); got != tt.want {
   115  				t.Errorf("add() = %v, want %v", got, tt.want)
   116  			}
   117  		})
   118  	}
   119  
   120  }
   121  
   122  type fakeIdentifiable struct {
   123  	id string
   124  }
   125  
   126  func (f fakeIdentifiable) ID() artifact.ID {
   127  	return artifact.ID(f.id)
   128  }