You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
193 lines
4.5 KiB
193 lines
4.5 KiB
package models
|
|
|
|
import (
|
|
"encoding/json"
|
|
"testing"
|
|
)
|
|
|
|
func TestAssetJSONSerialization(t *testing.T) {
|
|
asset := Asset{
|
|
ID: "asset123",
|
|
Color: "red",
|
|
Size: "10",
|
|
Owner: "Alice",
|
|
AppraisedValue: "2000",
|
|
}
|
|
|
|
jsonData, err := json.Marshal(asset)
|
|
if err != nil {
|
|
t.Fatalf("Failed to marshal asset: %v", err)
|
|
}
|
|
|
|
var unmarshaledAsset Asset
|
|
err = json.Unmarshal(jsonData, &unmarshaledAsset)
|
|
if err != nil {
|
|
t.Fatalf("Failed to unmarshal asset: %v", err)
|
|
}
|
|
|
|
if unmarshaledAsset.ID != asset.ID {
|
|
t.Errorf("Expected ID %s, got %s", asset.ID, unmarshaledAsset.ID)
|
|
}
|
|
}
|
|
|
|
func TestAssetUnmarshalWithNumberAppraisedValue(t *testing.T) {
|
|
// Test with number type appraisedValue (from blockchain)
|
|
jsonData := `{
|
|
"ID": "asset123",
|
|
"color": "red",
|
|
"size": "10",
|
|
"owner": "Alice",
|
|
"appraisedValue": 2000
|
|
}`
|
|
|
|
var asset Asset
|
|
err := json.Unmarshal([]byte(jsonData), &asset)
|
|
if err != nil {
|
|
t.Fatalf("Failed to unmarshal asset with number appraisedValue: %v", err)
|
|
}
|
|
|
|
if asset.AppraisedValue != "2000" {
|
|
t.Errorf("Expected AppraisedValue '2000', got '%s'", asset.AppraisedValue)
|
|
}
|
|
}
|
|
|
|
func TestAssetUnmarshalWithStringAppraisedValue(t *testing.T) {
|
|
// Test with string type appraisedValue (from API)
|
|
jsonData := `{
|
|
"ID": "asset123",
|
|
"color": "red",
|
|
"size": "10",
|
|
"owner": "Alice",
|
|
"appraisedValue": "2000"
|
|
}`
|
|
|
|
var asset Asset
|
|
err := json.Unmarshal([]byte(jsonData), &asset)
|
|
if err != nil {
|
|
t.Fatalf("Failed to unmarshal asset with string appraisedValue: %v", err)
|
|
}
|
|
|
|
if asset.AppraisedValue != "2000" {
|
|
t.Errorf("Expected AppraisedValue '2000', got '%s'", asset.AppraisedValue)
|
|
}
|
|
}
|
|
|
|
func TestAssetUnmarshalWithFloatAppraisedValue(t *testing.T) {
|
|
// Test with float type appraisedValue
|
|
jsonData := `{
|
|
"ID": "asset123",
|
|
"color": "red",
|
|
"size": "10",
|
|
"owner": "Alice",
|
|
"appraisedValue": 2000.50
|
|
}`
|
|
|
|
var asset Asset
|
|
err := json.Unmarshal([]byte(jsonData), &asset)
|
|
if err != nil {
|
|
t.Fatalf("Failed to unmarshal asset with float appraisedValue: %v", err)
|
|
}
|
|
|
|
if asset.AppraisedValue != "2000.5" {
|
|
t.Errorf("Expected AppraisedValue '2000.5', got '%s'", asset.AppraisedValue)
|
|
}
|
|
}
|
|
|
|
func TestAssetUnmarshalWithNumberFields(t *testing.T) {
|
|
// Test with number types for both size and appraisedValue (from blockchain)
|
|
jsonData := `{
|
|
"ID": "asset123",
|
|
"color": "red",
|
|
"size": 10,
|
|
"owner": "Alice",
|
|
"appraisedValue": 2000
|
|
}`
|
|
|
|
var asset Asset
|
|
err := json.Unmarshal([]byte(jsonData), &asset)
|
|
if err != nil {
|
|
t.Fatalf("Failed to unmarshal asset with number fields: %v", err)
|
|
}
|
|
|
|
if asset.Size != "10" {
|
|
t.Errorf("Expected Size '10', got '%s'", asset.Size)
|
|
}
|
|
|
|
if asset.AppraisedValue != "2000" {
|
|
t.Errorf("Expected AppraisedValue '2000', got '%s'", asset.AppraisedValue)
|
|
}
|
|
}
|
|
|
|
func TestAssetUnmarshalWithMixedTypes(t *testing.T) {
|
|
// Test with mixed types - string size, number appraisedValue
|
|
jsonData := `{
|
|
"ID": "asset456",
|
|
"color": "blue",
|
|
"size": "15",
|
|
"owner": "Bob",
|
|
"appraisedValue": 3000.50
|
|
}`
|
|
|
|
var asset Asset
|
|
err := json.Unmarshal([]byte(jsonData), &asset)
|
|
if err != nil {
|
|
t.Fatalf("Failed to unmarshal asset with mixed types: %v", err)
|
|
}
|
|
|
|
if asset.Size != "15" {
|
|
t.Errorf("Expected Size '15', got '%s'", asset.Size)
|
|
}
|
|
|
|
if asset.AppraisedValue != "3000.5" {
|
|
t.Errorf("Expected AppraisedValue '3000.5', got '%s'", asset.AppraisedValue)
|
|
}
|
|
}
|
|
|
|
func TestCreateAssetRequestWithoutID(t *testing.T) {
|
|
// Test CreateAssetRequest without ID field (for UUID generation)
|
|
jsonData := `{
|
|
"color": "blue",
|
|
"size": "15",
|
|
"owner": "Bob",
|
|
"appraisedValue": "3000"
|
|
}`
|
|
|
|
var req CreateAssetRequest
|
|
err := json.Unmarshal([]byte(jsonData), &req)
|
|
if err != nil {
|
|
t.Fatalf("Failed to unmarshal CreateAssetRequest without ID: %v", err)
|
|
}
|
|
|
|
if req.ID != "" {
|
|
t.Errorf("Expected empty ID when not provided, got '%s'", req.ID)
|
|
}
|
|
|
|
if req.Color != "blue" {
|
|
t.Errorf("Expected Color 'blue', got '%s'", req.Color)
|
|
}
|
|
|
|
if req.Owner != "Bob" {
|
|
t.Errorf("Expected Owner 'Bob', got '%s'", req.Owner)
|
|
}
|
|
}
|
|
|
|
func TestCreateAssetRequestWithID(t *testing.T) {
|
|
// Test CreateAssetRequest with ID field provided
|
|
jsonData := `{
|
|
"id": "custom-asset-123",
|
|
"color": "green",
|
|
"size": "20",
|
|
"owner": "Charlie",
|
|
"appraisedValue": "4000"
|
|
}`
|
|
|
|
var req CreateAssetRequest
|
|
err := json.Unmarshal([]byte(jsonData), &req)
|
|
if err != nil {
|
|
t.Fatalf("Failed to unmarshal CreateAssetRequest with ID: %v", err)
|
|
}
|
|
|
|
if req.ID != "custom-asset-123" {
|
|
t.Errorf("Expected ID 'custom-asset-123', got '%s'", req.ID)
|
|
}
|
|
}
|
|
|