-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor: print report from reports pkg
- Loading branch information
Showing
15 changed files
with
554 additions
and
98 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package rapina | ||
|
||
import ( | ||
"fmt" | ||
"net/url" | ||
"path" | ||
"strconv" | ||
"strings" | ||
"time" | ||
) | ||
|
||
// IsDate checks if date is in format YYYY-MM-DD. | ||
func IsDate(date string) bool { | ||
if len(date) != len("2021-04-26") || strings.Count(date, "-") != 2 { | ||
return false | ||
} | ||
|
||
y, errY := strconv.Atoi(date[0:4]) | ||
m, errM := strconv.Atoi(date[5:7]) | ||
d, errD := strconv.Atoi(date[8:10]) | ||
if errY != nil || errM != nil || errD != nil { | ||
return false | ||
} | ||
|
||
// Ok, we'll still be using this in 2200 :) | ||
if y < 1970 || y > 2200 { | ||
return false | ||
} | ||
if m < 1 || m > 12 { | ||
return false | ||
} | ||
nDays := [13]int{0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31} | ||
if d < 1 || d > nDays[m] { | ||
return false | ||
} | ||
return true | ||
} | ||
|
||
func IsUrl(str string) bool { | ||
u, err := url.Parse(str) | ||
return err == nil && u.Scheme != "" && u.Host != "" | ||
} | ||
|
||
// JoinURL joins strings as URL paths | ||
func JoinURL(base string, paths ...string) string { | ||
p := path.Join(paths...) | ||
return fmt.Sprintf("%s/%s", strings.TrimRight(base, "/"), strings.TrimLeft(p, "/")) | ||
} | ||
|
||
var _timeNow = time.Now | ||
|
||
// MonthsFromToday returns a list of months including the current. | ||
// Date formatted as YYYY-MM-DD. | ||
func MonthsFromToday(n int) []string { | ||
if n < 1 { | ||
n = 1 | ||
} | ||
if n > 100 { | ||
n = 100 | ||
} | ||
|
||
now := _timeNow() | ||
now = time.Date(now.Year(), now.Month(), 15, 12, 0, 0, 0, time.UTC) | ||
|
||
var monthYears []string | ||
for ; n > 0; n-- { | ||
monthYears = append(monthYears, now.Format("2006-01")) | ||
now = now.AddDate(0, -1, 0) | ||
} | ||
|
||
return monthYears | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
package rapina | ||
|
||
import ( | ||
"reflect" | ||
"testing" | ||
"time" | ||
) | ||
|
||
func TestIsDate(t *testing.T) { | ||
type args struct { | ||
date string | ||
} | ||
tests := []struct { | ||
name string | ||
args args | ||
want bool | ||
}{ | ||
{ | ||
name: "should be true", | ||
args: args{date: "2021-04-26"}, | ||
want: true, | ||
}, | ||
{ | ||
name: "should be true too", | ||
args: args{date: "2030-12-31"}, | ||
want: true, | ||
}, | ||
{ | ||
name: "should be false", | ||
args: args{date: "2021-04-31"}, | ||
want: false, | ||
}, | ||
{ | ||
name: "should be false too", | ||
args: args{date: "20/12/2000"}, | ||
want: false, | ||
}, | ||
{ | ||
name: "should be false three", | ||
args: args{date: "2021-07-32"}, | ||
want: false, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
if got := IsDate(tt.args.date); got != tt.want { | ||
t.Errorf("IsDate() = %v, want %v", got, tt.want) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestIsUrl(t *testing.T) { | ||
type args struct { | ||
str string | ||
} | ||
tests := []struct { | ||
name string | ||
args args | ||
want bool | ||
}{ | ||
{ | ||
name: "should be true", | ||
args: args{str: "http://example.com/path"}, | ||
want: true, | ||
}, | ||
{ | ||
name: "should be false", | ||
args: args{str: "example.com/path"}, | ||
want: false, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
if got := IsUrl(tt.args.str); got != tt.want { | ||
t.Errorf("IsUrl() = %v, want %v", got, tt.want) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestMonthsFromToday(t *testing.T) { | ||
timeNow1 := func() time.Time { | ||
return time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC) | ||
} | ||
timeNow2 := func() time.Time { | ||
return time.Date(2009, time.March, 31, 23, 0, 0, 0, time.UTC) | ||
} | ||
|
||
type args struct { | ||
n int | ||
} | ||
tests := []struct { | ||
name string | ||
args args | ||
timeNow func() time.Time | ||
want []string | ||
}{ | ||
{ | ||
name: "should show 3 months", | ||
args: args{n: 3}, | ||
timeNow: timeNow1, | ||
want: []string{"2009-11", "2009-10", "2009-09"}, | ||
}, | ||
{ | ||
name: "should show 2 months", | ||
args: args{n: 2}, | ||
timeNow: timeNow2, | ||
want: []string{"2009-03", "2009-02"}, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
_timeNow = tt.timeNow | ||
if got := MonthsFromToday(tt.args.n); !reflect.DeepEqual(got, tt.want) { | ||
t.Errorf("MonthsFromToday() = %#v, want %v", got, tt.want) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package rapina | ||
|
||
import "errors" | ||
|
||
var ( | ||
ErrInvalidDate = errors.New("invalid date format") | ||
) |
Oops, something went wrong.