github.com/vlifesystems/rulehunter@v0.0.0-20180501090014-673078aa4a83/html/category_test.go (about)

     1  package html
     2  
     3  import (
     4  	"fmt"
     5  	"io/ioutil"
     6  	"os"
     7  	"path/filepath"
     8  	"reflect"
     9  	"testing"
    10  
    11  	"github.com/vlifesystems/rulehunter/config"
    12  	"github.com/vlifesystems/rulehunter/internal/testhelpers"
    13  	"github.com/vlifesystems/rulehunter/progress"
    14  )
    15  
    16  // This tests:
    17  //    i) That the correct reports are listed for each category in date order
    18  //   ii) That reports with different categories that resolve to the same
    19  //       escaped category are listed under a single category
    20  //  iii) That the shortest category name is used if there are multiple ones that
    21  //       resolve to the same escaped category
    22  func TestGenerateCategoryPages(t *testing.T) {
    23  	tmpDir := testhelpers.TempDir(t)
    24  	defer os.RemoveAll(tmpDir)
    25  	cfg := &config.Config{
    26  		WWWDir:   filepath.Join(tmpDir),
    27  		BuildDir: "fixtures",
    28  	}
    29  	pm, err := progress.NewMonitor(tmpDir)
    30  	if err != nil {
    31  		t.Fatalf("NewMonitor: %s", err)
    32  	}
    33  	if err := generateCategoryPages(cfg, pm); err != nil {
    34  		t.Fatalf("generateCategoryPages: %s", err)
    35  	}
    36  
    37  	categoryFiles, err :=
    38  		ioutil.ReadDir(filepath.Join(cfg.WWWDir, "reports", "category"))
    39  	if err != nil {
    40  		t.Fatalf("ioutil.ReadDir(...) err: %s", err)
    41  	}
    42  	categoriesInfo := make(map[string]*categoryInfo)
    43  	for _, file := range categoryFiles {
    44  		if file.IsDir() {
    45  			categoryIndexFilename := filepath.Join(
    46  				cfg.WWWDir,
    47  				"reports",
    48  				"category",
    49  				file.Name(),
    50  				"index.html",
    51  			)
    52  			categoryInfo, err := getCategoryInfo(categoryIndexFilename)
    53  			if err == nil {
    54  				categoriesInfo[file.Name()] = categoryInfo
    55  			}
    56  		}
    57  	}
    58  
    59  	wantCategoriesInfo := map[string]*categoryInfo{
    60  		"groupa": &categoryInfo{
    61  			"Reports for category: groupA",
    62  			[]string{
    63  				"reports/category/groupa/how-to-make-a-loss/train/",
    64  			},
    65  		},
    66  		"groupb": &categoryInfo{
    67  			"Reports for category: group^^^B",
    68  			[]string{
    69  				"reports/category/groupb/how-to-keep-costs-low/train/",
    70  				"reports/category/groupb/how-to-make-a-profit/train/",
    71  			},
    72  		},
    73  	}
    74  
    75  	err = checkCategoriesInfoMatch(categoriesInfo, wantCategoriesInfo)
    76  	if err != nil {
    77  		t.Errorf("checkCategoriesInfoMatch: %s", err)
    78  	}
    79  }
    80  
    81  func checkCategoriesInfoMatch(c1, c2 map[string]*categoryInfo) error {
    82  	if len(c1) != len(c2) {
    83  		return fmt.Errorf("Different number of keys: %d != %d", len(c1), len(c2))
    84  	}
    85  	for category, ci := range c1 {
    86  		if err := checkCategoryInfoMatch(ci, c2[category]); err != nil {
    87  			return fmt.Errorf("Category info: %s, doesn't match: %s", category, err)
    88  		}
    89  	}
    90  	return nil
    91  }
    92  
    93  func checkCategoryInfoMatch(ci1, ci2 *categoryInfo) error {
    94  	if ci1.h1 != ci2.h1 {
    95  		return fmt.Errorf("h1's don't match (%s != %s)", ci1.h1, ci2.h1)
    96  	}
    97  	if !reflect.DeepEqual(ci1.reportUrls, ci2.reportUrls) {
    98  		return fmt.Errorf("reportUrls's don't match (%s != %s)",
    99  			ci1.reportUrls, ci2.reportUrls)
   100  	}
   101  	return nil
   102  }
   103  
   104  func getCategoryInfo(filename string) (*categoryInfo, error) {
   105  	reportUrls, err := getReportUrls(filename)
   106  	if err != nil {
   107  		return nil, fmt.Errorf("getReportUrls(%s) err: %s", filename, err)
   108  	}
   109  	h1, err := getH1(filename)
   110  	if err != nil {
   111  		return nil, fmt.Errorf("getH1(%s) err: %s", filename, err)
   112  	}
   113  	return &categoryInfo{
   114  		h1:         h1,
   115  		reportUrls: reportUrls,
   116  	}, nil
   117  }
   118  
   119  type categoryInfo struct {
   120  	h1         string
   121  	reportUrls []string
   122  }