<commit_before>//go:build linux && cgo && !agent
// +build linux,cgo,!agent

package db_test

import (
	"testing"

	"github.com/lxc/lxd/lxd/db"
	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"
)

// Add, get and remove an operation.
func TestOperation(t *testing.T) {
	tx, cleanup := db.NewTestClusterTx(t)
	defer cleanup()

	id, err := tx.CreateOperation("default", "abcd", db.OperationInstanceCreate)
	require.NoError(t, err)
	assert.Equal(t, int64(1), id)

	operations, err := tx.GetLocalOperations()
	require.NoError(t, err)
	assert.Len(t, operations, 1)
	assert.Equal(t, operations[0].UUID, "abcd")

	operation, err := tx.GetOperationByUUID("abcd")
	require.NoError(t, err)
	assert.Equal(t, id, operation.ID)
	assert.Equal(t, db.OperationInstanceCreate, operation.Type)

	uuids, err := tx.GetLocalOperationsUUIDs()
	require.NoError(t, err)
	assert.Equal(t, []string{"abcd"}, uuids)

	err = tx.RemoveOperation("abcd")
	require.NoError(t, err)

	_, err = tx.GetOperationByUUID("abcd")
	assert.Equal(t, db.ErrNoSuchObject, err)
}

// Add, get and remove an operation not associated with any project.
func TestOperationNoProject(t *testing.T) {
	tx, cleanup := db.NewTestClusterTx(t)
	defer cleanup()

	id, err := tx.CreateOperation("", "abcd", db.OperationInstanceCreate)
	require.NoError(t, err)
	assert.Equal(t, int64(1), id)

	operations, err := tx.GetLocalOperations()
	require.NoError(t, err)
	assert.Len(t, operations, 1)
	assert.Equal(t, operations[0].UUID, "abcd")

	operation, err := tx.GetOperationByUUID("abcd")
	require.NoError(t, err)
	assert.Equal(t, id, operation.ID)
	assert.Equal(t, db.OperationInstanceCreate, operation.Type)

	uuids, err := tx.GetLocalOperationsUUIDs()
	require.NoError(t, err)
	assert.Equal(t, []string{"abcd"}, uuids)

	err = tx.RemoveOperation("abcd")
	require.NoError(t, err)

	_, err = tx.GetOperationByUUID("abcd")
	assert.Equal(t, db.ErrNoSuchObject, err)
}
<commit_msg>lxd/db/operations/test: use filter parameter for generated operation methods<commit_after>//go:build linux && cgo && !agent
// +build linux,cgo,!agent

package db_test

import (
	"testing"

	"github.com/lxc/lxd/lxd/db"
	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"
)

// Add, get and remove an operation.
func TestOperation(t *testing.T) {
	tx, cleanup := db.NewTestClusterTx(t)
	defer cleanup()

	projectID, err := tx.GetProjectID("default")
	require.NoError(t, err)

	opInfo := db.Operation{
		NodeID:    tx.GetNodeID(),
		Type:      db.OperationInstanceCreate,
		UUID:      "abcd",
		ProjectID: &projectID,
	}
	id, err := tx.CreateOrReplaceOperation(opInfo)
	require.NoError(t, err)
	assert.Equal(t, int64(1), id)

	filter := db.OperationFilter{NodeID: tx.GetNodeID()}
	operations, err := tx.GetOperations(filter)
	require.NoError(t, err)
	assert.Len(t, operations, 1)
	assert.Equal(t, operations[0].UUID, "abcd")

	filter = db.OperationFilter{UUID: "abcd"}
	ops, err := tx.GetOperations(filter)
	require.NoError(t, err)
	assert.Equal(t, len(ops), 1)
	operation := ops[0]
	assert.Equal(t, id, operation.ID)
	assert.Equal(t, db.OperationInstanceCreate, operation.Type)

	filter = db.OperationFilter{NodeID: tx.GetNodeID()}
	ops, err = tx.GetOperations(filter)
	require.NoError(t, err)
	assert.Equal(t, "abcd", ops[0].UUID)

	filter = db.OperationFilter{UUID: "abcd"}
	err = tx.DeleteOperation(filter)
	require.NoError(t, err)

	filter = db.OperationFilter{UUID: "abcd"}
	ops, err = tx.GetOperations(filter)
	require.NoError(t, err)
	assert.Equal(t, len(ops), 0)
}

// Add, get and remove an operation not associated with any project.
func TestOperationNoProject(t *testing.T) {
	tx, cleanup := db.NewTestClusterTx(t)
	defer cleanup()

	opInfo := db.Operation{
		NodeID: tx.GetNodeID(),
		Type:   db.OperationInstanceCreate,
		UUID:   "abcd",
	}

	id, err := tx.CreateOrReplaceOperation(opInfo)
	require.NoError(t, err)
	assert.Equal(t, int64(1), id)

	filter := db.OperationFilter{NodeID: tx.GetNodeID()}
	operations, err := tx.GetOperations(filter)
	require.NoError(t, err)
	assert.Len(t, operations, 1)
	assert.Equal(t, operations[0].UUID, "abcd")

	filter = db.OperationFilter{UUID: "abcd"}
	ops, err := tx.GetOperations(filter)
	require.NoError(t, err)
	assert.Equal(t, len(ops), 1)
	operation := ops[0]
	require.NoError(t, err)
	assert.Equal(t, id, operation.ID)
	assert.Equal(t, db.OperationInstanceCreate, operation.Type)

	filter = db.OperationFilter{NodeID: tx.GetNodeID()}
	ops, err = tx.GetOperations(filter)
	require.NoError(t, err)
	assert.Equal(t, "abcd", ops[0].UUID)

	filter = db.OperationFilter{UUID: "abcd"}
	err = tx.DeleteOperation(filter)
	require.NoError(t, err)

	filter = db.OperationFilter{UUID: "abcd"}
	ops, err = tx.GetOperations(filter)
	require.NoError(t, err)
	assert.Equal(t, len(ops), 0)
}

{'fraction_non_alphanumeric': 0.158125, 'fraction_numerical': 0.005833333333333334, 'mean_word_length': 5.092639593908629, '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': '1846939', 'n_tokens_mistral': 1819, 'n_tokens_neox': 1807, 'n_words': 362}