From 5ab83243c6c265526c2cb057de6035a0360c82f8 Mon Sep 17 00:00:00 2001 From: reesporte Date: Thu, 2 Dec 2021 15:47:49 -0600 Subject: [PATCH] add tests checking for the proper errors on nil results --- api_internal_test.go | 84 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 84 insertions(+) create mode 100644 api_internal_test.go diff --git a/api_internal_test.go b/api_internal_test.go new file mode 100644 index 000000000..c3f7b8849 --- /dev/null +++ b/api_internal_test.go @@ -0,0 +1,84 @@ +package pilosa + +import ( + "context" + "fmt" + "reflect" + "strings" + "testing" +) + +func TestTranslateIndexDbOnNilIndex(t *testing.T) { + api := API{} + api.holder = &Holder{} + r := strings.NewReader("not important tbh") + err := api.TranslateIndexDB(context.Background(), "nonExistentIndex", 0, r) + expected := fmt.Errorf("index %q not found", "nonExistentIndex") + if !reflect.DeepEqual(err, expected) { + t.Fatalf("expected '%#v', got '%#v'", expected, err) + } +} + +func TestTranslateIndexDbOnNilTranslateStore(t *testing.T) { + api := API{} + indexes := make(map[string]*Index) + indexes["index"] = &Index{name: "index"} + api.holder = &Holder{indexes: indexes} + r := strings.NewReader("not important tbh") + err := api.TranslateIndexDB(context.Background(), "index", 0, r) + expected := fmt.Errorf("index %q has no translate store", "index") + if !reflect.DeepEqual(err, expected) { + t.Fatalf("expected '%#v', got '%#v'", expected, err) + } +} + +func TestTranslateFieldDbOnNilIndex(t *testing.T) { + api := API{} + api.holder = &Holder{} + r := strings.NewReader("not important tbh") + err := api.TranslateFieldDB(context.Background(), "nonExistentIndex", "field", r) + expected := fmt.Errorf("index %q not found", "nonExistentIndex") + if !reflect.DeepEqual(err, expected) { + t.Fatalf("expected '%#v', got '%#v'", expected, err) + } +} + +func TestTranslateFieldDbOnNilField(t *testing.T) { + api := API{} + indexes := make(map[string]*Index) + indexes["index"] = &Index{name: "index"} + api.holder = &Holder{indexes: indexes} + r := strings.NewReader("not important tbh") + err := api.TranslateFieldDB(context.Background(), "index", "nonExistentField", r) + expected := fmt.Errorf("field %q/%q not found", "index", "nonExistentField") + if !reflect.DeepEqual(err, expected) { + t.Fatalf("expected '%#v', got '%#v'", expected, err) + } +} + +func TestTranslateFieldDbOnNilFieldWithFieldName_keys(t *testing.T) { + api := API{} + indexes := make(map[string]*Index) + indexes["index"] = &Index{name: "index"} + api.holder = &Holder{indexes: indexes} + r := strings.NewReader("not important tbh") + err := api.TranslateFieldDB(context.Background(), "index", "_keys", r) + if err != nil { + t.Fatalf("expected 'nil', got '%#v'", err) + } +} + +func TestTranslateFieldDbOnNilTranslateStore(t *testing.T) { + api := API{} + indexes := make(map[string]*Index) + fields := make(map[string]*Field) + fields["field"] = &Field{} + indexes["index"] = &Index{name: "index", fields: fields} + api.holder = &Holder{indexes: indexes} + r := strings.NewReader("not important tbh") + err := api.TranslateFieldDB(context.Background(), "index", "field", r) + expected := fmt.Errorf("field %q/%q has no translate store", "index", "field") + if !reflect.DeepEqual(err, expected) { + t.Fatalf("expected '%#v', got '%#v'", expected, err) + } +}