source: geniuswebcore/test/geniusweb/issuevalue/DomainTest.py@ 94

Last change on this file since 94 was 90, checked in by Bart Vastenhouw, 3 years ago

Refactor to help reusing partiesserver.

File size: 5.7 KB
Line 
1from decimal import Decimal
2import json
3import unittest
4
5from pyson.ObjectMapper import ObjectMapper
6
7from geniusweb.issuevalue.Bid import Bid
8from geniusweb.issuevalue.DiscreteValue import DiscreteValue
9from geniusweb.issuevalue.DiscreteValueSet import DiscreteValueSet
10from geniusweb.issuevalue.Domain import Domain
11from geniusweb.issuevalue.NumberValue import NumberValue
12
13
14class DomainTest (unittest.TestCase) :
15 maxDiff=None
16
17 pyson=ObjectMapper()
18
19 # test value as in java GeniusWeb
20 jobsstring = '{"name":"jobs",' \
21 + '"issuesValues":{' \
22 + '"lease car":{"values":["yes","no"]},' \
23 + '"permanent contract":{"values":["yes","no"]},' \
24 + '"career development opportunities":{"values":["low","medium","high"]},' \
25 + '"fte":{"values":["0.6","0.8","1.0"]},' \
26 + '"salary":{"values":["2000","2500","3000","3500","4000"]},' \
27 + '"work from home":{"values":["0","1","2"]}}}'
28 jobsjson = json.loads(jobsstring)
29 yesno = DiscreteValueSet([DiscreteValue("yes"), DiscreteValue("no")])
30 leasecarvals = yesno
31 permcontractvals = yesno
32 carreervals=DiscreteValueSet([DiscreteValue("low"),DiscreteValue("medium"),DiscreteValue("high")])
33 ftevals=DiscreteValueSet([DiscreteValue("0.6"),DiscreteValue("0.8"),DiscreteValue("1.0")])
34 salaryvals=DiscreteValueSet([DiscreteValue("2000"),DiscreteValue("2500"),DiscreteValue("3000"),DiscreteValue("3500"),DiscreteValue("4000")])
35 workfromhomevals=DiscreteValueSet([DiscreteValue("0"),DiscreteValue("1"),DiscreteValue("2")])
36
37 jobs = Domain("jobs", {"lease car":leasecarvals, "permanent contract":permcontractvals, \
38 "career development opportunities":carreervals, "fte":ftevals, \
39 "salary": salaryvals, "work from home": workfromhomevals })
40 jobs1 = Domain("jobs", {"lease car":leasecarvals, "permanent contract":permcontractvals, \
41 "career development opportunities":carreervals, "fte":ftevals, \
42 "salary": salaryvals, "work from home": workfromhomevals })
43
44 jobs2 = Domain("jobs2", {"lease car":leasecarvals, "permanent contract":permcontractvals, \
45 "career development opportunities":carreervals, "fte":ftevals, \
46 "salary": salaryvals, "work from home": workfromhomevals })
47
48 jobs3 = Domain("jobs", {"Lcar":leasecarvals, "permanent contract":permcontractvals, \
49 "career development opportunities":carreervals, "fte":ftevals, \
50 "salary": salaryvals, "work from home": workfromhomevals })
51
52 def testSerialize(self):
53 print(str(self.pyson.toJson(self.jobs)))
54 self.assertEqual(self.jobsjson,self.pyson.toJson(self.jobs))
55
56 def testDeserializeJobs(self):
57 self.assertEqual(self.jobs, self.pyson.parse(self.jobsjson, Domain))
58
59 def testInit(self):
60 self.assertRaises(ValueError, lambda: Domain("name",None))
61 self.assertRaises(ValueError, lambda: Domain(None,{"issue"}))
62 self.assertRaises(ValueError, lambda: Domain("_invalid_name",{"issue"}))
63 self.assertRaises(ValueError, lambda: Domain("name",{}))
64
65 def testIsFitting(self):
66 self.assertEqual(None,self.jobs.isFitting(Bid({"lease car":DiscreteValue("no")})))
67 self.assertTrue("refers to non-domain issue" in self.jobs.isFitting(Bid({"no such issue":DiscreteValue("no")})))
68 self.assertNotEqual(None,self.jobs.isFitting(Bid({"lease car":NumberValue(Decimal(1))})))
69 self.assertTrue('illegal value' in self.jobs.isFitting(Bid({"lease car":DiscreteValue("maybe")})))
70
71 def testIsComplete(self):
72 self.assertEqual(None,self.jobs.isComplete(Bid({"lease car":DiscreteValue("no"),
73 "permanent contract":DiscreteValue("yes"),
74 "career development opportunities":DiscreteValue("high"),
75 "fte":DiscreteValue("1.0"),
76 "salary":DiscreteValue("3000"),
77 "work from home":DiscreteValue("1")
78 })))
79 self.assertTrue("do not match issues in domain" in self.jobs.isComplete(Bid({"lease car":DiscreteValue("no"),
80 "permanent contract":DiscreteValue("yes"),
81 "work from home":DiscreteValue("1")
82 })))
83
84 def testGetValues(self):
85 self.assertEqual(self.jobs.getValues("lease car"),self.yesno)
86
87 def testgetIssues(self):
88 self.assertEqual(self.jobs.getIssues(),{'permanent contract', 'fte', 'lease car', 'career development opportunities', 'salary', 'work from home'})
89
90 def testgetName(self):
91 self.assertEqual(self.jobs.getName(),"jobs")
92
93 def testRepr(self):
94 self.assertEqual("Domain[jobs,{'lease car': DiscreteValueSet[\"yes\", \"no\"], 'permanent contract': DiscreteValueSet[\"yes\", \"no\"], 'career development opportunities': DiscreteValueSet[\"low\", \"medium\", \"high\"], 'fte': DiscreteValueSet[\"0.6\", \"0.8\", \"1.0\"], 'salary': DiscreteValueSet[\"2000\", \"2500\", \"3000\", \"3500\", \"4000\"], 'work from home': DiscreteValueSet[\"0\", \"1\", \"2\"]}]"\
95 ,repr(self.jobs))
96
97 def testEqual(self):
98 self.assertEqual(self.jobs, self.jobs1)
99 self.assertNotEqual(self.jobs, self.jobs2)
100 self.assertNotEqual(self.jobs, self.jobs3)
101 self.assertNotEqual(self.jobs2, self.jobs3)
102 self.assertEqual(hash(self.jobs), hash(self.jobs1))
103 self.assertNotEqual(hash(self.jobs), hash(self.jobs2))
104
Note: See TracBrowser for help on using the repository browser.