Skip to content

Commit

Permalink
Merge ebbe84b into 4bdc6e0
Browse files Browse the repository at this point in the history
  • Loading branch information
BenjiRS94 committed Nov 2, 2022
2 parents 4bdc6e0 + ebbe84b commit 521903d
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 5 deletions.
1 change: 1 addition & 0 deletions config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ maxNodesToPersevere: 10000
costLimit: 200
descartaNoPrometedores: true
deixaEsmorzar: true
maxNingusPerTurn: 2
noVolenEsmorzar:
- david
- ningu
Expand Down
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@
'lxml',
'erppeek',
'ERPPeek-WST',
'mock',
],
classifiers = [
'Programming Language :: Python',
Expand Down
7 changes: 7 additions & 0 deletions tomatic/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,11 @@ def thisweek():
app.include_router(Planner, prefix='/api/planner')


def occurrencesInTurn(graella, day, houri, name):
nominated = graella.timetable[day][int(houri)]
return nominated.count(name)


class ApiError(Exception): pass

def yamlfy(status=200, data=[], **kwd):
Expand Down Expand Up @@ -208,6 +213,8 @@ async def editSlot(week, day, houri: int, turni: int, name, request: Request):
graella = schedules.load(week)
# TODO: Ensure day, houri, turni and name are in graella
oldName = graella.timetable[day][int(houri)][int(turni)]
if name == 'ningu' and occurrencesInTurn(graella, day, houri, name) == CONFIG.maxNingusPerTurn:
raise ApiError("Hi ha masses Ningu en aquest torn")
graella.timetable[day][int(houri)][int(turni)] = name
graella.overload = graella.get('overload', ns())
graella.overload[oldName] = graella.overload.get(oldName, 0) -1
Expand Down
87 changes: 83 additions & 4 deletions tomatic/api_test.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,100 @@
# -*- coding: utf-8 -*-

import b2btest
import copy
import yaml
import unittest
from datetime import datetime
from mock import patch

from yaml.loader import SafeLoader
from yamlns import namespace as ns
from . import api
from fastapi.testclient import TestClient


def setNow(year,month,day,hour,minute):
api.now=lambda:datetime(year,month,day,hour,minute)

class Api_Test(unittest.TestCase):

def setUp(self):
api.app.config['TESTING'] = True
self.app = api.app.test_client()
self.maxDiff = None
self.b2bdatapath = "testdata"
self.client = TestClient(api.app)

def getTimeTableObjectStructure(self, time_table):
return ns(
hours=['09:00', '10:15'],
turns=['L1', 'L2'],
timetable=ns(time_table)
)

@patch("tomatic.api.schedulestorage.Storage.load")
@patch("tomatic.api.schedulestorage.Storage.save")
def test_editSlot_baseCase(self, mocked_saver, mocked_loader):
# Given a time table
old_time_table = self.getTimeTableObjectStructure({
'dl': [
['vic', 'cire', 'carme'],
['vic', 'cire', 'carme'],
]
})
mocked_loader.return_value = old_time_table
day = 'dl'
hour_index = 0
turn_index = 0
name = 'cire'

# When we change one person in time table
response = self.client.patch(
'/api/graella/2022-10-31/{}/{}/{}/{}'.format(
day, hour_index, turn_index, name
),
json="vic"
)

# The request was success
self.assertEquals(response.status_code, 200)
# and API saves the time table with change applied
new_time_table = copy.deepcopy(old_time_table)
new_time_table.timetable[day][hour_index][turn_index] = name
mocked_saver.assert_called_with(new_time_table)

@patch("tomatic.api.schedulestorage.Storage.load")
@patch("tomatic.api.schedulestorage.Storage.save")
def test_editSlot_ningusLimit(self, mocked_saver, mocked_loader):
# Given a time table
old_time_table = self.getTimeTableObjectStructure({
'dl': [
['vic', 'ningu', 'ningu'],
['vic', 'ningu', 'ningu'],
]
})
mocked_loader.return_value = old_time_table
day = 'dl'
hour_index = 0
turn_index = 0
name = 'ningu'

# When we change one person in time table
response = self.client.patch(
'/api/graella/2022-10-31/{}/{}/{}/{}'.format(
day, hour_index, turn_index, name
),
json="vic"
)

# API returns 400 error
self.assertEquals(response.status_code, 400)
# with a message of the error that occurred
yaml_response = response.text
content_response = yaml.load(yaml_response, Loader=SafeLoader)
self.assertEquals(
content_response,
{'error': 'Hi ha masses Ningu en aquest torn'}
)
# and time table wasn't saved
mocked_saver.assert_not_called()


if __name__ == "__main__":

Expand Down
2 changes: 1 addition & 1 deletion tomatic/ui/components/tomatic.js
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ Tomatic.editCell = function(day,houri,turni,name,myname) {
Tomatic.requestGrid(Tomatic.grid().week);
}, function(error) {
Tomatic.error("Problemes editant la graella: "+
(error.error || "Inexperat"));
(error || "Inexperat"));
});
};
Tomatic.setPersonData = function (name, data) {
Expand Down

0 comments on commit 521903d

Please sign in to comment.