package mlp_test import ( "testing" "git.fromouter.space/mcg/draft" "git.fromouter.space/mcg/draft/mlp" ) // TestSet retrieves a set online and generates a couple packs with it // This test *requires* an internet connection! func TestSet(t *testing.T) { // Clean all loaded sets mlp.CleanSetCache() deSet, err := mlp.LoadSetHTTP(mlp.SetDefendersOfEquestria) if err != nil { t.Fatalf("Could not fetch set data: %s", err.Error()) } pack1 := draft.MakePack(deSet) pack2 := draft.MakePack(deSet) // 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 // This test *requires* an internet connection! 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 // This test *requires* an internet connection! func TestAllLoads(t *testing.T) { // Clean all loaded sets mlp.CleanSetCache() // Test LoadSetHTTP _, err := mlp.LoadSetHTTP(mlp.SetAbsoluteDiscord) 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()) } } // 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") } }