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
| var mongoURI string
func TestResolverAccountID(t *testing.T) { c := context.Background() s := mongoURI mc, err := mongo.Connect(c, options.Client().ApplyURI(s)) if err != nil { t.Fatalf("cannot connection db: %v", err) } m := NewMongo(mc.Database("coolcar")) m.col.InsertMany(c, []interface{}{ bson.M{ "_id": mustObjID("621ecd8d42ffa4ec7cf1a22c"), "open_id": "open_1", }, bson.M{ "_id": mustObjID("621ecd8d42ffa4ec7cf1a23c"), "open_id": "open_2", }, bson.M{ "_id": mustObjID("621ecd8d42ffa4ec7cf1a24c"), "open_id": "open_3", }, }) cases := []struct { name string openID string want string }{ { name: "existing_user", openID: "open_1", want: "621ecd8d42ffa4ec7cf1a22c", }, { name: "another_existing_user", openID: "open_2", want: "621ecd8d42ffa4ec7cf1a23c", }, { name: "new_user", openID: "open_3", want: "621ecd8d42ffa4ec7cf1a24c", }, } for _, cc := range cases { t.Run(cc.name, func(t *testing.T) { id, err := m.ResolverAccountID(context.Background(), cc.openID) if err != nil { t.Errorf("faild resolved account id %s: %v", cc.openID, err) } if id != cc.want { t.Errorf("resolve account id : want: %q, got: %q", cc.want, id) } }) } id, err := m.ResolverAccountID(c, "123") if err != nil { t.Errorf("faild resolved account id 123: %v", err) } else { want := "621ecd8d42ffa4ec7cf1a22c" if id != want { t.Errorf("resolve account id : want: %q, got: %q", want, id) } }
}
func mustObjID(hex string) primitive.ObjectID { oi, _ := primitive.ObjectIDFromHex(hex) return oi }
func TestMain(m *testing.M) { os.Exit(mongotesting.RunWithMongoInDocker(m, &mongoURI)) }
|