<commit_before>// Copyright 2014 <NAME>
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package oanda_test

import (
	"github.com/santegoeds/oanda"
	"sync"
	"time"

	"gopkg.in/check.v1"
)

type TestTranSuite struct {
	c *oanda.Client
}

var _ = check.Suite(&TestTranSuite{})

func (ts *TestTranSuite) SetUpTest(c *check.C) {
	var err error
	ts.c, err = newSandboxClientWithAccount()
	c.Assert(err, check.IsNil)
}

func (ts *TestTranSuite) TestTransactionApi(c *check.C) {
	trans, err := ts.c.Transactions()
	c.Assert(err, check.IsNil)
	c.Assert(trans, check.HasLen, 2)

	m := make(map[string]bool)
	for _, tran := range trans {
		m[tran.Type()] = true

		switch tran.Type() {
		case "CREATE":
			_, ok := tran.AsAccountCreate()
			c.Assert(ok, check.Equals, true)
		case "TRANSFER_FUNDS":
			_, ok := tran.AsTransferFunds()
			c.Assert(ok, check.Equals, true)
		}
	}

	c.Log(m)

	_, ok := m["CREATE"]
	c.Assert(ok, check.Equals, true)

	_, ok = m["TRANSFER_FUNDS"]
	c.Assert(ok, check.Equals, true)

	tran, err := ts.c.Transaction(trans[0].TranId())
	c.Assert(err, check.IsNil)
	c.Log(tran)
	c.Assert(tran.Type(), check.Equals, "TRANSFER_FUNDS")

	tfTran, ok := tran.AsTransferFunds()
	c.Assert(ok, check.Equals, true)
	c.Assert(tfTran.Amount(), check.Equals, 100000.0)
}

type TestEventsServerSuite struct {
	TestTranSuite
}

func (ts *TestTranSuite) TestEventsServer(c *check.C) {
	es, err := ts.c.NewEventsServer(ts.c.AccountId)
	c.Assert(err, check.IsNil)

	wg := sync.WaitGroup{}

	t := time.AfterFunc(5*time.Minute, func() {
		es.Stop()
		c.Fail()
	})

	expiry := time.Now().Add(24 * time.Hour)

	wg.Add(1)
	go func() {
		err := es.Run(func(accountId int, tran *oanda.Transaction) {
			c.Log(accountId, tran)

			es.Stop()<commit_msg>Some improvements to Transaction/Events tests<commit_after>// Copyright 2014 <NAME>
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package oanda_test

import (
	"github.com/santegoeds/oanda"
	"sync"
	"time"

	"gopkg.in/check.v1"
)

type TestTranSuite struct {
	c *oanda.Client
}

var _ = check.Suite(&TestTranSuite{})

func (ts *TestTranSuite) SetUpTest(c *check.C) {
	var err error
	ts.c, err = newSandboxClientWithAccount()
	c.Assert(err, check.IsNil)
}

func (ts *TestTranSuite) TestTransactionApi(c *check.C) {
	trans, err := ts.c.Transactions()
	c.Assert(err, check.IsNil)
	c.Assert(trans, check.HasLen, 2)

	m := make(map[string]bool)
	for _, tran := range trans {
		m[tran.Type()] = true

		switch tran.Type() {
		case "CREATE":
			acTran, ok := tran.AsAccountCreate()
			c.Assert(ok, check.Equals, true)
			c.Check(acTran.HomeCurrency(), check.Not(check.Equals), "")
			c.Check(acTran.Reason(), check.Not(check.Equals), "")

		case "TRANSFER_FUNDS":
			tfTran, ok := tran.AsTransferFunds()
			c.Assert(ok, check.Equals, true)
			c.Check(tfTran.Amount(), check.Equals, 100000.)

		}
	}

	c.Log(m)

	_, ok := m["CREATE"]
	c.Assert(ok, check.Equals, true)

	_, ok = m["TRANSFER_FUNDS"]
	c.Assert(ok, check.Equals, true)

	tran, err := ts.c.Transaction(trans[0].TranId())
	c.Assert(err, check.IsNil)

	c.Log(tran)
	c.Check(tran.Type(), check.Equals, trans[0].Type())
	c.Check(tran.AccountId(), check.Equals, trans[0].AccountId())
	c.Check(tran.Time(), check.Equals, trans[0].Time())

	tfTran1, ok := trans[0].AsTransferFunds()
	c.Assert(ok, check.Equals, true)

	tfTran2, ok := tran.AsTransferFunds()
	c.Assert(ok, check.Equals, true)

	c.Check(tfTran1.Amount(), check.Equals, tfTran2.Amount())
}

func (ts *TestTranSuite) TestEventsServer(c *check.C) {
	es, err := ts.c.NewEventsServer(ts.c.AccountId)
	c.Assert(err, check.IsNil)

	wg := sync.WaitGroup{}

	t := time.AfterFunc(5*time.Minute, func() {
		es.Stop()
		c.Fail()
	})

	expiry := time.Now().Add(24 * time.Hour)

	wg.Add(1)
	go func() {
		err := es.Run(func(accountId int, tran *oanda.Transaction) {
			c.Log(accountId, tran)

			es.Stop()
{'fraction_non_alphanumeric': 0.15748355263157895, 'fraction_numerical': 0.010485197368421052, 'mean_word_length': 3.924089068825911, 'pattern_counts': {'":': 4, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '1961511', 'n_tokens_mistral': 1913, 'n_tokens_neox': 1790, 'n_words': 440}