Example #1
0
func TestMakeClearUploader(t *testing.T) {
	mgodb.Setup("localhost", "tenpu_test")
	db := mgodb.NewDatabase("localhost", "tenpu_test")

	mgodb.CollectionDo(tenpu.CollectionName, func(c *mgo.Collection) {
		c.DropCollection()
	})

	st := gridfs.NewStorage()

	http.HandleFunc("/upload_avatar", tenpu.MakeClearUploader("OwnerId", "posts", st))
	http.HandleFunc("/load_avatar", tenpu.MakeFileLoader("id", st))
	ts := httptest.NewServer(http.DefaultServeMux)
	defer ts.Close()

	//upload attachment repeatly
	req, _ := http.NewRequest("POST", ts.URL+"/upload_avatar", strings.NewReader(singlePartContent))
	req.Header.Set("Content-Type", "multipart/form-data; boundary=----WebKitFormBoundarySHaDkk90eMKgsVUj")
	res, err := http.DefaultClient.Do(req)

	req, _ = http.NewRequest("POST", ts.URL+"/upload_avatar", strings.NewReader(singlePartContent))
	req.Header.Set("Content-Type", "multipart/form-data; boundary=----WebKitFormBoundarySHaDkk90eMKgsVUj")
	res, err = http.DefaultClient.Do(req)

	req, _ = http.NewRequest("POST", ts.URL+"/upload_avatar", strings.NewReader(singlePartContent))
	req.Header.Set("Content-Type", "multipart/form-data; boundary=----WebKitFormBoundarySHaDkk90eMKgsVUj")
	res, err = http.DefaultClient.Do(req)

	if err != nil {
		panic(err)
	}
	b, _ := ioutil.ReadAll(res.Body)
	strb := string(b)
	if !strings.Contains(strb, "4facead362911fa23c000002") {
		t.Errorf("%+v", strb)
	}

	dbc := tenpu.DatabaseClient{Database: db}
	atts := dbc.Attachments("4facead362911fa23c000002")
	if len(atts) != 1 {
		t.Errorf("%+v", atts[0])
	}

	res, err = http.Get(ts.URL + "/load_avatar?id=" + atts[0].Id)
	if err != nil {
		panic(err)
	}

	b, _ = ioutil.ReadAll(res.Body)
	strb = string(b)
	if strb != "the file content c\n" {
		t.Errorf("%+v", strb)
	}
}
Example #2
0
func TestUploader(t *testing.T) {
	mgodb.Setup("localhost", "tenpu_test")

	mgodb.CollectionDo(collectionName, func(c *mgo.Collection) {
		c.DropCollection()
	})

	_, meta, _, _ := m.MakeForUpload(nil)

	http.HandleFunc("/postupload", tenpu.MakeUploader(m))
	http.HandleFunc("/load", tenpu.MakeFileLoader(m))
	ts := httptest.NewServer(http.DefaultServeMux)
	defer ts.Close()

	req, _ := http.NewRequest("POST", ts.URL+"/postupload", strings.NewReader(multipartContent))
	req.Header.Set("Content-Type", "multipart/form-data; boundary=----WebKitFormBoundarySHaDkk90eMKgsVUj")
	res, err := http.DefaultClient.Do(req)
	if err != nil {
		panic(err)
	}
	b, _ := ioutil.ReadAll(res.Body)
	strb := string(b)
	if !strings.Contains(strb, "4facead362911fa23c000001") {
		t.Errorf("%+v", strb)
		return
	}

	atts := meta.Attachments("4facead362911fa23c000001")
	if len(atts) != 2 {
		t.Errorf("%+v", atts[0])
	}

	res, err = http.Get(ts.URL + "/load?id=" + atts[0].Id)
	if err != nil {
		panic(err)
	}

	b, _ = ioutil.ReadAll(res.Body)
	strb = string(b)
	if strb != "the file content a\n" {
		t.Errorf("%+v", strb)
	}
}
Example #3
0
func CountProduct(query bson.M) (num int, err error) {
	mgodb.CollectionDo(PRODUCTS, func(c *mgo.Collection) {
		num, err = c.Find(query).Count()
	})
	return
}
Example #4
0
func CountReview(query bson.M) (num int, err error) {
	mgodb.CollectionDo(REVIEWS, func(c *mgo.Collection) {
		num, err = c.Find(query).Count()
	})
	return
}
Example #5
0
func CountFollowBrand(query bson.M) (num int, err error) {
	mgodb.CollectionDo(FOLLOW_BRANDS, func(c *mgo.Collection) {
		num, err = c.Find(query).Count()
	})
	return
}
Example #6
0
func DeleteFollowBrand(query bson.M) (err error) {
	mgodb.CollectionDo(FOLLOW_BRANDS, func(rc *mgo.Collection) {
		_, err = rc.RemoveAll(query)
	})
	return
}
Example #7
0
File: tenpu.go Project: athom/tenpu
func RemoveAttachmentById(id string) (err error) {
	mgodb.CollectionDo(CollectionName, func(c *mgo.Collection) {
		c.Remove(bson.M{"_id": id})
	})
	return
}
Example #8
0
File: tenpu.go Project: athom/tenpu
func AttachmentById(id string) (r *Attachment) {
	mgodb.CollectionDo(CollectionName, func(c *mgo.Collection) {
		c.Find(bson.M{"_id": id}).One(&r)
	})
	return
}
Example #9
0
File: tenpu.go Project: athom/tenpu
func AttachmentsByOwnerIds(ownerids []string) (r []*Attachment) {
	mgodb.CollectionDo(CollectionName, func(c *mgo.Collection) {
		c.Find(bson.M{"ownerid": bson.M{"$in": ownerids}}).All(&r)
	})
	return
}
Example #10
0
func DeleteOwnItem(query bson.M) (err error) {
	mgodb.CollectionDo(OWN_ITEMS, func(rc *mgo.Collection) {
		_, err = rc.RemoveAll(query)
	})
	return
}
Example #11
0
func Update(selector, changer bson.M) (err error) {
	mgodb.CollectionDo(USERS, func(rc *mgo.Collection) {
		err = rc.Update(selector, changer)
	})
	return
}