forked from Giveth/coodcad
-
Notifications
You must be signed in to change notification settings - Fork 0
/
policies_test.py
169 lines (142 loc) · 6.61 KB
/
policies_test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
import copy
import unittest
from collections import namedtuple
from unittest.mock import patch
from entities import Proposal, ProposalStatus
from hatch import Commons, TokenBatch, VestingOptions
from network_utils import bootstrap_network, add_proposal, get_edges_by_type
from policies import (GenerateNewFunding, GenerateNewParticipant,
GenerateNewProposal, ActiveProposals)
class TestGenerateNewParticipant(unittest.TestCase):
def setUp(self):
self.commons = Commons(10000, 1000)
self.sentiment = 0.5
self.network = bootstrap_network([TokenBatch(1000, VestingOptions(10, 30))
for _ in range(4)], 1, 3000, 4e6)
def test_p_randomly(self):
"""
Simply test that the code runs.
"""
state = {
"commons": self.commons,
"sentiment": self.sentiment
}
with patch("policies.probability") as p:
p.return_value = True
ans = GenerateNewParticipant.p_randomly(None, 0, 0, state)
self.assertEqual(ans["new_participant"], True)
self.assertIsNotNone(ans["new_participant_investment"])
self.assertIsNotNone(ans["new_participant_tokens"])
def test_su_add_to_network(self):
"""
Test that the state update function did add the Participant to the
network, and that the network maintained its integrity (i.e. all edges
were properly setup)
"""
with patch("network_utils.influence") as p:
p.return_value = 0.8
n_old_len = len(self.network.nodes)
_input = {
"new_participant": True,
"new_participant_investment": 16.872149388283283,
"new_participant_tokens": 1.0545093367677052
}
_, network = GenerateNewParticipant.su_add_to_network(
None, 0, 0, {"network": self.network.copy()}, _input)
network_len = len(network.nodes)
self.assertEqual(n_old_len, 5)
self.assertEqual(network_len, 6)
self.assertEqual(network.nodes(data="item")[
5].holdings_nonvesting.value, 1.0545093367677052)
# There are 4 Participants in the network, all of them should have
# influence edges to the newly added Participant.
self.assertEqual(len(network.in_edges(5)), 4)
# Check that all of these edges are support type edges.
for u, v in network.in_edges(5):
self.assertEqual(network.edges[u, v]["type"], "influence")
class TestGenerateNewProposal(unittest.TestCase):
def setUp(self):
self.network = bootstrap_network([TokenBatch(1000, VestingOptions(10, 30))
for _ in range(4)], 1, 3000, 4e6)
def test_p_randomly(self):
"""
Simply test that the code runs.
"""
with patch("entities.probability") as p:
p.return_value = True
ans = GenerateNewProposal.p_randomly(
None, 0, 0, {"network": self.network, "funding_pool": 100000})
self.assertTrue(ans["new_proposal"])
self.assertIn("proposed_by_participant", ans)
p.return_value = False
ans = GenerateNewProposal.p_randomly(
None, 0, 0, {"network": self.network, "funding_pool": 100000})
self.assertFalse(ans["new_proposal"])
def test_su_add_to_network(self):
"""
Test that the state update function did add a new Proposal to the
network, and that the network maintained its integrity (i.e. all edges
were properly setup)
"""
result_from_policy = {
"proposed_by_participant": 0,
"new_proposal": True,
}
state = {"network": self.network.copy(),
"funding_pool": 100000, "token_supply": 10000}
_, network = GenerateNewProposal.su_add_to_network(
None, 0, 0, state, result_from_policy)
self.assertEqual(len(network.nodes), 6)
self.assertIsInstance(network.nodes[5]["item"], Proposal)
# There are 4 Participants in the network, all of them should have edges
# to the newly added Proposal.
self.assertEqual(len(network.in_edges(5)), 4)
# Check that all of these edges are support type edges.
for u, v in network.in_edges(5):
self.assertEqual(network.edges[u, v]["type"], "support")
# Check that the Participant that created the Proposal has an affinity
# of 1 towards it
self.assertEqual(network.edges[0, 5]["affinity"], 1)
class TestGenerateNewFunding(unittest.TestCase):
def test_p_exit_tribute_of_average_speculator_position_size(self):
"""
Simply test that the code works.
"""
m_commons = namedtuple("MockCommons", "exit_tribute")
m_commons.exit_tribute = 0.10
state = {
"commons": m_commons}
ans = GenerateNewFunding.p_exit_tribute_of_average_speculator_position_size(
None, 0, 0, state)
self.assertTrue(ans["funding"] > 0)
def test_su_add_funding(self):
commons = Commons(10000, 100)
_, commons_new = GenerateNewFunding.su_add_funding(
None, 0, 0, {"commons": copy.copy(commons)}, {"funding": 111})
self.assertEqual(commons_new._funding_pool, 2111)
class TestActiveProposals(unittest.TestCase):
def setUp(self):
self.network = bootstrap_network([TokenBatch(1000, VestingOptions(10, 30))
for _ in range(4)], 1, 3000, 4e6)
self.network, _ = add_proposal(self.network, Proposal(100, 1))
self.network.nodes[4]["item"].status = ProposalStatus.ACTIVE
self.network.nodes[5]["item"].status = ProposalStatus.ACTIVE
def test_p_influenced_by_grant_size(self):
"""
Simply test that the code works.
"""
with patch("policies.probability") as p:
p.return_value = True
ans = ActiveProposals.p_influenced_by_grant_size(
None, 0, 0, {"network": copy.copy(self.network)})
self.assertEqual(ans["failed"], [4, 5])
def test_su_set_proposal_status(self):
"""
Simply test that the code works.
"""
_, network1 = ActiveProposals.su_set_proposal_status(
None, 0, 0, {"network": copy.copy(self.network)}, {"failed": [4, 5]})
self.assertEqual(network1.nodes[4]
["item"].status, ProposalStatus.FAILED)
self.assertEqual(network1.nodes[5]
["item"].status, ProposalStatus.FAILED)