@@ -35,6 +35,81 @@ func TestCrawler(t *testing.T) {
35
35
assert .NoError (t , err )
36
36
37
37
assert .Equal (t , uint64 (3 ), count )
38
+
39
+ wd , wdErr := os .Getwd ()
40
+ require .NoError (t , wdErr )
41
+
42
+ _ , visitedCatalog := visited .Load (filepath .Join (wd , "testdata/v1.0.0/catalog-with-collection-of-items.json" ))
43
+ assert .True (t , visitedCatalog )
44
+
45
+ _ , visitedCollection := visited .Load (filepath .Join (wd , "testdata/v1.0.0/collection-with-items.json" ))
46
+ assert .True (t , visitedCollection )
47
+
48
+ _ , visitedItem := visited .Load (filepath .Join (wd , "testdata/v1.0.0/item-in-collection.json" ))
49
+ assert .True (t , visitedItem )
50
+ }
51
+
52
+ func TestCrawlerFilterItem (t * testing.T ) {
53
+ count := uint64 (0 )
54
+ visited := & sync.Map {}
55
+
56
+ visitor := func (location string , resource crawler.Resource ) error {
57
+ atomic .AddUint64 (& count , 1 )
58
+ _ , loaded := visited .LoadOrStore (location , true )
59
+ if loaded {
60
+ return fmt .Errorf ("already visited %s" , location )
61
+ }
62
+ return nil
63
+ }
64
+ c := crawler .New (visitor , & crawler.Options {
65
+ Filter : func (location string ) bool {
66
+ return ! strings .HasSuffix (location , "/item-in-collection.json" )
67
+ },
68
+ })
69
+
70
+ err := c .Crawl (context .Background (), "testdata/v1.0.0/catalog-with-collection-of-items.json" )
71
+ assert .NoError (t , err )
72
+
73
+ assert .Equal (t , uint64 (2 ), count )
74
+
75
+ wd , wdErr := os .Getwd ()
76
+ require .NoError (t , wdErr )
77
+
78
+ _ , visitedCatalog := visited .Load (filepath .Join (wd , "testdata/v1.0.0/catalog-with-collection-of-items.json" ))
79
+ assert .True (t , visitedCatalog )
80
+
81
+ _ , visitedCollection := visited .Load (filepath .Join (wd , "testdata/v1.0.0/collection-with-items.json" ))
82
+ assert .True (t , visitedCollection )
83
+ }
84
+
85
+ func TestCrawlerFilterCollection (t * testing.T ) {
86
+ count := uint64 (0 )
87
+ visited := & sync.Map {}
88
+
89
+ visitor := func (location string , resource crawler.Resource ) error {
90
+ atomic .AddUint64 (& count , 1 )
91
+ _ , loaded := visited .LoadOrStore (location , true )
92
+ if loaded {
93
+ return fmt .Errorf ("already visited %s" , location )
94
+ }
95
+ return nil
96
+ }
97
+ c := crawler .New (visitor , & crawler.Options {
98
+ Filter : func (location string ) bool {
99
+ return ! strings .HasSuffix (location , "/collection-with-items.json" )
100
+ },
101
+ })
102
+
103
+ err := c .Crawl (context .Background (), "testdata/v1.0.0/catalog-with-collection-of-items.json" )
104
+ assert .NoError (t , err )
105
+
106
+ assert .Equal (t , uint64 (1 ), count )
107
+
108
+ wd , wdErr := os .Getwd ()
109
+ require .NoError (t , wdErr )
110
+
111
+ _ , visitedCatalog := visited .Load (filepath .Join (wd , "testdata/v1.0.0/catalog-with-collection-of-items.json" ))
112
+ assert .True (t , visitedCatalog )
38
113
}
39
114
40
115
func TestCrawlerHTTP (t * testing.T ) {
0 commit comments