<commit_before>package okcoin

import (
	// "github.com/davecgh/go-spew/spew"
	"bitfx2/exchange"
	"math"
	"os"
	"testing"
	"time"
)

var book exchange.Book
var ok = New(os.Getenv("OKUSD_KEY"), os.Getenv("OKUSD_SECRET"), "ltc", "usd", 1, 0.002, .1)

func TestPriority(t *testing.T) {
	if ok.Priority() != 1 {
		t.Fatal("Priority should be 1")
	}
}

func TestFee(t *testing.T) {
	if ok.Fee()-0.002 > 0.000001 {
		t.Fatal("Fee should be 0.002")
	}
}

func TestUpdatePositon(t *testing.T) {
	if ok.Position() != 0 {
		t.Fatal("Should start with zero position")
	}
	ok.SetPosition(10)
	t.Logf("Set position to 10")
	if ok.Position() != 10 {
		t.Fatal("Position should have updated to 10")
	}
}

func TestMaxPos(t *testing.T) {
	if math.Abs(ok.MaxPos()-.1) > .000001 {
		t.Fatal("MaxPos should be .1")
	}
}

// USD tesing ******************************************************************

func TestCurrencyCodeUSD(t *testing.T) {
	if ok.CurrencyCode() != 0 {
		t.Fatal("Currency code should be 0")
	}
}

func TestCommunicateBookUSD(t *testing.T) {
	bookChan := make(chan exchange.Book)
	doneChan := make(chan bool)
	if err := ok.CommunicateBook(bookChan, doneChan); err != nil {
		t.Fatal(err)
	}

	// Notify doneChan in 5 seconds
	go func() {
		time.Sleep(5 * time.Second)
		doneChan <- true
		t.Logf("Notified doneChan")
	}()

	for book = range bookChan {
		t.Logf("Received book data")
		// spew.Dump(book)
		if len(book.Bids) != 20 || len(book.Asks) != 20 {
			t.Fatal("Expected 20 book entries")
		}
		if book.Bids[0].Price < book.Bids[1].Price {
			t.Fatal("Bids not sorted correctly")
		}
		if book.Asks[0].Price > book.Asks[1].Price {
			t.Fatal("Asks not sorted correctly")
		}
	}
}

func TestNewOrderUSD(t *testing.T) {
	action := "buy"
	otype := "limit"
	amount := 0.1
	price := book.Bids[0].Price - 0.20

	// Test submitting a new order
	id, err := ok.SendOrder(action, otype, amount, price)
	if err != nil || id == 0 {
		t.Fatal(err)
	}
	t.Logf("Placed a new buy order of 0.1 ltc_usd @ %v limit with ID: %d", price, id)

	// Check status
	order, err := ok.GetOrderStatus(id)
	if err != nil {
		t.Fatal(err)
	}
	if order.Status != "live" {
		t.Fatal("Order should be live")
	}
	t.Logf("Order confirmed live")
	if order.FilledAmount != 0 {
		t.Fatal("Order should not be filled")
	}
	t.Logf("Order confirmed unfilled")

	// Test cancelling the order
	success, err := ok.CancelOrder(id)
	if err != nil {
		t.Fatal(err)
	}
	if !success {
		t.Fatal("Order not cancelled")
	}
	t.Logf("Sent cancellation")

	// Check status
	tryAgain := true
	for tryAgain {
		t.Logf("checking status...")
		order, err = ok.GetOrderStatus(id)
		tryAgain = order.Status == ""
	}
	if err != nil {
		t.Fatal(err)
	}
	if order.Status != "dead" {
		t.Fatal("Order should be dead after cancel")
	}
	t.Logf("Order confirmed dead")
	if order.FilledAmount != 0 {
		t.Fatal("Order should not be filled")
	}
	t.Logf("Order confirmed unfilled")

	// Test bad order
	id, err = ok.SendOrder("kill", otype, amount, price)
	if id != 0 {
		t.Fatal("Expected id = 0")
	}
	if err == nil {
		t.Fatal("Expected error on bad order")
	}
}

// CNY tesing ******************************************************************

func TestCurrencyCodeCNY(t *testing.T) {
	// Reset global variables
	book = exchange.Book{}
	ok = New(os.Getenv("OKCNY_KEY"), os.Getenv("OKCNY_SECRET"), "ltc", "cny", 1, 0.002, .1)

	if ok.CurrencyCode() != 1 {
		t.Fatal("Currency code should be 1")
	}
}

func TestCommunicateBookCNY(t *testing.T) {
	bookChan := make(chan exchange.Book)
	doneChan := make(chan bool)
	err := ok.CommunicateBook(bookChan, doneChan)
	if err != nil {
		t.Fatal(err)
	}

	// Notify doneChan in 5 seconds
	go func() {
		time.Sleep(5 * time.Second)
		doneChan <- true
		t.Logf("Notified doneChan")
	}()

	for book = range bookChan {
		t.Logf("Received book data")
		// spew.Dump(book)
		if len(book.Bids) != 20 || len(book.Asks) != 20 {
			t.Fatal("Expected 20 book entries")
		}
		if book.Bids[0].Price < book.Bids[1].Price {
			t.Fatal("Bids not sorted correctly")
		}
		if book.Asks[0].Price > book.Asks[1].Price {
			t.Fatal("Asks not sorted correctly")
		}
	}
}

func TestNewOrderCNY(t *testing.T) {
	action := "buy"
	otype := "limit"
	amount := 0.1
	price := book.Bids[0].Price - 1

	// Test submitting a new order
	id, err := ok.SendOrder(action, otype, amount, price)
	if err != nil || id == 0 {
		t.Fatal(err)
	}
	t.Logf("Placed a new buy order of 0.1 ltc_cny @ %v limit with ID: %d", price, id)

	// Check status
	order, err := ok.GetOrderStatus(id)
	if err != nil {
		t.Fatal(err)
	}
	if order.Status != "live" {
		t.Fatal("Order should be live")
	}<commit_msg>update test for returning an initial book and not depending on channel closing<commit_after>package okcoin

import (
	// "github.com/davecgh/go-spew/spew"
	"bitfx2/exchange"
	"math"
	"os"
	"testing"
)

var book exchange.Book
var ok = New(os.Getenv("OKUSD_KEY"), os.Getenv("OKUSD_SECRET"), "ltc", "usd", 1, 0.002, .1)

func TestPriority(t *testing.T) {
	if ok.Priority() != 1 {
		t.Fatal("Priority should be 1")
	}
}

func TestFee(t *testing.T) {
	if ok.Fee()-0.002 > 0.000001 {
		t.Fatal("Fee should be 0.002")
	}
}

func TestUpdatePositon(t *testing.T) {
	if ok.Position() != 0 {
		t.Fatal("Should start with zero position")
	}
	ok.SetPosition(10)
	t.Logf("Set position to 10")
	if ok.Position() != 10 {
		t.Fatal("Position should have updated to 10")
	}
}

func TestMaxPos(t *testing.T) {
	if math.Abs(ok.MaxPos()-.1) > .000001 {
		t.Fatal("MaxPos should be .1")
	}
}

// USD tesing ******************************************************************

func TestCurrencyCodeUSD(t *testing.T) {
	if ok.CurrencyCode() != 0 {
		t.Fatal("Currency code should be 0")
	}
}

func TestCommunicateBookUSD(t *testing.T) {
	bookChan := make(chan exchange.Book)
	doneChan := make(chan bool)
	if book = ok.CommunicateBook(bookChan, doneChan); book.Error != nil {
		t.Fatal(book.Error)
	}

	book = <-bookChan
	t.Logf("Received book data")
	// spew.Dump(book)
	if len(book.Bids) != 20 || len(book.Asks) != 20 {
		t.Fatal("Expected 20 book entries")
	}
	if book.Bids[0].Price < book.Bids[1].Price {
		t.Fatal("Bids not sorted correctly")
	}
	if book.Asks[0].Price > book.Asks[1].Price {
		t.Fatal("Asks not sorted correctly")
	}
	doneChan <- true
}

func TestNewOrderUSD(t *testing.T) {
	action := "buy"
	otype := "limit"
	amount := 0.1
	price := book.Bids[0].Price - 0.20

	// Test submitting a new order
	id, err := ok.SendOrder(action, otype, amount, price)
	if err != nil || id == 0 {
		t.Fatal(err)
	}
	t.Logf("Placed a new buy order of 0.1 ltc_usd @ %v limit with ID: %d", price, id)

	// Check status
	order, err := ok.GetOrderStatus(id)
	if err != nil {
		t.Fatal(err)
	}
	if order.Status != "live" {
		t.Fatal("Order should be live")
	}
	t.Logf("Order confirmed live")
	if order.FilledAmount != 0 {
		t.Fatal("Order should not be filled")
	}
	t.Logf("Order confirmed unfilled")

	// Test cancelling the order
	success, err := ok.CancelOrder(id)
	if err != nil {
		t.Fatal(err)
	}
	if !success {
		t.Fatal("Order not cancelled")
	}
	t.Logf("Sent cancellation")

	// Check status
	tryAgain := true
	for tryAgain {
		t.Logf("checking status...")
		order, err = ok.GetOrderStatus(id)
		tryAgain = order.Status == ""
	}
	if err != nil {
		t.Fatal(err)
	}
	if order.Status != "dead" {
		t.Fatal("Order should be dead after cancel")
	}
	t.Logf("Order confirmed dead")
	if order.FilledAmount != 0 {
		t.Fatal("Order should not be filled")
	}
	t.Logf("Order confirmed unfilled")

	// Test bad order
	id, err = ok.SendOrder("kill", otype, amount, price)
	if id != 0 {
		t.Fatal("Expected id = 0")
	}
	if err == nil {
		t.Fatal("Expected error on bad order")
	}
}

// CNY tesing ******************************************************************

func TestCurrencyCodeCNY(t *testing.T) {
	// Reset global variables
	book = exchange.Book{}
	ok = New(os.Getenv("OKCNY_KEY"), os.Getenv("OKCNY_SECRET"), "ltc", "cny", 1, 0.002, .1)

	if ok.CurrencyCode() != 1 {
		t.Fatal("Currency code should be 1")
	}
}

func TestCommunicateBookCNY(t *testing.T) {
	bookChan := make(chan exchange.Book)
	doneChan := make(chan bool)
	if book = ok.CommunicateBook(bookChan, doneChan); book.Error != nil {
		t.Fatal(book.Error)
	}

	book = <-bookChan
	t.Logf("Received book data")
	// spew.Dump(book)
	if len(book.Bids) != 20 || len(book.Asks) != 20 {
		t.Fatal("Expected 20 book entries")
	}
	if book.Bids[0].Price < book.Bids[1].Price {
		t.Fatal("Bids not sorted correctly")
	}
	if book.Asks[0].Price > book.Asks[1].Price {
		t.Fatal("Asks not sorted correctly")
	}
	doneChan <- true
}

func TestNewOrderCNY(t *testing.T) {
	action := "buy"
	otype := "limit"
	amount := 0.1
	price := book.Bids[0].Price - 1

	// Test submitting a new order
	id, err := ok.SendOrder(action, otype, amount, price)
	if err != nil || id == 0 {
		t.Fatal(err)
	}
	t.Logf("Placed a new buy order of 0.1 ltc_cny @ %v limit with ID: %d", price, id)

	// Check status
	order, err := ok.GetOrderStatus(id)
	if err != nil {
		t.Fatal(err)
	}
	if order.Status != "live" {
		t.Fatal("Order should be live")
	}
{'fraction_non_alphanumeric': 0.19294765840220385, 'fraction_numerical': 0.020495867768595043, 'mean_word_length': 3.493069306930693, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 11, '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': '7518851', 'n_tokens_mistral': 3892, 'n_tokens_neox': 3500, 'n_words': 885}