Преглед изворни кода

compare two file's md5 should be correct.

master
Patrick Peng Sun пре 8 година
родитељ
комит
5510623972
2 измењених фајлова са 35 додато и 6 уклоњено
  1. +13
    -6
      crmAttachment_test.go
  2. +22
    -0
      fileinfo.go

+ 13
- 6
crmAttachment_test.go Прегледај датотеку

package main package main


import "testing"

import "encoding/json"
import (
"encoding/json"
"os"
"testing"
"time"
)


func TestCRMAttachFile(t *testing.T) { func TestCRMAttachFile(t *testing.T) {

path1 := "media_for_test/200x200.png"
path2 := "/tmp/wechat_hitxy_测试_" + time.Now().Format("2006-Jan-02-05-04-05")
r, e := crmCreateAttachment("media_for_test/200x200.png") r, e := crmCreateAttachment("media_for_test/200x200.png")


AssertEqual(t, e, nil, "should be no error when create attachment") AssertEqual(t, e, nil, "should be no error when create attachment")
AssertEqual(t, r.ID != "", true, "ID should not be empty ") AssertEqual(t, r.ID != "", true, "ID should not be empty ")
// //
crmDownloadAttachmentAs(r.ID, "/tmp/wechat_hitxy_测试")
crmDownloadAttachmentAs(r.ID, path2)

AssertEqual(t, md5CompFile(path1, path2), true, "attachment should be downloade as same file ")


//TODO: two file should be equal
delete, e := crmDeleteEntity("Attachment", r.ID) delete, e := crmDeleteEntity("Attachment", r.ID)
AssertEqual(t, delete, true, "temp attachment should have been deleted") AssertEqual(t, delete, true, "temp attachment should have been deleted")

os.Remove(path2)
} }


func TestDecodeJsonResponse(t *testing.T) { func TestDecodeJsonResponse(t *testing.T) {

+ 22
- 0
fileinfo.go Прегледај датотеку

package main package main


import ( import (
"crypto/md5"
"fmt" "fmt"
"io"
"io/ioutil" "io/ioutil"
"log" "log"
"os" "os"
os.MkdirAll(d, 0700) os.MkdirAll(d, 0700)
} }
} }

func md5File(path string) (md5str string) {
f, err := os.Open(path)
if err != nil {
log.Println(err)
return
}
defer f.Close()

h := md5.New()
if _, err := io.Copy(h, f); err != nil {
log.Fatal(err)
}

return fmt.Sprintf("%x", h.Sum(nil))
}

func md5CompFile(path1, path2 string) (same bool) {
return md5File(path1) == md5File(path2)
}

Loading…
Откажи
Сачувај