Skip to content

Commit

Permalink
sf_base: tests unify service get
Browse files Browse the repository at this point in the history
  • Loading branch information
simahawk authored and rousseldenis committed Dec 6, 2023
1 parent f5befad commit 87b4ccf
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 33 deletions.
9 changes: 9 additions & 0 deletions shopfloor_base/tests/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,15 @@ def work_on_services(self, env=None, **params):
**params
)

def get_service(self, usage, **kw):
with self.work_on_services(**kw) as work:
service = work.component(usage=usage)
# Thanks to shopfloor.app we don't need controllers
# but not having a controller means that non decorated methods
# stay undecorated as they are not fixed at startup by base_rest.
self.env["shopfloor.app"]._prepare_non_decorated_endpoints(service)
return service

@contextmanager
def work_on_actions(self, **params):
params = params or {}
Expand Down
12 changes: 2 additions & 10 deletions shopfloor_base/tests/common_misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,6 @@ def assert_schema(self, schema, data):


class MenuTestMixin(object):
def _get_service(self, profile=None):
with self.work_on_services(profile=profile or self.profile) as work:
return work.component(usage="menu")

def _assert_menu_response(self, response, menus, **kw):
self.assert_response(
response,
Expand Down Expand Up @@ -51,12 +47,8 @@ def _test_openapi(self, **kw):


class ScanAnythingTestMixin(object):
def _get_service(self):
with self.work_on_services() as work:
return work.component(usage="scan_anything")

def _test_response_ok(self, rec_type, data, identifier, record_types=None):
service = self._get_service()
service = self.get_service("scan_anything")
params = {"identifier": identifier, "record_types": record_types}
response = service.dispatch("scan", params=params)
self.assert_response(
Expand All @@ -65,7 +57,7 @@ def _test_response_ok(self, rec_type, data, identifier, record_types=None):
)

def _test_response_ko(self, identifier, record_types=None):
service = self._get_service()
service = self.get_service("scan_anything")
tried = record_types or [x.record_type for x in service._scan_handlers()]
params = {"identifier": identifier, "record_types": record_types}
response = service.dispatch("scan", params=params)
Expand Down
4 changes: 2 additions & 2 deletions shopfloor_base/tests/test_menu_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def setUpClassVars(cls):

def test_menu_search(self):
"""Request /menu/search"""
service = self._get_service()
service = self.get_service("menu", profile=self.profile)
# Simulate the client searching menus
response = service.dispatch("search")
menus = self.env["shopfloor.menu"].search([])
Expand All @@ -28,7 +28,7 @@ def test_menu_search_restricted(self):
other_profile = self.env.ref("shopfloor_base.profile_demo_1")
menus_without_profile.profile_id = other_profile

service = self._get_service()
service = self.get_service("menu", profile=self.profile)
response = service.dispatch("search")

my_menus = menus - menus_without_profile
Expand Down
8 changes: 2 additions & 6 deletions shopfloor_base/tests/test_profile_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,11 @@


class ProfileCase(CommonCase):
def setUp(self):
super().setUp()
with self.work_on_services() as work:
self.service = work.component(usage="profile")

def test_profile_search(self):
"""Request /profile/search"""
service = self.get_service("profile")
# Simulate the client searching profiles
response = self.service.dispatch("search")
response = service.dispatch("search")
self.assert_response(
response,
data={
Expand Down
2 changes: 1 addition & 1 deletion shopfloor_base/tests/test_scan_anything_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ def schema(self):
return (PartnerFinder, CurrencyFinder)

def test_scan(self):
service = self._get_service()
service = self.get_service("scan_anything")
test_handlers = self._get_test_handlers()

for finder_class in test_handlers:
Expand Down
8 changes: 2 additions & 6 deletions shopfloor_base/tests/test_user_config_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,11 @@ def setUpClassVars(cls):
cls.profile = cls.env.ref("shopfloor_base.profile_demo_1")
cls.profile2 = cls.env.ref("shopfloor_base.profile_demo_2")

def setUp(self):
super().setUp()
with self.work_on_services(profile=self.profile) as work:
self.service = work.component(usage="app")

def test_user_config(self):
"""Request /app/user_config"""
# Simulate the client asking the configuration
response = self.service.dispatch("user_config")
service = self.get_service("app", profile=self.profile)
response = service.dispatch("user_config")
profiles = self.env["shopfloor.profile"].search([])
self.assert_response(
response,
Expand Down
14 changes: 6 additions & 8 deletions shopfloor_base/tests/test_user_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,11 @@ def setUpClassVars(cls, *args, **kwargs):
[("id", "not in", cls.menu_items.ids)]
).sudo().write({"profile_id": profile1.id})

def setUp(self):
super().setUp()
with self.work_on_services(profile=self.profile) as work:
self.service = work.component(usage="user")

def test_menu_no_profile(self):
"""Request /user/menu"""
service = self.get_service("user", profile=self.profile)
# Simulate the client asking the menu
response = self.service.dispatch("menu")
response = service.dispatch("menu")
menus = self.menu_items
self.assert_response(
response,
Expand All @@ -43,15 +39,17 @@ def test_menu_by_profile(self):
menu.profile_id = self.profile
(menus - menu).profile_id = self.profile2

response = self.service.dispatch("menu")
service = self.get_service("user", profile=self.profile)
response = service.dispatch("menu")
self.assert_response(
response,
data={"menus": [self._data_for_menu_item(menu)]},
)

def test_user_info(self):
"""Request /user/user_info"""
response = self.service.dispatch("user_info")
service = self.get_service("user", profile=self.profile)
response = service.dispatch("user_info")
self.assert_response(
response,
data={"user_info": {"id": self.env.user.id, "name": self.env.user.name}},
Expand Down

0 comments on commit 87b4ccf

Please sign in to comment.