github.com/verygoodsoftwarenotvirus/cartogopher@v0.0.0-20160213112503-f5fe2b6d4bd1/writer_test.go (about)

     1  package cartogopher
     2  
     3  import (
     4  	"os"
     5  	"strconv"
     6  	"testing"
     7  )
     8  
     9  func TestWriterCreationAndOutput(t *testing.T) {
    10  	testCSVFileName := "test_csvs/writer_test.csv"
    11  	testHeaders := []string{"first", "second", "third"}
    12  	testFile, err := os.Create(testCSVFileName)
    13  	if err != nil {
    14  		t.Errorf("Error returned while creating file: %v", err)
    15  	}
    16  	defer testFile.Close()
    17  	writer := NewWriter(testFile, testHeaders)
    18  
    19  	// first test that writing a row at all works.
    20  	completeMap := map[string]string{
    21  		"first":  "one",
    22  		"second": "two",
    23  		"third":  "three",
    24  	}
    25  	err = writer.Write(completeMap)
    26  	if err != nil {
    27  		t.Errorf("Error returned while writing row to file: %v", err)
    28  	}
    29  
    30  	// let's test making an empty map and writing it.
    31  	emptyMap := make(map[string]string, len(completeMap))
    32  	err = writer.Write(emptyMap)
    33  	if err != nil {
    34  		t.Errorf("Error returned while writing row to file: %v", err)
    35  	}
    36  
    37  	// then test that the row can be an 'incomplete' map
    38  	incompleteMap := map[string]string{
    39  		"first": "The Fellowship of the Ring",
    40  		"third": "The Return of the King",
    41  	}
    42  	err = writer.Write(incompleteMap)
    43  	if err != nil {
    44  		t.Errorf("Error returned while writing row to file: %v", err)
    45  	}
    46  
    47  	// providing a map that is too large, however, should throw an error
    48  	tooBigAMap := map[string]string{
    49  		"first":  "one",
    50  		"second": "two",
    51  		"third":  "three",
    52  	}
    53  	for i := 0; i < 42; i++ {
    54  		tooBigAMap[strconv.Itoa(i)] = strconv.Itoa(i)
    55  	}
    56  	err = writer.Write(tooBigAMap)
    57  	if err == nil {
    58  		t.Errorf("No error returned while writing a row too big for the file")
    59  	}
    60  
    61  	// providing a map with an invalid field should throw an error
    62  	badMap := map[string]string{
    63  		"none":   "Athos",
    64  		"should": "Aramis",
    65  		"match":  "Porthos",
    66  	}
    67  	err = writer.Write(badMap)
    68  	if err == nil {
    69  		t.Errorf("No error returned while writing a bad row to the file")
    70  	}
    71  	// write the rows already
    72  	writer.Flush()
    73  
    74  	// test the content of the created CSV.
    75  	// TODO
    76  
    77  	// clean up created files.
    78  	os.Remove(testCSVFileName)
    79  }
    80  
    81  func BenchmarkWriterShort(b *testing.B) {
    82  	testCSVFileName := "test_csvs/writer_short_benchmark.csv"
    83  	testHeaders := []string{"first", "second", "third"}
    84  	testFile, err := os.Create(testCSVFileName)
    85  	if err != nil {
    86  		b.Errorf("Error returned while creating file: %v", err)
    87  	}
    88  	defer testFile.Close()
    89  	writer := NewWriter(testFile, testHeaders)
    90  
    91  	simpleRow := map[string]string{
    92  		"first":  "one",
    93  		"second": "two",
    94  		"third":  "three",
    95  	}
    96  	err = writer.Write(simpleRow)
    97  	if err != nil {
    98  		b.Errorf("Error returned while writing row to file: %v", err)
    99  	}
   100  	writer.Flush()
   101  	os.Remove(testCSVFileName)
   102  }
   103  
   104  func BenchmarkWriterLonger(b *testing.B) {
   105  	testCSVFileName := "test_csvs/writer_longer_benchmark.csv"
   106  	arbitrarilyLargeNumber := 100
   107  	testHeaders := make([]string, arbitrarilyLargeNumber)
   108  	for i := 0; i < arbitrarilyLargeNumber; i++ {
   109  		testHeaders[i] = strconv.Itoa(i)
   110  	}
   111  	testFile, err := os.Create(testCSVFileName)
   112  	if err != nil {
   113  		b.Errorf("Error returned while creating file: %v", err)
   114  	}
   115  	defer testFile.Close()
   116  	writer := NewWriter(testFile, testHeaders)
   117  
   118  	for rowNumber := 0; rowNumber < arbitrarilyLargeNumber*10; rowNumber++ {
   119  		newRow := map[string]string{}
   120  		for i := 0; i < arbitrarilyLargeNumber; i++ {
   121  			newRow[strconv.Itoa(i)] = strconv.Itoa(i)
   122  		}
   123  		err = writer.Write(newRow)
   124  		if err != nil {
   125  			b.Errorf("Error returned while writing row to file: %v", err)
   126  		}
   127  	}
   128  
   129  	writer.Flush()
   130  	os.Remove(testCSVFileName)
   131  }