From 810a602a1bbcb09029d26a283dd5dc8ecabaf1b1 Mon Sep 17 00:00:00 2001 From: Tom Carrio Date: Sat, 14 Jul 2018 22:35:05 -0400 Subject: [PATCH] Added testing on query helper functions --- builder/openstack/image_query_test.go | 100 ++++++++++++++++++++++++++ 1 file changed, 100 insertions(+) create mode 100644 builder/openstack/image_query_test.go diff --git a/builder/openstack/image_query_test.go b/builder/openstack/image_query_test.go new file mode 100644 index 000000000..90808bdaf --- /dev/null +++ b/builder/openstack/image_query_test.go @@ -0,0 +1,100 @@ +package openstack + +import ( + "testing" + "github.com/gophercloud/gophercloud/openstack/imageservice/v2/images" +) + +func TestGetImageFilter(t *testing.T) { + passedExpectedMap := map[string]images.ImageDateFilter{ + "gt": images.FilterGT, + "gte": images.FilterGTE, + "lt": images.FilterLT, + "lte": images.FilterLTE, + "neq": images.FilterNEQ, + "ne": images.FilterEQ, + } + + for passed, expected := range passedExpectedMap { + filter, err := getDateFilter(passed) + if err != nil { + t.Errorf("Passed %s, received error: %s", passed, err.Error()) + } else if filter != expected { + t.Errorf("Expected %s, got %s", string(expected), filter) + } + } +} + +func TestBuildImageFilter(t *testing.T) { + testOpts := images.ListOpts{} + + filters := map[string]string{ + "limit": "3", + "name": "Ubuntu 16.04", + "visibility": "public", + "image_status": "active", + "size_min": "0", + "sort": "created_at:desc", + } + + multiErr := buildImageFilters(filters, &testOpts) + + if multiErr != nil { + for _, err := range multiErr.Errors { + t.Error(err) + } + } + + if testOpts.Limit != 3 { + t.Errorf("Limit did not parse correctly") + } + + if testOpts.Name != filters["name"] { + t.Errorf("Name did not parse correctly") + } + + if testOpts.Visibility != images.ImageVisibility(filters["visibility"]) { + t.Errorf("Visibility did not parse correctly") + } + + if testOpts.Status != images.ImageStatus(filters["image_status"]) { + t.Errorf("Image status did not parse correctly") + } + + if testOpts.SizeMin != 0 { + t.Errorf("Size min did not parse correctly") + } + + if testOpts.Sort != filters["sort"] { + t.Errorf("Limit did not parse correctly") + } +} + +func TestApplyMostRecent(t *testing.T) { + testOpts := images.ListOpts{ + Name: "RHEL 7.0", + SizeMin: 0, + } + + applyMostRecent(&testOpts) + + if testOpts.Sort != "created_at:desc" { + t.Errorf("Error applying most recent filter: sort") + } + + if testOpts.SortDir != "desc" || testOpts.SortKey != "created_at" { + t.Errorf("Error applying most recent filter: sort_dir/sort_key") + } +} + +func TestDateToImageDateQuery(t *testing.T) { + tests := [][2]string{ + {"2006-01-02T15:04:05Z07:00", "created_at"}, + } + + for _, test := range tests { + if _, err := dateToImageDateQuery(&test[0], &test[1]); err != nil { + t.Error(err) + } + } +} \ No newline at end of file