1# -*- coding: utf-8 -*-
2#
3# Copyright (c) 2019 Jordi Mas i Hernandez <jmas@softcatala.org>
4#
5# This program is free software; you can redistribute it and/or
6# modify it under the terms of the GNU Lesser General Public
7# License as published by the Free Software Foundation; either
8# version 2.1 of the License, or (at your option) any later version.
9#
10# This program is distributed in the hope that it will be useful,
11# but WITHOUT ANY WARRANTY; without even the implied warranty of
12# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13# Lesser General Public License for more details.
14#
15# You should have received a copy of the GNU Lesser General Public
16# License along with this program; if not, write to the
17# Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18# Boston, MA 02111-1307, USA.
19
20
21import unittest
22from store.session import Session
23from store.sessionstore import SessionStore
24
25class TestSessionStore(unittest.TestCase):
26
27    def test_new_two_objects(self):
28        session_a = Session("session_A")
29        store_a = SessionStore()
30        store_a.add(session_a)
31
32        session_b = Session("session_B")
33        session_c = Session("session_C")
34        store_b = SessionStore()
35        store_b.add(session_b)
36        store_b.add(session_c)
37
38        self.assertEqual(1, len(store_a))
39        self.assertEqual(2, len(store_b))
40
41    def _on_session_added(self, store, session):
42        self.added_called = True
43
44    def test_add(self):
45        self.added_called = False
46        session = Session("session_A")
47        store = SessionStore()
48        store.connect_after('session-added', self._on_session_added)
49        store.add(session)
50
51        self.assertEqual(1, len(store))
52        self.assertTrue(self.added_called)
53
54    def _on_session_updated(self, store, session):
55        self.updated_called = True
56
57
58    def test_add_same_object_update(self):
59        self.updated_called = False
60        session = Session("session_A")
61        store = SessionStore()
62        store.connect_after('session-added', self._on_session_updated)
63        store.add(session)
64        session.name = 'Session B'
65        store.add(session)
66
67        self.assertEqual(1, len(store))
68        self.assertEqual('Session B', store[0].name)
69        self.assertTrue(self.updated_called)
70
71    def test_add_equal_object(self):
72        session_a = Session("session_A")
73        session_b = Session("session_A")
74        store = SessionStore()
75        store.add(session_a)
76        store.add(session_b)
77        self.assertEqual(1, len(store))
78
79    def test_remove(self):
80        session = Session("session_A")
81        store = SessionStore()
82        store.add(session)
83        store.remove(session)
84        self.assertEqual(0, len(store))
85
86if __name__ == '__main__':
87    unittest.main()
88