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 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246
|
# Part of Odoo. See LICENSE file for full copyright and licensing details.
import pytz
from datetime import datetime, time, timedelta
from unittest.mock import patch
from odoo import fields
from odoo.tests import common
from odoo.addons.lunch.tests.common import TestsCommon
class TestSupplier(TestsCommon):
def setUp(self):
super(TestSupplier, self).setUp()
self.monday_1am = datetime(2018, 10, 29, 1, 0, 0)
self.monday_10am = datetime(2018, 10, 29, 10, 0, 0)
self.monday_1pm = datetime(2018, 10, 29, 13, 0, 0)
self.monday_8pm = datetime(2018, 10, 29, 20, 0, 0)
self.saturday_3am = datetime(2018, 11, 3, 3, 0, 0)
self.saturday_10am = datetime(2018, 11, 3, 10, 0, 0)
self.saturday_1pm = datetime(2018, 11, 3, 13, 0, 0)
self.saturday_8pm = datetime(2018, 11, 3, 20, 0, 0)
@common.users('cle-lunch-manager')
def test_send_email_cron(self):
self.supplier_kothai.cron_id.ensure_one()
self.assertEqual(self.supplier_kothai.cron_id.nextcall.time(), time(15, 0))
self.assertEqual(self.supplier_kothai.cron_id.code, f"""\
# This cron is dynamically controlled by Lunch Supplier.
# Do NOT modify this cron, modify the related record instead.
env['lunch.supplier'].browse([{self.supplier_kothai.id}])._send_auto_email()""")
cron_id = self.supplier_kothai.cron_id.id
self.supplier_kothai.unlink()
self.assertFalse(self.env['ir.cron'].sudo().search([('id', '=', cron_id)]))
@common.users('cle-lunch-manager')
def test_compute_available_today(self):
tests = [(self.monday_1am, True), (self.monday_10am, True),
(self.monday_1pm, True), (self.monday_8pm, True),
(self.saturday_3am, False), (self.saturday_10am, False),
(self.saturday_1pm, False), (self.saturday_8pm, False)]
for value, result in tests:
with patch.object(fields.Datetime, 'now', return_value=value) as _:
assert self.supplier_pizza_inn.available_today == result,\
'supplier pizza inn should %s considered available on %s' % ('be' if result else 'not be', value)
self.supplier_pizza_inn.invalidate_recordset(['available_today'])
@common.users('cle-lunch-manager')
def test_search_available_today(self):
'''
This test checks that _search_available_today returns a valid domain
'''
self.env.user.tz = 'Europe/Brussels'
Supplier = self.env['lunch.supplier']
tests = [(self.monday_1am, 1.0, 'mon'), (self.monday_10am, 10.0, 'mon'),
(self.monday_1pm, 13.0, 'mon'), (self.monday_8pm, 20.0, 'mon'),
(self.saturday_3am, 3.0, 'sat'), (self.saturday_10am, 10.0, 'sat'),
(self.saturday_1pm, 13.0, 'sat'), (self.saturday_8pm, 20.0, 'sat')]
# It should return an empty domain if we compare to values other than datetime
assert Supplier._search_available_today('>', 7) == []
assert Supplier._search_available_today('>', True) == []
for value, rvalue, dayname in tests:
with patch.object(fields.Datetime, 'now', return_value=value) as _:
assert Supplier._search_available_today('=', True) == ['&', '|', ('recurrency_end_date', '=', False),
('recurrency_end_date', '>', value.replace(tzinfo=pytz.UTC).astimezone(pytz.timezone(self.env.user.tz))),
(dayname, '=', True)],\
'Wrong domain generated for values (%s, %s)' % (value, rvalue)
with patch.object(fields.Datetime, 'now', return_value=self.monday_10am) as _:
assert self.supplier_pizza_inn in Supplier.search([('available_today', '=', True)])
@common.users('cle-lunch-manager')
def test_auto_email_send(self):
with patch.object(fields.Datetime, 'now', return_value=self.monday_1pm) as _:
with patch.object(fields.Date, 'today', return_value=self.monday_1pm.date()) as _:
with patch.object(fields.Date, 'context_today', return_value=self.monday_1pm.date()) as _:
line_pizza = self.env['lunch.order'].create({
'product_id': self.product_pizza.id,
'date': self.monday_1pm.date(),
'supplier_id': self.supplier_pizza_inn.id,
})
line_pizza.action_order()
assert line_pizza.state == 'ordered'
self.supplier_pizza_inn._send_auto_email()
assert line_pizza.state == 'sent'
line_pizza_olive = self.env['lunch.order'].create({
'product_id': self.product_pizza.id,
'topping_ids_1': [(6, 0, [self.topping_olives.id])],
'date': self.monday_1pm.date(),
'supplier_id': self.supplier_pizza_inn.id,
})
line_tuna = self.env['lunch.order'].create({
'product_id': self.product_sandwich_tuna.id,
'date': self.monday_1pm.date(),
'supplier_id': self.supplier_coin_gourmand.id,
})
(line_pizza_olive | line_tuna).action_order()
assert line_pizza_olive.state == 'ordered'
assert line_tuna.state == 'ordered'
self.supplier_pizza_inn._send_auto_email()
assert line_pizza_olive.state == 'sent'
assert line_tuna.state == 'ordered'
line_pizza_2 = self.env['lunch.order'].create({
'product_id': self.product_pizza.id,
'quantity': 2,
'date': self.monday_1pm.date(),
'supplier_id': self.supplier_pizza_inn.id,
})
line_pizza_olive_2 = self.env['lunch.order'].create({
'product_id': self.product_pizza.id,
'topping_ids_1': [(6, 0, [self.topping_olives.id])],
'date': self.monday_1pm.date(),
'supplier_id': self.supplier_pizza_inn.id,
})
line_tuna_2 = self.env['lunch.order'].create({
'product_id': self.product_sandwich_tuna.id,
'quantity': 2,
'date': self.monday_1pm.date(),
'supplier_id': self.supplier_coin_gourmand.id,
})
######################################################
# id: # lines: # state: # quantity:
#######################################################
# 1 # line_pizza # sent # 1
# 2 # line_pizza_olive # sent # 1
# 3 # line_tuna # ordered # 1
# 4 # line_pizza_2 # new # 2
# 5 # line_pizza_olive_2 # new # 1
# 6 # line_tuna_2 # new # 2
(line_pizza_2 | line_pizza_olive_2 | line_tuna_2).action_order()
######################################################
# id: # lines: # state: # quantity:
#######################################################
# 1 # line_pizza # sent # 1
# 2 # line_pizza_olive # sent # 1
# 3 # line_tuna # ordered # 3 (1 + 2 from line_tuna_2 id=6)
# 4 # line_pizza_2 # ordered # 2
# 5 # line_pizza_olive_2 # ordered # 1
assert all(line.state == 'ordered' for line in [line_pizza_2, line_pizza_olive_2])
self.assertEqual(line_tuna_2.active, False)
self.assertEqual(line_tuna.quantity, 3)
self.supplier_pizza_inn._send_auto_email()
@common.users('cle-lunch-manager')
def test_cron_sync_create(self):
cron_ny = self.supplier_kothai.cron_id # I am at New-York
self.assertTrue(cron_ny.active)
self.assertEqual(cron_ny.name, "Lunch: send automatic email to Kothai")
self.assertEqual(
[line for line in cron_ny.code.splitlines() if not line.lstrip().startswith("#")],
["env['lunch.supplier'].browse([%i])._send_auto_email()" % self.supplier_kothai.id])
self.assertEqual(cron_ny.nextcall, datetime(2021, 1, 29, 15, 0)) # New-york is UTC-5
@common.users('cle-lunch-manager')
def test_cron_sync_active(self):
cron_ny = self.supplier_kothai.cron_id
self.supplier_kothai.active = False
self.assertFalse(cron_ny.active)
self.supplier_kothai.active = True
self.assertTrue(cron_ny.active)
self.supplier_kothai.send_by = 'phone'
self.assertFalse(cron_ny.active)
self.supplier_kothai.send_by = 'mail'
self.assertTrue(cron_ny.active)
@common.users('cle-lunch-manager')
def test_cron_sync_nextcall(self):
cron_ny = self.supplier_kothai.cron_id
old_nextcall = cron_ny.nextcall
self.supplier_kothai.automatic_email_time -= 5
self.assertEqual(cron_ny.nextcall, old_nextcall - timedelta(hours=5) + timedelta(days=1))
# Simulate cron execution
cron_ny.sudo().lastcall = old_nextcall - timedelta(hours=5)
cron_ny.sudo().nextcall += timedelta(days=1)
self.supplier_kothai.automatic_email_time += 7
self.assertEqual(cron_ny.nextcall, old_nextcall + timedelta(days=1, hours=2))
self.supplier_kothai.automatic_email_time -= 1
self.assertEqual(cron_ny.nextcall, old_nextcall + timedelta(days=1, hours=1))
def test_remove_toppings(self):
partner = self.env['res.partner'].create({
'name': 'Partner',
})
supplier = self.env['lunch.supplier'].create({
'partner_id': partner.id,
'send_by': 'phone',
'topping_ids_2': [
(0, 0, {
'name': 'salt',
'price': 7,
'company_id': self.env.company.id
}),
],
'topping_ids_3': [
(0, 0, {
'name': 'sugar',
'price': 10,
'company_id': self.env.company.id
}),
],
})
# simulating the delete as it's done on frontend
supplier.write({
'topping_ids_2': [(2, supplier.topping_ids_2.id)],
})
self.assertFalse(supplier.topping_ids_2)
# simulating the delete as it's done on frontend
supplier.write({
'topping_ids_3': [(2, supplier.topping_ids_3.id)],
})
self.assertFalse(supplier.topping_ids_3)
|