<commit_before>package cmagic

import (
	"fmt"
	"testing"
	"reflect"
)

type Customer struct {
	Id   string
	Name string
}

var ns KeySpace

func init() {
	var err error
	c, err := Connect([]string{"127.0.0.1"}, "", "")
	if err != nil {
		panic(err)
	}
	err = c.DropKeySpace("test")
	if err != nil {
		panic(err)
	}
	err = c.CreateKeySpace("test")
	if err != nil {
		panic(err)
	}
	ns, err = ConnectToKeySpace("test", []string{"127.0.0.1"}, "", "")
	if err != nil {
		panic(err)
	}
	//ns.DebugMode(true)
}

// cqlsh> CREATE KEYSPACE test WITH REPLICATION = { 'class' : 'SimpleStrategy', 'replication_factor' : 1 };
// cqlsh> CREATE TABLE test.customer (id text PRIMARY KEY, name text);
func TestEq(t *testing.T) {
	cs := ns.Table("customer", Customer{}, Keys{})
	err := cs.Set(Customer{
		Id:   "50",
		Name: "Joe",
	})
	if err != nil {
		t.Fatal(err)
	}
	res, err := cs.Where(Eq("Id", "50")).Query().Read()
	if err != nil {
		t.Fatal(err)
	}
	if len(res) == 0 {
		t.Fatal("Not found")
	}
	if res[0].(*Customer).Id != "50" {
		t.Fatal(res[0].(*Customer))
	}
}

func TestMultipleRowResults(t *testing.T) {
	name := "customer_multipletest"
	ns.(*K).DropTable(name)
	cs := ns.Table(name, Customer{}, Keys{
		PartitionKeys:     []string{"Name"},
		ClusteringColumns: []string{"Id"},
	})
	err := cs.(*T).Create()
	if err != nil {
		t.Fatal(err)
	}
	cs.Set(Customer{
		Id:   "12",
		Name: "John",
	})
	cs.Set(Customer{
		Id:   "13",
		Name: "John",
	})
	res, err := cs.Where(Eq("Name", "John")).Query().Read()
	if err != nil {
		t.Fatal(err)
	}
	if len(res) != 2 {
		t.Fatal(len(res), res)
	}
}

func TestIn(t *testing.T) {
	cs := ns.Table("customer", Customer{}, Keys{})
	err := cs.Set(Customer{
		Id:   "100",
		Name: "Joe",
	})
	if err != nil {
		t.Fatal(err)
	}
	err = cs.Set(Customer{
		Id:   "200",
		Name: "Jane",
	})
	if err != nil {
		t.Fatal(err)
	}
	res, err := cs.Where(In("Id", "100", "200")).Query().Read()
	if len(res) != 2 {
		for _, v := range res {
			fmt.Println(*(v.(*Customer)))
		}
		t.Fatal("Not found", len(res))
	}
	if res[0].(*Customer).Id != "100" || res[1].(*Customer).Id != "200" {
		t.Fatal(res[0].(*Customer), res[1].(*Customer))
	}
}

// cqlsh> CREATE TABLE test.customer1 (id text, name text, PRIMARY KEY((id, name)));
func TestAnd(t *testing.T) {
	cs := ns.Table("customer1", Customer{}, Keys{})
	err := cs.Set(Customer{
		Id:   "100",
		Name: "Joe",
	})
	if err != nil {
		t.Fatal(err)
	}
	res, err := cs.Where(Eq("Id", "100"), Eq("Name", "Joe")).Query().Read()
	if err != nil {
		t.Fatal(err)
	}
	if len(res) != 1 {
		t.Fatal("Not found ", len(res))
	}
}

func TestQueryReturnError(t *testing.T) {
	cs := ns.Table("customer2", Customer{}, Keys{})
	_, err := cs.Where(Eq("Id", "100"), Eq("Name", "Joe")).Query().Read()
	if err == nil {
		t.Fatal("Table customer2 does not exist - should return error")
	}
}

type Customer3 struct {
	Id string 
	Field1 string 
	Field2 int 
	Field3 int32
	Field4 int64
	Field5 float32
	Field6 float64
	Field7 bool
}

func TestTypesMarshal(t *testing.T) {
	c := Customer3{
		Id: "1",
		Field1: "A",
		Field2: 1,
		Field3: 2,
		Field4: 3,
		Field5: 4.0,
		Field6: 5.0,
		Field7: true,
	}
	tbl := ns.Table("customer3", Customer3{}, Keys{PartitionKeys: []string{"Id"}})
	err := tbl.(*T).Create()
	if ex, err := ns.(*K).Exists("customer3"); err != nil {
		t.Fatal(err)
	} else if !ex {
		err := tbl.(*T).Create()
		if err != nil {
			t.Fatal("Create table failed :", err)
		}
	}
	if err = tbl.Set(c); err != nil {
		t.Fatal(err)
	}
	res, err := tbl.Where(Eq("Id", "1")).Query().Read()
	if err != nil {
		t.Fatal(err)
	}
	if len(res) != 1 {
		t.Fatal()
	}
	if !reflect.DeepEqual(c, *res[0].(*Customer3)) {
		t.Fatal(c, *res[0].(*Customer3))
	}
}<commit_msg>Tests pass now, now we drop keyspace before testing<commit_after>package cmagic

import (
	"fmt"
	"testing"
	"reflect"
)

type Customer struct {
	Id   string
	Name string
}

var ns KeySpace

func init() {
	var err error
	c, err := Connect([]string{"127.0.0.1"}, "", "")
	if err != nil {
		panic(err)
	}
	err = c.DropKeySpace("test")
	if err != nil {
		panic(err)
	}
	err = c.CreateKeySpace("test")
	if err != nil {
		panic(err)
	}
	ns, err = ConnectToKeySpace("test", []string{"127.0.0.1"}, "", "")
	if err != nil {
		panic(err)
	}
	//ns.DebugMode(true)
}

func TestEq(t *testing.T) {
	cs := ns.Table("customer", Customer{}, Keys{PartitionKeys:[]string{"Id"}})
	createIf(ns, cs.(*T), t)
	err := cs.Set(Customer{
		Id:   "50",
		Name: "Joe",
	})
	if err != nil {
		t.Fatal(err)
	}
	res, err := cs.Where(Eq("Id", "50")).Query().Read()
	if err != nil {
		t.Fatal(err)
	}
	if len(res) == 0 {
		t.Fatal("Not found")
	}
	if res[0].(*Customer).Id != "50" {
		t.Fatal(res[0].(*Customer))
	}
}

func TestMultipleRowResults(t *testing.T) {
	name := "customer_multipletest"
	ns.(*K).DropTable(name)
	cs := ns.Table(name, Customer{}, Keys{
		PartitionKeys:     []string{"Name"},
		ClusteringColumns: []string{"Id"},
	})
	err := cs.(*T).Create()
	if err != nil {
		t.Fatal(err)
	}
	cs.Set(Customer{
		Id:   "12",
		Name: "John",
	})
	cs.Set(Customer{
		Id:   "13",
		Name: "John",
	})
	res, err := cs.Where(Eq("Name", "John")).Query().Read()
	if err != nil {
		t.Fatal(err)
	}
	if len(res) != 2 {
		t.Fatal(len(res), res)
	}
}

func TestIn(t *testing.T) {
	cs := ns.Table("customer", Customer{}, Keys{})
	err := cs.Set(Customer{
		Id:   "100",
		Name: "Joe",
	})
	if err != nil {
		t.Fatal(err)
	}
	err = cs.Set(Customer{
		Id:   "200",
		Name: "Jane",
	})
	if err != nil {
		t.Fatal(err)
	}
	res, err := cs.Where(In("Id", "100", "200")).Query().Read()
	if len(res) != 2 {
		for _, v := range res {
			fmt.Println(*(v.(*Customer)))
		}
		t.Fatal("Not found", len(res))
	}
	if res[0].(*Customer).Id != "100" || res[1].(*Customer).Id != "200" {
		t.Fatal(res[0].(*Customer), res[1].(*Customer))
	}
}

func TestAnd(t *testing.T) {
	cs := ns.Table("customer1", Customer{}, Keys{PartitionKeys:[]string{"Id", "Name"}})
	createIf(ns, cs.(*T), t)
	err := cs.Set(Customer{
		Id:   "100",
		Name: "Joe",
	})
	if err != nil {
		t.Fatal(err)
	}
	res, err := cs.Where(Eq("Id", "100"), Eq("Name", "Joe")).Query().Read()
	if err != nil {
		t.Fatal(err)
	}
	if len(res) != 1 {
		t.Fatal("Not found ", len(res))
	}
}

func TestQueryReturnError(t *testing.T) {
	cs := ns.Table("customer2", Customer{}, Keys{})
	_, err := cs.Where(Eq("Id", "100"), Eq("Name", "Joe")).Query().Read()
	if err == nil {
		t.Fatal("Table customer2 does not exist - should return error")
	}
}

type Customer3 struct {
	Id string 
	Field1 string 
	Field2 int 
	Field3 int32
	Field4 int64
	Field5 float32
	Field6 float64
	Field7 bool
}

func TestTypesMarshal(t *testing.T) {
	c := Customer3{
		Id: "1",
		Field1: "A",
		Field2: 1,
		Field3: 2,
		Field4: 3,
		Field5: 4.0,
		Field6: 5.0,
		Field7: true,
	}
	tbl := ns.Table("customer3", Customer3{}, Keys{PartitionKeys: []string{"Id"}})
	err := tbl.(*T).Create()
	if ex, err := ns.(*K).Exists("customer3"); err != nil {
		t.Fatal(err)
	} else if !ex {
		err := tbl.(*T).Create()
		if err != nil {
			t.Fatal("Create table failed :", err)
		}
	}
	if err = tbl.Set(c); err != nil {
		t.Fatal(err)
	}
	res, err := tbl.Where(Eq("Id", "1")).Query().Read()
	if err != nil {
		t.Fatal(err)
	}
	if len(res) != 1 {
		t.Fatal()
	}
	if !reflect.DeepEqual(c, *res[0].(*Customer3)) {
		t.Fatal(c, *res[0].(*Customer3))
	}
}
{'fraction_non_alphanumeric': 0.22914957615531856, 'fraction_numerical': 0.028438610883237625, 'mean_word_length': 3.388122375524895, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 6, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 4, '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': '3655529', 'n_tokens_mistral': 3358, 'n_tokens_neox': 3076, 'n_words': 688}