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 136 137 138 139 140 141 142 143 144 145 146 147 148
| package car
import ( "context" carpb "coolcar/car/api/gen/v1" "coolcar/car/dao" "coolcar/shared/id" mgo "coolcar/shared/mongo" mongotesting "coolcar/shared/mongo/testing" "encoding/json" "fmt" "os" "testing"
"go.uber.org/zap" )
func TestCarUpdate(t *testing.T) { c := context.Background() mc, err := mongotesting.NewClient(c) if err != nil { t.Fatalf("cannot create mongo client: %v", err) } logger, err := zap.NewDevelopment() if err != nil { t.Fatalf("cannot create logger : %v", err) } s := &Service{ Logger: logger, Mongo: dao.NewMongo(mc.Database("coolcar")), } carID := id.CarID("521c60186800fc9e2ca14801") mgo.NewObjIDWithValue(carID) car1, err := s.CreateCar(c, &carpb.CreateCarRequest{}) if err != nil { t.Fatalf("cannot create car: %v", err) } fmt.Println(car1) cases := []struct { name string op func() error want string wantErr bool }{ { name: "get_car", op: func() error { return nil }, want: `{"status":1,"position":{"latitude":30,"longitude":120}}`, }, { name: "unlock_car", op: func() error { _, err := s.UnlockCar(c, &carpb.UnlockCarRequest{ Id: carID.String(), TripId: "test_tripID", Driver: &carpb.Driver{ Id: "test_driver", AvatarUrl: "test_avatarUrl", }, }) return err }, want: `{"status":2,"driver":{"id":"test_driver","avatar_url":"test_avatarUrl"},"position":{"latitude":30,"longitude":120},"trip_id":"test_tripID"}`, }, { name: "unlock_complete", op: func() error { _, err := s.UpdateCar(c, &carpb.UpdateCarRequest{ Id: carID.String(), Position: &carpb.Location{ Latitude: 31, Longitude: 120, }, Status: carpb.CarStatus_UNLOCKED, }) return err }, want: `{"status":3,"driver":{"id":"test_driver","avatar_url":"test_avatarUrl"},"position":{"latitude":31,"longitude":120},"trip_id":"test_tripID"}`, }, { name: "unlock_car_by_another_driver", op: func() error { _, err := s.UnlockCar(c, &carpb.UnlockCarRequest{ Id: carID.String(), TripId: "bad_tripID", Driver: &carpb.Driver{ Id: "bad_driver", AvatarUrl: "bad_avatarUrl", }, }) return err }, wantErr: true, }, { name: "lock_car", op: func() error { _, err := s.LockCar(c, &carpb.LockCarRequest{ Id: carID.String(), }) return err }, want: `{"status":4,"driver":{"id":"test_driver","avatar_url":"test_avatarUrl"},"position":{"latitude":31,"longitude":120},"trip_id":"test_tripID"}`, }, { name: "lock_complete", op: func() error { _, err := s.UpdateCar(c, &carpb.UpdateCarRequest{ Id: carID.String(), Status: carpb.CarStatus_LOCKED, }) return err }, want: `{"status":1,"driver":{},"position":{"latitude":31,"longitude":120}}`, }, }
for _, cc := range cases { err := cc.op() if cc.wantErr { if err == nil { t.Errorf("%s: want error; got node", cc.name) } else { continue } } car, err := s.GetCar(c, &carpb.GetCarRequest{ Id: carID.String(), }) if err != nil { t.Errorf("%s: operation faild: %v", cc.name, err) continue } b, err := json.Marshal(car) if err != nil { t.Errorf("%s:faild marshal response: %v", cc.name, err) } got := string(b) if cc.want != got { t.Errorf("%s:incorrect response; want: %s, got: %s", cc.name, cc.want, got) } } } func TestMain(m *testing.M) { os.Exit(mongotesting.RunWithMongoInDocker(m)) }
|