Fixed test for structs
Seems like init function of other packages are not executed in tests
Cette révision appartient à :
Parent
03ded2e027
révision
e64a6c599c
2 fichiers modifiés avec 37 ajouts et 33 suppressions
|
@ -18,55 +18,56 @@ type Categories []Category
|
|||
var categories Categories
|
||||
var Index map[string]int
|
||||
|
||||
func init() {
|
||||
if len(categories) == 0 {
|
||||
var cats map[string]string
|
||||
if config.IsSukebei() {
|
||||
cats = config.Conf.Torrents.SukebeiCategories
|
||||
} else {
|
||||
cats = config.Conf.Torrents.CleanCategories
|
||||
}
|
||||
func initCategories() {
|
||||
var cats map[string]string
|
||||
if config.IsSukebei() {
|
||||
cats = config.Conf.Torrents.SukebeiCategories
|
||||
} else {
|
||||
cats = config.Conf.Torrents.CleanCategories
|
||||
}
|
||||
|
||||
// Sorting categories alphabetically
|
||||
var index []string
|
||||
ids := make(map[string]string)
|
||||
Index = make(map[string]int, len(cats))
|
||||
for id, name := range cats {
|
||||
index = append(index, name)
|
||||
ids[name] = id
|
||||
}
|
||||
sort.Strings(index)
|
||||
// Sorting categories alphabetically
|
||||
var index []string
|
||||
ids := make(map[string]string)
|
||||
Index = make(map[string]int, len(cats))
|
||||
for id, name := range cats {
|
||||
index = append(index, name)
|
||||
ids[name] = id
|
||||
}
|
||||
sort.Strings(index)
|
||||
|
||||
// Creating index of categories
|
||||
for k, name := range index {
|
||||
categories = append(categories, Category{ids[name], name})
|
||||
Index[ids[name]] = k
|
||||
}
|
||||
// Creating index of categories
|
||||
for k, name := range index {
|
||||
categories = append(categories, Category{ids[name], name})
|
||||
Index[ids[name]] = k
|
||||
}
|
||||
}
|
||||
|
||||
// All : function to get all categories depending on the actual website from config/categories.go
|
||||
func All() Categories {
|
||||
if len(categories) == 0 {
|
||||
initCategories()
|
||||
}
|
||||
return categories
|
||||
}
|
||||
|
||||
// Get : function to get a category by the key in the index array
|
||||
func Get(key int) Category {
|
||||
return categories[key]
|
||||
return All()[key]
|
||||
}
|
||||
|
||||
// Get : function to get a category by the id of the category from the database
|
||||
func GetByID(id string) (Category, bool) {
|
||||
if key, ok := Index[id]; ok {
|
||||
return categories[key], true
|
||||
return All()[key], true
|
||||
}
|
||||
return Category{"", ""}, false
|
||||
}
|
||||
|
||||
// Exists : Check if a category exist in config
|
||||
func (cats Categories) Exists(category string) bool {
|
||||
func (cats Categories) Exists(id string) bool {
|
||||
for _, cat := range cats {
|
||||
if cat.Name == category {
|
||||
if cat.ID == id {
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
@ -75,17 +76,15 @@ func (cats Categories) Exists(category string) bool {
|
|||
|
||||
// Exists : Check if a category exist in config
|
||||
func Exists(category string) bool {
|
||||
return categories.Exists(category)
|
||||
return All().Exists(category)
|
||||
}
|
||||
|
||||
// GetSelect : Format categories in map ordered alphabetically
|
||||
func GetSelect(keepParent bool, keepChild bool) Categories {
|
||||
catSelect := make(Categories, len(categories))
|
||||
k := 0
|
||||
for _, v := range categories {
|
||||
var catSelect Categories
|
||||
for _, v := range All() {
|
||||
if (keepParent && keepChild) || (len(v.ID) > 2 && !keepParent) || (len(v.ID) <= 2 && !keepChild) {
|
||||
catSelect[k] = v
|
||||
k++
|
||||
catSelect = append(catSelect, v)
|
||||
}
|
||||
}
|
||||
return catSelect
|
||||
|
|
|
@ -20,10 +20,15 @@ var _ = func() (_ struct{}) {
|
|||
}()
|
||||
|
||||
func TestGetCategories(t *testing.T) {
|
||||
cats := All()
|
||||
cats := make(map[string]string)
|
||||
|
||||
for _, v := range All() {
|
||||
cats[v.ID] = v.Name
|
||||
}
|
||||
if len(cats) == 0 {
|
||||
t.Skip("Couldn't load categories to test Categories")
|
||||
}
|
||||
|
||||
if !reflect.DeepEqual(cats, config.Conf.Torrents.CleanCategories) && !reflect.DeepEqual(cats, config.Conf.Torrents.SukebeiCategories) {
|
||||
t.Error("Categories doesn't correspond to the configured ones")
|
||||
}
|
||||
|
|
Référencer dans un nouveau ticket