<commit_before>package gapi

import (
	"testing"

	"github.com/gobs/pretty"
)

const (
	createdAndUpdateDashboardResponse = `{
		"slug": "test",
		"id": 1,
		"uid": "nErXDvCkzz",
		"status": "success",
		"version": 1
	}`

	getDashboardBySlugResponse = `{
		"dashboard": {
			"id": 1,
			"uid": "cIBgcSjkk",
			"title": "Production Overview",
			"version": 0
		},
		"meta": {
			"isStarred": false,
			"url": "/d/cIBgcSjkk/production-overview",
			"slug": "production-overview"
		}
	}`
)

func TestDashboardCreateAndUpdate(t *testing.T) {
	server, client := gapiTestTools(200, createdAndUpdateDashboardResponse)
	defer server.Close()

	dashboard := Dashboard{
		Model: map[string]interface{}{
			"title": "test",
		},
		Folder:    0,
		Overwrite: false,
	}

	resp, err := client.NewDashboard(dashboard)
	if err != nil {
		t.Fatal(err)
	}

	t.Log(pretty.PrettyFormat(resp))

	if resp.Uid != "nErXDvCkzz" {
		t.Errorf("Invalid uid - %s, Expected %s", resp.Uid, "nErXDvCkzz")
	}

	for _, code := range []int{400, 401, 403, 412} {
		server.code = code
		_, err = client.NewDashboard(dashboard)
		if err == nil {
			t.Errorf("%d not detected", code)
		}
	}
}

func TestDashboardGetBySlug(t *testing.T) {
	server, client := gapiTestTools(200, getDashboardBySlugResponse)
	defer server.Close()

	resp, err := client.Dashboard("test")
	if err != nil {
		t.Error(err)
	}

	t.Log(pretty.PrettyFormat(resp))

	uid, ok := resp.Model["uid"]
	if !ok || uid != "cIBgcSjkk" {
		t.Errorf("Invalid uid - %s, Expected %s", uid, "cIBgcSjkk")
	}

	for _, code := range []int{401, 403, 404} {
		server.code = code
		_, err = client.Dashboard("test")
		if err == nil {
			t.Errorf("%d not detected", code)
		}
	}
}

func TestDashboardDeleteBySlug(t *testing.T) {
	server, client := gapiTestTools(200, "")
	defer server.Close()

	err := client.DeleteDashboard("test")
	if err != nil {
		t.Error(err)
	}

	for _, code := range []int{401, 403, 404, 412} {
		server.code = code
		err = client.DeleteDashboard("test")
		if err == nil {
			t.Errorf("%d not detected", code)
		}
	}
}

// func TestDashboardNew(t *testing.T) {
// 	mock, client := newMockApiServer()
// 	defer mock.server.Close()

// 	response, err := client.NewDashboard(exampleDashboard)
// 	if err != nil {
// 		t.Error(err)
// 	}

// 	t.Log(pretty.PrettyFormat(response))

// 	if response.Uid == "" {
// 		t.Error("dashboard creation response should return the created dashboard UID")
// 	}
// }<commit_msg>*DashboardByUid tests<commit_after>package gapi

import (
	"testing"

	"github.com/gobs/pretty"
)

const (
	createdAndUpdateDashboardResponse = `{
		"slug": "test",
		"id": 1,
		"uid": "nErXDvCkzz",
		"status": "success",
		"version": 1
	}`

	getDashboardResponse = `{
		"dashboard": {
			"id": 1,
			"uid": "cIBgcSjkk",
			"title": "Production Overview",
			"version": 0
		},
		"meta": {
			"isStarred": false,
			"url": "/d/cIBgcSjkk/production-overview",
			"slug": "production-overview"
		}
	}`
)

func TestDashboardCreateAndUpdate(t *testing.T) {
	server, client := gapiTestTools(200, createdAndUpdateDashboardResponse)
	defer server.Close()

	dashboard := Dashboard{
		Model: map[string]interface{}{
			"title": "test",
		},
		Folder:    0,
		Overwrite: false,
	}

	resp, err := client.NewDashboard(dashboard)
	if err != nil {
		t.Fatal(err)
	}

	t.Log(pretty.PrettyFormat(resp))

	if resp.Uid != "nErXDvCkzz" {
		t.Errorf("Invalid uid - %s, Expected %s", resp.Uid, "nErXDvCkzz")
	}

	for _, code := range []int{400, 401, 403, 412} {
		server.code = code
		_, err = client.NewDashboard(dashboard)
		if err == nil {
			t.Errorf("%d not detected", code)
		}
	}
}

func TestDashboardGet(t *testing.T) {
	server, client := gapiTestTools(200, getDashboardResponse)
	defer server.Close()

	resp, err := client.Dashboard("test")
	if err != nil {
		t.Error(err)
	}
	uid, ok := resp.Model["uid"]
	if !ok || uid != "cIBgcSjkk" {
		t.Errorf("Invalid uid - %s, Expected %s", uid, "cIBgcSjkk")
	}

	resp, err = client.DashboardByUID("cIBgcSjkk")
	if err != nil {
		t.Error(err)
	}
	uid, ok = resp.Model["uid"]
	if !ok || uid != "cIBgcSjkk" {
		t.Errorf("Invalid uid - %s, Expected %s", uid, "cIBgcSjkk")
	}

	for _, code := range []int{401, 403, 404} {
		server.code = code
		_, err = client.Dashboard("test")
		if err == nil {
			t.Errorf("%d not detected", code)
		}

		_, err = client.DashboardByUID("cIBgcSjkk")
		if err == nil {
			t.Errorf("%d not detected", code)
		}
	}
}

func TestDashboardDelete(t *testing.T) {
	server, client := gapiTestTools(200, "")
	defer server.Close()

	err := client.DeleteDashboard("test")
	if err != nil {
		t.Error(err)
	}

	err = client.DeleteDashboardByUID("cIBgcSjkk")
	if err != nil {
		t.Error(err)
	}

	for _, code := range []int{401, 403, 404, 412} {
		server.code = code

		err = client.DeleteDashboard("test")
		if err == nil {
			t.Errorf("%d not detected", code)
		}

		err = client.DeleteDashboardByUID("cIBgcSjkk")
		if err == nil {
			t.Errorf("%d not detected", code)
		}
	}
}
{'fraction_non_alphanumeric': 0.1783772901147574, 'fraction_numerical': 0.018924904368834306, 'mean_word_length': 3.574585635359116, 'pattern_counts': {'":': 30, '<': 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': '2800555', 'n_tokens_mistral': 2194, 'n_tokens_neox': 1995, 'n_words': 412}