tests(all): improve tests, refactor(api): saving books
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
@@ -10,10 +10,10 @@ import (
|
||||
)
|
||||
|
||||
// Reimplemented KOReader Partial MD5 Calculation
|
||||
func CalculatePartialMD5(filePath string) (string, error) {
|
||||
func CalculatePartialMD5(filePath string) (*string, error) {
|
||||
file, err := os.Open(filePath)
|
||||
if err != nil {
|
||||
return "", err
|
||||
return nil, err
|
||||
}
|
||||
|
||||
defer file.Close()
|
||||
@@ -41,7 +41,8 @@ func CalculatePartialMD5(filePath string) (string, error) {
|
||||
}
|
||||
|
||||
allBytes := buf.Bytes()
|
||||
return fmt.Sprintf("%x", md5.Sum(allBytes)), nil
|
||||
fileHash := fmt.Sprintf("%x", md5.Sum(allBytes))
|
||||
return &fileHash, nil
|
||||
}
|
||||
|
||||
// Creates a token of n size
|
||||
@@ -53,3 +54,23 @@ func GenerateToken(n int) ([]byte, error) {
|
||||
}
|
||||
return b, nil
|
||||
}
|
||||
|
||||
// Calculate MD5 of a file
|
||||
func CalculateMD5(filePath string) (*string, error) {
|
||||
file, err := os.Open(filePath)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
defer file.Close()
|
||||
|
||||
hash := md5.New()
|
||||
_, err = io.Copy(hash, file)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
fileHash := fmt.Sprintf("%x", hash.Sum(nil))
|
||||
|
||||
return &fileHash, nil
|
||||
}
|
||||
|
||||
@@ -1,12 +1,26 @@
|
||||
package utils
|
||||
|
||||
import "testing"
|
||||
import (
|
||||
"github.com/stretchr/testify/assert"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestCalculatePartialMD5(t *testing.T) {
|
||||
partialMD5, err := CalculatePartialMD5("../_test_files/alice.epub")
|
||||
assert := assert.New(t)
|
||||
|
||||
want := "386d1cb51fe4a72e5c9fdad5e059bad9"
|
||||
if partialMD5 != want {
|
||||
t.Fatalf(`Expected: %v, Got: %v, Error: %v`, want, partialMD5, err)
|
||||
}
|
||||
desiredPartialMD5 := "386d1cb51fe4a72e5c9fdad5e059bad9"
|
||||
calculatedPartialMD5, err := CalculatePartialMD5("../_test_files/alice.epub")
|
||||
|
||||
assert.Nil(err, "error should be nil")
|
||||
assert.Equal(desiredPartialMD5, *calculatedPartialMD5, "should be equal")
|
||||
}
|
||||
|
||||
func TestCalculateMD5(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
|
||||
desiredMD5 := "0f36c66155de34b281c4791654d0b1ce"
|
||||
calculatedMD5, err := CalculateMD5("../_test_files/alice.epub")
|
||||
|
||||
assert.Nil(err, "error should be nil")
|
||||
assert.Equal(desiredMD5, *calculatedMD5, "should be equal")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user