Best Ginkgo code snippet using ginkgo.GinkgoT
lock_watcher_test.go
Source:lock_watcher_test.go
...44 serviceName,45 keyName,46 )47 48 mockAgent.AssertExpectations(GinkgoT())49 Expect(receiver).NotTo(BeNil())50 Expect(err).To(BeNil())51 52 // replace with a fresh instance53 mockAgent = consulmocks.MockAgent{}54 55 // not used in setup56 mockSession = consulmocks.MockSession{}57 mockKV = consulmocks.MockKV{}58 mockHealth = consulmocks.MockHealth{}59 })60 61 registersService := func() {62 // expect the call to ServiceRegister63 mockAgent.On(64 "ServiceRegister",65 mock.AnythingOfType("*consulapi.AgentServiceRegistration"),66 ).Return(nil)67 68 // invoke the object under test69 receiver.RegisterService()70 71 mockAgent.AssertExpectations(GinkgoT())72 73 // retrieve the call and its arguments74 svcRegCall := mockAgent.Calls[0]75 Expect(svcRegCall.Method).To(Equal("ServiceRegister"))76 77 svcReg := svcRegCall.Arguments.Get(0).(*consulapi.AgentServiceRegistration)78 79 // verify the service registration80 Expect(svcReg.ID).To(Equal(serviceName))81 Expect(svcReg.Name).To(Equal(serviceName))82 Expect(svcReg.Check.TTL).To(Equal("306s")) // 3 times the update interval83 }84 85 It("registers the service", registersService)86 87 passesHealthCheck := func() {88 mockAgent.On("PassTTL", "service:" + serviceName, "").Return(nil)89 90 receiver.UpdateHealthCheck()91 92 mockAgent.AssertExpectations(GinkgoT())93 }94 95 It("passes a health check", passesHealthCheck)96 97 initsNewSession := func() {98 var nilQueryMeta *consulapi.QueryMeta = nil99 100 // searching for an existing session101 mockSession.On(102 "List",103 mock.AnythingOfType("*consulapi.QueryOptions"),104 ).Return([]*consulapi.SessionEntry{}, nilQueryMeta, nil)105 106 // health check must be passing before creating a session tied to that107 // health check108 mockAgent.On("PassTTL", "service:" + serviceName, "").Return(nil)109 110 // create the session111 mockSession.On(112 "Create",113 mock.AnythingOfType("*consulapi.SessionEntry"),114 mock.AnythingOfType("*consulapi.WriteOptions"),115 ).Return(sessionID, &consulapi.WriteMeta{}, nil)116 117 // set it off118 newSessionId, err := receiver.InitSession()119 120 mockAgent.AssertExpectations(GinkgoT())121 122 Expect(newSessionId).To(Equal(sessionID))123 Expect(err).To(BeNil())124 125 // verify call to Session.Create()126 sessCreateCall := mockSession.Calls[1]127 Expect(sessCreateCall.Method).To(Equal("Create"))128 129 sess := sessCreateCall.Arguments.Get(0).(*consulapi.SessionEntry)130 131 // verify the session create request132 Expect(sess.Name).To(Equal(serviceName))133 Expect(sess.LockDelay).To(Equal(lockDelay))134 Expect(len(sess.Checks)).To(Equal(2))135 Expect(sess.Checks).To(ContainElement("serfHealth"))136 Expect(sess.Checks).To(ContainElement("service:" + serviceName))137 }138 139 It("initializes a new session", initsNewSession)140 141 It("finds an existing session", func() {142 sessionID := "42"143 144 // searching for an existing session145 mockSession.On(146 "List",147 mock.AnythingOfType("*consulapi.QueryOptions"),148 ).Return(149 []*consulapi.SessionEntry{150 &consulapi.SessionEntry{151 Node: "some-other-node",152 Name: "some-other-name",153 },154 &consulapi.SessionEntry{155 Node: nodeName,156 Name: "some-other-name",157 },158 &consulapi.SessionEntry{159 Node: "some-other-node",160 Name: serviceName,161 },162 &consulapi.SessionEntry{ // this is the one!163 Node: nodeName,164 Name: serviceName,165 ID: sessionID,166 },167 },168 new(consulapi.QueryMeta),169 nil,170 )171 172 // set it off173 existingSessionId, err := receiver.InitSession()174 175 mockAgent.AssertExpectations(GinkgoT())176 177 Expect(existingSessionId).To(Equal(sessionID))178 Expect(err).To(BeNil())179 })180 Describe("lock acquisition", func() {181 validSession := &consulapi.SessionEntry{}182 genericQueryOpts := mock.AnythingOfType("*consulapi.QueryOptions")183 BeforeEach(func() {184 initsNewSession()185 })186 187 // if the session is invalid, return error188 // if the key's already locked by this session, return true.189 // if the key's locked by someone else, return false.190 // if the key's not locked, try to acquire it and return result191 It("aborts if the session is invalid", func() {192 mockSession.On("Info", sessionID, genericQueryOpts).Return(193 nil,194 new(consulapi.QueryMeta),195 nil,196 )197 198 _, err := receiver.AcquireLock()199 200 mockSession.AssertExpectations(GinkgoT())201 mockKV.AssertExpectations(GinkgoT())202 Expect(err).NotTo(BeNil())203 })204 205 It("is already locked by us", func() {206 mockSession.On("Info", sessionID, genericQueryOpts).Return(207 validSession,208 new(consulapi.QueryMeta),209 nil,210 )211 mockKV.On("Get", keyName, genericQueryOpts).Return(212 &consulapi.KVPair{213 Key: keyName,214 Session: sessionID,215 },216 new(consulapi.QueryMeta),217 nil,218 )219 220 success, err := receiver.AcquireLock()221 222 mockSession.AssertExpectations(GinkgoT())223 mockKV.AssertExpectations(GinkgoT())224 225 Expect(success).To(Equal(true))226 Expect(err).To(BeNil())227 })228 229 It("is locked by someone else", func() {230 mockSession.On("Info", sessionID, genericQueryOpts).Return(231 validSession,232 new(consulapi.QueryMeta),233 nil,234 )235 mockKV.On("Get", keyName, genericQueryOpts).Return(236 &consulapi.KVPair{237 Key: keyName,238 Session: "some-other-session",239 },240 new(consulapi.QueryMeta),241 nil,242 )243 244 success, err := receiver.AcquireLock()245 246 mockSession.AssertExpectations(GinkgoT())247 mockKV.AssertExpectations(GinkgoT())248 249 Expect(success).To(Equal(false))250 Expect(err).To(BeNil())251 })252 It("is able to be successfully locked", func() {253 mockSession.On("Info", sessionID, genericQueryOpts).Return(254 validSession,255 new(consulapi.QueryMeta),256 nil,257 )258 mockKV.On("Get", keyName, genericQueryOpts).Return(259 &consulapi.KVPair{260 Key: keyName,261 Session: "",262 },263 new(consulapi.QueryMeta),264 nil,265 )266 267 mockKV.On(268 "Acquire",269 mock.AnythingOfType("*consulapi.KVPair"),270 mock.AnythingOfType("*consulapi.WriteOptions"),271 ).Return(true, new(consulapi.WriteMeta), nil)272 273 success, err := receiver.AcquireLock()274 275 mockSession.AssertExpectations(GinkgoT())276 mockKV.AssertExpectations(GinkgoT())277 278 // verify call to KV.Acquire()279 kvAcquire := mockKV.Calls[1]280 Expect(kvAcquire.Method).To(Equal("Acquire"))281 282 kvp := kvAcquire.Arguments.Get(0).(*consulapi.KVPair)283 Expect(kvp.Key).To(Equal(keyName))284 Expect(kvp.Session).To(Equal(sessionID))285 286 Expect(success).To(Equal(true))287 Expect(err).To(BeNil())288 })289 It("is not able to be successfully locked", func() {290 mockSession.On("Info", sessionID, genericQueryOpts).Return(291 validSession,292 new(consulapi.QueryMeta),293 nil,294 )295 mockKV.On("Get", keyName, genericQueryOpts).Return(296 &consulapi.KVPair{297 Key: keyName,298 Session: "",299 },300 new(consulapi.QueryMeta),301 nil,302 )303 304 mockKV.On(305 "Acquire",306 mock.AnythingOfType("*consulapi.KVPair"),307 mock.AnythingOfType("*consulapi.WriteOptions"),308 ).Return(false, new(consulapi.WriteMeta), nil)309 310 success, err := receiver.AcquireLock()311 312 mockSession.AssertExpectations(GinkgoT())313 mockKV.AssertExpectations(GinkgoT())314 315 // verify call to KV.Acquire()316 kvAcquire := mockKV.Calls[1]317 Expect(kvAcquire.Method).To(Equal("Acquire"))318 319 kvp := kvAcquire.Arguments.Get(0).(*consulapi.KVPair)320 Expect(kvp.Key).To(Equal(keyName))321 Expect(kvp.Session).To(Equal(sessionID))322 323 Expect(success).To(Equal(false))324 Expect(err).To(BeNil())325 })326 327 It("subsequent acquires are blocking queries", func() {328 mockSession.On("Info", sessionID, genericQueryOpts).Return(329 validSession,330 new(consulapi.QueryMeta),331 nil,332 )333 // initial Acquire(): locked by someone else334 mockKV.On("Get", keyName, genericQueryOpts).Return(335 &consulapi.KVPair{336 Key: keyName,337 Session: "some-other-session",338 },339 &consulapi.QueryMeta{340 LastIndex: 10,341 },342 nil,343 ).Once()344 345 // next Acquire(): still locked, but uses previous LastIndex to it blocks346 mockKV.On("Get", keyName, genericQueryOpts).Return(347 &consulapi.KVPair{348 Key: keyName,349 Session: "some-other-session",350 },351 new(consulapi.QueryMeta),352 nil,353 ).Once()354 success, err := receiver.AcquireLock()355 success, err = receiver.AcquireLock()356 Expect(success).To(Equal(false))357 Expect(err).To(BeNil())358 mockSession.AssertExpectations(GinkgoT())359 mockKV.AssertExpectations(GinkgoT())360 361 // verify calls to KV.Get()362 var kvGet mock.Call363 var queryOpts *consulapi.QueryOptions364 365 // first call366 kvGet = mockKV.Calls[0]367 Expect(kvGet.Method).To(Equal("Get"))368 369 queryOpts = kvGet.Arguments.Get(1).(*consulapi.QueryOptions)370 Expect(queryOpts).NotTo(BeNil())371 Expect(queryOpts.WaitIndex).To(Equal(uint64(0)))372 Expect(queryOpts.WaitTime).To(Equal(lockDelay))373 // second call374 kvGet = mockKV.Calls[1]375 Expect(kvGet.Method).To(Equal("Get"))376 377 queryOpts = kvGet.Arguments.Get(1).(*consulapi.QueryOptions)378 Expect(queryOpts).NotTo(BeNil())379 Expect(queryOpts.WaitIndex).To(Equal(uint64(10)))380 Expect(queryOpts.WaitTime).To(Equal(lockDelay))381 })382 383 // AcquireLock can return false 384 Describe("handles errors gracefully", func() {385 It("returns false when unable to retrieve session info", func() {386 mockSession.On("Info", sessionID, genericQueryOpts).Return(387 nil,388 nil,389 fmt.Errorf("Unexpected response code: 500 (rpc error: No cluster leader)"),390 )391 392 locked, err := receiver.AcquireLock()393 394 mockSession.AssertExpectations(GinkgoT())395 mockKV.AssertExpectations(GinkgoT())396 Expect(locked).To(Equal(false))397 Expect(err).To(BeNil())398 })399 400 It("returns false when unable to retrieve key", func() {401 mockSession.On("Info", sessionID, genericQueryOpts).Return(402 validSession,403 new(consulapi.QueryMeta),404 nil,405 )406 mockKV.On("Get", keyName, genericQueryOpts).Return(407 nil,408 nil,409 fmt.Errorf("Unexpected response code: 500 (rpc error: No cluster leader)"),410 )411 412 locked, err := receiver.AcquireLock()413 414 mockSession.AssertExpectations(GinkgoT())415 mockKV.AssertExpectations(GinkgoT())416 Expect(locked).To(Equal(false))417 Expect(err).To(BeNil())418 })419 420 It("returns false when error acquiring lock on key", func() {421 mockSession.On("Info", sessionID, genericQueryOpts).Return(422 validSession,423 new(consulapi.QueryMeta),424 nil,425 )426 mockKV.On("Get", keyName, genericQueryOpts).Return(427 &consulapi.KVPair{428 Key: keyName,429 Session: "",430 },431 new(consulapi.QueryMeta),432 nil,433 )434 435 mockKV.On(436 "Acquire",437 mock.AnythingOfType("*consulapi.KVPair"),438 mock.AnythingOfType("*consulapi.WriteOptions"),439 ).Return(440 false,441 nil,442 fmt.Errorf("Unexpected response code: 500 (rpc error: No cluster leader)"),443 )444 445 locked, err := receiver.AcquireLock()446 447 mockSession.AssertExpectations(GinkgoT())448 mockKV.AssertExpectations(GinkgoT())449 Expect(locked).To(Equal(false))450 Expect(err).To(BeNil())451 })452 })453 })454 Describe("lock watching", func() {455 BeforeEach(func() {456 initsNewSession()457 })458 459 It("returns immediately if we don't have the lock", func(done Done) {460 resultKvp := consulapi.KVPair{461 Key: keyName,462 Session: "",463 }464 465 mockKV.On(466 "Get",467 keyName,468 mock.AnythingOfType("*consulapi.QueryOptions"),469 ).Return(470 &resultKvp,471 new(consulapi.QueryMeta),472 nil,473 )474 // channel used to notify when lock has been lost; it'll just get475 // closed476 c := receiver.WatchLock()477 478 // wait for the lock to be lost479 select {480 case _, more := <-c:481 Expect(more).To(Equal(false))482 }483 484 mockKV.AssertExpectations(GinkgoT())485 // test's done *bing!*486 close(done)487 })488 489 It("polls the key until the lock is gone", func(done Done) {490 genericQueryOpts := mock.AnythingOfType("*consulapi.QueryOptions")491 492 // first call; we have the lock493 mockKV.On("Get", keyName, genericQueryOpts).Return(494 &consulapi.KVPair{495 Key: keyName,496 Session: sessionID,497 },498 &consulapi.QueryMeta{499 LastIndex: 10,500 },501 nil,502 ).Once()503 504 // 2nd call; lost lock505 mockKV.On("Get", keyName, genericQueryOpts).Return(506 &consulapi.KVPair{507 Key: keyName,508 Session: "",509 },510 new(consulapi.QueryMeta),511 nil,512 ).Once()513 // channel used to notify when lock has been lost; it'll just get514 // closed515 c := receiver.WatchLock()516 517 // wait for the lock to be lost518 select {519 case _, more := <-c:520 Expect(more).To(Equal(false))521 }522 523 mockKV.AssertExpectations(GinkgoT())524 // verify calls to KV.Get()525 var kvGet mock.Call526 var queryOpts *consulapi.QueryOptions527 528 // first call529 kvGet = mockKV.Calls[0]530 Expect(kvGet.Method).To(Equal("Get"))531 532 queryOpts = kvGet.Arguments.Get(1).(*consulapi.QueryOptions)533 Expect(queryOpts).NotTo(BeNil())534 Expect(queryOpts.WaitIndex).To(Equal(uint64(0)))535 Expect(queryOpts.WaitTime).To(BeNumerically(">", 0))536 // second call537 kvGet = mockKV.Calls[1]...
validation_test.go
Source:validation_test.go
...6)7var _ = Describe("Testing with Ginkgo", func() {8 It("date time string", func() {9 if !isValidDateTime("2012-06-03T13:26:00") {10 GinkgoT().Errorf("WAT!")11 }12 })13 It("badly formatted start at fails", func() {14 args := make(map[string][]string)15 args["start_at"] = []string{"i am not a time"}16 expectError(expectation{t: GinkgoT(), args: args})17 })18 It("well formatted start at is okay", func() {19 args := make(map[string][]string)20 args["start_at"] = []string{"2000-02-02T00:02:02 +00:00"}21 args["end_at"] = []string{"2000-02-09T00:02:02 +00:00"}22 expectSuccess(expectation{t: GinkgoT(), args: args})23 })24 It("multiple start at args fail", func() {25 args := make(map[string][]string)26 args["start_at"] = []string{"2000-02-02T00:02:02 +00:00", "2000-03-02T00:02:02 +00:00"}27 expectError(expectation{t: GinkgoT(), args: args})28 })29 It("invalid start date fails", func() {30 args := make(map[string][]string)31 args["start_at"] = []string{"2000-14-28T00:02:02 +00:00"}32 expectError(expectation{t: GinkgoT(), args: args})33 })34 It("date parsing", func() {35 v, err := time.Parse(time.RFC3339, "2000-14-28T00:02:02 +00:00")36 if err == nil {37 GinkgoT().Errorf("time was parsed as %v", v)38 }39 })40 It("badly formatted end at fails", func() {41 args := make(map[string][]string)42 args["end_at"] = []string{"i am not a time"}43 expectError(expectation{t: GinkgoT(), args: args})44 })45 It("well formatted end at is allowed", func() {46 args := make(map[string][]string)47 args["start_at"] = []string{"2000-01-26T00:02:02 +00:00"}48 args["end_at"] = []string{"2000-02-02T00:02:02 +00:00"}49 expectSuccess(expectation{t: GinkgoT(), args: args})50 })51 It("filter by query requires field and name", func() {52 args := make(map[string][]string)53 args["filter_by"] = []string{"bar"}54 expectError(expectation{t: GinkgoT(), args: args})55 })56 It("well formatted filter by is okay", func() {57 args := make(map[string][]string)58 args["filter_by"] = []string{"foo:bar"}59 expectSuccess(expectation{t: GinkgoT(), args: args})60 })61 It("all filter by args are validated", func() {62 args := make(map[string][]string)63 args["filter_by"] = []string{"foo:bar", "baz"}64 expectError(expectation{t: GinkgoT(), args: args})65 })66 It("filter by field name is validated", func() {67 args := make(map[string][]string)68 args["filter_by"] = []string{"with-hyphen:bar"}69 expectError(expectation{t: GinkgoT(), args: args})70 })71 It("filter by field name cannot look like mongo thing", func() {72 args := make(map[string][]string)73 args["filter_by"] = []string{"$foo:bar"}74 expectError(expectation{t: GinkgoT(), args: args})75 })76 It("sort by ascending is okay", func() {77 args := make(map[string][]string)78 args["sort_by"] = []string{"foo:ascending"}79 expectSuccess(expectation{t: GinkgoT(), args: args})80 })81 It("sort by descending is okay", func() {82 args := make(map[string][]string)83 args["sort_by"] = []string{"foo:descending"}84 expectSuccess(expectation{t: GinkgoT(), args: args})85 })86 It("sort by anything else fails", func() {87 args := make(map[string][]string)88 args["sort_by"] = []string{"foo:random"}89 expectError(expectation{t: GinkgoT(), args: args})90 args["sort_by"] = []string{"lulz"}91 expectError(expectation{t: GinkgoT(), args: args})92 })93 It("sort by requires a valid field name", func() {94 args := make(map[string][]string)95 args["sort_by"] = []string{"with-hyphen:ascending"}96 expectError(expectation{t: GinkgoT(), args: args})97 })98 It("limit should be a positive integer", func() {99 args := make(map[string][]string)100 args["limit"] = []string{"not_a_number"}101 expectError(expectation{t: GinkgoT(), args: args})102 args["limit"] = []string{"-3"}103 expectError(expectation{t: GinkgoT(), args: args})104 args["limit"] = []string{"3"}105 expectSuccess(expectation{t: GinkgoT(), args: args})106 })107 It("group by on internal name fails", func() {108 args := make(map[string][]string)109 args["group_by"] = []string{"_internal_field"}110 expectError(expectation{t: GinkgoT(), args: args})111 })112 It("group by on invalid field name fails", func() {113 args := make(map[string][]string)114 args["group_by"] = []string{"with-hyphen"}115 expectError(expectation{t: GinkgoT(), args: args})116 })117 It("sort by with period only fails", func() {118 args := make(map[string][]string)119 args["sort_by"] = []string{"foo:ascending"}120 args["period"] = []string{"week"}121 args["start_at"] = []string{"2012-11-12T00:00:00Z"}122 args["end_at"] = []string{"2012-12-03T00:00:00Z"}123 expectError(expectation{t: GinkgoT(), args: args})124 })125 It("sort by with period and group by is okay", func() {126 args := make(map[string][]string)127 args["sort_by"] = []string{"foo:ascending"}128 args["period"] = []string{"week"}129 args["group_by"] = []string{"foobar"}130 args["start_at"] = []string{"2012-11-12T00:00:00Z"}131 args["end_at"] = []string{"2012-12-03T00:00:00Z"}132 expectSuccess(expectation{t: GinkgoT(), args: args})133 })134 It("collect without group by fails", func() {135 args := make(map[string][]string)136 args["collect"] = []string{"bar"}137 expectError(expectation{t: GinkgoT(), args: args})138 })139 It("collect and group by is okay", func() {140 args := make(map[string][]string)141 args["collect"] = []string{"bar"}142 args["group_by"] = []string{"foo"}143 expectSuccess(expectation{t: GinkgoT(), args: args})144 })145 It("collect is okay", func() {146 args := make(map[string][]string)147 args["collect"] = []string{"a_aAbBzZ_"}148 args["group_by"] = []string{"foo"}149 expectSuccess(expectation{t: GinkgoT(), args: args})150 })151 It("collect with function fails", func() {152 args := make(map[string][]string)153 args["collect"] = []string{"something);while(1){myBadFunction()}"}154 args["group_by"] = []string{"foo"}155 expectError(expectation{t: GinkgoT(), args: args})156 })157 It("collect with a hyphen fails", func() {158 args := make(map[string][]string)159 args["collect"] = []string{"with-hyphen"}160 args["group_by"] = []string{"foo"}161 expectError(expectation{t: GinkgoT(), args: args})162 })163 It("collect with a mongo thing fails", func() {164 args := make(map[string][]string)165 args["collect"] = []string{"$foo"}166 args["group_by"] = []string{"foo"}167 expectError(expectation{t: GinkgoT(), args: args})168 })169 It("collect on same field as group by fails", func() {170 args := make(map[string][]string)171 args["collect"] = []string{"foo"}172 args["group_by"] = []string{"foo"}173 expectError(expectation{t: GinkgoT(), args: args})174 })175 It("collect on internal field fails", func() {176 args := make(map[string][]string)177 args["collect"] = []string{"_foo"}178 args["group_by"] = []string{"foo"}179 expectError(expectation{t: GinkgoT(), args: args})180 })181 It("multiple collect is okay", func() {182 args := make(map[string][]string)183 args["collect"] = []string{"bar", "baz"}184 args["group_by"] = []string{"foo"}185 expectSuccess(expectation{t: GinkgoT(), args: args})186 })187 It("collect with later internal parameter fails", func() {188 args := make(map[string][]string)189 args["collect"] = []string{"bar", "_baz"}190 args["group_by"] = []string{"foo"}191 expectError(expectation{t: GinkgoT(), args: args})192 })193 It("collect has a whitelist of methods", func() {194 args := make(map[string][]string)195 args["group_by"] = []string{"foo"}196 for _, method := range []string{"sum", "count", "set", "mean"} {197 args["collect"] = []string{fmt.Sprintf("field:%s", method)}198 expectSuccess(expectation{t: GinkgoT(), args: args})199 }200 })201 It("collect with invalid method fails", func() {202 args := make(map[string][]string)203 args["group_by"] = []string{"foo"}204 args["collect"] = []string{"field:foobar"}205 expectError(expectation{t: GinkgoT(), args: args})206 })207 It("duration requires other parameters", func() {208 args := make(map[string][]string)209 args["duration"] = []string{"3"}210 expectError(expectation{t: GinkgoT(), args: args})211 })212 It("duration must be positive integer", func() {213 args := make(map[string][]string)214 args["duration"] = []string{"0"}215 args["period"] = []string{"day"}216 expectError(expectation{t: GinkgoT(), args: args})217 args["duration"] = []string{"3"}218 expectSuccess(expectation{t: GinkgoT(), args: args})219 args["duration"] = []string{"-3"}220 args["period"] = []string{"day"}221 expectError(expectation{t: GinkgoT(), args: args})222 })223 It("duration is a valid number", func() {224 args := make(map[string][]string)225 args["duration"] = []string{"not_a_number"}226 args["period"] = []string{"day"}227 expectError(expectation{t: GinkgoT(), args: args})228 })229 It("period and duration with start at is okay", func() {230 args := make(map[string][]string)231 args["duration"] = []string{"3"}232 args["period"] = []string{"day"}233 args["start_at"] = []string{"2000-02-02T00:00:00+00:00"}234 expectSuccess(expectation{t: GinkgoT(), args: args})235 })236 It("start at alone fails", func() {237 args := make(map[string][]string)238 args["start_at"] = []string{"2000-02-02T00:00:00+00:00"}239 expectError(expectation{t: GinkgoT(), args: args})240 })241 It("end at alone fails", func() {242 args := make(map[string][]string)243 args["end_at"] = []string{"2000-02-02T00:00:00+00:00"}244 expectError(expectation{t: GinkgoT(), args: args})245 })246 It("duration with start at and end at fails", func() {247 args := make(map[string][]string)248 args["duration"] = []string{"3"}249 args["period"] = []string{"day"}250 args["start_at"] = []string{"2000-02-02T00:00:00+00:00"}251 args["end_at"] = []string{"2000-02-09T00:00:00+00:00"}252 expectError(expectation{t: GinkgoT(), args: args})253 })254 It("period has a limited vocabulary", func() {255 args := make(map[string][]string)256 args["duration"] = []string{"3"}257 args["period"] = []string{"fortnight"}258 args["start_at"] = []string{"2000-02-02T00:00:00+00:00"}259 expectError(expectation{t: GinkgoT(), args: args})260 })261 It("period with start at and end at is okay", func() {262 args := make(map[string][]string)263 args["period"] = []string{"week"}264 args["start_at"] = []string{"2000-02-02T00:00:00+00:00"}265 args["end_at"] = []string{"2000-02-09T00:00:00+00:00"}266 expectSuccess(expectation{t: GinkgoT(), args: args, allowRawQueries: true})267 })268 It("no raw queries with period with start at and end at on wednesday fails", func() {269 args := make(map[string][]string)270 args["period"] = []string{"week"}271 args["start_at"] = []string{"2000-02-02T00:00:00+00:00"}272 args["end_at"] = []string{"2000-02-09T00:00:00+00:00"}273 expectError(expectation{t: GinkgoT(), args: args, allowRawQueries: false})274 })275 It("no raw queries with period with start at and end at on monday is okay", func() {276 args := make(map[string][]string)277 args["period"] = []string{"week"}278 args["start_at"] = []string{"2000-02-07T00:00:00+00:00"}279 args["end_at"] = []string{"2000-02-14T00:00:00+00:00"}280 expectSuccess(expectation{t: GinkgoT(), args: args, allowRawQueries: false})281 })282 It("no raw queries means use midnight", func() {283 args := make(map[string][]string)284 args["period"] = []string{"day"}285 args["start_at"] = []string{"2000-02-02T00:00:00+00:00"}286 args["end_at"] = []string{"2000-02-09T00:00:00+00:00"}287 expectSuccess(expectation{t: GinkgoT(), args: args})288 })289 It("no raw queries for a day period with hourse in the middle of the day fails", func() {290 args := make(map[string][]string)291 args["period"] = []string{"day"}292 args["start_at"] = []string{"2000-02-02T12:00:00+00:00"}293 args["end_at"] = []string{"2000-02-09T13:00:00+00:00"}294 expectError(expectation{t: GinkgoT(), args: args})295 })296 It("no raw queries for an hour period allows time in the middle of the day", func() {297 args := make(map[string][]string)298 args["period"] = []string{"hour"}299 args["start_at"] = []string{"2000-02-02T12:00:00+00:00"}300 args["end_at"] = []string{"2000-02-09T13:00:00+00:00"}301 expectSuccess(expectation{t: GinkgoT(), args: args})302 })303 It("no raw queries for a day period less than7 days fails", func() {304 args := make(map[string][]string)305 args["period"] = []string{"day"}306 args["start_at"] = []string{"2000-02-02T00:00:00+00:00"}307 args["end_at"] = []string{"2000-02-08T00:00:00+00:00"}308 expectError(expectation{t: GinkgoT(), args: args})309 })310 It("no raw queries for an hour period are allowed", func() {311 args := make(map[string][]string)312 args["period"] = []string{"hour"}313 args["start_at"] = []string{"2000-02-02T00:00:00+00:00"}314 args["end_at"] = []string{"2000-02-08T00:00:00+00:00"}315 expectSuccess(expectation{t: GinkgoT(), args: args})316 })317 It("no raw queries with month period with start at and end at on third fails", func() {318 args := make(map[string][]string)319 args["period"] = []string{"month"}320 args["start_at"] = []string{"2000-02-03T00:00:00+00:00"}321 args["end_at"] = []string{"2000-03-03T00:00:00+00:00"}322 expectError(expectation{t: GinkgoT(), args: args, allowRawQueries: false})323 })324 It("no raw queries with month period with start at and end at on first is okay", func() {325 args := make(map[string][]string)326 args["period"] = []string{"month"}327 args["start_at"] = []string{"2000-02-01T00:00:00+00:00"}328 args["end_at"] = []string{"2000-03-01T00:00:00+00:00"}329 expectSuccess(expectation{t: GinkgoT(), args: args, allowRawQueries: false})330 })331})332type expectation struct {333 t GinkgoTInterface334 args map[string][]string335 allowRawQueries bool336}337func expectError(e expectation) {338 if ValidateRequestArgs(e.args, e.allowRawQueries) == nil {339 e.t.Errorf("%v should have failed", e.args)340 }341}342func expectSuccess(e expectation) {343 if err := ValidateRequestArgs(e.args, e.allowRawQueries); err != nil {344 e.t.Errorf("%v should have been okay but was %v", e.args, err)345 }346}...
golang_test_test.go
Source:golang_test_test.go
...13 writer := httptest.NewRecorder()14 request, _ := http.NewRequest("GET", "/post/1", nil)15 mux.ServeHTTP(writer, request)16 if writer.Code != 200 {17 GinkgoT().Errorf("Response code is %v", writer.Code)18 }19 var post Post20 json.Unmarshal(writer.Body.Bytes(), &post)21 if post.Id != 1 {22 GinkgoT().Errorf("Cannot retrieve JSON post")23 }24 })25 It("put post", func() {26 mux := http.NewServeMux()27 post := &FakePost{}28 mux.HandleFunc("/post/", handlerRequest(post))29 writer := httptest.NewRecorder()30 json := strings.NewReader(`{"content": "Updated post", "author": "NAO"}`)31 request, _ := http.NewRequest("PUT", "/post/1", json)32 mux.ServeHTTP(writer, request)33 if writer.Code != 200 {34 GinkgoT().Errorf("Response code is %v", writer.Code)35 }36 if post.Content != "Updated post" {37 GinkgoT().Errorf("Content is not corrent", post.Content)38 }39 })40})
GinkgoT
Using AI Code Generation
1import (2func TestGinkgo(t *testing.T) {3 RegisterFailHandler(Fail)4 RunSpecs(t, "Ginkgo Suite")5}6var _ = Describe("Ginkgo", func() {7 It("should work", func() {8 fmt.Println("Hello World")9 Expect(1).To(Equal(1))10 })11})12import (13func TestGinkgo(t *testing.T) {14 RegisterFailHandler(Fail)15 RunSpecs(t, "Ginkgo Suite")16}17var _ = Describe("Ginkgo", func() {18 It("should work", func() {19 fmt.Println("Hello World")20 Expect(1).To(Equal(1))21 })22})23import (24func TestGinkgo(t *testing.T) {25 RegisterFailHandler(Fail)26 RunSpecs(t, "Ginkgo Suite")27}28var _ = Describe("Ginkgo", func() {29 It("should work", func() {30 fmt.Println("Hello World")31 Expect(1).To(Equal(1))32 })33})34import (35func TestGinkgo(t *testing.T) {36 RegisterFailHandler(Fail)37 RunSpecs(t, "Ginkgo Suite")38}39var _ = Describe("Ginkgo", func() {40 It("should work", func() {41 fmt.Println("Hello World")42 Expect(1).To(Equal(1))43 })44})
GinkgoT
Using AI Code Generation
1import (2func TestGinkgoT(t *testing.T) {3 RegisterFailHandler(Fail)4 RunSpecs(t, "GinkgoT Suite")5}6var _ = Describe("GinkgoT", func() {7 It("should run", func() {8 fmt.Println("GinkgoT")9 })10})11import (12func TestGinkgoT(t *testing.T) {13 RegisterFailHandler(Fail)14 RunSpecs(t, "GinkgoT Suite")15}16var _ = Describe("GinkgoT", func() {17 It("should run", func() {18 GinkgoT().Log("GinkgoT")19 })20})21import (22func TestGinkgoWriter(t *testing.T) {23 RegisterFailHandler(Fail)24 RunSpecs(t, "GinkgoWriter Suite")25}26var _ = Describe("GinkgoWriter", func() {27 It("should run", func() {28 fmt.Println("GinkgoWriter")29 })30})31import (32func TestGinkgoWriter(t *testing.T) {33 RegisterFailHandler(Fail)34 RunSpecs(t, "GinkgoWriter Suite")35}36var _ = Describe("GinkgoWriter", func() {37 It("should run", func() {
GinkgoT
Using AI Code Generation
1import (2func TestGinkgoT(t *testing.T) {3 ginkgo.GinkgoT()4 fmt.Println("Hello World")5}6import (7func TestGinkgoT(t *testing.T) {8 ginkgo.GinkgoT()9 fmt.Println("Hello World")10}11import (12func TestGinkgoT(t *testing.T) {13 ginkgo.GinkgoT()14 fmt.Println("Hello World")15}16import (17func TestGinkgoT(t *testing.T) {18 ginkgo.GinkgoT()19 fmt.Println("Hello World")20}21import (22func TestGinkgoT(t *testing.T) {23 ginkgo.GinkgoT()24 fmt.Println("Hello World")25}26import (27func TestGinkgoT(t *testing.T) {28 ginkgo.GinkgoT()29 fmt.Println("Hello World")30}31import (32func TestGinkgoT(t *testing.T) {
GinkgoT
Using AI Code Generation
1import (2func TestGinkgoT(t *testing.T) {3 ginkgo.GinkgoT()4}5import (6func TestGinkgoWriter(t *testing.T) {7 ginkgo.GinkgoWriter()8}9import (10func TestGinkgoParallelNode(t *testing.T) {11 ginkgo.GinkgoParallelNode()12}13import (14func TestGinkgoParallelTotal(t *testing.T) {15 ginkgo.GinkgoParallelTotal()16}17import (18func TestGinkgoParallelStream(t *testing.T) {19 ginkgo.GinkgoParallelStream()20}21import (22func TestGinkgoBuildVersion(t *testing.T) {23 ginkgo.GinkgoBuildVersion()24}25import (26func TestGinkgoRegisterFailHandler(t *testing.T) {27 ginkgo.GinkgoRegisterFailHandler()28}29import (
GinkgoT
Using AI Code Generation
1func Test1(t *testing.T) {2 gomega.RegisterFailHandler(ginkgo.Fail)3 ginkgo.RunSpecs(t, "Test1 Suite")4}5var _ = Describe("Test1", func() {6 Context("Test1", func() {7 It("Test1", func() {8 ginkgo.GinkgoT().Error("This is an error message")9 })10 })11})12To use GinkgoT() in the BeforeEach and AfterEach blocks, use the following code:13func Test1(t *testing.T) {14 gomega.RegisterFailHandler(ginkgo.Fail)15 ginkgo.RunSpecs(t, "Test1 Suite")16}17var _ = Describe("Test1", func() {18 Context("Test1", func() {19 BeforeEach(func() {20 ginkgo.GinkgoT().Error("This is an error message")21 })22 It("Test1", func() {23 })24 })25})
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!