github.com/vlifesystems/rulehunter@v0.0.0-20180501090014-673078aa4a83/html/tag_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 tag in date order 18 // ii) That reports with different tags that resolve to the same 19 // escaped tag are listed under a single tag 20 // iii) That the shortest tag name is used if there are multiple ones that 21 // resolve to the same escaped tag 22 func TestGenerateTagPages(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 := generateTagPages(cfg, pm); err != nil { 34 t.Fatalf("generateTagPages: %s", err) 35 } 36 37 tagFiles, err := 38 ioutil.ReadDir(filepath.Join(cfg.WWWDir, "reports", "tag")) 39 if err != nil { 40 t.Fatalf("ioutil.ReadDir(...) err: %s", err) 41 } 42 43 tagsInfo := make(map[string]*tagInfo) 44 for _, file := range tagFiles { 45 if file.IsDir() { 46 tagIndexFilename := filepath.Join( 47 cfg.WWWDir, 48 "reports", 49 "tag", 50 file.Name(), 51 "index.html", 52 ) 53 if tagInfo, err := getTagInfo(tagIndexFilename); err == nil { 54 tagsInfo[file.Name()] = tagInfo 55 } else { 56 t.Fatalf("getTagInfo: %s", err) 57 } 58 } 59 } 60 61 noTagIndexFilename := filepath.Join( 62 cfg.WWWDir, 63 "reports", 64 "notag", 65 "index.html", 66 ) 67 if tagInfo, err := getTagInfo(noTagIndexFilename); err == nil { 68 tagsInfo[""] = tagInfo 69 } else { 70 t.Fatalf("getTagInfo: %s", err) 71 } 72 73 wantTagsInfo := map[string]*tagInfo{ 74 "": &tagInfo{ 75 "Reports for tag: ", 76 []string{ 77 "reports/nocategory/how-to-not-contain-tags-or-cats/train/", 78 }, 79 }, 80 "bank": &tagInfo{ 81 "Reports for tag: bank", 82 []string{ 83 "reports/category/groupb/how-to-keep-costs-low/train/", 84 "reports/category/groupb/how-to-make-a-profit/train/", 85 "reports/category/groupa/how-to-make-a-loss/train/", 86 }, 87 }, 88 "expensive": &tagInfo{ 89 "Reports for tag: expensive", 90 []string{ 91 "reports/category/groupb/how-to-keep-costs-low/train/", 92 }, 93 }, 94 "fahrenheit-451": &tagInfo{ 95 "Reports for tag: Fahrenheit 451", 96 []string{ 97 "reports/category/groupb/how-to-keep-costs-low/train/", 98 "reports/category/groupb/how-to-make-a-profit/train/", 99 "reports/category/groupa/how-to-make-a-loss/train/", 100 }, 101 }, 102 "fred-ned": &tagInfo{ 103 "Reports for tag: fred / ned", 104 []string{ 105 "reports/category/groupb/how-to-keep-costs-low/train/", 106 "reports/category/groupb/how-to-make-a-profit/train/", 107 "reports/category/groupa/how-to-make-a-loss/train/", 108 }, 109 }, 110 "hot-in-the-city": &tagInfo{ 111 "Reports for tag: hot in the city", 112 []string{ 113 "reports/category/groupb/how-to-keep-costs-low/train/", 114 "reports/category/groupb/how-to-make-a-profit/train/", 115 "reports/category/groupa/how-to-make-a-loss/train/", 116 }, 117 }, 118 "test": &tagInfo{ 119 "Reports for tag: test", 120 []string{ 121 "reports/category/groupb/how-to-make-a-profit/train/", 122 }, 123 }, 124 } 125 126 if err := checkTagsInfoMatch(tagsInfo, wantTagsInfo); err != nil { 127 t.Errorf("checkTagsInfoMatch(...) err: %s", err) 128 } 129 } 130 131 func checkTagsInfoMatch(t1, t2 map[string]*tagInfo) error { 132 if len(t1) != len(t2) { 133 return fmt.Errorf("Different number of keys: %d != %d", len(t1), len(t2)) 134 } 135 for tag, ti := range t1 { 136 if err := checkTagInfoMatch(ti, t2[tag]); err != nil { 137 return fmt.Errorf("Tag info: %s, doesn't match: %s", tag, err) 138 } 139 } 140 return nil 141 } 142 143 func checkTagInfoMatch(ti1, ti2 *tagInfo) error { 144 if ti1.h1 != ti2.h1 { 145 return fmt.Errorf("h1's don't match (%s != %s)", ti1.h1, ti2.h1) 146 } 147 if !reflect.DeepEqual(ti1.reportUrls, ti2.reportUrls) { 148 return fmt.Errorf("reportUrls's don't match (%s != %s)", 149 ti1.reportUrls, ti2.reportUrls) 150 } 151 return nil 152 } 153 154 func getTagInfo(filename string) (*tagInfo, error) { 155 reportUrls, err := getReportUrls(filename) 156 if err != nil { 157 return nil, fmt.Errorf("getReportUrls(%s) err: %s", filename, err) 158 } 159 h1, err := getH1(filename) 160 if err != nil { 161 return nil, fmt.Errorf("getH1(%s) err: %s", filename, err) 162 } 163 return &tagInfo{ 164 h1: h1, 165 reportUrls: reportUrls, 166 }, nil 167 } 168 169 type tagInfo struct { 170 h1 string 171 reportUrls []string 172 }