// TestUpsertCreateScript tests if we can create a script record in the db. func TestUpsertCreateScript(t *testing.T) { const fixture = "basic.json" scr1, db := setup(t, fixture) defer teardown(t, db) t.Log("Given the need to save a script into the database.") { t.Log("\tWhen using script", scr1) { if err := script.Upsert(tests.Context, db, scr1); err != nil { t.Fatalf("\t%s\tShould be able to create a script : %s", tests.Failed, err) } t.Logf("\t%s\tShould be able to create a script.", tests.Success) if _, err := script.GetLastHistoryByName(tests.Context, db, scr1.Name); err != nil { t.Fatalf("\t%s\tShould be able to retrieve the script from history: %s", tests.Failed, err) } t.Logf("\t%s\tShould be able to retrieve the script from history.", tests.Success) scr2, err := script.GetByName(tests.Context, db, scr1.Name) if err != nil { t.Fatalf("\t%s\tShould be able to retrieve the script : %s", tests.Failed, err) } t.Logf("\t%s\tShould be able to retrieve the script.", tests.Success) if !reflect.DeepEqual(scr1, scr2) { t.Logf("\t%+v", scr1) t.Logf("\t%+v", scr2) t.Errorf("\t%s\tShould be able to get back the same script values.", tests.Failed) } else { t.Logf("\t%s\tShould be able to get back the same script values.", tests.Success) } } } }
// TestUpsertUpdateScript validates update operation of a given Script. func TestUpsertUpdateScript(t *testing.T) { const fixture = "basic.json" scr1, db := setup(t, fixture) defer teardown(t, db) t.Log("Given the need to update a script into the database.") { t.Log("\tWhen using two scripts") { if err := script.Upsert(tests.Context, db, scr1); err != nil { t.Fatalf("\t%s\tShould be able to create a script : %s", tests.Failed, err) } t.Logf("\t%s\tShould be able to create a script.", tests.Success) scr2 := scr1 scr2.Commands = append(scr2.Commands, map[string]interface{}{"command": 4}) if err := script.Upsert(tests.Context, db, scr2); err != nil { t.Fatalf("\t%s\tShould be able to update a script record: %s", tests.Failed, err) } t.Logf("\t%s\tShould be able to update a script record.", tests.Success) if _, err := script.GetLastHistoryByName(tests.Context, db, scr1.Name); err != nil { t.Fatalf("\t%s\tShould be able to retrieve the script from history: %s", tests.Failed, err) } t.Logf("\t%s\tShould be able to retrieve the script from history.", tests.Success) updScr, err := script.GetByName(tests.Context, db, scr2.Name) if err != nil { t.Fatalf("\t%s\tShould be able to retrieve a script record: %s", tests.Failed, err) } t.Logf("\t%s\tShould be able to retrieve a script record.", tests.Success) if updScr.Name != scr1.Name { t.Errorf("\t%s\tShould be able to get back the same script name.", tests.Failed) } else { t.Logf("\t%s\tShould be able to get back the same script name.", tests.Success) } if lendiff := len(updScr.Commands) - len(scr1.Commands); lendiff != 1 { t.Errorf("\t%s\tShould have one more parameter in script record: %d", tests.Failed, lendiff) } else { t.Logf("\t%s\tShould have one more parameter in script record.", tests.Success) } if !reflect.DeepEqual(scr2.Commands[0], updScr.Commands[0]) { t.Logf("\t%+v", scr2.Commands[0]) t.Logf("\t%+v", updScr.Commands[0]) t.Errorf("\t%s\tShould be abe to validate the script param values in db.", tests.Failed) } else { t.Logf("\t%s\tShould be abe to validate the script param values in db.", tests.Success) } } } }
// TestAPIFailureScripts validates the failure of the api using a nil session. func TestAPIFailureScripts(t *testing.T) { const fixture = "basic.json" scr1, db := setup(t, fixture) defer teardown(t, db) scrName := prefix + "_unknown" t.Log("Given the need to validate failure of API with bad session.") { t.Log("When giving a nil session") { err := script.Upsert(tests.Context, nil, scr1) if err == nil { t.Fatalf("\t%s\tShould be refused create by api with bad session", tests.Failed) } t.Logf("\t%s\tShould be refused create by api with bad session: %s", tests.Success, err) _, err = script.GetNames(tests.Context, nil) if err == nil { t.Fatalf("\t%s\tShould be refused get request by api with bad session", tests.Failed) } t.Logf("\t%s\tShould be refused get request by api with bad session: %s", tests.Success, err) _, err = script.GetAll(tests.Context, nil, nil) if err == nil { t.Fatalf("\t%s\tShould be refused get request by api with bad session", tests.Failed) } t.Logf("\t%s\tShould be refused get request by api with bad session: %s", tests.Success, err) _, err = script.GetByName(tests.Context, nil, scrName) if err == nil { t.Fatalf("\t%s\tShould be refused get request by api with bad session", tests.Failed) } t.Logf("\t%s\tShould be refused get request by api with bad session: %s", tests.Success, err) _, err = script.GetByNames(tests.Context, nil, nil) if err == nil { t.Fatalf("\t%s\tShould be refused get request by api with bad session", tests.Failed) } t.Logf("\t%s\tShould be refused get request by api with bad session: %s", tests.Success, err) _, err = script.GetLastHistoryByName(tests.Context, nil, scrName) if err == nil { t.Fatalf("\t%s\tShould be refused get request by api with bad session", tests.Failed) } t.Logf("\t%s\tShould be refused get request by api with bad session: %s", tests.Success, err) err = script.Delete(tests.Context, nil, scrName) if err == nil { t.Fatalf("\t%s\tShould be refused delete by api with bad session", tests.Failed) } t.Logf("\t%s\tShould be refused delete by api with bad session: %s", tests.Success, err) } } }
// TestGetLastScriptHistoryByName validates retrieval of Script from the history // collection. func TestGetLastScriptHistoryByName(t *testing.T) { const fixture = "basic.json" scr1, db := setup(t, fixture) defer teardown(t, db) scrName := prefix + "_basic" t.Log("Given the need to retrieve a script from history.") { t.Log("\tWhen using script", scr1) { if err := script.Upsert(tests.Context, db, scr1); err != nil { t.Fatalf("\t%s\tShould be able to create a script : %s", tests.Failed, err) } t.Logf("\t%s\tShould be able to create a script.", tests.Success) scr1.Commands = append(scr1.Commands, map[string]interface{}{"command": 4}) if err := script.Upsert(tests.Context, db, scr1); err != nil { t.Fatalf("\t%s\tShould be able to create a script : %s", tests.Failed, err) } t.Logf("\t%s\tShould be able to create a script.", tests.Success) scr2, err := script.GetLastHistoryByName(tests.Context, db, scrName) if err != nil { t.Fatalf("\t%s\tShould be able to retrieve the last script from history : %s", tests.Failed, err) } t.Logf("\t%s\tShould be able to retrieve the last script from history.", tests.Success) if !reflect.DeepEqual(scr1, scr2) { t.Logf("\t%+v", scr1) t.Logf("\t%+v", scr2) t.Errorf("\t%s\tShould be able to get back the same script values.", tests.Failed) } else { t.Logf("\t%s\tShould be able to get back the same script values.", tests.Success) } } } }