"""
Test basic operations with SymbolRegister data structure.
"""
import unittest
import random

from ros.core.moment import Moment
from ros.core.register import SymbolDayRegister
from tests import STOCKS_DAILY


random.seed(1)


class AddMomentToRegisterTestCase(unittest.TestCase):

    def setUp(self):
        self.symbol_day_register = SymbolDayRegister()

    def test_add_moment(self):
        moment = Moment.from_dict(STOCKS_DAILY['AAPL']['records'][0])
        moment.symbol = 'AAPL'
        self.symbol_day_register.add_moment(moment)
        self.assertIn('AAPL', self.symbol_day_register.series.keys())
        self.assertEqual(moment, self.symbol_day_register.series['AAPL'].get_moments()[0])

    def test_add_multiple_moments(self):

        moments = list()
        for symbol in ('AAPL', 'BYND'):
            _chunk = [Moment.from_dict(x) for x in STOCKS_DAILY[symbol]['records']]
            for moment in _chunk:
                moment.symbol = symbol
            moments += _chunk
        moments_shuffled = moments.copy()
        random.shuffle(moments_shuffled)
        self.assertNotEqual(moments, moments_shuffled)

        self.symbol_day_register.add_moments(moments_shuffled)

        actual_moments = list()
        for symbol in ('AAPL', 'BYND'):
            actual_moments += self.symbol_day_register.series[symbol].get_moments()
        self.assertEqual(moments, actual_moments)


class GetFromRegisterTestCase(unittest.TestCase):

    def setUp(self):
        self.symbol_day_register = SymbolDayRegister()

    def test_get(self):
        moments = list()
        for symbol in ('AAPL', 'BYND'):
            _chunk = [Moment.from_dict(x) for x in STOCKS_DAILY[symbol]['records']]
            for moment in _chunk:
                moment.symbol = symbol
            moments += _chunk

        self.symbol_day_register.add_moments(moments)

        for symbol in ('AAPL', 'BYND'):
            for _ in range(3):
                i = random.randint(0, len(STOCKS_DAILY[symbol]['records']) - 1)
                moment = Moment.from_dict(STOCKS_DAILY[symbol]['records'][i])
                moment.symbol = symbol
                self.assertEqual(moment, self.symbol_day_register.get(symbol, moment.epoch_s))

"""
Test basic operations with SymbolRegister data structure.
"""
import unittest
import random

from ros.core.moment import Moment
from ros.core.register import SymbolDayRegister
from tests import STOCKS_DAILY


random.seed(1)


class AddMomentToRegisterTestCase(unittest.TestCase):

    def setUp(self):
        self.symbol_day_register = SymbolDayRegister()

    def test_add_moment(self):
        moment = Moment.from_dict(STOCKS_DAILY['AAPL']['records'][0])
        moment.symbol = 'AAPL'
        self.symbol_day_register.add_moment(moment)
        self.assertIn('AAPL', self.symbol_day_register.series.keys())
        self.assertEqual(moment, self.symbol_day_register.series['AAPL'].get_moments()[0])

    def test_add_multiple_moments(self):

        moments = list()
        for symbol in ('AAPL', 'BYND'):
            _chunk = [Moment.from_dict(x) for x in STOCKS_DAILY[symbol]['records']]
            for moment in _chunk:
                moment.symbol = symbol
            moments += _chunk
        moments_shuffled = moments.copy()
        random.shuffle(moments_shuffled)
        self.assertNotEqual(moments, moments_shuffled)

        self.symbol_day_register.add_moments(moments_shuffled)

        actual_moments = list()
        for symbol in ('AAPL', 'BYND'):
            actual_moments += self.symbol_day_register.series[symbol].get_moments()
        self.assertEqual(moments, actual_moments)


class GetFromRegisterTestCase(unittest.TestCase):

    def setUp(self):
        self.symbol_day_register = SymbolDayRegister()

    def test_get(self):
        moments = list()
        for symbol in ('AAPL', 'BYND'):
            _chunk = [Moment.from_dict(x) for x in STOCKS_DAILY[symbol]['records']]
            for moment in _chunk:
                moment.symbol = symbol
            moments += _chunk

        self.symbol_day_register.add_moments(moments)

        for symbol in ('AAPL', 'BYND'):
            for _ in range(3):
                i = random.randint(0, len(STOCKS_DAILY[symbol]['records']) - 1)
                moment = Moment.from_dict(STOCKS_DAILY[symbol]['records'][i])
                moment.symbol = symbol
                self.assertEqual(moment, self.symbol_day_register.get(symbol, moment.epoch_s))

Test basic operations with SymbolRegister data structure.