<commit_before>package main

import (
	"fmt"
	"os"
	"testing"

	"github.com/doozr/qbot/queue"
)

func TestDifferentQueueIsSaved(t *testing.T) {
	var fileWritten string
	var contentWritten []byte
	var permsWritten os.FileMode
	writeFile := func(f string, c []byte, p os.FileMode) error {
		fileWritten = f
		contentWritten = c
		permsWritten = p
		return nil
	}

	persist := createPersister(writeFile, "output.json", queue.Queue{})
	err := persist(queue.Queue{
		queue.Item{ID: "U12345", Reason: "A reason"},
		queue.Item{ID: "U67890", Reason: "Another reason"},
	})
	if err != nil {
		t.Fatal("Unexpected error ", err)
	}

	if fileWritten != "output.json" {
		t.Fatal("Incorrect file written: ", fileWritten)
	}

	if string(contentWritten) != `[{"ID":"U12345","Reason":"A reason"},{"ID":"U67890","Reason":"Another reason"}]` {
		t.Fatal("Incorrect content written: ", contentWritten)
	}

	if permsWritten != 0644 {
		t.Fatal("Incorrect file perms: ", permsWritten)
	}
}

func TestIdenticalQueueIsNotWritten(t *testing.T) {
	writeFile := func(f string, c []byte, p os.FileMode) error {
		t.Fatal("Unexpected call to writeFile")
		return nil
	}

	q := queue.Queue{
		queue.Item{ID: "U12345", Reason: "A reason"},
		queue.Item{ID: "U67890", Reason: "Another reason"},
	}

	persist := createPersister(writeFile, "output.json", q)
	err := persist(q)
	if err != nil {
		t.Fatal("Unexpected error ", err)
	}
}

func TestReturnsErrorIfWriteFails(t *testing.T) {
	writeFile := func(f string, c []byte, p os.FileMode) error {
		return fmt.Errorf("Error!")
	}

	persist := createPersister(writeFile, "output.json", queue.Queue{})
	err := persist(queue.Queue{queue.Item{ID: "U1234", Reason: "A reason"}})
	if err == nil {
		t.Fatal("Expected error")
	}
}

func TestSavesIfFirstAttemptFails(t *testing.T) {
	calls := 0
	var contentWritten []byte
	writeFile := func(f string, c []byte, p os.FileMode) error {
		if calls == 0 {
			calls++
			return fmt.Errorf("Error!")
		}
		calls++
		contentWritten = c
		return nil
	}

	q := queue.Queue{
		queue.Item{ID: "U12345", Reason: "A reason"},
		queue.Item{ID: "U67890", Reason: "Another reason"},
	}

	persist := createPersister(writeFile, "output.json", queue.Queue{})
	err := persist(q)
	if err == nil {
		t.Fatal("Expected error on first call")
	}

	if contentWritten != nil {
		t.Fatal("Expected no content written, got ", string(contentWritten))
	}

	err = persist(q)
	if err != nil {
		t.Fatal("Unexpected error on second call ", err)
	}

	if calls != 2 {
		t.Fatal("Expected 2 calls, got ", calls)
	}

	if string(contentWritten) != `[{"ID":"U12345","Reason":"A reason"},{"ID":"U67890","Reason":"Another reason"}]` {
		t.Fatal("Incorrect content written: ", contentWritten)
	}
}<commit_msg>Clean up persist tests<commit_after>package main

import (
	"fmt"
	"os"
	"testing"

	"github.com/doozr/qbot/queue"
)

func TestDifferentQueueIsSaved(t *testing.T) {
	var fileWritten string
	var contentWritten []byte
	var permsWritten os.FileMode
	writeFile := func(f string, c []byte, p os.FileMode) error {
		fileWritten = f
		contentWritten = c
		permsWritten = p
		return nil
	}

	persist := createPersister(writeFile, "output.json", queue.Queue{})
	persist(queue.Queue{
		queue.Item{ID: "U12345", Reason: "A reason"},
		queue.Item{ID: "U67890", Reason: "Another reason"},
	})

	if fileWritten != "output.json" {
		t.Fatal("Incorrect file written: ", fileWritten)
	}

	if string(contentWritten) != `[{"ID":"U12345","Reason":"A reason"},{"ID":"U67890","Reason":"Another reason"}]` {
		t.Fatal("Incorrect content written: ", contentWritten)
	}

	if permsWritten != 0644 {
		t.Fatal("Incorrect file perms: ", permsWritten)
	}
}

func TestIdenticalQueueIsNotWritten(t *testing.T) {
	writeFile := func(f string, c []byte, p os.FileMode) error {
		t.Fatal("Unexpected call to writeFile")
		return nil
	}

	q := queue.Queue{
		queue.Item{ID: "U12345", Reason: "A reason"},
		queue.Item{ID: "U67890", Reason: "Another reason"},
	}

	persist := createPersister(writeFile, "output.json", q)
	persist(q)
}

func TestReturnsErrorIfWriteFails(t *testing.T) {
	writeFile := func(f string, c []byte, p os.FileMode) error {
		return fmt.Errorf("Error!")
	}

	persist := createPersister(writeFile, "output.json", queue.Queue{})
	err := persist(queue.Queue{queue.Item{ID: "U1234", Reason: "A reason"}})
	if err == nil {
		t.Fatal("Expected error")
	}
}

func TestStillSavesAfterFailure(t *testing.T) {
	called := false
	var contentWritten []byte
	writeFile := func(f string, c []byte, p os.FileMode) error {
		if !called {
			called = true
			return fmt.Errorf("Error!")
		}
		contentWritten = c
		return nil
	}

	q := queue.Queue{
		queue.Item{ID: "U12345", Reason: "A reason"},
		queue.Item{ID: "U67890", Reason: "Another reason"},
	}

	persist := createPersister(writeFile, "output.json", queue.Queue{})
	persist(q)
	persist(q)

	if string(contentWritten) != `[{"ID":"U12345","Reason":"A reason"},{"ID":"U67890","Reason":"Another reason"}]` {
		t.Fatal("Incorrect content written: ", contentWritten)
	}
}
{'fraction_non_alphanumeric': 0.16935644550707313, 'fraction_numerical': 0.023909145248057383, 'mean_word_length': 4.559246954595792, 'pattern_counts': {'":': 16, '<': 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': '456627', 'n_tokens_mistral': 2028, 'n_tokens_neox': 1770, 'n_words': 430}