|
| 1 | +package scraper |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/KubrickCode/loa-work/src/go/libs/loaApi" |
| 8 | +) |
| 9 | + |
| 10 | +type mockAPIClient struct { |
| 11 | + getAuctionItemListFunc func(params *loaApi.GetAuctionItemListParams) (*loaApi.GetAuctionItemListResponse, error) |
| 12 | + getCategoryListFunc func() (*loaApi.GetCategoryListResponse, error) |
| 13 | + getMarketItemFunc func(params *loaApi.GetMarketItemParams) (*loaApi.GetMarketItemResponse, error) |
| 14 | + getMarketItemListFunc func(params *loaApi.GetMarketItemListParams) (*loaApi.GetMarketItemListResponse, error) |
| 15 | +} |
| 16 | + |
| 17 | +func (m *mockAPIClient) GetAuctionItemList(params *loaApi.GetAuctionItemListParams) (*loaApi.GetAuctionItemListResponse, error) { |
| 18 | + if m.getAuctionItemListFunc != nil { |
| 19 | + return m.getAuctionItemListFunc(params) |
| 20 | + } |
| 21 | + return nil, errors.New("not implemented") |
| 22 | +} |
| 23 | + |
| 24 | +func (m *mockAPIClient) GetCategoryList() (*loaApi.GetCategoryListResponse, error) { |
| 25 | + if m.getCategoryListFunc != nil { |
| 26 | + return m.getCategoryListFunc() |
| 27 | + } |
| 28 | + return nil, errors.New("not implemented") |
| 29 | +} |
| 30 | + |
| 31 | +func (m *mockAPIClient) GetMarketItem(params *loaApi.GetMarketItemParams) (*loaApi.GetMarketItemResponse, error) { |
| 32 | + if m.getMarketItemFunc != nil { |
| 33 | + return m.getMarketItemFunc(params) |
| 34 | + } |
| 35 | + return nil, errors.New("not implemented") |
| 36 | +} |
| 37 | + |
| 38 | +func (m *mockAPIClient) GetMarketItemList(params *loaApi.GetMarketItemListParams) (*loaApi.GetMarketItemListResponse, error) { |
| 39 | + if m.getMarketItemListFunc != nil { |
| 40 | + return m.getMarketItemListFunc(params) |
| 41 | + } |
| 42 | + return nil, errors.New("not implemented") |
| 43 | +} |
| 44 | + |
| 45 | +func TestGetCategories_Success(t *testing.T) { |
| 46 | + mockClient := &mockAPIClient{ |
| 47 | + getCategoryListFunc: func() (*loaApi.GetCategoryListResponse, error) { |
| 48 | + return &loaApi.GetCategoryListResponse{ |
| 49 | + Categories: []loaApi.Category{ |
| 50 | + { |
| 51 | + Code: 10000, |
| 52 | + CodeName: "Test Category 1", |
| 53 | + Subs: []loaApi.SubCategory{ |
| 54 | + {Code: 10001, CodeName: "Test Sub Category 1"}, |
| 55 | + }, |
| 56 | + }, |
| 57 | + { |
| 58 | + Code: 20000, |
| 59 | + CodeName: "Test Category 2", |
| 60 | + Subs: []loaApi.SubCategory{}, |
| 61 | + }, |
| 62 | + }, |
| 63 | + }, nil |
| 64 | + }, |
| 65 | + } |
| 66 | + |
| 67 | + scraper := &Scraper{ |
| 68 | + client: mockClient, |
| 69 | + db: nil, |
| 70 | + } |
| 71 | + |
| 72 | + categories, err := scraper.getCategories() |
| 73 | + if err != nil { |
| 74 | + t.Fatalf("Expected no error, got %v", err) |
| 75 | + } |
| 76 | + |
| 77 | + if len(categories) != 3 { |
| 78 | + t.Errorf("Expected 3 categories (2 parent + 1 sub), got %d", len(categories)) |
| 79 | + } |
| 80 | + |
| 81 | + expectedCodes := []int{10000, 10001, 20000} |
| 82 | + for i, category := range categories { |
| 83 | + if category.Code != expectedCodes[i] { |
| 84 | + t.Errorf("Expected category code %d, got %d", expectedCodes[i], category.Code) |
| 85 | + } |
| 86 | + } |
| 87 | +} |
| 88 | + |
| 89 | +func TestGetCategories_APIError(t *testing.T) { |
| 90 | + expectedErr := errors.New("API connection failed") |
| 91 | + mockClient := &mockAPIClient{ |
| 92 | + getCategoryListFunc: func() (*loaApi.GetCategoryListResponse, error) { |
| 93 | + return nil, expectedErr |
| 94 | + }, |
| 95 | + } |
| 96 | + |
| 97 | + scraper := &Scraper{ |
| 98 | + client: mockClient, |
| 99 | + db: nil, |
| 100 | + } |
| 101 | + |
| 102 | + _, err := scraper.getCategories() |
| 103 | + if err == nil { |
| 104 | + t.Fatal("Expected error, got nil") |
| 105 | + } |
| 106 | + |
| 107 | + if err != expectedErr { |
| 108 | + t.Errorf("Expected error %v, got %v", expectedErr, err) |
| 109 | + } |
| 110 | +} |
| 111 | + |
| 112 | +func TestGetCategories_EmptyResponse(t *testing.T) { |
| 113 | + mockClient := &mockAPIClient{ |
| 114 | + getCategoryListFunc: func() (*loaApi.GetCategoryListResponse, error) { |
| 115 | + return &loaApi.GetCategoryListResponse{ |
| 116 | + Categories: []loaApi.Category{}, |
| 117 | + }, nil |
| 118 | + }, |
| 119 | + } |
| 120 | + |
| 121 | + scraper := &Scraper{ |
| 122 | + client: mockClient, |
| 123 | + db: nil, |
| 124 | + } |
| 125 | + |
| 126 | + _, err := scraper.getCategories() |
| 127 | + if err == nil { |
| 128 | + t.Fatal("Expected error for empty categories, got nil") |
| 129 | + } |
| 130 | + |
| 131 | + if !errors.Is(err, ErrNoMarketItemCategories) { |
| 132 | + t.Errorf("Expected ErrNoMarketItemCategories, got %v", err) |
| 133 | + } |
| 134 | +} |
| 135 | + |
| 136 | +func TestGetFlattenCategories(t *testing.T) { |
| 137 | + categories := []loaApi.Category{ |
| 138 | + { |
| 139 | + Code: 10000, |
| 140 | + CodeName: "Parent 1", |
| 141 | + Subs: []loaApi.SubCategory{ |
| 142 | + {Code: 10001, CodeName: "Child 1-1"}, |
| 143 | + {Code: 10002, CodeName: "Child 1-2"}, |
| 144 | + }, |
| 145 | + }, |
| 146 | + { |
| 147 | + Code: 20000, |
| 148 | + CodeName: "Parent 2", |
| 149 | + Subs: []loaApi.SubCategory{ |
| 150 | + {Code: 20001, CodeName: "Child 2-1"}, |
| 151 | + }, |
| 152 | + }, |
| 153 | + } |
| 154 | + |
| 155 | + flattened := GetFlattenCategories(categories) |
| 156 | + |
| 157 | + if len(flattened) != 5 { |
| 158 | + t.Errorf("Expected 5 flattened categories, got %d", len(flattened)) |
| 159 | + } |
| 160 | + |
| 161 | + expectedCodes := []int{10000, 10001, 10002, 20000, 20001} |
| 162 | + for i, category := range flattened { |
| 163 | + if category.Code != expectedCodes[i] { |
| 164 | + t.Errorf("At index %d: expected code %d, got %d", i, expectedCodes[i], category.Code) |
| 165 | + } |
| 166 | + } |
| 167 | + |
| 168 | + for _, category := range flattened { |
| 169 | + if category.ID != 0 { |
| 170 | + t.Errorf("Expected ID to be 0 (new record), got %d", category.ID) |
| 171 | + } |
| 172 | + } |
| 173 | +} |
| 174 | + |
| 175 | +func TestGetFlattenCategories_EmptySubCategories(t *testing.T) { |
| 176 | + categories := []loaApi.Category{ |
| 177 | + { |
| 178 | + Code: 10000, |
| 179 | + CodeName: "Category without subs", |
| 180 | + Subs: []loaApi.SubCategory{}, |
| 181 | + }, |
| 182 | + } |
| 183 | + |
| 184 | + flattened := GetFlattenCategories(categories) |
| 185 | + |
| 186 | + if len(flattened) != 1 { |
| 187 | + t.Errorf("Expected 1 category, got %d", len(flattened)) |
| 188 | + } |
| 189 | + |
| 190 | + if flattened[0].Code != 10000 { |
| 191 | + t.Errorf("Expected code 10000, got %d", flattened[0].Code) |
| 192 | + } |
| 193 | + |
| 194 | + if flattened[0].Name != "Category without subs" { |
| 195 | + t.Errorf("Expected name 'Category without subs', got %s", flattened[0].Name) |
| 196 | + } |
| 197 | +} |
0 commit comments