Browse Source

[ADD] emc_api: get subscription requests by date

pull/71/head
robin.keunen 5 years ago
parent
commit
79d7cf5d72
  1. 36
      easy_my_coop_api/services/subscription_request_service.py
  2. 70
      easy_my_coop_api/tests/test_subscription_requests.py

36
easy_my_coop_api/services/subscription_request_service.py

@ -2,10 +2,16 @@
# Robin Keunen <robin@coopiteasy.be> # Robin Keunen <robin@coopiteasy.be>
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html). # License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html).
import logging
from odoo.addons.component.core import Component from odoo.addons.component.core import Component
from odoo.addons.base_rest.http import wrapJsonException
from werkzeug.exceptions import NotFound
from odoo.fields import Date from odoo.fields import Date
from odoo import _
from . import schemas from . import schemas
_logger = logging.getLogger(__name__)
class SubscriptionRequestService(Component): class SubscriptionRequestService(Component):
_inherit = "base.rest.service" _inherit = "base.rest.service"
@ -47,14 +53,19 @@ class SubscriptionRequestService(Component):
NotFound(_("No subscription request for id %s") % _id) NotFound(_("No subscription request for id %s") % _id)
) )
return {"response": "Get called with message " + message}
def search(self, params=None):
def search(self, date_from=None, date_to=None):
# fixme remove sudo # fixme remove sudo
if params is None:
requests = self.env["subscription.request"].sudo().search([])
else:
requests = self.env["subscription.request"].sudo().search([])
_logger.info("search from %s to %s" % (date_from, date_to))
domain = []
if date_from:
date_from = Date.from_string(date_from)
domain.append(("date", ">=", date_from))
if date_to:
date_to = Date.from_string(date_to)
domain.append(("date", "<=", date_to))
requests = self.env["subscription.request"].sudo().search(domain)
response = { response = {
"count": len(requests), "count": len(requests),
@ -69,7 +80,16 @@ class SubscriptionRequestService(Component):
return schemas.S_SUBSCRIPTION_REQUEST return schemas.S_SUBSCRIPTION_REQUEST
def _validator_search(self): def _validator_search(self):
return {}
return {
"date_from": {
"type": "string",
"check_with": schemas.date_validator,
},
"date_to": {
"type": "string",
"check_with": schemas.date_validator,
},
}
def _validator_return_search(self): def _validator_return_search(self):
return schemas.S_SUBSCRIPTION_REQUEST_LIST return schemas.S_SUBSCRIPTION_REQUEST_LIST

70
easy_my_coop_api/tests/test_subscription_requests.py

@ -6,6 +6,7 @@
import json import json
from datetime import date, timedelta from datetime import date, timedelta
import odoo
from odoo.fields import Date from odoo.fields import Date
from odoo.addons.base_rest.controllers.main import _PseudoCollection from odoo.addons.base_rest.controllers.main import _PseudoCollection
from odoo.addons.component.core import WorkContext from odoo.addons.component.core import WorkContext
@ -14,20 +15,33 @@ from .common import BaseEMCRestCase
class TestSRController(BaseEMCRestCase): class TestSRController(BaseEMCRestCase):
def test_service(self):
# kept as example
# useful if you need to change data in database and check db type
def setUp(self):
super().setUp()
collection = _PseudoCollection("emc.services", self.env) collection = _PseudoCollection("emc.services", self.env)
emc_services_env = WorkContext( emc_services_env = WorkContext(
model_name="rest.service.registration", collection=collection model_name="rest.service.registration", collection=collection
) )
service = emc_services_env.component(usage="subscription_request")
self.service = emc_services_env.component(usage="subscription_request")
def test_service(self):
# kept as example
# useful if you need to change data in database and check db type
result = service.get(self.demo_request_1.id)
result = self.service.get(self.demo_request_1.id)
self.assertEquals(self.demo_request_1_dict, result) self.assertEquals(self.demo_request_1_dict, result)
def test_get_route(self):
all_sr = self.service.search()
self.assertTrue(all_sr)
sr_date = self.demo_request_1.date
date_from = Date.to_string(sr_date - timedelta(days=1))
date_to = Date.to_string(sr_date + timedelta(days=1))
date_sr = self.service.search(date_from=date_from, date_to=date_to)
self.assertTrue(date_sr)
def test_route_get(self):
id_ = self.demo_request_1.id id_ = self.demo_request_1.id
route = "/api/subscription_request/%s" % id_ route = "/api/subscription_request/%s" % id_
content = self.http_get_content(route) content = self.http_get_content(route)
@ -46,19 +60,39 @@ class TestSRController(BaseEMCRestCase):
def test_route_search_all(self): def test_route_search_all(self):
route = "/api/subscription_request" route = "/api/subscription_request"
response = self.http_get(route)
self.assertEquals(response.status_code, 200)
content = self.http_get_content(route)
self.assertIn(self.demo_request_1_dict, content["rows"])
content = json.loads(response.content)
def test_route_search_by_date(self):
sr_date = self.demo_request_1.date
date_from = Date.to_string(sr_date - timedelta(days=1))
date_to = Date.to_string(sr_date + timedelta(days=1))
self.assertIn(
self.demo_request_1_dict, content["rows"]
route = "/api/subscription_request?date_from=%s" % date_from
content = self.http_get_content(route)
self.assertIn(self.demo_request_1_dict, content["rows"])
route = "/api/subscription_request?date_to=%s" % date_to
content = self.http_get_content(route)
self.assertIn(self.demo_request_1_dict, content["rows"])
route = "/api/subscription_request?date_from=%s&date_to=%s" % (
date_from,
date_to,
) )
content = self.http_get_content(route)
self.assertIn(self.demo_request_1_dict, content["rows"])
# def test_search_route_from_date(self):
# from_ = Date.to_string(date.today() - timedelta(days=12))
# response = self.http_get("/api/subscription_request?from=%s" % from_)
# self.assertEquals(response.status_code, 200)
#
# content = json.loads(response.content)
# self.assertTrue("message" in content)
route = "/api/subscription_request?date_from=%s" % "2300-01-01"
content = self.http_get_content(route)
self.assertEquals(content["count"], 0)
route = "/api/subscription_request?date_to=%s" % "1900-01-01"
content = self.http_get_content(route)
self.assertEquals(content["count"], 0)
@odoo.tools.mute_logger("odoo.addons.base_rest.http")
def test_route_search_acd_date_returns_bad_request(self):
route = "/api/subscription_request?date_from=%s" % "20200101"
response = self.http_get(route)
self.assertEquals(response.status_code, 400)
Loading…
Cancel
Save