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 135
| func TestCreateTrip(t *testing.T) { c := context.Background() mc, err := mongotesting.NewClient(c) if err != nil { t.Fatalf("cannot connection db: %v", err) } db := mc.Database("coolcar") err = mongotesting.SetupIndex(c, db) if err != nil { t.Fatalf("cannot SetupIndex : %v", err) } m := NewMongo(db)
cases := []struct { name string tripID string accountID string tripStatus rentalpb.TripStatus wantErr bool }{ { name: "finised", tripID: "622c60186800fc9e2ca1480f", accountID: "account1", tripStatus: rentalpb.TripStatus_FINISHED, }, { name: "another_finised", tripID: "622c60186800fc9e2ca1480d", accountID: "account1", tripStatus: rentalpb.TripStatus_FINISHED, }, { name: "in_progress", tripID: "622c60186800fc9e2ca1480c", accountID: "account1", tripStatus: rentalpb.TripStatus_IN_PROGRES, }, { name: "another_in_progress", tripID: "622c60186800fc9e2ca1480e", accountID: "account1", tripStatus: rentalpb.TripStatus_IN_PROGRES, wantErr: true, }, { name: "in_progress_by_another_account", tripID: "622c60186800fc9e2ca1481e", accountID: "account2", tripStatus: rentalpb.TripStatus_IN_PROGRES, }, }
for _, cc := range cases { mgo.NewObjID = func() primitive.ObjectID { return objid.MustFromID(id.TripID(cc.tripID)) } tr, err := m.CreateTrip(c, &rentalpb.Trip{ AccountID: cc.accountID, Status: cc.tripStatus, }) if cc.wantErr { if err == nil { t.Errorf("%s:error expected; got none", cc.name) } continue } if err != nil { t.Errorf("%s:error creating trip: %v", cc.name, err) continue }
if tr.ID.Hex() != cc.tripID { t.Errorf("%s: incorrect trip id;want: %q;got:%q", cc.name, cc.tripID, tr.ID.Hex()) } }
} func TestGetTrip(t *testing.T) { c := context.Background() mc, err := mongotesting.NewDefalutClient(c) if err != nil { t.Fatalf("cannot connection db: %v", err) } m := NewMongo(mc.Database("coolcar")) acct := "account1" mgo.NewObjID = primitive.NewObjectID trip, err := m.CreateTrip(c, &rentalpb.Trip{ AccountID: acct, CarID: "car1", Start: &rentalpb.LocationStatus{ PoiName: "startpoint", Location: &rentalpb.Location{ Latitude: 30, Longitude: 120, }, }, End: &rentalpb.LocationStatus{ PoiName: "endpoint", FeeCent: 10000, KmDriven: 100000, Location: &rentalpb.Location{ Latitude: 35, Longitude: 125, }, }, Current: &rentalpb.LocationStatus{ PoiName: "currentpoint", FeeCent: 10000, KmDriven: 100000, Location: &rentalpb.Location{ Latitude: 35, Longitude: 125, }, }, Status: rentalpb.TripStatus_FINISHED, }) if err != nil { t.Errorf("cannot create trip: %v", err) } got, err := m.GetTrip(c, objid.ToTripID(trip.ID), id.AccountID(acct)) if err != nil { t.Errorf("cannot get trip : %v", err) } if diff := cmp.Diff(trip, got, protocmp.Transform()); diff != "" { t.Errorf("result differs;-want +got: %s", diff) } } func TestMain(m *testing.M) { os.Exit(mongotesting.RunWithMongoInDocker(m)) }
|