Best Gauge code snippet using validation.Pid
resource_google_project_service_test.go
Source:resource_google_project_service_test.go
1package google2import (3 "fmt"4 "regexp"5 "testing"6 "time"7 "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"8 "github.com/hashicorp/terraform-plugin-sdk/v2/terraform"9)10func TestProjectServiceServiceValidateFunc(t *testing.T) {11 cases := map[string]struct {12 val interface{}13 ExpectValidationError bool14 }{15 "ignoredProjectService": {16 val: "dataproc-control.googleapis.com",17 ExpectValidationError: true,18 },19 "bannedProjectService": {20 val: "bigquery-json.googleapis.com",21 ExpectValidationError: true,22 },23 "third party API": {24 val: "whatever.example.com",25 ExpectValidationError: false,26 },27 "not a domain": {28 val: "monitoring",29 ExpectValidationError: true,30 },31 "not a string": {32 val: 5,33 ExpectValidationError: true,34 },35 }36 for tn, tc := range cases {37 _, errs := validateProjectServiceService(tc.val, "service")38 if tc.ExpectValidationError && len(errs) == 0 {39 t.Errorf("bad: %s, %q passed validation but was expected to fail", tn, tc.val)40 }41 if !tc.ExpectValidationError && len(errs) > 0 {42 t.Errorf("bad: %s, %q failed validation but was expected to pass. errs: %q", tn, tc.val, errs)43 }44 }45}46// Test that services can be enabled and disabled on a project47func TestAccProjectService_basic(t *testing.T) {48 t.Parallel()49 // Multiple fine-grained resources50 skipIfVcr(t)51 org := getTestOrgFromEnv(t)52 pid := fmt.Sprintf("tf-test-%d", randInt(t))53 services := []string{"iam.googleapis.com", "cloudresourcemanager.googleapis.com"}54 vcrTest(t, resource.TestCase{55 PreCheck: func() { testAccPreCheck(t) },56 Providers: testAccProviders,57 Steps: []resource.TestStep{58 {59 Config: testAccProjectService_basic(services, pid, pname, org),60 Check: resource.ComposeTestCheckFunc(61 testAccCheckProjectService(t, services, pid, true),62 ),63 },64 {65 ResourceName: "google_project_service.test",66 ImportState: true,67 ImportStateVerify: true,68 ImportStateVerifyIgnore: []string{"disable_on_destroy"},69 },70 {71 ResourceName: "google_project_service.test2",72 ImportState: true,73 ImportStateVerify: true,74 ImportStateVerifyIgnore: []string{"disable_on_destroy", "project"},75 },76 // Use a separate TestStep rather than a CheckDestroy because we need the project to still exist.77 {78 Config: testAccProject_create(pid, pname, org),79 Check: resource.ComposeTestCheckFunc(80 testAccCheckProjectService(t, services, pid, false),81 ),82 },83 // Create services with disabling turned off.84 {85 Config: testAccProjectService_noDisable(services, pid, pname, org),86 Check: resource.ComposeTestCheckFunc(87 testAccCheckProjectService(t, services, pid, true),88 ),89 },90 // Check that services are still enabled even after the resources are deleted.91 {92 Config: testAccProject_create(pid, pname, org),93 Check: resource.ComposeTestCheckFunc(94 testAccCheckProjectService(t, services, pid, true),95 ),96 },97 },98 })99}100func TestAccProjectService_disableDependentServices(t *testing.T) {101 // Multiple fine-grained resources102 skipIfVcr(t)103 t.Parallel()104 org := getTestOrgFromEnv(t)105 billingId := getTestBillingAccountFromEnv(t)106 pid := fmt.Sprintf("tf-test-%d", randInt(t))107 services := []string{"cloudbuild.googleapis.com", "containerregistry.googleapis.com"}108 vcrTest(t, resource.TestCase{109 PreCheck: func() { testAccPreCheck(t) },110 Providers: testAccProviders,111 Steps: []resource.TestStep{112 {113 Config: testAccProjectService_disableDependentServices(services, pid, pname, org, billingId, "false"),114 },115 {116 ResourceName: "google_project_service.test",117 ImportState: true,118 ImportStateVerify: true,119 ImportStateVerifyIgnore: []string{"disable_on_destroy"},120 },121 {122 Config: testAccProjectService_dependencyRemoved(services, pid, pname, org, billingId),123 ExpectError: regexp.MustCompile("Please specify disable_dependent_services=true if you want to proceed with disabling all services."),124 },125 {126 Config: testAccProjectService_disableDependentServices(services, pid, pname, org, billingId, "true"),127 },128 {129 ResourceName: "google_project_service.test",130 ImportState: true,131 ImportStateVerify: true,132 ImportStateVerifyIgnore: []string{"disable_on_destroy"},133 },134 {135 Config: testAccProjectService_dependencyRemoved(services, pid, pname, org, billingId),136 ExpectNonEmptyPlan: true,137 },138 },139 })140}141func TestAccProjectService_handleNotFound(t *testing.T) {142 t.Parallel()143 org := getTestOrgFromEnv(t)144 pid := fmt.Sprintf("tf-test-%d", randInt(t))145 service := "iam.googleapis.com"146 vcrTest(t, resource.TestCase{147 PreCheck: func() { testAccPreCheck(t) },148 Providers: testAccProviders,149 Steps: []resource.TestStep{150 {151 Config: testAccProjectService_handleNotFound(service, pid, pname, org),152 Check: resource.ComposeTestCheckFunc(153 testAccCheckProjectService(t, []string{service}, pid, true),154 ),155 },156 // Delete the project, implicitly deletes service, expect the plan to want to create the service again157 {158 Config: testAccProjectService_handleNotFoundNoProject(service, pid),159 ExpectNonEmptyPlan: true,160 },161 },162 })163}164func TestAccProjectService_renamedService(t *testing.T) {165 t.Parallel()166 if len(renamedServices) == 0 {167 t.Skip()168 }169 var newName string170 for _, new := range renamedServices {171 newName = new172 }173 org := getTestOrgFromEnv(t)174 pid := fmt.Sprintf("tf-test-%d", randInt(t))175 vcrTest(t, resource.TestCase{176 PreCheck: func() { testAccPreCheck(t) },177 Providers: testAccProviders,178 Steps: []resource.TestStep{179 {180 Config: testAccProjectService_single(newName, pid, pname, org),181 },182 {183 ResourceName: "google_project_service.test",184 ImportState: true,185 ImportStateVerify: true,186 ImportStateVerifyIgnore: []string{"disable_on_destroy", "disable_dependent_services"},187 },188 },189 })190}191func testAccCheckProjectService(t *testing.T, services []string, pid string, expectEnabled bool) resource.TestCheckFunc {192 return func(s *terraform.State) error {193 config := googleProviderConfig(t)194 currentlyEnabled, err := listCurrentlyEnabledServices(pid, "", config.userAgent, config, time.Minute*10)195 if err != nil {196 return fmt.Errorf("Error listing services for project %q: %v", pid, err)197 }198 for _, expected := range services {199 exists := false200 for actual := range currentlyEnabled {201 if expected == actual {202 exists = true203 }204 }205 if expectEnabled && !exists {206 return fmt.Errorf("Expected service %s is not enabled server-side", expected)207 }208 if !expectEnabled && exists {209 return fmt.Errorf("Expected disabled service %s is enabled server-side", expected)210 }211 }212 return nil213 }214}215func testAccProjectService_basic(services []string, pid, name, org string) string {216 return fmt.Sprintf(`217resource "google_project" "acceptance" {218 project_id = "%s"219 name = "%s"220 org_id = "%s"221}222resource "google_project_service" "test" {223 project = google_project.acceptance.project_id224 service = "%s"225}226resource "google_project_service" "test2" {227 project = google_project.acceptance.id228 service = "%s"229}230`, pid, name, org, services[0], services[1])231}232func testAccProjectService_disableDependentServices(services []string, pid, name, org, billing, disableDependentServices string) string {233 return fmt.Sprintf(`234resource "google_project" "acceptance" {235 project_id = "%s"236 name = "%s"237 org_id = "%s"238 billing_account = "%s"239}240resource "google_project_service" "test" {241 project = google_project.acceptance.project_id242 service = "%s"243}244resource "google_project_service" "test2" {245 project = google_project.acceptance.project_id246 service = "%s"247 disable_dependent_services = %s248}249`, pid, name, org, billing, services[0], services[1], disableDependentServices)250}251func testAccProjectService_dependencyRemoved(services []string, pid, name, org, billing string) string {252 return fmt.Sprintf(`253resource "google_project" "acceptance" {254 project_id = "%s"255 name = "%s"256 org_id = "%s"257 billing_account = "%s"258}259resource "google_project_service" "test" {260 project = google_project.acceptance.project_id261 service = "%s"262}263`, pid, name, org, billing, services[0])264}265func testAccProjectService_noDisable(services []string, pid, name, org string) string {266 return fmt.Sprintf(`267resource "google_project" "acceptance" {268 project_id = "%s"269 name = "%s"270 org_id = "%s"271}272resource "google_project_service" "test" {273 project = google_project.acceptance.project_id274 service = "%s"275 disable_on_destroy = false276}277resource "google_project_service" "test2" {278 project = google_project.acceptance.project_id279 service = "%s"280 disable_on_destroy = false281}282`, pid, name, org, services[0], services[1])283}284func testAccProjectService_handleNotFound(service, pid, name, org string) string {285 return fmt.Sprintf(`286resource "google_project" "acceptance" {287 project_id = "%s"288 name = "%s"289 org_id = "%s"290}291// by passing through locals, we break the dependency chain292// see terraform-provider-google#1292293locals {294 project_id = google_project.acceptance.project_id295}296resource "google_project_service" "test" {297 project = local.project_id298 service = "%s"299}300`, pid, name, org, service)301}302func testAccProjectService_handleNotFoundNoProject(service, pid string) string {303 return fmt.Sprintf(`304resource "google_project_service" "test" {305 project = "%s"306 service = "%s"307}308`, pid, service)309}310func testAccProjectService_single(service string, pid, name, org string) string {311 return fmt.Sprintf(`312resource "google_project" "acceptance" {313 project_id = "%s"314 name = "%s"315 org_id = "%s"316}317resource "google_project_service" "test" {318 project = google_project.acceptance.project_id319 service = "%s"320 disable_dependent_services = true321}322`, pid, name, org, service)323}...
incoming_test.go
Source:incoming_test.go
1//stm: #unit2package sub3import (4 "bytes"5 "context"6 "testing"7 "github.com/filecoin-project/go-address"8 "github.com/filecoin-project/go-legs/dtsync"9 "github.com/filecoin-project/lotus/api/mocks"10 "github.com/filecoin-project/lotus/chain/types"11 "github.com/golang/mock/gomock"12 blocks "github.com/ipfs/go-block-format"13 "github.com/ipfs/go-cid"14 "github.com/libp2p/go-libp2p-core/peer"15 pubsub "github.com/libp2p/go-libp2p-pubsub"16 pb "github.com/libp2p/go-libp2p-pubsub/pb"17)18type getter struct {19 msgs []*types.Message20}21func (g *getter) GetBlock(ctx context.Context, c cid.Cid) (blocks.Block, error) { panic("NYI") }22func (g *getter) GetBlocks(ctx context.Context, ks []cid.Cid) <-chan blocks.Block {23 ch := make(chan blocks.Block, len(g.msgs))24 for _, m := range g.msgs {25 by, err := m.Serialize()26 if err != nil {27 panic(err)28 }29 b, err := blocks.NewBlockWithCid(by, m.Cid())30 if err != nil {31 panic(err)32 }33 ch <- b34 }35 close(ch)36 return ch37}38func TestFetchCidsWithDedup(t *testing.T) {39 msgs := []*types.Message{}40 for i := 0; i < 10; i++ {41 msgs = append(msgs, &types.Message{42 From: address.TestAddress,43 To: address.TestAddress,44 Nonce: uint64(i),45 })46 }47 cids := []cid.Cid{}48 for _, m := range msgs {49 cids = append(cids, m.Cid())50 }51 g := &getter{msgs}52 //stm: @CHAIN_INCOMING_FETCH_MESSAGES_BY_CID_00153 // the cids have a duplicate54 res, err := FetchMessagesByCids(context.TODO(), g, append(cids, cids[0]))55 t.Logf("err: %+v", err)56 t.Logf("res: %+v", res)57 if err == nil {58 t.Errorf("there should be an error")59 }60 if err == nil && (res[0] == nil || res[len(res)-1] == nil) {61 t.Fatalf("there is a nil message: first %p, last %p", res[0], res[len(res)-1])62 }63}64func TestIndexerMessageValidator_Validate(t *testing.T) {65 validCid, err := cid.Decode("QmbpDgg5kRLDgMxS8vPKNFXEcA6D5MC4CkuUdSWDVtHPGK")66 if err != nil {67 t.Fatal(err)68 }69 tests := []struct {70 name string71 selfPID string72 senderPID string73 extraData []byte74 wantValidation pubsub.ValidationResult75 }{76 {77 name: "invalid extra data is rejected",78 selfPID: "12D3KooWQiCbqEStCkdqUvr69gQsrp9urYJZUCkzsQXia7mbqbFW",79 senderPID: "12D3KooWE8yt84RVwW3sFcd6WMjbUdWrZer2YtT4dmtj3dHdahSZ",80 extraData: []byte("f0127896"), // note, casting encoded address to byte is invalid.81 wantValidation: pubsub.ValidationReject,82 },83 {84 name: "same sender and receiver is ignored",85 selfPID: "12D3KooWQiCbqEStCkdqUvr69gQsrp9urYJZUCkzsQXia7mbqbFW",86 senderPID: "12D3KooWQiCbqEStCkdqUvr69gQsrp9urYJZUCkzsQXia7mbqbFW",87 wantValidation: pubsub.ValidationIgnore,88 },89 }90 for _, tc := range tests {91 tc := tc92 t.Run(tc.name, func(t *testing.T) {93 mc := gomock.NewController(t)94 node := mocks.NewMockFullNode(mc)95 subject := NewIndexerMessageValidator(peer.ID(tc.selfPID), node, node)96 message := dtsync.Message{97 Cid: validCid,98 Addrs: nil,99 ExtraData: tc.extraData,100 }101 buf := bytes.NewBuffer(nil)102 if err := message.MarshalCBOR(buf); err != nil {103 t.Fatal(err)104 }105 topic := "topic"106 pbm := &pb.Message{107 Data: buf.Bytes(),108 Topic: &topic,109 From: nil,110 Seqno: nil,111 }112 validate := subject.Validate(context.Background(), peer.ID(tc.senderPID), &pubsub.Message{113 Message: pbm,114 ReceivedFrom: peer.ID(tc.senderPID),115 ValidatorData: nil,116 })117 if validate != tc.wantValidation {118 t.Fatalf("expected %v but got %v", tc.wantValidation, validate)119 }120 })121 }122}...
Pid
Using AI Code Generation
1import (2type Person struct {3}4func main() {5 validate := validator.New()6 person := Person{7 }8 err := validate.Struct(person)9 if err != nil {10 fmt.Println(err)11 }12}
Pid
Using AI Code Generation
1import (2type User struct {3}4func main() {5 validate := validator.New()6 user := User{7 }8 err := validate.Struct(user)9 if err != nil {10 if _, ok := err.(*validator.InvalidValidationError); ok {11 fmt.Println(err)12 }13 for _, err := range err.(validator.ValidationErrors) {14 fmt.Println(err.Namespace())15 fmt.Println(err.Field())16 fmt.Println(err.Tag())17 fmt.Println(err.ActualTag())18 fmt.Println(err.Kind())19 fmt.Println(err.Type())20 fmt.Println(err.Value())21 fmt.Println(err.Param())22 fmt.Println()23 }24 }25}
Pid
Using AI Code Generation
1func main() {2 v := validation.Validation{}3 v.Required(1, "id").Message("ID can not be empty")4 if v.HasErrors() {5 for _, err := range v.Errors {6 fmt.Println(err.Key, err.Message)7 }8 }9}10func main() {11 v := validation.Validation{}12 v.Required(1, "id").Message("ID can not be empty")13 if v.HasErrors() {14 for _, err := range v.Errors {15 fmt.Println(err.Key, err.Message)16 }17 }18}19func main() {20 v := validation.Validation{}21 v.Required(1, "id").Message("ID can not be empty")22 if v.HasErrors() {23 for _, err := range v.Errors {24 fmt.Println(err.Key, err.Message)25 }26 }27}28func main() {29 v := validation.Validation{}30 v.Required(1, "id").Message("ID can not be empty")31 if v.HasErrors() {32 for _, err := range v.Errors {33 fmt.Println(err.Key, err.Message)34 }35 }36}37func main() {38 v := validation.Validation{}39 v.Required(1, "id").Message("ID can not be empty")40 if v.HasErrors() {41 for _, err := range v.Errors {42 fmt.Println(err.Key, err.Message)43 }44 }45}46func main() {47 v := validation.Validation{}48 v.Required(1, "id").Message("ID can not be empty")49 if v.HasErrors() {50 for _, err := range v.Errors {51 fmt.Println(err.Key, err.Message)52 }53 }54}55func main() {56 v := validation.Validation{}57 v.Required(1, "id").Message("ID can not be empty")58 if v.HasErrors() {59 for _, err := range v.Errors {60 fmt.Println(err.Key
Pid
Using AI Code Generation
13 import (28 func main() {312 e := Example{415 }518 validate := validator.New()621 err := validate.Struct(e)724 if err != nil {827 for _, e := range err.(validator.ValidationErrors) {930 fmt.Println(e)1031 }1132 }1233 }1336 type Example struct {1439 }153 import (168 func main() {1712 e := Example{1815 }1918 validate := validator.New()2021 err := validate.Struct(e)2124 if err != nil {2227 for _, e := range err.(validator.ValidationErrors) {2330 fmt.Println(e)2431 }2532 }2633 }2736 type Example struct {2839 }293 import (308 func main() {3112 e := Example{3215 }
Pid
Using AI Code Generation
1import (2func main() {3 validate := validator.New()4 pid := validate.RegisterValidation("pid", validatePid)5 fmt.Println(pid)6}7import (8func main() {9 validate := validator.New()10 pid := validate.RegisterValidation("pid", validatePid)11 fmt.Println(pid)12}13import (14func main() {15 fmt.Println(Add(1, 2))16}17func Add(x, y int) int {18}19import (20func main() {21 checker := types.NewChecker(&conf, nil)22 pkg, _ := checker.Import("fmt")23 fmt.Println(pkg.Name())24}25import (26func main() {27 fmt.Println(Add(1, 2))
Pid
Using AI Code Generation
1import "fmt"2func main() {3 fmt.Println(pid)4 fmt.Println("Is valid? ", validation.Pid(pid))5}6import "fmt"7func main() {8 fmt.Println(pid)9 fmt.Println("Is valid? ", validation.Pid(pid))10}
Pid
Using AI Code Generation
1func main() {2 x := validation.Pid(a)3 fmt.Println(x)4}5func main() {6 x := validation.Pid(a)7 fmt.Println(x)8}9func main() {10 x := validation.Pid(a)11 fmt.Println(x)12}13func main() {14 x := validation.Pid(a)15 fmt.Println(x)16}17func main() {18 x := validation.Pid(a)19 fmt.Println(x)20}21func main() {22 x := validation.Pid(a)23 fmt.Println(x)24}25func main() {26 x := validation.Pid(a)27 fmt.Println(x)28}29func main() {30 x := validation.Pid(a)31 fmt.Println(x)32}33func main() {
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!!