<commit_before>package twiliolo

import (
	"errors"
	"fmt"
	"net/url"
	"testing"
	"time"

	"github.com/stretchr/testify/assert"
)

var (
	dateCreated = time.Now().Add(-5 * time.Hour)
	dateUpdated = time.Now().Add(-2 * time.Hour)
	testNumber  = IncomingPhoneNumber{
		Sid:                  "TwiliololIncomingFake",
		AccountSid:           "TwilioloFake",
		FriendlyName:         "<NAME>",
		PhoneNumber:          "+33912345678",
		VoiceURL:             "http://test.com",
		VoiceMethod:          "POST",
		VoiceFallbackURL:     "http://fail.com",
		VoiceFallbackMethod:  "GET",
		VoiceCallerIDLookup:  true,
		VoiceApplicationSid:  "",
		StatusCallback:       "http://status.com",
		StatusCallbackMethod: "GET",
		SmsURL:               "http://sms.com",
		SmsMethod:            "GET",
		SmsFallbackURL:       "http://fail-sms.com",
		SmsFallbackMethod:    "GET",
		DateCreated:          dateCreated.Format(time.RFC1123Z),
		DateUpdated:          dateUpdated.Format(time.RFC1123Z),
		Capabilities:         Capabilites{MMS: false, SMS: false, Voice: true},
		APIVersion:           VERSION,
		URI:                  "/2010-04-01/Accounts/TwilioloFake/IncomingPhoneNumbers/TwiliololIncomingFake.json",
	}
)

func TestUpdatePhoneNumber(t *testing.T) {
	t.Run("OK - Success update", func(t *testing.T) {
		phoneNumber := testNumber
		phoneNumber.FriendlyName = "New Friendly Name"

		client := new(MockClient)
		client.postFn = func(params url.Values, uri string, _ []RequestOption) ([]byte, error) {
			assert.Equal(t, "/IncomingPhoneNumbers/TwiliololIncomingFake.json", uri)
			assert.Equal(t, "New Friendly Name", params.Get("FriendlyName"))

			response := fmt.Sprintf(`
			{
				"sid": "TwiliololIncomingFake",
				"account_sid": "TwilioloFake",
				"friendly_name": "<NAME>",
				"phone_number": "+33912345678",
				"voice_url": "http://test.com",
				"voice_method": "POST",
				"voice_fallback_url": "http://fail.com",
				"voice_fallback_method": "GET",
				"status_callback": "http://status.com",
				"status_callback_method": "GET",
				"voice_caller_id_lookup": true,
				"voice_application_sid": null,
				"date_created": "%s",
				"date_updated": "%s",
				"sms_url": "http://sms.com",
				"sms_method": "GET",
				"sms_fallback_url": "http://fail-sms.com",
				"sms_fallback_method": "GET",
				"sms_application_sid": null,
				"capabilities": {
					"voice": true,
					"sms": false,
					"mms": false
				},
				"beta": false,
				"api_version": "2010-04-01",
				"uri": "\/2010-04-01\/Accounts\/TwilioloFake\/IncomingPhoneNumbers\/TwiliololIncomingFake.json"
			}`, dateCreated.Format(time.RFC1123Z), time.Now().Format(time.RFC1123Z))

			return []byte(response), nil
		}

		err := UpdateIncomingPhoneNumber(client, &phoneNumber)
		assert.NoError(t, err)
		assert.True(t, phoneNumber.DateUpdated > testNumber.DateUpdated)
	})

	t.Run("NOK - Missing ID", func(t *testing.T) {
		phoneNumber := IncomingPhoneNumber{
			FriendlyName: "I am invalid",
		}

		client := new(MockClient)
		err := UpdateIncomingPhoneNumber(client, &phoneNumber)

		assert.Error(t, err)
		assert.Equal(t, ErrIncomingPhoneMissingData, err)
	})

	t.Run("NOK - Error on API call", func(t *testing.T) {
		client := new(MockClient)
		client.postFn = func(params url.Values, uri string, _ []RequestOption) ([]byte, error) {
			assert.Equal(t, "/IncomingPhoneNumbers/TwiliololIncomingFake.json", uri)

			return nil, errors.New("Error in API")
		}

		err := UpdateIncomingPhoneNumber(client, &testNumber)

		assert.Error(t, err)<commit_msg>Change test for UpdatePhone<commit_after>package twiliolo

import (
	"errors"
	"fmt"
	"net/url"
	"testing"
	"time"

	"github.com/stretchr/testify/assert"
)

var (
	dateCreated = time.Now().Add(-48 * time.Hour)
	dateUpdated = time.Now().Add(-24 * time.Hour)
	testNumber  = IncomingPhoneNumber{
		Sid:                  "TwiliololIncomingFake",
		AccountSid:           "TwilioloFake",
		FriendlyName:         "<NAME>",
		PhoneNumber:          "+33912345678",
		VoiceURL:             "http://test.com",
		VoiceMethod:          "POST",
		VoiceFallbackURL:     "http://fail.com",
		VoiceFallbackMethod:  "GET",
		VoiceCallerIDLookup:  true,
		VoiceApplicationSid:  "",
		StatusCallback:       "http://status.com",
		StatusCallbackMethod: "GET",
		SmsURL:               "http://sms.com",
		SmsMethod:            "GET",
		SmsFallbackURL:       "http://fail-sms.com",
		SmsFallbackMethod:    "GET",
		DateCreated:          dateCreated.Format(time.RFC1123Z),
		DateUpdated:          dateUpdated.Format(time.RFC1123Z),
		Capabilities:         Capabilites{MMS: false, SMS: false, Voice: true},
		APIVersion:           VERSION,
		URI:                  "/2010-04-01/Accounts/TwilioloFake/IncomingPhoneNumbers/TwiliololIncomingFake.json",
	}
)

func TestUpdatePhoneNumber(t *testing.T) {
	t.Run("OK - Success update", func(t *testing.T) {
		phoneNumber := testNumber
		phoneNumber.FriendlyName = "New Friendly Name"
		newUpdated := time.Now().Format(time.RFC1123Z)

		client := new(MockClient)
		client.postFn = func(params url.Values, uri string, _ []RequestOption) ([]byte, error) {
			assert.Equal(t, "/IncomingPhoneNumbers/TwiliololIncomingFake.json", uri)
			assert.Equal(t, "New Friendly Name", params.Get("FriendlyName"))
			response := fmt.Sprintf(`
			{
				"sid": "TwiliololIncomingFake",
				"account_sid": "TwilioloFake",
				"friendly_name": "<NAME>",
				"phone_number": "+33912345678",
				"voice_url": "http://test.com",
				"voice_method": "POST",
				"voice_fallback_url": "http://fail.com",
				"voice_fallback_method": "GET",
				"status_callback": "http://status.com",
				"status_callback_method": "GET",
				"voice_caller_id_lookup": true,
				"voice_application_sid": null,
				"date_created": "%s",
				"date_updated": "%s",
				"sms_url": "http://sms.com",
				"sms_method": "GET",
				"sms_fallback_url": "http://fail-sms.com",
				"sms_fallback_method": "GET",
				"sms_application_sid": null,
				"capabilities": {
					"voice": true,
					"sms": false,
					"mms": false
				},
				"beta": false,
				"api_version": "2010-04-01",
				"uri": "\/2010-04-01\/Accounts\/TwilioloFake\/IncomingPhoneNumbers\/TwiliololIncomingFake.json"
			}`, dateCreated.Format(time.RFC1123Z), newUpdated)

			return []byte(response), nil
		}

		err := UpdateIncomingPhoneNumber(client, &phoneNumber)
		assert.NoError(t, err)
		assert.Equal(t, newUpdated, phoneNumber.DateUpdated)
	})

	t.Run("NOK - Missing ID", func(t *testing.T) {
		phoneNumber := IncomingPhoneNumber{
			FriendlyName: "I am invalid",
		}

		client := new(MockClient)
		err := UpdateIncomingPhoneNumber(client, &phoneNumber)

		assert.Error(t, err)
		assert.Equal(t, ErrIncomingPhoneMissingData, err)
	})

	t.Run("NOK - Error on API call", func(t *testing.T) {
		client := new(MockClient)
		client.postFn = func(params url.Values, uri string, _ []RequestOption) ([]byte, error) {
			assert.Equal(t, "/IncomingPhoneNumbers/TwiliololIncomingFake.json", uri)

			return nil, errors.New("Error in API")
		}

		err := UpdateIncomingPhoneNumber(client, &testNumber)

		assert.Error(t, err)
{'fraction_non_alphanumeric': 0.15760407816482583, 'fraction_numerical': 0.0184083828943642, 'mean_word_length': 3.5044642857142856, 'pattern_counts': {'":': 52, '<': 7, '<?xml version=': 0, '>': 8, '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': '5369442', 'n_tokens_mistral': 2884, 'n_tokens_neox': 2472, 'n_words': 430}