File size: 4,342 Bytes
16cdcb7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package subscription_test

import (
	"bytes"
	"encoding/json"
	"reflect"
	"testing"
	"time"

	"github.com/alpacahq/alpacadecimal"
	"github.com/samber/lo"

	"github.com/openmeterio/openmeter/openmeter/productcatalog"
	"github.com/openmeterio/openmeter/openmeter/subscription"
	"github.com/openmeterio/openmeter/pkg/datetime"
	"github.com/openmeterio/openmeter/pkg/models"
)

func TestSubscriptionItemSpecSerialize(t *testing.T) {
	sis := subscription.SubscriptionItemSpec{
		CreateSubscriptionItemInput: subscription.CreateSubscriptionItemInput{
			CreateSubscriptionItemCustomerInput: subscription.CreateSubscriptionItemCustomerInput{},
			CreateSubscriptionItemPlanInput: subscription.CreateSubscriptionItemPlanInput{
				PhaseKey: "phase-key",
				ItemKey:  "item-key",
				RateCard: &productcatalog.UsageBasedRateCard{
					RateCardMeta: productcatalog.RateCardMeta{
						Key:         "rate-card-key",
						Name:        "rate-card-name",
						Description: lo.ToPtr("rate-card-description"),
						FeatureKey:  lo.ToPtr("feature-key"),
						FeatureID:   lo.ToPtr("feature-id"),
						EntitlementTemplate: productcatalog.NewEntitlementTemplateFrom(productcatalog.MeteredEntitlementTemplate{
							IssueAfterReset: lo.ToPtr(100.0),
						}),
						Price: productcatalog.NewPriceFrom(productcatalog.UnitPrice{
							Amount: alpacadecimal.NewFromInt(100),
						}),
					},
				},
			},
		},
	}

	// Now let's marshal and unmarshal the subscription item spec
	sisBytes, err := json.MarshalIndent(sis, "", "  ")
	if err != nil {
		t.Fatalf("failed to marshal subscription item spec: %v", err)
	}

	bt2 := bytes.Clone(sisBytes)

	var sis2 subscription.SubscriptionItemSpec
	err = json.Unmarshal(bt2, &sis2)
	if err != nil {
		t.Fatalf("failed to unmarshal subscription item spec: %v", err)
	}

	// Now let's compare the two
	if !reflect.DeepEqual(sis, sis2) {
		t.Fatalf("subscription item spec is not equal \n\ninput: %v\n\n serialized: \n%s\n\n unserialized: \n%v\n\n", sis, string(bt2), sis2)
	}
}

func TestSubscriptionItemSerialize(t *testing.T) {
	fixedTime := time.Date(2023, 1, 1, 0, 0, 0, 0, time.UTC)
	si := subscription.SubscriptionItem{
		NamespacedID: models.NamespacedID{
			Namespace: "test-namespace",
			ID:        "test-id",
		},
		ManagedModel: models.ManagedModel{
			CreatedAt: fixedTime,
			UpdatedAt: fixedTime,
		},
		MetadataModel: models.MetadataModel{
			Metadata: map[string]string{
				"key1": "value1",
				"key2": "value2",
			},
		},
		ActiveFromOverrideRelativeToPhaseStart: lo.ToPtr(datetime.NewISODuration(0, 0, 0, 1, 0, 0, 0)),
		ActiveToOverrideRelativeToPhaseStart:   lo.ToPtr(datetime.NewISODuration(0, 0, 0, 2, 0, 0, 0)),
		CadencedModel: models.CadencedModel{
			ActiveFrom: fixedTime,
			ActiveTo:   lo.ToPtr(fixedTime.Add(24 * time.Hour)),
		},
		BillingBehaviorOverride: subscription.BillingBehaviorOverride{
			RestartBillingPeriod: lo.ToPtr(true),
		},
		SubscriptionId: "subscription-id",
		PhaseId:        "phase-id",
		Key:            "item-key",
		RateCard: &productcatalog.UsageBasedRateCard{
			RateCardMeta: productcatalog.RateCardMeta{
				Key:         "rate-card-key",
				Name:        "rate-card-name",
				Description: lo.ToPtr("rate-card-description"),
				FeatureKey:  lo.ToPtr("feature-key"),
				FeatureID:   lo.ToPtr("feature-id"),
				EntitlementTemplate: productcatalog.NewEntitlementTemplateFrom(productcatalog.MeteredEntitlementTemplate{
					IssueAfterReset: lo.ToPtr(100.0),
				}),
				Price: productcatalog.NewPriceFrom(productcatalog.UnitPrice{
					Amount: alpacadecimal.NewFromInt(100),
				}),
			},
			BillingCadence: datetime.NewISODuration(0, 0, 0, 0, 1, 0, 0),
		},
		EntitlementID: lo.ToPtr("entitlement-id"),
		Name:          "item-name",
		Description:   lo.ToPtr("item-description"),
	}

	// Now let's marshal and unmarshal the subscription item
	siBytes, err := json.MarshalIndent(si, "", "  ")
	if err != nil {
		t.Fatalf("failed to marshal subscription item: %v", err)
	}

	bt2 := bytes.Clone(siBytes)

	var si2 subscription.SubscriptionItem
	err = json.Unmarshal(bt2, &si2)
	if err != nil {
		t.Fatalf("failed to unmarshal subscription item: %v", err)
	}

	// Now let's compare the two
	if !reflect.DeepEqual(si, si2) {
		t.Fatalf("subscription item is not equal \n\ninput: %v\n\n serialized: \n%s\n\n unserialized: \n%v\n\n", si, string(bt2), si2)
	}
}