<commit_before>package s3gof3r

import (
	"bytes"
	"errors"
	"io"
	"io/ioutil"
	"os"
	"testing"
)

type putTest struct {
	url       string
	file_path string
	header    string
	check     bool
	out       string
}

type getTest struct {
	path   string
	config *Config
	rSize  int64
	err    error
}

var getTests = []getTest{
	{"testfile", nil, 22, nil},
	{"NoKey", nil, 0, &respError{StatusCode: 404, Message: "The specified key does not exist."}},
}

func TestGetReader(t *testing.T) {
	b, err := testBucket()
	if err != nil {
		t.Fatal(err)
	}
	for _, tt := range getTests {
		r, h, err := b.GetReader(tt.path, tt.config)
		if !errorMatch(err, tt.err) {
			t.Errorf("GetReader called with %v\n Expected: %v\n Actual:   %v\n", tt, tt.err, err)
		}
		if err != nil {
			break
		}
		t.Logf("headers %v\n", h)
		w := ioutil.Discard

		n, err := io.Copy(w, r)
		if err != nil {
			t.Error(err)
		}
		if n != tt.rSize {
			t.Errorf("Expected size: %d. Actual: %d", tt.rSize, n)

		}
		err = r.Close()
		if err != nil {
			t.Error(err)
		}

	}
}

func testBucket() (*Bucket, error) {
	k, err := EnvKeys()
	if err != nil {
		return nil, err
	}
	bucket := os.Getenv("TEST_BUCKET")
	if bucket == "" {
		return nil, errors.New("TEST_BUCKET must be set in environment.")

	}
	s3 := New("", k)
	b := s3.Bucket(bucket)

	w, err := b.PutWriter("testfile", nil, nil)
	if err != nil {
		return nil, err
	}
	r := bytes.NewReader([]byte("Test file content....."))
	_, err = io.Copy(w, r)
	if err != nil {
		return nil, err
	}
	err = w.Close()
	if err != nil {
		return nil, err
	}

	return b, nil

}

func errorMatch(expect, actual error) bool {

	if expect == nil && actual == nil {
		return true
	}

	if expect == nil || actual == nil {
		return false
	}

	return expect.Error() == actual.Error()

}

func ExampleBucket_PutWriter() {

	file, err := os.Open("fileName") // open file to upload
	if err != nil {
		return
	}

	k, err := EnvKeys() // get S3 keys from environment
	if err != nil {
		return
	}
	// Open bucket to put file into
	s3 := New("", k)
	b := s3.Bucket("bucketName")

	// Open a PutWriter for upload
	w, err := b.PutWriter(file.Name(), nil, nil)
	if err != nil {
		return
	}
	defer w.Close()
	if _, err = io.Copy(w, file); err != nil { // Copy into S3
		return
	}
}<commit_msg>Add GetReader example to tests. #5<commit_after>package s3gof3r

import (
	"bytes"
	"errors"
	"fmt"
	"io"
	"io/ioutil"
	"os"
	"testing"
)

type putTest struct {
	url       string
	file_path string
	header    string
	check     bool
	out       string
}

type getTest struct {
	path   string
	config *Config
	rSize  int64
	err    error
}

var getTests = []getTest{
	{"testfile", nil, 22, nil},
	{"NoKey", nil, 0, &respError{StatusCode: 404, Message: "The specified key does not exist."}},
}

func TestGetReader(t *testing.T) {
	b, err := testBucket()
	if err != nil {
		t.Fatal(err)
	}
	for _, tt := range getTests {
		r, h, err := b.GetReader(tt.path, tt.config)
		if !errorMatch(err, tt.err) {
			t.Errorf("GetReader called with %v\n Expected: %v\n Actual:   %v\n", tt, tt.err, err)
		}
		if err != nil {
			break
		}
		t.Logf("headers %v\n", h)
		w := ioutil.Discard

		n, err := io.Copy(w, r)
		if err != nil {
			t.Error(err)
		}
		if n != tt.rSize {
			t.Errorf("Expected size: %d. Actual: %d", tt.rSize, n)

		}
		err = r.Close()
		if err != nil {
			t.Error(err)
		}

	}
}

func testBucket() (*Bucket, error) {
	k, err := EnvKeys()
	if err != nil {
		return nil, err
	}
	bucket := os.Getenv("TEST_BUCKET")
	if bucket == "" {
		return nil, errors.New("TEST_BUCKET must be set in environment.")

	}
	s3 := New("", k)
	b := s3.Bucket(bucket)

	w, err := b.PutWriter("testfile", nil, nil)
	if err != nil {
		return nil, err
	}
	r := bytes.NewReader([]byte("Test file content....."))
	_, err = io.Copy(w, r)
	if err != nil {
		return nil, err
	}
	err = w.Close()
	if err != nil {
		return nil, err
	}

	return b, nil

}

func errorMatch(expect, actual error) bool {

	if expect == nil && actual == nil {
		return true
	}

	if expect == nil || actual == nil {
		return false
	}

	return expect.Error() == actual.Error()

}

func ExampleBucket_PutWriter() error {

	k, err := EnvKeys() // get S3 keys from environment
	if err != nil {
		return err
	}
	// Open bucket to put file into
	s3 := New("", k)
	b := s3.Bucket("bucketName")

	// open file to upload
	file, err := os.Open("fileName")
	if err != nil {
		return err
	}

	// Open a PutWriter for upload
	w, err := b.PutWriter(file.Name(), nil, nil)
	if err != nil {
		return err
	}
	if _, err = io.Copy(w, file); err != nil { // Copy into S3
		return err
	}
	if err = w.Close(); err != nil {
		return err
	}
	return nil
}

func ExampleBucket_GetReader() error {

	k, err := EnvKeys() // get S3 keys from environment
	if err != nil {
		return err
	}

	// Open bucket to put file into
	s3 := New("", k)
	b := s3.Bucket("bucketName")

	r, h, err := b.GetReader("keyName", nil)
	if err != nil {
		return err
	}
	// stream to standard output
	if _, err = io.Copy(os.Stdout, r); err != nil {
		return err
	}
	err = r.Close()
	if err != nil {
		return err
	}
	fmt.Println(h) // print key header data
	return nil
}
{'fraction_non_alphanumeric': 0.16280428432327165, 'fraction_numerical': 0.007010710808179162, 'mean_word_length': 2.757132406730066, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2306051', 'n_tokens_mistral': 2135, 'n_tokens_neox': 2019, 'n_words': 615}