aboutsummaryrefslogtreecommitdiff
path: root/object_iterator_test.go
diff options
context:
space:
mode:
authorStefan Majewsky <majewsky@gmx.net>2018-02-19 21:30:33 +0100
committerStefan Majewsky <majewsky@gmx.net>2018-02-19 21:33:49 +0100
commit60d4779889baedc44972d4749daa073efca3b25c (patch)
treed47746971f659d6f7e3affe428f239b289954f5b /object_iterator_test.go
parent8f777460661bbbcbe42730979140f525b382110e (diff)
downloadgo-schwift-60d4779889baedc44972d4749daa073efca3b25c.tar.gz
reorganize code
* Gophercloud dependencies move into subpackage gopherschwift. * Tests move into subpackage tests (to avoid import cycles). + Rename "Client" to "Backend".
Diffstat (limited to 'object_iterator_test.go')
-rw-r--r--object_iterator_test.go180
1 files changed, 0 insertions, 180 deletions
diff --git a/object_iterator_test.go b/object_iterator_test.go
deleted file mode 100644
index 92fe4b1..0000000
--- a/object_iterator_test.go
+++ /dev/null
@@ -1,180 +0,0 @@
-/******************************************************************************
-*
-* Copyright 2018 Stefan Majewsky <majewsky@gmx.net>
-*
-* Licensed under the Apache License, Version 2.0 (the "License");
-* you may not use this file except in compliance with the License.
-* You may obtain a copy of the License at
-*
-* http://www.apache.org/licenses/LICENSE-2.0
-*
-* Unless required by applicable law or agreed to in writing, software
-* distributed under the License is distributed on an "AS IS" BASIS,
-* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-* See the License for the specific language governing permissions and
-* limitations under the License.
-*
-******************************************************************************/
-
-package schwift
-
-import (
- "bytes"
- "fmt"
- "testing"
-)
-
-var objectExampleContent = []byte(`{"message":"Hello World!"}`)
-var objectExampleContentEtag = etagOf(objectExampleContent)
-
-func TestObjectIterator(t *testing.T) {
- testWithContainer(t, func(c *Container) {
- oname := func(idx int) string {
- return fmt.Sprintf("schwift-test-listing%d", idx)
- }
-
- //create test objects that can be listed
- for idx := 1; idx <= 4; idx++ {
- hdr := make(ObjectHeaders)
- hdr.ContentType().Set("application/json")
- err := c.Object(oname(idx)).Upload(bytes.NewReader(objectExampleContent), hdr, nil)
- expectSuccess(t, err)
- }
-
- //test iteration with empty last page
- iter := c.Objects()
- iter.Prefix = "schwift-test-listing"
- os, err := iter.NextPage(2)
- expectSuccess(t, err)
- expectObjectNames(t, os, oname(1), oname(2))
- os, err = iter.NextPage(2)
- expectSuccess(t, err)
- expectObjectNames(t, os, oname(3), oname(4))
- os, err = iter.NextPage(2)
- expectSuccess(t, err)
- expectObjectNames(t, os)
- os, err = iter.NextPage(2)
- expectSuccess(t, err)
- expectObjectNames(t, os)
-
- //test iteration with partial last page
- iter = c.Objects()
- iter.Prefix = "schwift-test-listing"
- os, err = iter.NextPage(3)
- expectSuccess(t, err)
- expectObjectNames(t, os, oname(1), oname(2), oname(3))
- os, err = iter.NextPage(3)
- expectSuccess(t, err)
- expectObjectNames(t, os, oname(4))
- os, err = iter.NextPage(4)
- expectSuccess(t, err)
- expectObjectNames(t, os)
-
- //test detailed iteration
- iter = c.Objects()
- iter.Prefix = "schwift-test-listing"
- ois, err := iter.NextPageDetailed(2)
- expectSuccess(t, err)
- expectObjectInfos(t, ois, oname(1), oname(2))
- ois, err = iter.NextPageDetailed(3)
- expectSuccess(t, err)
- expectObjectInfos(t, ois, oname(3), oname(4))
- ois, err = iter.NextPageDetailed(3)
- expectSuccess(t, err)
- expectObjectInfos(t, ois)
- ois, err = iter.NextPageDetailed(3)
- expectSuccess(t, err)
- expectObjectInfos(t, ois)
-
- //test Foreach
- c.Invalidate()
- iter = c.Objects()
- iter.Prefix = "schwift-test-listing"
- idx := 0
- expectSuccess(t, iter.Foreach(func(o *Object) error {
- idx++
- expectString(t, o.Name(), oname(idx))
- return nil
- }))
- expectInt(t, idx, 4)
-
- if c.headers == nil {
- t.Error("ObjectIterator.Foreach did not initialize Container.Headers")
- }
-
- //test ForeachDetailed
- c.Invalidate()
- iter = c.Objects()
- iter.Prefix = "schwift-test-listing"
- idx = 0
- expectSuccess(t, iter.ForeachDetailed(func(info ObjectInfo) error {
- idx++
- expectString(t, info.Object.Name(), oname(idx))
- return nil
- }))
- expectInt(t, idx, 4)
-
- if c.headers == nil {
- t.Error("ObjectIterator.ForeachDetailed did not initialize Container.Headers")
- }
-
- //test Collect
- iter = c.Objects()
- iter.Prefix = "schwift-test-listing"
- os, err = iter.Collect()
- expectSuccess(t, err)
- expectObjectNames(t, os, oname(1), oname(2), oname(3), oname(4))
-
- //test CollectDetailed
- iter = c.Objects()
- iter.Prefix = "schwift-test-listing"
- ois, err = iter.CollectDetailed()
- expectSuccess(t, err)
- expectObjectInfos(t, ois, oname(1), oname(2), oname(3), oname(4))
- })
-}
-
-func expectObjectNames(t *testing.T, actualObjects []*Object, expectedNames ...string) {
- t.Helper()
- if len(actualObjects) != len(expectedNames) {
- t.Errorf("expected %d objects, got %d objects",
- len(expectedNames), len(actualObjects))
- return
- }
- for idx, c := range actualObjects {
- if c.Name() != expectedNames[idx] {
- t.Errorf("expected objects[%d].Name() == %q, got %q",
- idx, expectedNames[idx], c.Name())
- }
- }
-}
-
-func expectObjectInfos(t *testing.T, actualInfos []ObjectInfo, expectedNames ...string) {
- t.Helper()
- if len(actualInfos) != len(expectedNames) {
- t.Errorf("expected %d objects, got %d objects",
- len(expectedNames), len(actualInfos))
- return
- }
- for idx, info := range actualInfos {
- if info.Object.Name() != expectedNames[idx] {
- t.Errorf("expected objects[%d].Name() == %q, got %q",
- idx, expectedNames[idx], info.Object.Name())
- }
- if info.SizeBytes != uint64(len(objectExampleContent)) {
- t.Errorf("expected objects[%d] sizeBytes == %d, got %d",
- idx, len(objectExampleContent), info.SizeBytes)
- }
- if info.ContentType != "application/json" {
- t.Errorf(`expected objects[%d] contentType == "application/json", got %q`,
- idx, info.ContentType)
- }
- if info.Etag != objectExampleContentEtag {
- t.Errorf("expected objects[%d] etag == %q, got %q",
- idx, objectExampleContentEtag, info.Etag)
- }
- if info.LastModified.IsZero() {
- t.Errorf("objects[%d].LastModified is zero", idx)
- }
- }
-}