Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[add] test #42

Merged
merged 1 commit into from
Jul 3, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
94 changes: 94 additions & 0 deletions speedtest/server_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
package speedtest

import "testing"

func TestFetchServerList(t *testing.T) {
user := User{
IP: "111.111.111.111",
Lat: "35.22",
Lon: "138.44",
Isp: "Hello",
}
serverList, err := FetchServerList(&user)
if err != nil {
t.Errorf(err.Error())
}
if len(serverList.Servers) == 0 {
t.Errorf("Failed to fetch server list.")
}
if serverList.Servers[0].Country != "Japan" {
t.Errorf("got unexpected country '%v', expected 'Japan'", serverList.Servers[0].Country)
}
}

func TestDistance(t *testing.T) {
d := distance(0.0, 0.0, 1.0, 1.0)
if d < 157 || 158 < d {
t.Errorf("got: %v, expected between 157 and 158", d)
}

d = distance(0.0, 180.0, 0.0, -180.0)
if d != 0 {
t.Errorf("got: %v, expected 0", d)
}

d1 := distance(100.0, 100.0, 100.0, 101.0)
d2 := distance(100.0, 100.0, 100.0, 99.0)
if d1 != d2 {
t.Errorf("%v and %v should be save value", d1, d2)
}
}

func TestFindServer(t *testing.T) {
servers := [] *Server {
&Server{
ID: "1",
},
&Server{
ID: "2",
},
&Server{
ID: "3",
},
}
serverList := ServerList{Servers: servers}

serverID := []int{}
s, err := serverList.FindServer(serverID)
if err != nil {
t.Errorf(err.Error())
}
if len(s) != 1 {
t.Errorf("Unexpected server length. got: %v, expected: 1", len(s))
}
if s[0].ID != "1" {
t.Errorf("Unexpected server ID. got: %v, expected: '1'", s[0].ID)
}

serverID = []int{2}
s, err = serverList.FindServer(serverID)
if err != nil {
t.Errorf(err.Error())
}
if len(s) != 1 {
t.Errorf("Unexpected server length. got: %v, expected: 1", len(s))
}
if s[0].ID != "2" {
t.Errorf("Unexpected server ID. got: %v, expected: '2'", s[0].ID)
}

serverID = []int{3, 1}
s, err = serverList.FindServer(serverID)
if err != nil {
t.Errorf(err.Error())
}
if len(s) != 2 {
t.Errorf("Unexpected server length. got: %v, expected: 2", len(s))
}
if s[0].ID != "3" {
t.Errorf("Unexpected server ID. got: %v, expected: '3'", s[0].ID)
}
if s[1].ID != "1" {
t.Errorf("Unexpected server ID. got: %v, expected: '1'", s[0].ID)
}
}
39 changes: 39 additions & 0 deletions speedtest/user_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package speedtest

import "testing"
import "strings"

func TestFetchUserInfo(t *testing.T) {
user, err := FetchUserInfo()
if err != nil {
t.Errorf(err.Error())
}
// IP
if len(user.IP) < 7 || len(user.IP) > 15 {
t.Errorf("Invalid IP length. got: %v;", user.IP)
}
if strings.Count(user.IP, ".") != 3 {
t.Errorf("Invalid IP format. got: %v", user.IP)
}

// Lat
if len(user.Lat) > 8 {
t.Errorf("Invalid Latitude. got: %v;", user.Lat)
}
if strings.Count(user.Lat, ".") != 1 {
t.Errorf("Invalid Latitude format. got: %v", user.Lat)
}

// Lon
if len(user.Lon) > 8 {
t.Errorf("Invalid Londitude. got: %v;", user.Lon)
}
if strings.Count(user.Lon, ".") != 1 {
t.Errorf("Invalid Londitude format. got: %v", user.Lon)
}

// Isp
if len(user.Isp) == 0 {
t.Errorf("Invalid Iso. got: %v;", user.Isp)
}
}