github.com/andrew00x/gomovies@v0.1.0/pkg/catalog/index_simple_test.go (about)

     1  package catalog
     2  
     3  import (
     4  	"sort"
     5  	"testing"
     6  
     7  	"github.com/stretchr/testify/assert"
     8  )
     9  
    10  func TestAddMovieInIndex(t *testing.T) {
    11  	index := SimpleIndex{make(map[string]map[int]void)}
    12  	movies := []struct {
    13  		id     int
    14  		title  string
    15  		genres []string
    16  	}{
    17  		{id: 1, title: "Green Mile.mkv", genres: []string{"crime", "drama"} },
    18  		{id: 2, title: "Fight Club.mkv", genres: []string{"drama"}},
    19  	}
    20  	for _, m := range movies {
    21  		index.Add(m.title, m.id)
    22  		for _, genre := range m.genres {
    23  			index.Add(genre, m.id)
    24  		}
    25  	}
    26  	expected := map[string]map[int]void{
    27  		"green mile.mkv": {1: emptyValue},
    28  		"fight club.mkv": {2: emptyValue},
    29  		"drama": {1: emptyValue, 2: emptyValue},
    30  		"crime": {1: emptyValue},
    31  	}
    32  	assert.Equal(t, expected, index.idx)
    33  }
    34  
    35  func TestFindMovieInIndex(t *testing.T) {
    36  	index := SimpleIndex{make(map[string]map[int]void)}
    37  	movies := []struct {
    38  		id    int
    39  		title string
    40  	}{
    41  		{id: 1, title: "Back to the Future 1.mkv"},
    42  		{id: 2, title: "Back to the Future 2.mkv"},
    43  		{id: 3, title: "The Replacements.mkv"},
    44  	}
    45  	for _, m := range movies {
    46  		index.Add(m.title, m.id)
    47  	}
    48  	expected := []int{1, 2}
    49  	result := index.Find("Futur")
    50  	sort.Ints(result)
    51  	assert.Equal(t, expected, result)
    52  }
    53  
    54  func TestFindMovieInIndexIgnoringCase(t *testing.T) {
    55  	index := SimpleIndex{make(map[string]map[int]void)}
    56  	movies := []struct {
    57  		id    int
    58  		title string
    59  	}{
    60  		{id: 1, title: "Brave Heart.mkv"},
    61  		{id: 2, title: "Rush Hour 1.mkv"},
    62  		{id: 3, title: "Rush Hour 2.mkv"},
    63  	}
    64  	for _, m := range movies {
    65  		index.Add(m.title, m.id)
    66  	}
    67  	expected := []int{2, 3}
    68  	result := index.Find("hoUr")
    69  	sort.Ints(result)
    70  	assert.Equal(t, expected, result)
    71  }