Esempio n. 1
0
    def __init__(self, **kwargs):
        super(GameManager, self).__init__(**kwargs)
        # creating our storage manager
        self.manager = DatabaseManager(SimpleImplementation())

        #creating an all cards deck (not necesary if we can disaply the all cards some other way)
        name = self.manager.create_attribute("name", "string", "All Cards")
        deck = self.manager.create_deck([], [
            name,
        ])
        self.manager.add_deck(deck)
        # generate some default data
        self.manager.generate_data()
        for decks in self.manager.implementation.decks:
            for card in decks.cards_:
                self.manager.implementation.decks[0].cards_.append(card)
class SimpleImplementationTest(unittest.TestCase):

    MAGIC_NUMBER = 100

    def setUp(self):
        self.manager = DatabaseManager(SimpleImplementation())

    def tearDown(self):
        self.manager.implementation.id_gen_ = 0

    def test_create_attribute(self):
        author = self.manager.create_attribute("author", "string", "AndreiRO")

        self.assertEqual(author.attribute_key_, "author")
        self.assertEqual(author.attribute_type_, "string")
        self.assertEqual(author.attribute_value_, "AndreiRO")

    def test_create_qa(self):
        author = self.manager.create_attribute("author", "string", "AndreiRO")
        opt = self.manager.create_attribute("mode", "string", "single_option")
        q = self.manager.create_attribute("question", "string", "Say hi!")
        qa = self.manager.create_qa([author, opt, q])

        self.assertNotEqual(qa.id_, None)
        self.assertEqual(qa.attributes_, [author, opt, q])
        self.assertEqual(qa.find_attribute("author"), author)

    def test_create_card(self):
        author = self.manager.create_attribute("author", "string", "AndreiRO")
        opt = self.manager.create_attribute("mode", "string", "single_option")
        q = self.manager.create_attribute("question", "string", "Say hi!")
        a = self.manager.create_attribute("answer", "string", "hi!")

        question = self.manager.create_qa([author, opt, q])
        answer = self.manager.create_qa([author, opt, a])

        c = self.manager.create_card(question, [
            answer,
        ], [author, opt])

        l = [question, answer, c]
        for i in range(len(l) - 1):
            for j in range(i + 1, len(l)):
                self.assertNotEqual(l[i].id_, l[j].id_)

        self.assertNotEqual(c.id_, None)
        self.assertEqual(c.question_, question)
        self.assertEqual(c.answers_, [
            answer,
        ])
        self.assertEqual(c.attributes_, [author, opt])

    def test_create_deck(self):
        author = self.manager.create_attribute("author", "string", "AndreiRO")
        opt = self.manager.create_attribute("mode", "string", "single_option")
        q = self.manager.create_attribute("question", "string", "Say hi!")
        a = self.manager.create_attribute("answer", "string", "hi!")

        question = self.manager.create_qa([author, opt, q])
        answer = self.manager.create_qa([author, opt, a])

        c = self.manager.create_card(question, [
            answer,
        ], [author, opt])
        d = self.manager.create_deck([
            c,
        ], [
            author,
        ])

        l = [question, answer, c, d]
        for i in range(len(l) - 1):
            for j in range(i + 1, len(l)):
                self.assertNotEqual(l[i].id_, l[j].id_)

        self.assertNotEqual(d.id_, None)
        self.assertEqual(d.cards_, [
            c,
        ])
        self.assertEqual(d.attributes_, [
            author,
        ])

    def test_add_deck(self):
        author = self.manager.create_attribute("author", "string", "AndreiRO")
        opt = self.manager.create_attribute("mode", "string", "single_option")
        q = self.manager.create_attribute("question", "string", "Say hi!")
        a = self.manager.create_attribute("answer", "string", "hi!")

        question = self.manager.create_qa([author, opt, q])
        answer = self.manager.create_qa([author, opt, a])

        c = self.manager.create_card(question, [
            answer,
        ], [author, opt])
        d = self.manager.create_deck([
            c,
        ], [
            author,
        ])

        self.manager.add_deck(d)
        self.assertEqual(self.manager.implementation.decks, [
            d,
        ])

        i = 0
        while i < SimpleImplementationTest.MAGIC_NUMBER:
            i += 1
            self.manager.add_deck(self.manager.create_deck(list(), list()))

        self.assertEqual(i + 1, len(self.manager.implementation.decks))

    def test_remove_deck(self):
        author = self.manager.create_attribute("author", "string", "AndreiRO")
        opt = self.manager.create_attribute("mode", "string", "single_option")
        q = self.manager.create_attribute("question", "string", "Say hi!")
        a = self.manager.create_attribute("answer", "string", "hi!")

        question = self.manager.create_qa([author, opt, q])
        answer = self.manager.create_qa([author, opt, a])

        c = self.manager.create_card(question, [
            answer,
        ], [author, opt])
        d = self.manager.create_deck([
            c,
        ], [
            author,
        ])
        self.manager.add_deck(d)

        self.assertTrue(self.manager.remove_deck(d.id_))
        self.assertEqual(self.manager.implementation.decks, list())

        i = 0
        while i < SimpleImplementationTest.MAGIC_NUMBER:
            i += 1
            self.manager.add_deck(self.manager.create_deck(list(), list()))

        self.assertEqual(i, len(self.manager.implementation.decks))

        i = 0
        while i < SimpleImplementationTest.MAGIC_NUMBER:
            self.manager.remove_deck(
                self.manager.remove_deck(
                    self.manager.implementation.decks[0].id_))
            i += 1

        self.assertEqual(0, len(self.manager.implementation.decks))

    def test_add_deck_card(self):
        i = 0
        while i < SimpleImplementationTest.MAGIC_NUMBER:
            d = self.manager.create_deck(list(), list())
            self.manager.add_deck(d)
            i += 1

        ids = []
        i = 0

        while i < SimpleImplementationTest.MAGIC_NUMBER:
            to_add = randint(1, 10)
            if to_add % 2 == 0:
                ids.append(self.manager.implementation.decks[i].id_)
            i += 1

        c = self.manager.create_card(None, list(), list())
        self.manager.add_deck_card(c, ids)
        added = 0
        for d in self.manager.implementation.decks:
            if d.id_ in ids:
                added += 1
                self.assertTrue(c in d.cards_)

        self.assertEqual(added, len(ids))

    def test_remove_deck_card(self):
        i = 0
        while i < SimpleImplementationTest.MAGIC_NUMBER:
            d = self.manager.create_deck(list(), list())
            self.manager.add_deck(d)
            i += 1

        ids = []
        i = 0

        while i < SimpleImplementationTest.MAGIC_NUMBER:
            to_add = randint(1, 10)
            if to_add % 2 == 0:
                ids.append(self.manager.implementation.decks[i].id_)
            i += 1

        c = self.manager.create_card(None, list(), list())
        self.manager.add_deck_card(c, ids)
        deleted = 0
        for d in self.manager.implementation.decks:
            if d.id_ in ids:
                self.manager.remove_deck_card(d.id_, c.id_)
                self.assertTrue(c not in d.cards_)
                deleted += 1

        self.assertEqual(deleted, len(ids))

    def test_add_deck_attribute(self):
        attributes = []
        a1 = self.manager.create_attribute("a1", "v1", "string")
        a2 = self.manager.create_attribute("a2", "v2", "string")
        a3 = self.manager.create_attribute("a3", "v3", "string")

        d = self.manager.create_deck(list(), [a1, a2])
        self.manager.add_deck(d)
        self.assertTrue(a1 in self.manager.implementation.decks[0].attributes_)
        self.assertTrue(a2 in self.manager.implementation.decks[0].attributes_)

        self.manager.add_deck_attribute(d.id_, a3)
        self.assertTrue(a3 in self.manager.implementation.decks[0].attributes_)

    def test_remove_deck_attribute(self):
        attributes = []
        a1 = self.manager.create_attribute("a1", "v1", "string")
        a2 = self.manager.create_attribute("a2", "v2", "string")
        a3 = self.manager.create_attribute("a3", "v3", "string")

        d = self.manager.create_deck(list(), [a1, a2, a3])
        self.manager.add_deck(d)

        self.manager.remove_deck_attribute(d.id_, "a2")
        self.assertTrue(
            a2 not in self.manager.implementation.decks[0].attributes_)
        self.assertTrue(a1 in self.manager.implementation.decks[0].attributes_)
        self.assertTrue(a3 in self.manager.implementation.decks[0].attributes_)

    def test_set_card_question(self):
        c = self.manager.create_card(None, list(), list())
        q = self.manager.create_qa(list())
        d = self.manager.create_deck([
            c,
        ], list())

        self.manager.add_deck(d)

        self.assertEqual(c.question_, None)
        self.manager.set_card_question(c.id_, q)
        self.assertEqual(c.question_, q)

    def test_get_card_question(self):
        c = self.manager.create_card(None, list(), list())
        q = self.manager.create_qa(list())
        d = self.manager.create_deck([
            c,
        ], list())

        self.manager.add_deck(d)
        self.manager.set_card_question(c.id_, q)
        self.assertEqual(q, self.manager.get_card_question(c.id_, d.id_))

    def test_add_card_answer(self):
        c = self.manager.create_card(None, list(), list())
        d1 = self.manager.create_deck([
            c,
        ], list())
        d2 = self.manager.create_deck([
            c,
        ], list())
        a = self.manager.create_qa(list())

        self.manager.add_deck(d1)
        self.manager.add_deck(d2)

        self.manager.add_card_answer(c.id_, a)
        self.assertEqual(c.answers_[0], a)
        self.assertEqual(len(c.answers_), 1)

    def test_get_card_answers(self):
        a1 = self.manager.create_qa(list())
        a2 = self.manager.create_qa(list())
        c = self.manager.create_card(None, [a1, a2], list())
        d = self.manager.create_deck([
            c,
        ], list())

        self.manager.add_deck(d)
        self.assertEqual([a1, a2], self.manager.get_card_answers(c.id_))

    def test_remove_card_answer(self):
        a1 = self.manager.create_qa(list())
        a2 = self.manager.create_qa(list())
        c = self.manager.create_card(None, [a1, a2], list())
        d = self.manager.create_deck([
            c,
        ], list())

        self.manager.add_deck(d)
        self.manager.remove_card_answer(c.id_, a2.id_)
        self.assertEqual([
            a1,
        ], self.manager.get_card_answers(c.id_))

    def test_add_card_attribute(self):
        a1 = self.manager.create_attribute("a1", "v1", "string")
        a2 = self.manager.create_attribute("a2", "v2", "strng")
        c = self.manager.create_card(None, list(), list())
        d1 = self.manager.create_deck([
            c,
        ], list())
        d2 = self.manager.create_deck([
            c,
        ], list())

        self.manager.add_deck(d1)
        self.manager.add_deck(d2)

        self.manager.add_card_attribute(c.id_, a1)
        self.manager.add_card_attribute(c.id_, a2)

        self.assertEqual(c.attributes_, [a1, a2])

    def test_remove_card_attribute(self):
        a1 = self.manager.create_attribute("a1", "v1", "string")
        a2 = self.manager.create_attribute("a2", "v2", "strng")
        c = self.manager.create_card(None, list(), [a1, a2])
        d1 = self.manager.create_deck([
            c,
        ], list())
        d2 = self.manager.create_deck([
            c,
        ], list())

        self.manager.add_deck(d1)
        self.manager.add_deck(d2)

        self.manager.remove_card_attribute(c.id_, "a2")
        self.assertEqual(c.attributes_, [
            a1,
        ])

    def test_add_question_attribute(self):
        a1 = self.manager.create_attribute("a1", "v1", "string")
        a2 = self.manager.create_attribute("a2", "v2", "strng")
        q = self.manager.create_qa(list())
        c = self.manager.create_card(q, list(), list())

        d1 = self.manager.create_deck([
            c,
        ], list())
        d2 = self.manager.create_deck([
            c,
        ], list())

        self.manager.add_deck(d1)
        self.manager.add_deck(d2)

        self.manager.add_question_attribute(q.id_, a1)
        self.manager.add_question_attribute(q.id_, a2)

        self.assertEqual(q.attributes_, [a1, a2])

    def test_remove_question_attribute(self):
        a1 = self.manager.create_attribute("a1", "v1", "string")
        a2 = self.manager.create_attribute("a2", "v2", "strng")
        q = self.manager.create_qa([a1, a2])
        c = self.manager.create_card(q, list(), list())

        d1 = self.manager.create_deck([
            c,
        ], list())
        d2 = self.manager.create_deck([
            c,
        ], list())

        self.manager.add_deck(d1)
        self.manager.add_deck(d2)

        self.manager.remove_question_attribute(q.id_, "a2")
        self.assertEqual(q.attributes_, [a1])

    def test_add_answer_attribute(self):
        a1 = self.manager.create_attribute("a1", "v1", "string")
        a2 = self.manager.create_attribute("a2", "v2", "strng")
        ans = self.manager.create_qa(list())
        c = self.manager.create_card(None, [
            ans,
        ], list())

        d1 = self.manager.create_deck([
            c,
        ], list())
        d2 = self.manager.create_deck([
            c,
        ], list())

        self.manager.add_deck(d1)
        self.manager.add_deck(d2)

        self.manager.add_answer_attribute(ans.id_, a1)
        self.manager.add_answer_attribute(ans.id_, a2)

        self.assertEqual(ans.attributes_, [a1, a2])

    def test_remove_answer_attribute(self):
        a1 = self.manager.create_attribute("a1", "v1", "string")
        a2 = self.manager.create_attribute("a2", "v2", "strng")
        ans = self.manager.create_qa([a1, a2])
        c = self.manager.create_card(None, [
            ans,
        ], list())

        d1 = self.manager.create_deck([
            c,
        ], list())
        d2 = self.manager.create_deck([
            c,
        ], list())

        self.manager.add_deck(d1)
        self.manager.add_deck(d2)
        self.manager.remove_answer_attribute(ans.id_, "a2")

        self.assertEqual(ans.attributes_, [a1])

    def test_find_card_by_attribute(self):
        author = self.manager.create_attribute("author", "string", "AndreiRO")
        name1 = self.manager.create_attribute("name", "string", "First")
        name2 = self.manager.create_attribute("name", "string", "Second")

        c1 = self.manager.create_card(None, None, [author, name1])
        c2 = self.manager.create_card(None, None, [author, name2])
        d = self.manager.create_deck([c1, c2], [
            author,
        ])

        self.manager.add_deck(d)

        self.assertEqual(
            self.manager.find_card_by_attribute("author", "AndreiRO"),
            [c1, c2])
        self.assertEqual(self.manager.find_card_by_attribute("name", "First"),
                         [
                             c1,
                         ])

    def test_find_question_by_attribute(self):
        author = self.manager.create_attribute("author", "string", "AndreiRO")
        name1 = self.manager.create_attribute("question", "string", "First?")
        name2 = self.manager.create_attribute("question", "string", "Second?")

        q1 = self.manager.create_qa([author, name1])
        q2 = self.manager.create_qa([author, name2])
        c = self.manager.create_card(q1, None, None)
        c2 = self.manager.create_card(q2, None, None)
        d = self.manager.create_deck([c, c2], [
            author,
        ])

        self.manager.add_deck(d)

        self.assertEqual(
            self.manager.find_question_by_attribute("author", "AndreiRO"),
            [q1, q2])
        self.assertEqual(
            self.manager.find_question_by_attribute("question", "First?"), [
                q1,
            ])

    def test_find_answer_by_attribute(self):
        author = self.manager.create_attribute("author", "string", "AndreiRO")
        name1 = self.manager.create_attribute("question", "string", "First!")
        name2 = self.manager.create_attribute("question", "string", "Second!")

        a1 = self.manager.create_qa([author, name1])
        a2 = self.manager.create_qa([author, name2])
        c = self.manager.create_card(None, [
            a1,
        ], None)
        c2 = self.manager.create_card(None, [
            a2,
        ], None)
        d = self.manager.create_deck([c, c2], [
            author,
        ])

        self.manager.add_deck(d)

        self.assertEqual(
            self.manager.find_answer_by_attribute("author", "AndreiRO"),
            [a1, a2])
        self.assertEqual(
            self.manager.find_answer_by_attribute("question", "First!"), [
                a1,
            ])

    def test_find_deck_by_attribute(self):
        author = self.manager.create_attribute("author", "string", "AndreiRO")
        name1 = self.manager.create_attribute("name", "string", "First")
        name2 = self.manager.create_attribute("name", "string", "Second")

        d = self.manager.create_deck(None, [author, name1])
        d2 = self.manager.create_deck(None, [author, name2])

        self.manager.add_deck(d)
        self.manager.add_deck(d2)

        self.assertEqual(
            self.manager.find_deck_by_attribute("author", "AndreiRO"), [d, d2])
        self.assertEqual(self.manager.find_deck_by_attribute("name", "First"),
                         [
                             d,
                         ])

    def test_generate_data(self):
        self.manager.generate_data()
        self.assertEqual(len(self.manager.implementation.decks), 2)
        for d in self.manager.implementation.decks:
            self.assertNotEqual(d.attributes_, None)
            for c in d.cards_:
                self.assertNotEqual(c.attributes_, None)
                self.assertNotEqual(c.question_, None)
                self.assertNotEqual(c.answers_, None)
 def setUp(self):
     self.manager = DatabaseManager(SimpleImplementation())
Esempio n. 4
0
class GameManager(ScreenManager):

    # variable that helps us keep track of the current deck
    current_deck = ObjectProperty(None)
    # variable that helps us know if we currently have a modalview dispalyed
    modal_state = ObjectProperty(1)

    ## mainbutton nu are rost aici , trebuie implementat altfel !

    mainbutton = ObjectProperty(None)

    def __init__(self, **kwargs):
        super(GameManager, self).__init__(**kwargs)
        # creating our storage manager
        self.manager = DatabaseManager(SimpleImplementation())

        #creating an all cards deck (not necesary if we can disaply the all cards some other way)
        name = self.manager.create_attribute("name", "string", "All Cards")
        deck = self.manager.create_deck([], [
            name,
        ])
        self.manager.add_deck(deck)
        # generate some default data
        self.manager.generate_data()
        for decks in self.manager.implementation.decks:
            for card in decks.cards_:
                self.manager.implementation.decks[0].cards_.append(card)
        #self.current="add_card"

    # method called from PlayDeck to switch to Study
    def switch_to_study(self, *args):
        self.current_deck = self.manager.find_deck_by_attribute(
            "name", self.ids.s6.ids.deck_label.text)[0]
        #reseting the counter for cards played in study
        self.ids.s7.i = 0
        # changing the actionlabel text from study to display the currently played deck
        self.ids.s7.ids.stal.text = 'Study ' + self.current_deck.find_attribute(
            "name").attribute_value_
        #reset the Study screen before switching to it
        self.ids.s7.reset()
        self.current = 'study'

    #method called from SoloMenu to switch to PlayDeck
    def switch_to_deckplay(self, button):
        #changing the deck_label text and the actionlabel text to the text of the button we just released from the decks button in SoloMenu
        self.ids.s6.ids.deck_label.text = button.text
        self.ids.s6.ids.action_deck_label.text = button.text
        self.current = 'play_deck'

    #method called from PlayDeck to switch to AddCard
    def set_deck(self, text):
        #changing the text of the deck selecter button into the name of our current deck
        self.ids.s3.ids.butd.text = text
        self.current = 'add_card'

    #method called from CardBrowser to switch to AddCard
    def switch_to_add_card(self, *args):
        #selecting the card we just pressed from the cardbrowser
        self.current_screen.current_card = self.manager.find_card_by_qa(
            self.manager.find_question_by_attribute("question",
                                                    args[0].text)[0])
        #changing the create card button into save changes button
        self.ids.s3.ids.ac_create_card.text = 'Save changes'
        #making sure we display our cards question and answer in AddCard
        self.ids.s3.ids.lab_q.text = args[0].text
        self.ids.s3.ids.lab_a.text = self.manager.find_card_by_qa(
            self.manager.find_question_by_attribute("question", args[0].text)
            [0]).answers_[0].find_attribute("answer").attribute_value_
        self.current = 'add_card'

    #method called from the sidepanel to switch to CardBrowser
    def switch_to_card_browser(self):
        self.current = 'card_browser'

        #creating a dropdown for the user to select deck
        dropdown = DropDown()
        self.ids.s8.ids.ddbox.clear_widgets()

        self.mainbutton = Button(text='All Cards',
                                 color=[0, 0, 0, 1],
                                 background_color=[1, 1, 1, 1],
                                 background_normal='')
        self.ids.s8.ids.ddbox.add_widget(self.mainbutton)
        self.mainbutton.fast_bind('on_release', dropdown.open)

        for decks in self.manager.implementation.decks:
            btn = Button(text=decks.find_attribute("name").attribute_value_,
                         size_hint_y=None,
                         height='48dp',
                         color=[0, 0, 0, 1],
                         background_color=[1, 1, 1, 1],
                         background_normal='')
            btn.fast_bind('on_release', self.dd_select, btn, dropdown)
            dropdown.add_widget(btn)

        dropdown.bind(
            on_select=lambda instance, x: setattr(self.mainbutton, 'text', x))

        #dispalying the cards
        self.cb_display_cards('basic', self.mainbutton)

    def dd_select(self, *args):
        args[1].select(args[0].text)
        self.cb_display_cards('basic', args[0])

    #method to display the cards
    def cb_display_cards(self, *args):

        ## TODO:
        """ Add search bar in the ActionBar by inheriting it from ActionItem """

        ## BUG:
        """ Find a way to cancel re.search() special regex characters . We want it to look
            only normal """

        ## TODO:
        """ Add better implementation of this """

        #clear all previous dispalys
        self.ids.s8.ids.gl.clear_widgets()
        self.ids.s8.ids.search.state = 'normal'
        # checking if we want to display after a search or after a selected deck( 'basic' is for decks and 'search' is for search)
        if args[0] is 'basic':
            # dispaying the cards of the deck indicated by the dropdown's mainbutton
            for card in self.manager.find_deck_by_attribute(
                    "name", args[1].text
            )[0].cards_:  #find_deck_by_attribute("name",args[1].text)[0].cards_:
                self.ids.s8.ids.gl.add_widget(
                    Button(
                        size_hint_y=None,
                        height='25dp',
                        text=card.question_.find_attribute("question").
                        attribute_value_,  #.replace(args[2],'[color=#FF0000]%s[/color]' % args[2]) ,
                        color=[0, 0, 0, 1],
                        background_color=[1, 1, 1, 1],
                        background_normal='',
                        background_down='',
                        on_release=self.switch_to_add_card))

                self.ids.s8.ids.gl.add_widget(
                    Button(
                        size_hint_y=None,
                        height='25dp',
                        text=card.answers_[0].find_attribute("answer").
                        attribute_value_,  #.replace(args[2],'[color=#FF0000]%s[/color]' % args[2]) ,
                        color=[0, 0, 0, 1],
                        background_color=[1, 1, 1, 1],
                        background_normal='',
                        background_down='',
                        on_release=self.switch_to_add_card))
        elif args[0] is 'search':
            #displaying only the cards of the deck indicated by the dropdown's mainbuuton
            for card in self.manager.find_deck_by_attribute(
                    "name", args[1].text
            )[0].cards_:  #find_deck_by_attribute("name",args[1].text)[0].cards_:
                #looking for our searchinput in questions or answers
                if args[2] in card.question_.find_attribute("question").attribute_value_ or \
                    args[2] in card.answers_[0].find_attribute("answer").attribute_value_:
                    self.ids.s8.ids.gl.add_widget(
                        Button(
                            size_hint_y=None,
                            markup=True,
                            height='25dp',
                            color=[0, 0, 0, 1],
                            text=card.question_.find_attribute("question").
                            attribute_value_,  #.replace(args[2],'[color=#FF0000]%s[/color]' % args[2]) ,
                            background_color=[1, 1, 1, 1],
                            background_normal='',
                            background_down='',
                            on_release=self.switch_to_add_card))

                    self.ids.s8.ids.gl.add_widget(
                        Button(
                            size_hint_y=None,
                            markup=True,
                            height='25dp',
                            color=[0, 0, 0, 1],
                            text=card.answers_[0].find_attribute("answer").
                            attribute_value_,  #.replace(args[2],'[color=#FF0000]%s[/color]' % args[2]) ,
                            background_color=[1, 1, 1, 1],
                            background_normal='',
                            background_down='',
                            on_release=self.switch_to_add_card))

    #method to switch to SoloMenu
    def switch_to_solo_menu(self):
        self.current = 'main_menu'
        #recreating our gridlayout
        for deck in self.manager.implementation.decks:
            btn = Button(color=(0, 0, 0, 1),
                         text=deck.find_attribute("name").attribute_value_,
                         size_hint_y=None,
                         height='50dp',
                         background_normal='',
                         background_color=(1, 1, 1, 1),
                         on_release=self.switch_to_deckplay)
            self.ids.s5.ids.gl1.add_widget(btn)
class SimpleImplementationTest(unittest.TestCase):

	MAGIC_NUMBER = 100

	def setUp(self):
		self.manager = DatabaseManager(SimpleImplementation())

	def tearDown(self):
		self.manager.implementation.id_gen_ = 0

	def test_create_attribute(self):
		author = self.manager.create_attribute("author", "string", "AndreiRO")

		self.assertEqual(author.attribute_key_, "author")
		self.assertEqual(author.attribute_type_, "string")
		self.assertEqual(author.attribute_value_, "AndreiRO")

	def test_create_qa(self):
		author = self.manager.create_attribute("author", "string", "AndreiRO")
		opt = self.manager.create_attribute("mode", "string", "single_option")
		q = self.manager.create_attribute("question", "string", "Say hi!")
		qa = self.manager.create_qa([author, opt, q])

		self.assertNotEqual(qa.id_, None)
		self.assertEqual(qa.attributes_, [author, opt, q])
		self.assertEqual(qa.find_attribute("author"), author)

	def test_create_card(self):
		author = self.manager.create_attribute("author", "string", "AndreiRO")
		opt = self.manager.create_attribute("mode", "string", "single_option")
		q = self.manager.create_attribute("question", "string", "Say hi!")
		a = self.manager.create_attribute("answer", "string", "hi!")

		question = self.manager.create_qa([author, opt, q])
		answer = self.manager.create_qa([author, opt, a])

		c = self.manager.create_card(question, [answer, ], [author, opt])

		l = [question, answer, c]
		for i in range(len(l) - 1):
			for j in range(i + 1, len(l)):
				self.assertNotEqual(l[i].id_, l[j].id_)

		self.assertNotEqual(c.id_, None)
		self.assertEqual(c.question_, question)
		self.assertEqual(c.answers_, [answer,])
		self.assertEqual(c.attributes_, [author, opt])

	def test_create_deck(self):
		author = self.manager.create_attribute("author", "string", "AndreiRO")
		opt = self.manager.create_attribute("mode", "string", "single_option")
		q = self.manager.create_attribute("question", "string", "Say hi!")
		a = self.manager.create_attribute("answer", "string", "hi!")

		question = self.manager.create_qa([author, opt, q])
		answer = self.manager.create_qa([author, opt, a])

		c = self.manager.create_card(question, [answer, ], [author, opt])		
		d = self.manager.create_deck([c, ], [author, ])

		l = [question, answer, c, d]
		for i in range(len(l) - 1):
			for j in range(i + 1, len(l)):
				self.assertNotEqual(l[i].id_, l[j].id_)
		
		self.assertNotEqual(d.id_, None)
		self.assertEqual(d.cards_, [c,])
		self.assertEqual(d.attributes_, [author, ])

	def test_add_deck(self):
		author = self.manager.create_attribute("author", "string", "AndreiRO")
		opt = self.manager.create_attribute("mode", "string", "single_option")
		q = self.manager.create_attribute("question", "string", "Say hi!")
		a = self.manager.create_attribute("answer", "string", "hi!")

		question = self.manager.create_qa([author, opt, q])
		answer = self.manager.create_qa([author, opt, a])

		c = self.manager.create_card(question, [answer, ], [author, opt])		
		d = self.manager.create_deck([c, ], [author, ])

		self.manager.add_deck(d)
		self.assertEqual(self.manager.implementation.decks, [d, ])

		i = 0
		while i < SimpleImplementationTest.MAGIC_NUMBER:
			i += 1
			self.manager.add_deck(self.manager.create_deck(list(), list()))

		self.assertEqual(i + 1, len(self.manager.implementation.decks))

	def test_remove_deck(self):
		author = self.manager.create_attribute("author", "string", "AndreiRO")
		opt = self.manager.create_attribute("mode", "string", "single_option")
		q = self.manager.create_attribute("question", "string", "Say hi!")
		a = self.manager.create_attribute("answer", "string", "hi!")

		question = self.manager.create_qa([author, opt, q])
		answer = self.manager.create_qa([author, opt, a])

		c = self.manager.create_card(question, [answer, ], [author, opt])		
		d = self.manager.create_deck([c, ], [author, ])
		self.manager.add_deck(d)

		self.assertTrue(self.manager.remove_deck(d.id_))
		self.assertEqual(self.manager.implementation.decks, list())

		i = 0
		while i < SimpleImplementationTest.MAGIC_NUMBER:
			i += 1
			self.manager.add_deck(self.manager.create_deck(list(), list()))

		self.assertEqual(i, len(self.manager.implementation.decks))

		i = 0
		while i < SimpleImplementationTest.MAGIC_NUMBER:
			self.manager.remove_deck(self.manager.remove_deck(self.manager.implementation.decks[0].id_))
			i += 1

		self.assertEqual(0, len(self.manager.implementation.decks))

	def test_add_deck_card(self):
		i = 0
		while i < SimpleImplementationTest.MAGIC_NUMBER:
			d = self.manager.create_deck(list(), list())
			self.manager.add_deck(d)
			i += 1

		ids = []
		i = 0

		while i < SimpleImplementationTest.MAGIC_NUMBER:
			to_add = randint(1,10)
			if to_add % 2 == 0:
				ids.append(self.manager.implementation.decks[i].id_)
			i += 1

		c = self.manager.create_card(None, list(), list())
		self.manager.add_deck_card(c, ids)
		added = 0
		for d in self.manager.implementation.decks:
			if d.id_ in ids:
				added += 1
				self.assertTrue(c in d.cards_)

		self.assertEqual(added, len(ids))

	def test_remove_deck_card(self):
		i = 0
		while i < SimpleImplementationTest.MAGIC_NUMBER:
			d = self.manager.create_deck(list(), list())
			self.manager.add_deck(d)
			i += 1

		ids = []
		i = 0

		while i < SimpleImplementationTest.MAGIC_NUMBER:
			to_add = randint(1,10)
			if to_add % 2 == 0:
				ids.append(self.manager.implementation.decks[i].id_)
			i += 1

		c = self.manager.create_card(None, list(), list())
		self.manager.add_deck_card(c, ids)
		deleted = 0
		for d in self.manager.implementation.decks:
			if d.id_ in ids:
				self.manager.remove_deck_card(d.id_, c.id_)
				self.assertTrue(c not in d.cards_)
				deleted += 1

		self.assertEqual(deleted, len(ids))

	def test_add_deck_attribute(self):
		attributes = []
		a1 = self.manager.create_attribute("a1", "v1", "string")
		a2 = self.manager.create_attribute("a2", "v2", "string")
		a3 = self.manager.create_attribute("a3", "v3", "string")

		d = self.manager.create_deck(list(),[a1, a2])
		self.manager.add_deck(d)
		self.assertTrue(a1 in self.manager.implementation.decks[0].attributes_)
		self.assertTrue(a2 in self.manager.implementation.decks[0].attributes_)

		self.manager.add_deck_attribute(d.id_, a3)
		self.assertTrue(a3 in self.manager.implementation.decks[0].attributes_)


	def test_remove_deck_attribute(self):
		attributes = []
		a1 = self.manager.create_attribute("a1", "v1", "string")
		a2 = self.manager.create_attribute("a2", "v2", "string")
		a3 = self.manager.create_attribute("a3", "v3", "string")

		d = self.manager.create_deck(list(),[a1, a2, a3])
		self.manager.add_deck(d)
		
		self.manager.remove_deck_attribute(d.id_, "a2")
		self.assertTrue(a2 not in self.manager.implementation.decks[0].attributes_)
		self.assertTrue(a1 in self.manager.implementation.decks[0].attributes_)
		self.assertTrue(a3 in self.manager.implementation.decks[0].attributes_)

	def test_set_card_question(self):
		c = self.manager.create_card(None, list(), list())
		q = self.manager.create_qa(list())
		d = self.manager.create_deck([c, ], list())

		self.manager.add_deck(d)

		self.assertEqual(c.question_, None)
		self.manager.set_card_question(c.id_, q)
		self.assertEqual(c.question_, q)

	def test_get_card_question(self):
		c = self.manager.create_card(None, list(), list())
		q = self.manager.create_qa(list())
		d = self.manager.create_deck([c, ], list())

		self.manager.add_deck(d)
		self.manager.set_card_question(c.id_, q)
		self.assertEqual(q, self.manager.get_card_question(c.id_, d.id_))
	
	def test_add_card_answer(self):
		c = self.manager.create_card(None, list(), list())
		d1 = self.manager.create_deck([c, ], list())
		d2 = self.manager.create_deck([c, ], list())
		a = self.manager.create_qa(list())

		self.manager.add_deck(d1)
		self.manager.add_deck(d2)

		self.manager.add_card_answer(c.id_, a)
		self.assertEqual(c.answers_[0], a)
		self.assertEqual(len(c.answers_), 1)

	def test_get_card_answers(self):
		a1 = self.manager.create_qa(list())
		a2 = self.manager.create_qa(list())
		c = self.manager.create_card(None, [a1, a2], list())
		d = self.manager.create_deck([c, ], list())

		self.manager.add_deck(d)
		self.assertEqual([a1, a2], self.manager.get_card_answers(c.id_))

	def test_remove_card_answer(self):
		a1 = self.manager.create_qa(list())
		a2 = self.manager.create_qa(list())
		c = self.manager.create_card(None, [a1, a2], list())
		d = self.manager.create_deck([c, ], list())

		self.manager.add_deck(d)
		self.manager.remove_card_answer(c.id_, a2.id_)
		self.assertEqual([a1, ], self.manager.get_card_answers(c.id_))		
	
	def test_add_card_attribute(self):
		a1 = self.manager.create_attribute("a1", "v1", "string")
		a2 = self.manager.create_attribute("a2", "v2", "strng")
		c = self.manager.create_card(None, list(), list())
		d1 = self.manager.create_deck([c, ], list())
		d2 = self.manager.create_deck([c, ], list())

		self.manager.add_deck(d1)
		self.manager.add_deck(d2)

		self.manager.add_card_attribute(c.id_, a1)
		self.manager.add_card_attribute(c.id_, a2)

		self.assertEqual(c.attributes_, [a1, a2])
		
	def test_remove_card_attribute(self):
		a1 = self.manager.create_attribute("a1", "v1", "string")
		a2 = self.manager.create_attribute("a2", "v2", "strng")
		c = self.manager.create_card(None, list(), [a1, a2])
		d1 = self.manager.create_deck([c, ], list())
		d2 = self.manager.create_deck([c, ], list())

		self.manager.add_deck(d1)
		self.manager.add_deck(d2)

		self.manager.remove_card_attribute(c.id_, "a2")
		self.assertEqual(c.attributes_, [a1,])

		
	def test_add_question_attribute(self):
		a1 = self.manager.create_attribute("a1", "v1", "string")
		a2 = self.manager.create_attribute("a2", "v2", "strng")
		q = self.manager.create_qa(list())
		c = self.manager.create_card(q, list(), list())

		d1 = self.manager.create_deck([c, ], list())
		d2 = self.manager.create_deck([c, ], list())

		self.manager.add_deck(d1)
		self.manager.add_deck(d2)

		self.manager.add_question_attribute(q.id_, a1)
		self.manager.add_question_attribute(q.id_, a2)

		self.assertEqual(q.attributes_, [a1, a2])
	
	def test_remove_question_attribute(self):
		a1 = self.manager.create_attribute("a1", "v1", "string")
		a2 = self.manager.create_attribute("a2", "v2", "strng")
		q = self.manager.create_qa([a1, a2])
		c = self.manager.create_card(q, list(), list())

		d1 = self.manager.create_deck([c, ], list())
		d2 = self.manager.create_deck([c, ], list())

		self.manager.add_deck(d1)
		self.manager.add_deck(d2)

		self.manager.remove_question_attribute(q.id_, "a2")
		self.assertEqual(q.attributes_, [a1])
	
	def test_add_answer_attribute(self):
		a1 = self.manager.create_attribute("a1", "v1", "string")
		a2 = self.manager.create_attribute("a2", "v2", "strng")
		ans = self.manager.create_qa(list())
		c = self.manager.create_card(None, [ans, ], list())

		d1 = self.manager.create_deck([c, ], list())
		d2 = self.manager.create_deck([c, ], list())

		self.manager.add_deck(d1)
		self.manager.add_deck(d2)

		self.manager.add_answer_attribute(ans.id_, a1)
		self.manager.add_answer_attribute(ans.id_, a2)

		self.assertEqual(ans.attributes_, [a1, a2])

	def test_remove_answer_attribute(self):
		a1 = self.manager.create_attribute("a1", "v1", "string")
		a2 = self.manager.create_attribute("a2", "v2", "strng")
		ans = self.manager.create_qa([a1, a2])
		c = self.manager.create_card(None, [ans, ], list())

		d1 = self.manager.create_deck([c, ], list())
		d2 = self.manager.create_deck([c, ], list())

		self.manager.add_deck(d1)
		self.manager.add_deck(d2)
		self.manager.remove_answer_attribute(ans.id_, "a2")

		self.assertEqual(ans.attributes_, [a1])

	def test_find_card_by_attribute(self):
		author = self.manager.create_attribute("author", "string", "AndreiRO")	
		name1 = self.manager.create_attribute("name", "string", "First")
		name2 = self.manager.create_attribute("name", "string", "Second")

		c1 = self.manager.create_card(None, None, [author, name1])
		c2 = self.manager.create_card(None, None, [author, name2])
		d = self.manager.create_deck([c1, c2], [author, ])

		self.manager.add_deck(d)

		self.assertEqual(
			self.manager.find_card_by_attribute("author", "AndreiRO"),
			[c1, c2]
		)
		self.assertEqual(
			self.manager.find_card_by_attribute("name", "First"),
			[c1, ]
		)		
	
	def test_find_question_by_attribute(self):
		author = self.manager.create_attribute("author", "string", "AndreiRO")	
		name1 = self.manager.create_attribute("question", "string", "First?")
		name2 = self.manager.create_attribute("question", "string", "Second?")

		q1 = self.manager.create_qa([author, name1])
		q2 = self.manager.create_qa([author, name2])
		c = self.manager.create_card(q1, None, None)
		c2 = self.manager.create_card(q2, None, None)
		d = self.manager.create_deck([c, c2], [author, ])

		self.manager.add_deck(d)

		self.assertEqual(
			self.manager.find_question_by_attribute("author", "AndreiRO"),
			[q1, q2]
		)
		self.assertEqual(
			self.manager.find_question_by_attribute("question", "First?"),
			[q1, ]
		)
	
	def test_find_answer_by_attribute(self):
		author = self.manager.create_attribute("author", "string", "AndreiRO")	
		name1 = self.manager.create_attribute("question", "string", "First!")
		name2 = self.manager.create_attribute("question", "string", "Second!")

		a1 = self.manager.create_qa([author, name1])
		a2 = self.manager.create_qa([author, name2])
		c = self.manager.create_card(None, [a1, ], None)
		c2 = self.manager.create_card(None, [a2, ], None)
		d = self.manager.create_deck([c, c2], [author, ])

		self.manager.add_deck(d)

		self.assertEqual(
			self.manager.find_answer_by_attribute("author", "AndreiRO"),
			[a1, a2]
		)
		self.assertEqual(
			self.manager.find_answer_by_attribute("question", "First!"),
			[a1, ]
		)

	def test_find_deck_by_attribute(self):
		author = self.manager.create_attribute("author", "string", "AndreiRO")	
		name1 = self.manager.create_attribute("name", "string", "First")
		name2 = self.manager.create_attribute("name", "string", "Second")
		

		d = self.manager.create_deck(None, [author, name1])
		d2 = self.manager.create_deck(None, [author, name2])

		self.manager.add_deck(d)
		self.manager.add_deck(d2)

		self.assertEqual(
			self.manager.find_deck_by_attribute("author", "AndreiRO"),
			[d, d2]
		)
		self.assertEqual(
			self.manager.find_deck_by_attribute("name", "First"),
			[d, ]
		)

	def test_generate_data(self):
		self.manager.generate_data()
		self.assertEqual(len(self.manager.implementation.decks), 2)
		for d in self.manager.implementation.decks:
			self.assertNotEqual(d.attributes_, None)		
			for c in d.cards_:
				self.assertNotEqual(c.attributes_, None)
				self.assertNotEqual(c.question_, None)
				self.assertNotEqual(c.answers_, None)
	def setUp(self):
		self.manager = DatabaseManager(SimpleImplementation())