<commit_before>package test

import (
	"context"
	"net/http/httptest"
	"testing"

	"github.com/Clever/wag/samples/gen-wag-patch/client"
	"github.com/Clever/wag/samples/gen-wag-patch/models"
	"github.com/Clever/wag/samples/gen-wag-patch/server"
	"github.com/stretchr/testify/assert"
)

type WagPatchController struct {
	Data *models.Data
}

func (w *WagPatchController) Wagpatch(ctx context.Context, i *models.PatchData) (*models.Data, error) {

	if i.ID != nil {
		w.Data.ID = *i.ID
	}
	if i.ArrayField != nil {
		w.Data.ArrayField = i.ArrayField
	}
	if i.Num != nil {
		w.Data.Num = *i.Num
	}
	if i.Nested != nil {
		w.Data.Nested = i.Nested
	}
	return w.Data, nil
}

func TestWagPatch(t *testing.T) {
	s := server.New(&WagPatchController{Data: &models.Data{}}, "")
	testServer := httptest.NewServer(s.Handler)
	c := client.New(testServer.URL)

	// Patch to starting values
	// TODO: Add nested field
	id := "id"
	num := int64(4)
	out, err := c.Wagpatch(context.Background(), &models.PatchData{
		ID:         &id,
		ArrayField: []string{"start"},
		Num:        &num,
		Nested:     &models.NestedData{Name: "test"}})
	assert.NoError(t, err)
	assert.Equal(t, "id", out.ID)
	assert.Equal(t, int64(4), out.Num)
	assert.Equal(t, 1, len(out.ArrayField))
	assert.Equal(t, "test", out.Nested.Name)

	// Setting the values to nil shouldn't do anything
	out, err = c.Wagpatch(context.Background(), &models.PatchData{
		ID:         nil,
		ArrayField: nil,
		Num:        nil,
		Nested:     nil,
	})
	assert.NoError(t, err)
	assert.Equal(t, "id", out.ID)
	assert.Equal(t, int64(4), out.Num)
	assert.Equal(t, 1, len(out.ArrayField))
	assert.Equal(t, "test", out.Nested.Name)

	id = ""
	num = int64(0)
	out, err = c.Wagpatch(context.Background(), &models.PatchData{
		ID: &id,
		//ArrayField: []string{},
		Num:    &num,
		Nested: &models.NestedData{}})
	assert.NoError(t, err)
	assert.Equal(t, "", out.ID)
	assert.Equal(t, int64(0), out.Num)
	// TODO: Fix this in go-swagger
	//assert.Equal(t, 0, len(out.ArrayField))
	assert.Equal(t, "", out.Nested.Name)<commit_msg>Update the test<commit_after>package test

import (
	"context"
	"net/http/httptest"
	"testing"

	"github.com/Clever/wag/samples/gen-wag-patch/client"
	"github.com/Clever/wag/samples/gen-wag-patch/models"
	"github.com/Clever/wag/samples/gen-wag-patch/server"
	"github.com/stretchr/testify/assert"
)

type WagPatchController struct {
	Data *models.Data
}

func (w *WagPatchController) Wagpatch(ctx context.Context, i *models.PatchData) (*models.Data, error) {

	if i.ID != nil {
		w.Data.ID = *i.ID
	}
	if i.ArrayField != nil {
		w.Data.ArrayField = i.ArrayField
	}
	if i.Num != nil {
		w.Data.Num = *i.Num
	}
	if i.Nested != nil {
		w.Data.Nested = i.Nested
	}
	return w.Data, nil
}

func TestWagPatch(t *testing.T) {
	s := server.New(&WagPatchController{Data: &models.Data{}}, "")
	testServer := httptest.NewServer(s.Handler)
	c := client.New(testServer.URL)

	// Patch to starting values
	// TODO: Add nested field
	id := "id"
	num := int64(4)
	out, err := c.Wagpatch(context.Background(), &models.PatchData{
		ID:         &id,
		ArrayField: []string{"start"},
		Num:        &num,
		Nested:     &models.NestedData{Name: "test"}})
	assert.NoError(t, err)
	assert.Equal(t, "id", out.ID)
	assert.Equal(t, int64(4), out.Num)
	assert.Equal(t, 1, len(out.ArrayField))
	assert.Equal(t, "test", out.Nested.Name)

	// Setting the values to nil shouldn't do anything
	out, err = c.Wagpatch(context.Background(), &models.PatchData{
		ID:         nil,
		ArrayField: nil,
		Num:        nil,
		Nested:     nil,
	})
	assert.NoError(t, err)
	assert.Equal(t, "id", out.ID)
	assert.Equal(t, int64(4), out.Num)
	assert.Equal(t, 1, len(out.ArrayField))
	assert.Equal(t, "test", out.Nested.Name)

	id = ""
	num = int64(0)
	out, err = c.Wagpatch(context.Background(), &models.PatchData{
		ID:         &id,
		ArrayField: []string{},
		Num:        &num,
		Nested:     &models.NestedData{}})
	assert.NoError(t, err)
	assert.Equal(t, "", out.ID)
	assert.Equal(t, int64(0), out.Num)
	assert.Equal(t, 0, len(out.ArrayField))
	assert.Equal(t, "", out.Nested.Name)
{'fraction_non_alphanumeric': 0.17901687454145268, 'fraction_numerical': 0.00880410858400587, 'mean_word_length': 3.58006718924972, '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': '1742734', 'n_tokens_mistral': 1725, 'n_tokens_neox': 1657, 'n_words': 320}