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