package mlp_test import ( "fmt" "net/http" "net/http/httptest" "os" "testing" "git.fromouter.space/mcg/draft" "git.fromouter.space/mcg/draft/mlp" "git.fromouter.space/mcg/draft/mlp/testdata" ) // TestSet retrieves a set online and generates a couple packs with it func TestSet(t *testing.T) { // Clean all loaded sets mlp.CleanSetCache() prSet, err := mlp.LoadSetHTTP(mlp.SetPremiere) if err != nil { t.Fatalf("Could not fetch set data: %s", err.Error()) } pack1 := draft.MakePack(prSet) pack2 := draft.MakePack(prSet) // Make sure both packs have the exact number of cards if len(pack1) != 12 { t.Fatalf("Expected 12 cards in pack 1 but got %d", len(pack1)) } if len(pack2) != 12 { t.Fatalf("Expected 12 cards in pack 2 but got %d", len(pack2)) } t.Logf("Cards in pack 1: %s\n", pack1) t.Logf("Cards in pack 2: %s\n", pack2) } // TestWrongSet tries to fetch a set that doesn't exist func TestWrongSet(t *testing.T) { _, err := mlp.LoadSetHTTP("nopenope") if err == nil { t.Fatalf("Expected an error but didn't get one!") } } // TestAllLoads tests all the LoadSet* functions func TestAllLoads(t *testing.T) { // Clean all loaded sets mlp.CleanSetCache() // Test LoadSetHTTP _, err := mlp.LoadSetHTTP(mlp.SetFriendsForever) if err != nil { t.Fatalf("[LoadSetHTTP] Could not fetch set data from the internet: %s", err.Error()) } // Load set from bytes _, err = mlp.LoadSetBytes(mlp.SetCelestialSolstice, []byte("{}")) if err != nil { t.Fatalf("[LoadSetBytes] Could not load set: %s", err.Error()) } // Load set to memory err = mlp.LoadSetData(map[mlp.SetID][]byte{ mlp.SetRockNRave: []byte("{}"), }) if err != nil { t.Fatalf("[LoadSetData] Could not load set: %s", err.Error()) } // Load set from memory _, err = mlp.LoadSetMemory(mlp.SetRockNRave) if err != nil { t.Fatalf("[LoadSetMemory] Could not load set: %s", err.Error()) } // Load all remaining sets err = mlp.LoadAllSets() if err != nil { t.Fatalf("[LoadAllSets] Could not load remaining sets: %s", err.Error()) } } // TestNotLoadedErr tests that LoadSetMemory fails if set is not cached func TestNotLoadedErr(t *testing.T) { // Clean all sets from memory first mlp.CleanSetCache() // Load set from memory (should fail) _, err := mlp.LoadSetMemory(mlp.SetFriendsForever) if err == nil { t.Fatal("[LoadSetMemory] Set loaded but shouldn't") } else if err != mlp.ErrSetNotLoaded { t.Fatalf("[LoadSetMemory] Set not loaded but error is not the right one: %s", err.Error()) } // Actually load set err = mlp.LoadSetData(map[mlp.SetID][]byte{ mlp.SetFriendsForever: []byte("{}"), }) if err != nil { t.Fatalf("[LoadSetData] Could not load set: %s", err.Error()) } // Load set from memory (should succeed) _, err = mlp.LoadSetMemory(mlp.SetFriendsForever) if err != nil { t.Fatalf("[LoadSetMemory] Could not load set: %s", err.Error()) } } // TestLoadCache tests caching on all Load functions that support it func TestLoadCache(t *testing.T) { // Clean all sets from memory first mlp.CleanSetCache() // Load dummy set err := mlp.LoadSetData(map[mlp.SetID][]byte{ mlp.SetFriendsForever: []byte("{}"), }) if err != nil { t.Fatalf("[LoadSetData] Could not load set: %s", err.Error()) } // // Try all set loading functions to trigger the cache // _, err = mlp.LoadSetMemory(mlp.SetFriendsForever) if err != nil { t.Fatalf("[LoadSetMemory] Could not load set: %s", err.Error()) } _, err = mlp.LoadSetBytes(mlp.SetFriendsForever, []byte("THIS SHOULD BE IGNORED")) if err != nil { t.Fatalf("[LoadSetBytes] Could not load set: %s", err.Error()) } loadedset, err := mlp.LoadSetHTTP(mlp.SetFriendsForever) if err != nil { t.Fatalf("[LoadSetHTTP] Could not load set: %s", err.Error()) } // Check that loaded set via HTTP is the dummy cache and not the real thing if len(loadedset.CardList) != 0 { t.Fatalf("[LoadSetHTTP] Set not loaded from cache") } } // TestMalformedJSONLoad tests that LoadSetData/LoadSetBytes return an error when given malformed JSON func TestMalformedJSONLoad(t *testing.T) { // Clean all sets from memory first mlp.CleanSetCache() _, err := mlp.LoadSetBytes(mlp.SetFriendsForever, []byte("THIS SHOULD FAIL")) if err == nil { t.Fatalf("LoadSetBytes with invalid data succeeded but shouldn't have") } err = mlp.LoadSetData(map[mlp.SetID][]byte{ mlp.SetFriendsForever: []byte("THIS SHOULD FAIL"), }) if err == nil { t.Fatalf("LoadSetData with invalid data succeeded but shouldn't have") } _, err = mlp.LoadSetHTTP(mlp.SetID("broken")) if err == nil { t.Fatalf("LoadSetHTTP with invalid JSON succeded but shouldn't have") } } func TestMain(m *testing.M) { testServer := httptest.NewServer(http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) { switch req.URL.Path { case "/nopenope.json": // 404 http.Error(res, "Not found", http.StatusNotFound) case "/broken.json": // Broken response fmt.Fprintf(res, "{{{{") case "/pr.json": fmt.Fprintf(res, testdata.SetPremiere) default: fmt.Fprintf(res, testdata.SetFriendForever) } })) mlp.HTTPSource = testServer.URL + "/" defer testServer.Close() os.Exit(m.Run()) }