ducky/subscriptions
ducky/subscriptions/subscription_store_test.go
First implementation of datastore interface. Define Subscriptions, and the SubscriptionChange type that can modify Subscriptions. Define the subscriptionStore, which describes how Subscriptions are to be created, retrieved, updated, and deleted in/from the storage backend they're stored in. Create a Memstore implementation of the subscriptionStore, which stores all our data in-memory, for use in testing. Write tests to cover the subscriptionStore interface, testing the entire Memstore. We'll plug future storage backends into these tests, to make sure they all behave the same, and to exercise each storage backend without requiring a suite of tests for each. At this point, we have 100% test coverage and no complaints from golint or go vet. I expect it's all downhill from here.
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/subscription_store_test.go Thu Jun 11 23:15:01 2015 -0400 1.3 @@ -0,0 +1,628 @@ 1.4 +package subscriptions 1.5 + 1.6 +import ( 1.7 + "strconv" 1.8 + "testing" 1.9 + "time" 1.10 + 1.11 + "code.secondbit.org/uuid.hg" 1.12 +) 1.13 + 1.14 +const ( 1.15 + subscriptionChangeStripeCustomer = 1 << iota 1.16 + subscriptionChangeAmount 1.17 + subscriptionChangePeriod 1.18 + subscriptionChangeBeginCharging 1.19 + subscriptionChangeLastCharged 1.20 + subscriptionChangeLastNotified 1.21 + subscriptionChangeInLockout 1.22 +) 1.23 + 1.24 +var testSubscriptionStores = []subscriptionStore{ 1.25 + NewMemstore(), 1.26 +} 1.27 + 1.28 +func compareSubscriptions(sub1, sub2 Subscription) (bool, string, interface{}, interface{}) { 1.29 + if !sub1.ID.Equal(sub2.ID) { 1.30 + return false, "ID", sub1.ID, sub2.ID 1.31 + } 1.32 + if !sub1.UserID.Equal(sub2.UserID) { 1.33 + return false, "UserID", sub1.UserID, sub2.UserID 1.34 + } 1.35 + if sub1.StripeCustomer != sub2.StripeCustomer { 1.36 + return false, "StripeCustomer", sub1.StripeCustomer, sub2.StripeCustomer 1.37 + } 1.38 + if sub1.Amount != sub2.Amount { 1.39 + return false, "Amount", sub1.Amount, sub2.Amount 1.40 + } 1.41 + if sub1.Period != sub2.Period { 1.42 + return false, "Period", sub1.Period, sub2.Period 1.43 + } 1.44 + if !sub1.Created.Equal(sub2.Created) { 1.45 + return false, "Created", sub1.Created, sub2.Created 1.46 + } 1.47 + if !sub1.BeginCharging.Equal(sub2.BeginCharging) { 1.48 + return false, "BeginCharging", sub1.BeginCharging, sub2.BeginCharging 1.49 + } 1.50 + if !sub1.LastCharged.Equal(sub2.LastCharged) { 1.51 + return false, "LastCharged", sub1.LastCharged, sub2.LastCharged 1.52 + } 1.53 + if !sub1.LastNotified.Equal(sub2.LastNotified) { 1.54 + return false, "LastNotified", sub1.LastNotified, sub2.LastNotified 1.55 + } 1.56 + if sub1.InLockout != sub2.InLockout { 1.57 + return false, "InLockout", sub1.InLockout, sub2.InLockout 1.58 + } 1.59 + return true, "", nil, nil 1.60 +} 1.61 + 1.62 +func subscriptionMapContains(subscriptionMap map[string]Subscription, subscriptions ...Subscription) (bool, []Subscription) { 1.63 + var missing []Subscription 1.64 + for _, sub := range subscriptions { 1.65 + if _, ok := subscriptionMap[sub.ID.String()]; !ok { 1.66 + missing = append(missing, sub) 1.67 + } 1.68 + } 1.69 + if len(missing) > 0 { 1.70 + return false, missing 1.71 + } 1.72 + return true, missing 1.73 +} 1.74 + 1.75 +func TestCreateSubscription(t *testing.T) { 1.76 + for _, store := range testSubscriptionStores { 1.77 + err := store.reset() 1.78 + if err != nil { 1.79 + t.Fatalf("Error resetting %T: %+v\n", store, err) 1.80 + } 1.81 + customerID := uuid.NewID() 1.82 + sub := Subscription{ 1.83 + ID: uuid.NewID(), 1.84 + UserID: customerID, 1.85 + StripeCustomer: "stripeCustomer1", 1.86 + Amount: 200, 1.87 + Period: MonthlyPeriod, 1.88 + Created: time.Now(), 1.89 + BeginCharging: time.Now().Add(time.Hour), 1.90 + } 1.91 + err = store.createSubscription(sub) 1.92 + if err != nil { 1.93 + t.Errorf("Error creating subscription in %T: %+v\n", store, err) 1.94 + } 1.95 + retrieved, err := store.getSubscriptions([]uuid.ID{sub.ID}) 1.96 + if err != nil { 1.97 + t.Errorf("Error retrieving subscription from %T: %+v\n", store, err) 1.98 + } 1.99 + if _, returned := retrieved[sub.ID.String()]; !returned { 1.100 + t.Errorf("Error retrieving subscription from %T: %s wasn't in the results.", store, sub.ID) 1.101 + } 1.102 + ok, field, expected, result := compareSubscriptions(sub, retrieved[sub.ID.String()]) 1.103 + if !ok { 1.104 + t.Errorf("Expected %s to be %v, got %v from %T\n", field, expected, result, store) 1.105 + } 1.106 + err = store.createSubscription(sub) 1.107 + if err != ErrSubscriptionAlreadyExists { 1.108 + t.Errorf("Unexpected error creating subscription in %T (wanted %+v): %+v\n", store, ErrSubscriptionAlreadyExists, err) 1.109 + } 1.110 + sub.ID = uuid.NewID() 1.111 + err = store.createSubscription(sub) 1.112 + if err != ErrStripeCustomerAlreadyExists { 1.113 + t.Errorf("Unexpected error creating subscription in %T (wanted %+v): %+v\n", store, ErrStripeCustomerAlreadyExists, err) 1.114 + } 1.115 + sub.StripeCustomer = "stripeCustomer2" 1.116 + err = store.createSubscription(sub) 1.117 + if err != nil { 1.118 + t.Errorf("Error creating subscription in %T: %+v\n", store, err) 1.119 + } 1.120 + } 1.121 +} 1.122 + 1.123 +func TestUpdateSubscription(t *testing.T) { 1.124 + variations := 1 << 7 1.125 + sub := Subscription{ 1.126 + ID: uuid.NewID(), 1.127 + UserID: uuid.NewID(), 1.128 + StripeCustomer: "default", 1.129 + Amount: -1, 1.130 + Period: MonthlyPeriod, 1.131 + Created: time.Now().Add(time.Hour * -24), 1.132 + BeginCharging: time.Now().Add(time.Hour * -24), 1.133 + LastCharged: time.Now().Add(time.Hour * -24), 1.134 + LastNotified: time.Now().Add(time.Hour * -24), 1.135 + InLockout: true, 1.136 + } 1.137 + sub2 := Subscription{ 1.138 + ID: uuid.NewID(), 1.139 + UserID: uuid.NewID(), 1.140 + StripeCustomer: "stripeCustomer2", 1.141 + Amount: -2, 1.142 + Period: MonthlyPeriod, 1.143 + Created: time.Now(), 1.144 + BeginCharging: time.Now(), 1.145 + LastCharged: time.Now(), 1.146 + LastNotified: time.Now(), 1.147 + InLockout: false, 1.148 + } 1.149 + 1.150 + for i := 1; i < variations; i++ { 1.151 + var stripeCustomer string 1.152 + var amount int 1.153 + var inLockout bool 1.154 + var per period 1.155 + var beginCharging, lastCharged, lastNotified time.Time 1.156 + 1.157 + change := SubscriptionChange{} 1.158 + empty := change.IsEmpty() 1.159 + if !empty { 1.160 + t.Errorf("Expected empty to be %t, was %t\n", true, empty) 1.161 + } 1.162 + expectation := sub 1.163 + result := sub 1.164 + strI := strconv.Itoa(i) 1.165 + 1.166 + if i&subscriptionChangeStripeCustomer != 0 { 1.167 + stripeCustomer = "stripeCustomer-" + strI 1.168 + change.StripeCustomer = &stripeCustomer 1.169 + expectation.StripeCustomer = stripeCustomer 1.170 + } 1.171 + 1.172 + if i&subscriptionChangeAmount != 0 { 1.173 + amount = i 1.174 + change.Amount = &amount 1.175 + expectation.Amount = amount 1.176 + } 1.177 + 1.178 + if i&subscriptionChangePeriod != 0 { 1.179 + per = period("period-" + strI) 1.180 + change.Period = &per 1.181 + expectation.Period = per 1.182 + } 1.183 + 1.184 + if i&subscriptionChangeBeginCharging != 0 { 1.185 + beginCharging = time.Now().Add(time.Hour * time.Duration(i)) 1.186 + change.BeginCharging = &beginCharging 1.187 + expectation.BeginCharging = beginCharging 1.188 + } 1.189 + 1.190 + if i&subscriptionChangeLastCharged != 0 { 1.191 + lastCharged = time.Now().Add(time.Hour * time.Duration(i)) 1.192 + change.LastCharged = &lastCharged 1.193 + expectation.LastCharged = lastCharged 1.194 + } 1.195 + 1.196 + if i&subscriptionChangeLastNotified != 0 { 1.197 + lastNotified = time.Now().Add(time.Hour * time.Duration(i)) 1.198 + change.LastNotified = &lastNotified 1.199 + expectation.LastNotified = lastNotified 1.200 + } 1.201 + 1.202 + if i&subscriptionChangeInLockout != 0 { 1.203 + inLockout = i%2 == 0 1.204 + change.InLockout = &inLockout 1.205 + expectation.InLockout = inLockout 1.206 + } 1.207 + 1.208 + empty = change.IsEmpty() 1.209 + if empty { 1.210 + t.Errorf("Expected empty to be %t, was %t\n", false, empty) 1.211 + } 1.212 + 1.213 + result.ApplyChange(change) 1.214 + match, field, expected, got := compareSubscriptions(expectation, result) 1.215 + if !match { 1.216 + t.Errorf("Expected field `%s` to be `%v`, got `%v`\n", field, expected, got) 1.217 + } 1.218 + for _, store := range testSubscriptionStores { 1.219 + err := store.reset() 1.220 + if err != nil { 1.221 + t.Fatalf("Error resetting %T: %+v\n", store, err) 1.222 + } 1.223 + err = store.createSubscription(sub) 1.224 + if err != nil { 1.225 + t.Fatalf("Error saving subscription in %T: %s\n", store, err) 1.226 + } 1.227 + err = store.updateSubscription(sub.ID, change) 1.228 + if err != nil { 1.229 + t.Errorf("Error updating subscription in %T: %s\n", store, err) 1.230 + } 1.231 + retrieved, err := store.getSubscriptions([]uuid.ID{sub.ID}) 1.232 + if err != nil { 1.233 + t.Errorf("Error getting subscription from %T: %s\n", store, err) 1.234 + } 1.235 + ok, missing := subscriptionMapContains(retrieved, sub) 1.236 + if !ok { 1.237 + t.Errorf("Expected to retrieve %s from %T, but missing was %+v\n", sub.ID.String(), store, missing) 1.238 + } 1.239 + match, field, expected, got = compareSubscriptions(expectation, retrieved[sub.ID.String()]) 1.240 + if !match { 1.241 + t.Errorf("Expected field `%s` to be `%v`, got `%v` from %T\n", field, expected, got, store) 1.242 + } 1.243 + } 1.244 + } 1.245 + for _, store := range testSubscriptionStores { 1.246 + err := store.reset() 1.247 + if err != nil { 1.248 + t.Fatalf("Error resetting %T: %+v\n", store, err) 1.249 + } 1.250 + err = store.createSubscription(sub) 1.251 + if err != nil { 1.252 + t.Fatalf("Error saving subscription in %T: %+v\n", store, err) 1.253 + } 1.254 + err = store.createSubscription(sub2) 1.255 + if err != nil { 1.256 + t.Fatalf("Error saving subscription in %T: %+v\n", store, err) 1.257 + } 1.258 + change := SubscriptionChange{} 1.259 + err = store.updateSubscription(sub.ID, change) 1.260 + if err != ErrSubscriptionChangeEmpty { 1.261 + t.Errorf("Expected err to be %+v, but got %+v from %T\n", ErrSubscriptionChangeEmpty, err, store) 1.262 + } 1.263 + stripeCustomer := sub2.StripeCustomer 1.264 + change.StripeCustomer = &stripeCustomer 1.265 + err = store.updateSubscription(uuid.NewID(), change) 1.266 + if err != ErrSubscriptionNotFound { 1.267 + t.Errorf("Expected err to be %+v, but got %+v from %T\n", ErrSubscriptionNotFound, err, store) 1.268 + } 1.269 + err = store.updateSubscription(sub.ID, change) 1.270 + if err != ErrStripeCustomerAlreadyExists { 1.271 + t.Errorf("Expected err to be %+v, but got %+v from %T\n", ErrStripeCustomerAlreadyExists, err, store) 1.272 + } 1.273 + } 1.274 +} 1.275 + 1.276 +func TestDeleteSubscription(t *testing.T) { 1.277 + for _, store := range testSubscriptionStores { 1.278 + err := store.reset() 1.279 + if err != nil { 1.280 + t.Fatalf("Error resetting %T: %+v\n", store, err) 1.281 + } 1.282 + sub1 := Subscription{ 1.283 + ID: uuid.NewID(), 1.284 + UserID: uuid.NewID(), 1.285 + StripeCustomer: "stripeCustomer1", 1.286 + } 1.287 + sub2 := Subscription{ 1.288 + ID: uuid.NewID(), 1.289 + UserID: uuid.NewID(), 1.290 + StripeCustomer: "stripeCustomer2", 1.291 + } 1.292 + err = store.createSubscription(sub1) 1.293 + if err != nil { 1.294 + t.Fatalf("Error creating %+v in %T: %+v\n", sub1, store, err) 1.295 + } 1.296 + err = store.createSubscription(sub2) 1.297 + if err != nil { 1.298 + t.Fatalf("Error creating %+v in %T: %+v\n", sub1, store, err) 1.299 + } 1.300 + err = store.deleteSubscription(sub1.ID) 1.301 + if err != nil { 1.302 + t.Fatalf("Error deleting %+v in %T: %+v\n", sub1, store, err) 1.303 + } 1.304 + retrieved, err := store.getSubscriptions([]uuid.ID{sub1.ID, sub2.ID}) 1.305 + if err != nil { 1.306 + t.Errorf("Error retrieving subscriptions from %T: %+v\n", store, err) 1.307 + } 1.308 + ok, missing := subscriptionMapContains(retrieved, sub1) 1.309 + if ok { 1.310 + t.Errorf("Expected not to retrieve %s from %T, but missing was %+v\n", sub1.ID.String(), store, missing) 1.311 + } 1.312 + ok, missing = subscriptionMapContains(retrieved, sub2) 1.313 + if !ok { 1.314 + t.Errorf("Expected to retrieve %s from %T, but missing was %+v\n", sub2.ID.String(), store, missing) 1.315 + } 1.316 + _, err = store.getSubscriptionByUser(sub1.UserID) 1.317 + if err != ErrSubscriptionNotFound { 1.318 + t.Errorf("Expected err to be %+v, but got %+v from %T\n", ErrSubscriptionNotFound, err, store) 1.319 + } 1.320 + err = store.deleteSubscription(sub1.ID) 1.321 + if err != ErrSubscriptionNotFound { 1.322 + t.Errorf("Expected err to be %+v, but got %+v from %T\n", ErrSubscriptionNotFound, err, store) 1.323 + } 1.324 + } 1.325 +} 1.326 + 1.327 +func TestListSubscriptionsLastChargedBefore(t *testing.T) { 1.328 + for _, store := range testSubscriptionStores { 1.329 + err := store.reset() 1.330 + if err != nil { 1.331 + t.Fatalf("Error resetting %T: %+v\n", store, err) 1.332 + } 1.333 + sub1 := Subscription{ 1.334 + ID: uuid.NewID(), 1.335 + UserID: uuid.NewID(), 1.336 + StripeCustomer: "stripeCustomer1", 1.337 + Amount: 200, 1.338 + Period: MonthlyPeriod, 1.339 + Created: time.Now().Add(time.Hour * -24 * 32), 1.340 + BeginCharging: time.Now().Add(time.Hour * -24), 1.341 + LastCharged: time.Now().Add(time.Hour * -24), 1.342 + } 1.343 + sub2 := Subscription{ 1.344 + ID: uuid.NewID(), 1.345 + UserID: uuid.NewID(), 1.346 + StripeCustomer: "stripeCustomer2", 1.347 + Amount: 300, 1.348 + Period: MonthlyPeriod, 1.349 + Created: time.Now().Add(time.Hour * -24 * 61), 1.350 + BeginCharging: time.Now().Add(time.Hour * -24 * 31), 1.351 + LastCharged: time.Now().Add(time.Hour * -24 * 31), 1.352 + } 1.353 + sub3 := Subscription{ 1.354 + ID: uuid.NewID(), 1.355 + UserID: uuid.NewID(), 1.356 + StripeCustomer: "stripeCustomer3", 1.357 + Amount: 100, 1.358 + Period: MonthlyPeriod, 1.359 + Created: time.Now().Add(time.Hour * -1), 1.360 + BeginCharging: time.Now().Add(time.Hour * 31), 1.361 + LastCharged: time.Time{}, 1.362 + } 1.363 + err = store.createSubscription(sub1) 1.364 + if err != nil { 1.365 + t.Fatalf("Error creating %+v in %T: %+v\n", sub1, store, err) 1.366 + } 1.367 + err = store.createSubscription(sub2) 1.368 + if err != nil { 1.369 + t.Fatalf("Error creating %+v in %T: %+v\n", sub1, store, err) 1.370 + } 1.371 + err = store.createSubscription(sub3) 1.372 + if err != nil { 1.373 + t.Fatalf("Error creating %+v in %T: %+v\n", sub1, store, err) 1.374 + } 1.375 + t.Logf("sub1: %+v\n", sub1) 1.376 + t.Logf("sub2: %+v\n", sub2) 1.377 + t.Logf("sub3: %+v\n", sub3) 1.378 + // subscriptions last charged before right now 1.379 + // should be sub1, sub2, and sub3 1.380 + results, err := store.listSubscriptionsLastChargedBefore(time.Now()) 1.381 + if err != nil { 1.382 + t.Errorf("Unexpected error listing subscriptions in %T: %+v\n", store, err) 1.383 + } 1.384 + if len(results) != 3 { 1.385 + t.Errorf("Expected three results from %T, got %+v\n", store, results) 1.386 + } 1.387 + ok, field, expected, result := compareSubscriptions(sub3, results[0]) 1.388 + if !ok { 1.389 + t.Errorf("Expected %s in pos 0 to be %+v, got %+v from %T", field, expected, result, store) 1.390 + } 1.391 + ok, field, expected, result = compareSubscriptions(sub2, results[1]) 1.392 + if !ok { 1.393 + t.Errorf("Expected %s in pos 1 to be %+v, got %+v from %T", field, expected, result, store) 1.394 + } 1.395 + ok, field, expected, result = compareSubscriptions(sub1, results[2]) 1.396 + if !ok { 1.397 + t.Errorf("Expected %s in pos 2 to be %+v, got %+v from %T", field, expected, result, store) 1.398 + } 1.399 + // subscriptions last charged before a week ago 1.400 + // should be sub2, sub3 1.401 + results, err = store.listSubscriptionsLastChargedBefore(time.Now().Add(time.Hour * -24 * 7)) 1.402 + if err != nil { 1.403 + t.Errorf("Unexpected error listing subscriptions in %T: %+v\n", store, err) 1.404 + } 1.405 + if len(results) != 2 { 1.406 + t.Errorf("Expected two results from %T, got %+v\n", store, results) 1.407 + } 1.408 + ok, field, expected, result = compareSubscriptions(sub3, results[0]) 1.409 + if !ok { 1.410 + t.Errorf("Expected %s in pos 0 to be %+v, got %+v from %T", field, expected, result, store) 1.411 + } 1.412 + ok, field, expected, result = compareSubscriptions(sub2, results[1]) 1.413 + if !ok { 1.414 + t.Errorf("Expected %s in pos 1 to be %+v, got %+v from %T", field, expected, result, store) 1.415 + } 1.416 + // subscriptions last charged before 32 days ago 1.417 + // should be sub3 1.418 + results, err = store.listSubscriptionsLastChargedBefore(time.Now().Add(time.Hour * -24 * 32)) 1.419 + if err != nil { 1.420 + t.Errorf("Unexpected error listing subscriptions in %T: %+v\n", store, err) 1.421 + } 1.422 + if len(results) != 1 { 1.423 + t.Errorf("Expected one result from %T, got %+v\n", store, results) 1.424 + } 1.425 + ok, field, expected, result = compareSubscriptions(sub3, results[0]) 1.426 + if !ok { 1.427 + t.Errorf("Expected %s in pos 0 to be %+v, got %+v from %T", field, expected, result, store) 1.428 + } 1.429 + } 1.430 +} 1.431 + 1.432 +func TestGetSubscriptions(t *testing.T) { 1.433 + for _, store := range testSubscriptionStores { 1.434 + err := store.reset() 1.435 + if err != nil { 1.436 + t.Fatalf("Error resetting %T: %+v\n", store, err) 1.437 + } 1.438 + sub1 := Subscription{ 1.439 + ID: uuid.NewID(), 1.440 + UserID: uuid.NewID(), 1.441 + StripeCustomer: "stripeCustomer1", 1.442 + Amount: 200, 1.443 + Period: MonthlyPeriod, 1.444 + Created: time.Now(), 1.445 + BeginCharging: time.Now().Add(time.Hour), 1.446 + } 1.447 + sub2 := Subscription{ 1.448 + ID: uuid.NewID(), 1.449 + UserID: uuid.NewID(), 1.450 + StripeCustomer: "stripeCustomer2", 1.451 + Amount: 300, 1.452 + Period: MonthlyPeriod, 1.453 + Created: time.Now().Add(time.Hour * -720), 1.454 + BeginCharging: time.Now().Add(time.Hour*-720 + time.Hour*2), 1.455 + LastCharged: time.Now(), 1.456 + } 1.457 + sub3 := Subscription{ 1.458 + ID: uuid.NewID(), 1.459 + UserID: uuid.NewID(), 1.460 + StripeCustomer: "stripeCustomer3", 1.461 + Amount: 100, 1.462 + Period: MonthlyPeriod, 1.463 + Created: time.Now().Add(time.Hour * -1440), 1.464 + BeginCharging: time.Now().Add(time.Hour * -1440), 1.465 + LastNotified: time.Now().Add(time.Hour * -720), 1.466 + InLockout: true, 1.467 + } 1.468 + err = store.createSubscription(sub1) 1.469 + if err != nil { 1.470 + t.Fatalf("Error creating %+v in %T: %+v\n", sub1, store, err) 1.471 + } 1.472 + err = store.createSubscription(sub2) 1.473 + if err != nil { 1.474 + t.Fatalf("Error creating %+v in %T: %+v\n", sub1, store, err) 1.475 + } 1.476 + err = store.createSubscription(sub3) 1.477 + if err != nil { 1.478 + t.Fatalf("Error creating %+v in %T: %+v\n", sub1, store, err) 1.479 + } 1.480 + retrieved, err := store.getSubscriptions([]uuid.ID{}) 1.481 + if err != ErrNoSubscriptionID { 1.482 + t.Errorf("Error retrieving no subscriptions from %T. Expected %+v, got %+v\n", store, ErrNoSubscriptionID, err) 1.483 + } 1.484 + retrieved, err = store.getSubscriptions([]uuid.ID{sub1.ID}) 1.485 + if err != nil { 1.486 + t.Errorf("Error retrieving %s from %T: %+v\n", sub1.ID, store, err) 1.487 + } 1.488 + ok, missing := subscriptionMapContains(retrieved, sub1) 1.489 + if !ok { 1.490 + t.Logf("Results: %+v\n", retrieved) 1.491 + t.Errorf("Expected %+v to be in the results, was not for %T.\n", missing, store) 1.492 + } 1.493 + retrieved, err = store.getSubscriptions([]uuid.ID{sub1.ID, sub2.ID}) 1.494 + if err != nil { 1.495 + t.Errorf("Error retrieving %s and %s from %T: %+v\n", sub1.ID, sub2.ID, store, err) 1.496 + } 1.497 + ok, missing = subscriptionMapContains(retrieved, sub1, sub2) 1.498 + if !ok { 1.499 + t.Logf("Results: %+v\n", retrieved) 1.500 + t.Errorf("Expected %+v to be in the results, was not for %T.\n", missing, store) 1.501 + } 1.502 + retrieved, err = store.getSubscriptions([]uuid.ID{sub1.ID, sub3.ID}) 1.503 + if err != nil { 1.504 + t.Errorf("Error retrieving %s and %s from %T: %+v\n", sub1.ID, sub3.ID, store, err) 1.505 + } 1.506 + ok, missing = subscriptionMapContains(retrieved, sub1, sub3) 1.507 + if !ok { 1.508 + t.Logf("Results: %+v\n", retrieved) 1.509 + t.Errorf("Expected %+v to be in the results, was not for %T.\n", missing, store) 1.510 + } 1.511 + retrieved, err = store.getSubscriptions([]uuid.ID{sub1.ID, sub2.ID, sub3.ID}) 1.512 + if err != nil { 1.513 + t.Errorf("Error retrieving %s, %s, and %s from %T: %+v\n", sub1.ID, sub2.ID, sub3.ID, store, err) 1.514 + } 1.515 + ok, missing = subscriptionMapContains(retrieved, sub1, sub2, sub3) 1.516 + if !ok { 1.517 + t.Logf("Results: %+v\n", retrieved) 1.518 + t.Errorf("Expected %+v to be in the results, was not for %T.\n", missing, store) 1.519 + } 1.520 + retrieved, err = store.getSubscriptions([]uuid.ID{sub2.ID}) 1.521 + if err != nil { 1.522 + t.Errorf("Error retrieving %s from %T: %+v\n", sub2.ID, store, err) 1.523 + } 1.524 + ok, missing = subscriptionMapContains(retrieved, sub2) 1.525 + if !ok { 1.526 + t.Logf("Results: %+v\n", retrieved) 1.527 + t.Errorf("Expected %+v to be in the results, was not for %T.\n", missing, store) 1.528 + } 1.529 + retrieved, err = store.getSubscriptions([]uuid.ID{sub2.ID, sub3.ID}) 1.530 + if err != nil { 1.531 + t.Errorf("Error retrieving %s and %s from %T: %+v\n", sub2.ID, sub3.ID, store, err) 1.532 + } 1.533 + ok, missing = subscriptionMapContains(retrieved, sub2, sub3) 1.534 + if !ok { 1.535 + t.Logf("Results: %+v\n", retrieved) 1.536 + t.Errorf("Expected %+v to be in the results, was not for %T.\n", missing, store) 1.537 + } 1.538 + retrieved, err = store.getSubscriptions([]uuid.ID{sub3.ID}) 1.539 + if err != nil { 1.540 + t.Errorf("Error retrieving %s from %T: %+v\n", sub3.ID, store, err) 1.541 + } 1.542 + ok, missing = subscriptionMapContains(retrieved, sub3) 1.543 + if !ok { 1.544 + t.Logf("Results: %+v\n", retrieved) 1.545 + t.Errorf("Expected %+v to be in the results, was not for %T.\n", missing, store) 1.546 + } 1.547 + retrieved, err = store.getSubscriptions([]uuid.ID{uuid.NewID()}) 1.548 + if err != nil { 1.549 + t.Errorf("Error retrieving non-existent ID from %T: %+v\n", store, err) 1.550 + } 1.551 + if len(retrieved) != 0 { 1.552 + t.Errorf("Expected no results, %T returned %+v\n", store, retrieved) 1.553 + } 1.554 + retrieved, err = store.getSubscriptions([]uuid.ID{sub1.ID, sub2.ID, uuid.NewID(), sub3.ID}) 1.555 + if err != nil { 1.556 + t.Errorf("Error retrieving non-existent ID from %T: %+v\n", store, err) 1.557 + } 1.558 + if len(retrieved) != 3 { 1.559 + t.Errorf("Expected 3 results, %T returned %+v\n", store, retrieved) 1.560 + } 1.561 + ok, missing = subscriptionMapContains(retrieved, sub1, sub2, sub3) 1.562 + if !ok { 1.563 + t.Logf("Results: %+v\n", retrieved) 1.564 + t.Errorf("Expected %+v to be in the results, was not for %T.\n", missing, store) 1.565 + } 1.566 + } 1.567 +} 1.568 + 1.569 +func TestGetSubscriptionByUser(t *testing.T) { 1.570 + for _, store := range testSubscriptionStores { 1.571 + err := store.reset() 1.572 + if err != nil { 1.573 + t.Fatalf("Error resetting %T: %+v\n", store, err) 1.574 + } 1.575 + sub1 := Subscription{ 1.576 + ID: uuid.NewID(), 1.577 + UserID: uuid.NewID(), 1.578 + StripeCustomer: "stripeCustomer1", 1.579 + } 1.580 + sub2 := Subscription{ 1.581 + ID: uuid.NewID(), 1.582 + UserID: uuid.NewID(), 1.583 + StripeCustomer: "stripeCustomer2", 1.584 + } 1.585 + sub3 := Subscription{ 1.586 + ID: uuid.NewID(), 1.587 + UserID: uuid.NewID(), 1.588 + StripeCustomer: "stripeCustomer3", 1.589 + } 1.590 + err = store.createSubscription(sub1) 1.591 + if err != nil { 1.592 + t.Fatalf("Error creating %+v in %T: %+v\n", sub1, store, err) 1.593 + } 1.594 + err = store.createSubscription(sub2) 1.595 + if err != nil { 1.596 + t.Fatalf("Error creating %+v in %T: %+v\n", sub1, store, err) 1.597 + } 1.598 + err = store.createSubscription(sub3) 1.599 + if err != nil { 1.600 + t.Fatalf("Error creating %+v in %T: %+v\n", sub1, store, err) 1.601 + } 1.602 + retrieved, err := store.getSubscriptionByUser(sub1.UserID) 1.603 + if err != nil { 1.604 + t.Errorf("Error retrieving subscription %+v from %T: %+v\n", sub1, store, err) 1.605 + } 1.606 + ok, field, expected, result := compareSubscriptions(sub1, retrieved) 1.607 + if !ok { 1.608 + t.Errorf("Expected %s to be %+v, but was %+v in %T\n", field, expected, result, store) 1.609 + } 1.610 + retrieved, err = store.getSubscriptionByUser(sub2.UserID) 1.611 + if err != nil { 1.612 + t.Errorf("Error retrieving subscription %+v from %T: %+v\n", sub2, store, err) 1.613 + } 1.614 + ok, field, expected, result = compareSubscriptions(sub2, retrieved) 1.615 + if !ok { 1.616 + t.Errorf("Expected %s to be %+v, but was %+v in %T\n", field, expected, result, store) 1.617 + } 1.618 + retrieved, err = store.getSubscriptionByUser(sub3.UserID) 1.619 + if err != nil { 1.620 + t.Errorf("Error retrieving subscription %+v from %T: %+v\n", sub3, store, err) 1.621 + } 1.622 + ok, field, expected, result = compareSubscriptions(sub3, retrieved) 1.623 + if !ok { 1.624 + t.Errorf("Expected %s to be %+v, but was %+v in %T\n", field, expected, result, store) 1.625 + } 1.626 + retrieved, err = store.getSubscriptionByUser(uuid.NewID()) 1.627 + if err != ErrSubscriptionNotFound { 1.628 + t.Errorf("Expected err to be %+v, got %+v from %T\n", ErrSubscriptionNotFound, err, store) 1.629 + } 1.630 + } 1.631 +}