Skip to content

Improved Unit Test Coverage from 51.6% to 58.1% with Keploy AI #1505

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 14 commits into
base: master
Choose a base branch
from
51 changes: 51 additions & 0 deletions apps/nsq_stat/nsq_stat_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package main

import (
"testing"
)


// Test generated using Keploy
func TestNumValue_Set_ValidInput(t *testing.T) {
nv := &numValue{}
err := nv.Set("42")
if err != nil {
t.Errorf("Expected no error, got %v", err)
}
if nv.value != 42 {
t.Errorf("Expected value to be 42, got %v", nv.value)
}
if !nv.isSet {
t.Errorf("Expected isSet to be true, got %v", nv.isSet)
}
}

// Test generated using Keploy
func TestNumValue_Set_InvalidInput(t *testing.T) {
nv := &numValue{}
err := nv.Set("invalid")
if err == nil {
t.Errorf("Expected an error, got nil")
}
}


// Test generated using Keploy
func TestCheckAddrs_InvalidAddress(t *testing.T) {
addrs := []string{"http://example.com"}
err := checkAddrs(addrs)
if err == nil {
t.Errorf("Expected an error, got nil")
}
}


// Test generated using Keploy
func TestCheckAddrs_ValidAddresses(t *testing.T) {
addrs := []string{"example.com", "localhost:8080"}
err := checkAddrs(addrs)
if err != nil {
t.Errorf("Expected no error, got %v", err)
}
}

4 changes: 3 additions & 1 deletion apps/nsq_tail/nsq_tail.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ var (
topics = app.StringArray{}
)

var appExit = os.Exit

func init() {
flag.Var(&nsqdTCPAddrs, "nsqd-tcp-address", "nsqd TCP address (may be given multiple times)")
flag.Var(&lookupdHTTPAddrs, "lookupd-http-address", "lookupd HTTP address (may be given multiple times)")
Expand Down Expand Up @@ -63,7 +65,7 @@ func (th *TailHandler) HandleMessage(m *nsq.Message) error {
log.Fatalf("ERROR: failed to write to os.Stdout - %s", err)
}
if th.totalMessages > 0 && th.messagesShown >= th.totalMessages {
os.Exit(0)
appExit(0)
}
return nil
}
Expand Down
105 changes: 105 additions & 0 deletions apps/nsq_tail/nsq_tail_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
package main

import (
"os"
"testing"
"github.com/nsqio/go-nsq"
"bytes"
)


// Test generated using Keploy
func TestMain_VersionFlag(t *testing.T) {
os.Args = []string{"cmd", "--version"}
exitCode := 0
exitFunc := func(code int) {
exitCode = code
}
appExit = exitFunc
defer func() { appExit = os.Exit }()

main()

if exitCode != 0 {
t.Errorf("Expected exit code 0, got %d", exitCode)
}
}

// Test generated using Keploy
func TestTailHandler_HandleMessage_Exit(t *testing.T) {
handler := &TailHandler{
topicName: "test_topic",
totalMessages: 1,
messagesShown: 0,
}

message := &nsq.Message{
Body: []byte("test message"),
}

exitCode := 0
exitFunc := func(code int) {
exitCode = code
}
appExit = exitFunc
defer func() { appExit = os.Exit }()

handler.HandleMessage(message)

if exitCode != 0 {
t.Errorf("Expected exit code 0, got %d", exitCode)
}
}


// Test generated using Keploy
func TestTailHandler_HandleMessage_PrintTopic(t *testing.T) {
// Set printTopic flag to true
originalPrintTopic := *printTopic
*printTopic = true
defer func() { *printTopic = originalPrintTopic }()

// Replace os.Stdout with a pipe
r, w, err := os.Pipe()
if err != nil {
t.Fatalf("Failed to create pipe: %v", err)
}
originalStdout := os.Stdout
os.Stdout = w
defer func() {
os.Stdout = originalStdout
w.Close()
r.Close()
}()

handler := &TailHandler{
topicName: "test_topic",
totalMessages: 0,
messagesShown: 0,
}

message := &nsq.Message{
Body: []byte("test message"),
}

err = handler.HandleMessage(message)
if err != nil {
t.Errorf("Expected no error, got %v", err)
}

// Close writer to allow reading
w.Close()

// Read output
var buf bytes.Buffer
_, err = buf.ReadFrom(r)
if err != nil {
t.Fatalf("Failed to read from pipe: %v", err)
}

expectedOutput := "test_topic | test message\n"
if buf.String() != expectedOutput {
t.Errorf("Expected output %q, got %q", expectedOutput, buf.String())
}
}

107 changes: 107 additions & 0 deletions apps/nsq_to_file/file_logger_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
package main

import (
"testing"

"os"

"github.com/nsqio/go-nsq"
"github.com/nsqio/nsq/internal/lg"
)

// Test generated using Keploy
func TestFileLogger_HandleMessage(t *testing.T) {
logChan := make(chan *nsq.Message, 1)
f := &FileLogger{
logChan: logChan,
}

msg := &nsq.Message{}
err := f.HandleMessage(msg)
if err != nil {
t.Errorf("Expected no error, got %v", err)
}

select {
case receivedMsg := <-logChan:
if receivedMsg != msg {
t.Errorf("Expected message %v, got %v", msg, receivedMsg)
}
default:
t.Errorf("Expected message to be sent to log channel, but it was not")
}
}

// Test generated using Keploy
func TestNewFileLogger_MissingREVPlaceholder(t *testing.T) {
opts := &Options{
FilenameFormat: "<TOPIC>-<HOST>",
GZIP: true,
}
logf := func(lvl lg.LogLevel, f string, args ...interface{}) {}
cfg := nsq.NewConfig()

_, err := NewFileLogger(logf, opts, "test_topic", cfg)
if err == nil {
t.Errorf("Expected error due to missing <REV> in filename format, got nil")
}
}

// Test generated using Keploy
func TestNewFileLogger_InvalidTopicName(t *testing.T) {
opts := &Options{
Channel: "test_channel",
}
logf := func(lvl lg.LogLevel, f string, args ...interface{}) {}
cfg := nsq.NewConfig()

_, err := NewFileLogger(logf, opts, "", cfg)
if err == nil {
t.Errorf("Expected error due to invalid topic name, got nil")
}
}

// Test generated using Keploy
func TestExclusiveRename_Success(t *testing.T) {
srcFile, err := os.CreateTemp("", "srcFile")
if err != nil {
t.Fatalf("Failed to create temp source file: %v", err)
}
defer os.Remove(srcFile.Name())

dstFileName := srcFile.Name() + "_renamed"

err = exclusiveRename(srcFile.Name(), dstFileName)
if err != nil {
t.Errorf("Expected no error, got %v", err)
}

if _, err := os.Stat(dstFileName); os.IsNotExist(err) {
t.Errorf("Expected destination file %s to exist, but it does not", dstFileName)
}
defer os.Remove(dstFileName)
}

// Test generated using Keploy

// Test generated using Keploy
func TestMakeDirFromPath_InvalidPath(t *testing.T) {
path := "/invalid_path/testdir"

err := makeDirFromPath(func(lvl lg.LogLevel, f string, args ...interface{}) {}, path)
if err == nil {
t.Errorf("Expected error due to invalid path, got nil")
}
}



func TestExclusiveRename_SourceFileNotExist(t *testing.T) {
src := "nonexistent_file"
dst := "destination_file"

err := exclusiveRename(src, dst)
if err == nil {
t.Errorf("Expected error due to nonexistent source file, got nil")
}
}
34 changes: 34 additions & 0 deletions apps/nsq_to_file/nsq_to_file_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package main

import (
"testing"
)

// Test generated using Keploy
func TestFlagSetInitialization(t *testing.T) {
fs := flagSet()

if fs == nil {
t.Fatal("Expected FlagSet to be initialized, got nil")
}

if fs.Lookup("version") == nil {
t.Error("Expected 'version' flag to be defined")
}

if fs.Lookup("log-level") == nil {
t.Error("Expected 'log-level' flag to be defined")
}

if fs.Lookup("channel") == nil {
t.Error("Expected 'channel' flag to be defined")
}

if fs.Lookup("output-dir") == nil {
t.Error("Expected 'output-dir' flag to be defined")
}

if fs.Lookup("rotate-size") == nil {
t.Error("Expected 'rotate-size' flag to be defined")
}
}
14 changes: 14 additions & 0 deletions apps/nsq_to_file/options_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package main

import (
"testing"
)


// Test generated using Keploy
func TestNewOptions_NotNil(t *testing.T) {
opts := NewOptions()
if opts == nil {
t.Errorf("Expected NewOptions() to return a non-nil pointer.")
}
}
30 changes: 30 additions & 0 deletions apps/nsq_to_file/strftime_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package main

import (
"testing"
"time"
)


// Test generated using Keploy
func TestStrftime_SimpleFormat(t *testing.T) {
inputTime := time.Date(2023, 10, 1, 12, 0, 0, 0, time.UTC)
format := "Simple format"
expected := "Simple format"
result := strftime(format, inputTime)
if result != expected {
t.Errorf("Expected %v, got %v", expected, result)
}
}

// Test generated using Keploy
func TestStrftime_SingleSpecifier(t *testing.T) {
inputTime := time.Date(2023, 10, 1, 12, 0, 0, 0, time.UTC)
format := "%Y"
expected := "2023"
result := strftime(format, inputTime)
if result != expected {
t.Errorf("Expected %v, got %v", expected, result)
}
}

Loading