package store /* func TestStatItems(t *testing.T) { var tc = []struct { Input StatItems ExpectedOutput StatItems }{ { Input: StatItems{ {Key: "a", Count: 5}, {Key: "b", Count: 100}, {Key: "c", Count: 99}, {Key: "d", Count: 98}, {Key: "f", Count: 18}, }, ExpectedOutput: StatItems{ {Key: "a", Count: 5}, {Key: "f", Count: 18}, {Key: "d", Count: 98}, {Key: "c", Count: 99}, {Key: "b", Count: 100}, }, }, } for _, testCase := range tc { sort.Sort(testCase.Input) for i := range testCase.Input { if testCase.Input[i] != testCase.ExpectedOutput[i] { t.Fatalf("Not sorted correctly") } } } } func TestStats(t *testing.T) { var exampleAttempts = []models.LoginAttempt{ {Username: "root", Password: "root", Country: "NO"}, {Username: "root", Password: "root", Country: "US"}, {Username: "user", Password: "passWord", Country: "US"}, {Username: "ibm", Password: "ibm", Country: "US"}, {Username: "ubnt", Password: "ubnt", Country: "GB"}, {Username: "ubnt", Password: "ubnt", Country: "FI"}, {Username: "root", Password: "root", Country: "CH"}, {Username: "ubnt", Password: "12345", Country: "DE"}, {Username: "oracle", Password: "oracle", Country: "FI"}, } var tc = []struct { Attempts []models.LoginAttempt StatType LoginStats Limit int ExpectedOutput map[string]int }{ { Attempts: exampleAttempts, StatType: LoginStatsPasswords, Limit: 2, ExpectedOutput: map[string]int{ "root": 3, "ubnt": 2, }, }, { Attempts: exampleAttempts, StatType: LoginStatsPasswords, Limit: 1, ExpectedOutput: map[string]int{ "root": 3, }, }, { Attempts: exampleAttempts, StatType: LoginStatsCountry, Limit: 2, ExpectedOutput: map[string]int{ "US": 3, "FI": 2, }, }, { Attempts: exampleAttempts, StatType: LoginStatsCountry, Limit: 0, ExpectedOutput: map[string]int{ "US": 3, "FI": 2, "NO": 1, "GB": 1, "CH": 1, "DE": 1, }, }, } for _, c := range tc { ms := MemoryStore{} for _, a := range c.Attempts { if err := ms.AddAttempt(a); err != nil { t.Fatalf("Unable to add attempt: %s", err) } } stats, err := ms.Stats(c.StatType, c.Limit) if err != nil { t.Fatalf("Error getting stats: %s", err) } if len(stats) != len(c.ExpectedOutput) { t.Fatalf("Stats have wrong length") } for key := range stats { if c.ExpectedOutput[key] != stats[key] { t.Fatalf("Stats does not match expected output") } } } } */