-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
146 additions
and
78 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 |
---|---|---|
@@ -1,32 +1,83 @@ | ||
package auction | ||
|
||
import ( | ||
"std" | ||
"strconv" | ||
"testing" | ||
"time" | ||
|
||
"gno.land/p/demo/json" | ||
"gno.land/p/demo/testutils" | ||
"gno.land/p/demo/uassert" | ||
"gno.land/p/demo/ufmt" | ||
) | ||
|
||
const ( | ||
owner = testutils.TestAddress("owner") | ||
begin = time.Now().Add(1 * time.Hour) | ||
end = time.Now().Add(24 * time.Hour) | ||
minPrice = 100 | ||
deadline = begin.Add(24 * time.Hour) | ||
) | ||
func TestEncodeAuction(t *testing.T) { | ||
owner := testutils.TestAddress("owner") | ||
begin := time.Now().Add(1 * time.Hour) | ||
end := time.Now().Add(24 * time.Hour) | ||
auction := NewAuction("Test Auction", owner, "This is a test auction", begin, end, 100, "url") | ||
|
||
node, err := EncodeAuction(auction) | ||
uassert.NoError(t, err, "Unexpected error encoding auction") | ||
|
||
jsonStr, err := ToJSONString(node) | ||
uassert.NoError(t, err, "Unexpected error converting JSON node to string") | ||
ufmt.Println(jsonStr) | ||
|
||
expected := `{"title":"Test Auction","description":"This is a test auction","begin":` + strconv.Itoa(int(begin.Unix())) + `,"deadline":` + strconv.Itoa(int(end.Unix())) + `,"owner":"` + owner.String() + `","firstPrice":100,"bids":0,"img":"url"}` | ||
uassert.Equal(t, expected, jsonStr, "Encoded auction JSON mismatch") | ||
} | ||
|
||
var auction = NewAuction("Test Auction", owner, "This is a test auction", begin, end, minPrice, "url") | ||
func TestEncodeAuctionList(t *testing.T) { | ||
owner := testutils.TestAddress("owner") | ||
begin := time.Now().Add(1 * time.Hour) | ||
end := time.Now().Add(24 * time.Hour) | ||
auction1 := NewAuction("Test Auction 1", owner, "This is a test auction 1", begin, end, 100, "url1") | ||
auction2 := NewAuction("Test Auction 2", owner, "This is a test auction 2", begin, end, 200, "url2") | ||
|
||
auctions := []*Auction{auction1, auction2} | ||
node, err := EncodeAuctionList(auctions) | ||
uassert.NoError(t, err, "Unexpected error encoding auction list") | ||
|
||
jsonStr, err := ToJSONString(node) | ||
uassert.NoError(t, err, "Unexpected error converting JSON node to string") | ||
|
||
expected := `[{"title":"Test Auction 1","description":"This is a test auction 1","begin":` + strconv.Itoa(int(begin.Unix())) + `,"deadline":` + strconv.Itoa(int(end.Unix())) + `,"owner":"` + owner.String() + `","firstPrice":100,"bids":0,"img":"url1"},{"title":"Test Auction 2","description":"This is a test auction 2","begin":` + strconv.Itoa(int(begin.Unix())) + `,"deadline":` + strconv.Itoa(int(end.Unix())) + `,"owner":"` + owner.String() + `","firstPrice":200,"bids":0,"img":"url2"}]` | ||
uassert.Equal(t, expected, jsonStr, "Encoded auction list JSON mismatch") | ||
} | ||
|
||
func TestDecodeAuction(t *testing.T) { | ||
jsonStr := `{"title":"Test Auction","description":"This is a test auction","begin":` + strconv.Itoa(int(time.Now().Add(1*time.Hour).Unix())) + `,"deadline":` + strconv.Itoa(int(time.Now().Add(24*time.Hour).Unix())) + `,"owner":"owner","firstPrice":100,"bids":0,"img":"url"}` | ||
node, err := FromJSONString(jsonStr) | ||
uassert.NoError(t, err, "Unexpected error parsing JSON string") | ||
|
||
auction, err := DecodeAuction(node) | ||
uassert.NoError(t, err, "Unexpected error decoding auction") | ||
|
||
uassert.Equal(t, "Test Auction", auction.Title, "Auction title mismatch") | ||
uassert.Equal(t, "This is a test auction", auction.Description, "Auction description mismatch") | ||
uassert.Equal(t, int64(100), auction.Price, "Auction price mismatch") | ||
uassert.Equal(t, "url", auction.Img, "Auction image URL mismatch") | ||
} | ||
|
||
func TestToJSONString(t *testing.T) { | ||
expected := `{"Title":"Test Auction","Description":"This is a test auction","Begin":"` + strconv.Itoa(int(begin.Unix())) + `","End":"` + strconv.Itoa(int(end.Unix())) + `","MinPrice":100,"Deadline":"` + strconv.Itoa(int(deadline.Unix())) + `","Owner":"` + owner.String() + `","URL":"url"}` | ||
node := json.ObjectNode("", map[string]*json.Node{ | ||
"key": json.StringNode("key", "value"), | ||
}) | ||
|
||
jsonStr, err := ToJSONString(node) | ||
uassert.NoError(t, err, "Unexpected error converting JSON node to string") | ||
|
||
expected := `{"key":"value"}` | ||
uassert.Equal(t, expected, jsonStr, "JSON string mismatch") | ||
} | ||
|
||
func TestFromJSONString(t *testing.T) { | ||
jsonStr := `{"key":"value"}` | ||
node, err := FromJSONString(jsonStr) | ||
uassert.NoError(t, err, "Unexpected error parsing JSON string") | ||
|
||
result, err := auction.ToJSONString() | ||
if err != nil { | ||
t.Fatalf("unexpected error: %v", err) | ||
} | ||
uassert.Equal(t, result, expected) | ||
valueNode, err := node.GetKey("key") | ||
uassert.NoError(t, err, "Unexpected error getting key from JSON node") | ||
uassert.Equal(t, "value", valueNode.MustString(), "JSON node value mismatch") | ||
} |