Skip to content

Commit

Permalink
Move puppeteers testutils to testing dir
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 552824935
Change-Id: Ica4f8fc327fc1e4c57d8895de584876653caf7e8
  • Loading branch information
jagapiou authored and Copybara-Service committed Aug 1, 2023
1 parent 37516ba commit 3dcb691
Show file tree
Hide file tree
Showing 9 changed files with 31 additions and 25 deletions.
File renamed without changes.
14 changes: 8 additions & 6 deletions meltingpot/utils/puppeteers/alternator_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
from absl.testing import absltest
from absl.testing import parameterized

from meltingpot.testing import puppeteers
from meltingpot.utils.puppeteers import alternator
from meltingpot.utils.puppeteers import testutils

_GOAL_A = mock.sentinel.goal_a
_GOAL_B = mock.sentinel.goal_b
Expand All @@ -41,17 +41,19 @@ def test_goal_sequence(self, steps_per_goal):
[_GOAL_C] * steps_per_goal +
[_GOAL_A] * steps_per_goal +
[_GOAL_B] * steps_per_goal) * 2
actual, _ = testutils.goals_from_observations(puppeteer, observations)
actual, _ = puppeteers.goals_from_observations(puppeteer, observations)
self.assertSequenceEqual(actual, expected)

def test_resets_on_restart(self):
puppeteer = alternator.Alternator(
goals=[_GOAL_A, _GOAL_B, _GOAL_C], steps_per_goal=1)
observations = [{}] * 4
episode_1, state = testutils.goals_from_observations(
puppeteer, observations)
episode_2, _ = testutils.goals_from_observations(
puppeteer, observations, state=state)
episode_1, state = puppeteers.goals_from_observations(
puppeteer, observations
)
episode_2, _ = puppeteers.goals_from_observations(
puppeteer, observations, state=state
)
expected = [_GOAL_A, _GOAL_B, _GOAL_C, _GOAL_A]
self.assertSequenceEqual([episode_1, episode_2], [expected, expected])

Expand Down
7 changes: 4 additions & 3 deletions meltingpot/utils/puppeteers/clean_up_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
from absl.testing import absltest
from absl.testing import parameterized

from meltingpot.testing import puppeteers
from meltingpot.utils.puppeteers import clean_up
from meltingpot.utils.puppeteers import testutils

_NUM_COOPERATORS_KEY = 'num_cooperators'
_COOPERATE = mock.sentinel.cooperate
Expand All @@ -28,8 +28,9 @@

def _goals(puppeteer, num_cooperators, state=None):
observations = [{_NUM_COOPERATORS_KEY: n} for n in num_cooperators]
goals, state = testutils.goals_from_observations(puppeteer, observations,
state)
goals, state = puppeteers.goals_from_observations(
puppeteer, observations, state
)
return goals, state


Expand Down
7 changes: 4 additions & 3 deletions meltingpot/utils/puppeteers/coins_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
from absl.testing import absltest
from absl.testing import parameterized

from meltingpot.testing import puppeteers
from meltingpot.utils.puppeteers import coins
from meltingpot.utils.puppeteers import testutils

_COOPERATE = mock.sentinel.cooperate
_DEFECT = mock.sentinel.defect
Expand All @@ -29,8 +29,9 @@

def _goals(puppeteer, num_defections, state=None):
observations = [{_NUM_DEFECTIONS_KEY: n} for n in num_defections]
goals, state = testutils.goals_from_observations(puppeteer, observations,
state)
goals, state = puppeteers.goals_from_observations(
puppeteer, observations, state
)
return goals, state


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@
from absl.testing import parameterized
import numpy as np

from meltingpot.testing import puppeteers
from meltingpot.utils.puppeteers import coordination_in_the_matrix
from meltingpot.utils.puppeteers import in_the_matrix
from meltingpot.utils.puppeteers import testutils

_RESOURCE_A = in_the_matrix.Resource(
index=0,
Expand Down Expand Up @@ -53,7 +53,7 @@ def _goals_from_observations(puppeteer, inventories, interactions, state=None):
for inventory, interaction in itertools.zip_longest(inventories,
interactions):
observations.append(_observation(inventory, interaction))
return testutils.goals_from_observations(puppeteer, observations, state)
return puppeteers.goals_from_observations(puppeteer, observations, state)


class CounterPrevious(parameterized.TestCase):
Expand Down
4 changes: 2 additions & 2 deletions meltingpot/utils/puppeteers/fixed_goal_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
from absl.testing import absltest
from absl.testing import parameterized

from meltingpot.testing import puppeteers
from meltingpot.utils.puppeteers import fixed_goal
from meltingpot.utils.puppeteers import testutils


class FixedGoalTest(parameterized.TestCase):
Expand All @@ -28,7 +28,7 @@ def test_goal_sequence(self):
puppeteer = fixed_goal.FixedGoal(mock.sentinel.goal)
observations = [{}] * 3
expected = [mock.sentinel.goal] * 3
actual, _ = testutils.goals_from_observations(puppeteer, observations)
actual, _ = puppeteers.goals_from_observations(puppeteer, observations)
self.assertSequenceEqual(actual, expected)


Expand Down
12 changes: 7 additions & 5 deletions meltingpot/utils/puppeteers/gift_refinements_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
from absl.testing import absltest
from absl.testing import parameterized

from meltingpot.testing import puppeteers
from meltingpot.utils.puppeteers import gift_refinements
from meltingpot.utils.puppeteers import testutils

_COLLECT = mock.sentinel.collect
_CONSUME = mock.sentinel.consume
Expand All @@ -45,8 +45,9 @@ def test(self, inventory, expected):
consume_goal=_CONSUME,
gift_goal=_GIFT,
)
(actual,), _ = testutils.goals_from_observations(
puppeteer, [{'INVENTORY': inventory}])
(actual,), _ = puppeteers.goals_from_observations(
puppeteer, [{'INVENTORY': inventory}]
)
self.assertEqual(actual, expected)


Expand All @@ -68,8 +69,9 @@ def test(self, inventory, expected):
consume_goal=_CONSUME,
gift_goal=_GIFT,
)
(actual,), _ = testutils.goals_from_observations(
puppeteer, [{'INVENTORY': inventory}])
(actual,), _ = puppeteers.goals_from_observations(
puppeteer, [{'INVENTORY': inventory}]
)
self.assertEqual(actual, expected)


Expand Down
4 changes: 2 additions & 2 deletions meltingpot/utils/puppeteers/in_the_matrix_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@
import immutabledict
import numpy as np

from meltingpot.testing import puppeteers
from meltingpot.utils.puppeteers import in_the_matrix
from meltingpot.utils.puppeteers import testutils

_RESOURCE_0 = in_the_matrix.Resource(
index=0,
Expand Down Expand Up @@ -136,7 +136,7 @@ def _goals_from_observations(puppeteer,
for inventory, interaction in itertools.zip_longest(inventories,
interactions):
observations.append(_observation(inventory, interaction))
return testutils.goals_from_observations(puppeteer, observations, state)
return puppeteers.goals_from_observations(puppeteer, observations, state)


class SpecialistTest(parameterized.TestCase):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@
from absl.testing import parameterized
import numpy as np

from meltingpot.testing import puppeteers
from meltingpot.utils.puppeteers import in_the_matrix
from meltingpot.utils.puppeteers import running_with_scissors_in_the_matrix
from meltingpot.utils.puppeteers import testutils

_ROCK = in_the_matrix.Resource(
index=2,
Expand Down Expand Up @@ -53,7 +53,7 @@ def _goals_from_observations(puppeteer, inventories, interactions, state=None):
for inventory, interaction in itertools.zip_longest(inventories,
interactions):
observations.append(_observation(inventory, interaction))
return testutils.goals_from_observations(puppeteer, observations, state)
return puppeteers.goals_from_observations(puppeteer, observations, state)


class CounterPrevious(parameterized.TestCase):
Expand Down

0 comments on commit 3dcb691

Please sign in to comment.