source: geniuswebcore/test/geniusweb/protocol/session/learn/LearnStateTest.py@ 100

Last change on this file since 100 was 100, checked in by ruud, 19 months ago

python installs also wheel to avoid error messages

File size: 4.5 KB
Line 
1import unittest
2from unittest.mock import Mock
3
4from uri.uri import URI
5
6from geniusweb.actions.EndNegotiation import EndNegotiation
7from geniusweb.actions.LearningDone import LearningDone
8from geniusweb.actions.PartyId import PartyId
9from geniusweb.deadline.DeadlineTime import DeadlineTime
10from geniusweb.inform.Agreements import Agreements
11from geniusweb.progress.Progress import Progress
12from geniusweb.protocol.ProtocolException import ProtocolException
13from geniusweb.protocol.session.TeamInfo import TeamInfo
14from geniusweb.protocol.session.learn.LearnSettings import LearnSettings
15from geniusweb.protocol.session.learn.LearnState import LearnState
16from geniusweb.references.Parameters import Parameters
17from geniusweb.references.PartyRef import PartyRef
18from geniusweb.references.PartyWithParameters import PartyWithParameters
19from geniusweb.references.PartyWithProfile import PartyWithProfile
20from geniusweb.references.ProfileRef import ProfileRef
21
22
23class LearnStateTest(unittest.TestCase):
24
25 PARTY2 = "party2"
26 PARTY1 = "party1"
27 party1id = PartyId(PARTY1)
28 party2id = PartyId(PARTY2)
29 deadline = DeadlineTime(10000)
30 partyprofile1 = Mock(PartyWithProfile)
31 partyprofile2 = Mock(PartyWithProfile)
32 progressnotdone = Mock(Progress)
33 progressdone = Mock(Progress);
34
35 def setUp(self):
36 self.params = Parameters()
37 self.params = self.params.With("persistentstate",
38 "6bb5f909-0079-43ac-a8ac-a31794391074")
39 self.params = self.params.With("negotiationdata",
40 ["12b5f909-0079-43ac-a8ac-a31794391012"])
41
42 self.progressnotdone.isPastDeadline = Mock(return_value=False)
43 self.progressdone.isPastDeadline = Mock(return_value=True)
44
45 # define team1
46 party1ref = PartyRef(URI(self.PARTY1))
47 party1 = PartyWithParameters(party1ref, self.params)
48 profile1 = ProfileRef(URI("http://prof1"))
49 partywithp1 = PartyWithProfile(party1, profile1)
50 team1pp = [partywithp1]
51 self.team1 = TeamInfo(team1pp)
52
53 # define team2
54 party2ref = PartyRef(URI(self.PARTY2))
55 party2 = PartyWithParameters(party2ref, self.params)
56 profile2 = ProfileRef(URI("http://prof2"))
57 partywithp2 = PartyWithProfile(party2, profile2)
58 team2pp = [partywithp2]
59 self.team2 = TeamInfo(team2pp)
60
61 participants = [self.team1, self.team2]
62 settings = LearnSettings(participants, self.deadline)
63 self.state = LearnState([], [], None, settings)
64
65 self.connectedstate = self.state.WithParty(self.party1id, self.partyprofile1)\
66 .WithParty(self.party2id, self.partyprofile2).WithProgress(self.progressnotdone)
67
68 def testsmokeTestNull(self):
69 self.assertRaises(ValueError, lambda:LearnState([], [], None, None))
70
71 def testsmokeTest(self):
72 settings = Mock(LearnSettings)
73 LearnState([], [], None, settings)
74
75 def testgetAgreementsTest(self):
76 self.assertEqual(Agreements(), self.state.getAgreements())
77
78 def testisFinalTest(self):
79 # initial state is false, because we check
80 # that there are no learningDone reports
81 self.assertFalse(self.state.isFinal(0))
82 # not final because there are connected parties not yet done.
83 self.assertFalse(self.connectedstate.isFinal(0))
84 finalstate = self.connectedstate.WithProgress(self.progressdone)
85 self.assertTrue(finalstate.isFinal(0))
86
87 errorstate = self.connectedstate\
88 .WithException(ProtocolException("test", self.party1id))
89 self.assertTrue(errorstate.isFinal(0))
90
91 def testcheckActionTest(self):
92 # check basic illegal actions in Learning
93 self.assertNotEqual(None, self.connectedstate.checkAction(self.party1id, None))
94 self.assertNotEqual(None, self.connectedstate.checkAction(PartyId("unknown"), None))
95 self.assertNotEqual(None, self.connectedstate.checkAction(self.party1id,
96 EndNegotiation(self.party1id)))
97 self.assertNotEqual(None, self.connectedstate.checkAction(self.party1id,
98 LearningDone(self.party2id)))
99
100 # check a good action is allowed
101 self.assertEqual(None, self.connectedstate.checkAction(self.party1id,
102 LearningDone(self.party1id)))
103
104 # no learning twice not ok
105 learned = self.connectedstate.WithAction(self.party1id,
106 LearningDone(self.party1id))
107 self.assertNotEqual(None,
108 learned.checkAction(self.party1id, LearningDone(self.party1id)))
109
110 def testLearnDoneActionTest(self):
111 learned = self.connectedstate.WithAction(self.party1id,
112 LearningDone(self.party1id))
113
114 def testisFinalWhenAllLearned(self):
115 learned = self.connectedstate\
116 .WithAction(self.party1id, LearningDone(self.party1id))\
117 .WithAction(self.party2id, LearningDone(self.party2id))
118 self.assertTrue(learned.isFinal(0))
119
120 def testgetResultTest(self):
121 res = self.connectedstate.getResults()[0]
122 self.assertEqual(0, len(res.getAgreements().getMap()))
Note: See TracBrowser for help on using the repository browser.