def __init__(self): self.__data = { "aaaaaaaaaaaaaaaaaaaaaaa0": Event( "Evento 1, un titulo", "Breve (o no tanto) descripcion del evento 1. Relleno de contenido en este evento", dt.date(2020, 1, 1), dt.time(20, 50, 10), "Plaza Mayor 1", "Nadie real1.", "Tema libre1", "aaaaaaaaaaaaaaaaaaaaaaa0"), "aaaaaaaaaaaaaaaaaaaaaaa1": Event( "Evento 1, un titulo", "Breve (o no tanto) descripcion del evento 1. Relleno de contenido en este evento", dt.date(2020, 1, 1), dt.time(20, 50, 10), "Plaza Mayor 1", "Nadie real1.", "Tema libre1", "aaaaaaaaaaaaaaaaaaaaaaa1"), "aaaaaaaaaaaaaaaaaaaaaaa2": Event( "Evento 2, dos titulo", "Breve (o no tanto) descripcion del evento dos. Relleno de contenido en este evento", dt.date(2020, 2, 2), dt.time(20, 50, 20), "Plaza Mayor 2", "Nadie real2.", "Tema libre2", "aaaaaaaaaaaaaaaaaaaaaaa2"), "aaaaaaaaaaaaaaaaaaaaaaa3": Event( "Evento 3, tres titulo", "Breve (o no tanto) descripcion del evento 3. Relleno de contenido en este evento", dt.date(2020, 3, 3), dt.time(20, 50, 30), "Plaza Mayor 3", "Nadie real3.", "Tema libre3", "aaaaaaaaaaaaaaaaaaaaaaa3"), "aaaaaaaaaaaaaaaaaaaaaaa4": Event( "Evento 4, cuatro titulo", "Breve (o no tanto) descripcion del evento cuatro. Relleno de contenido en este evento", dt.date(2020, 4, 4), dt.time(20, 50, 40), "Plaza Mayor 4", "Nadie real4.", "Tema libre4", "aaaaaaaaaaaaaaaaaaaaaaa4"), }
def test_set_id(): e = Event(title, description, date, time, place, organizer, topics, id) assert e.get_id() == id, "Error in Id assignment" with pytest.raises(AttributeError): _ = Event(title, description, date, time, place, organizer, topics, 1) with pytest.raises(ValueError): _ = Event(title, description, date, time, place, organizer, topics, "")
def db_dict_to_event(self, data): # Changes key name from '_id' to 'id' and cast key value from ObjectId to str data["id"] = str(data.pop("_id")) date = data['date'].split('-') time = data['time'].split(':') return Event(data["title"], data["description"], dt.date(int(date[0]), int(date[1]), int(date[2])), dt.time(int(time[0]), int(time[1]), int(time[2])), data["place"], data["organizer"], data["topics"], data["id"])
def test_set_topics(): _ = Event(title, description, date, time, place, organizer, id=id) e = Event(title, description, date, time, place, organizer, topics, id) assert e.get_topics() == topics, "Error in topics assignment" with pytest.raises(AttributeError): _ = Event(title, description, date, time, place, organizer, []) with pytest.raises(ValueError): _ = Event(title, description, date, time, place, organizer, 'a' * 51)
def test_set_organizer(): _ = Event(title, description, date, time, place, topics=topics, id=id) e = Event(title, description, date, time, place, organizer, topics, id) assert e.get_organizer() == organizer, "Error in organizer assignment" with pytest.raises(AttributeError): _ = Event(title, description, date, time, place, None, topics, id) with pytest.raises(ValueError): _ = Event(title, description, date, time, place, 'a' * 51, topics, id)
def test_set_description(): e = Event(title, description, date, time, place, organizer, topics) assert e.get_description( ) == description, "Error in description assignment" with pytest.raises(AttributeError): _ = Event(title, None, date, time, place, organizer, topics) with pytest.raises(ValueError): _ = Event(title, "", date, time, place, organizer, topics) with pytest.raises(ValueError): _ = Event(title, 'a' * 401, date, time, place, organizer, topics) with pytest.raises(ValueError): _ = Event(title, 'a' * 19, date, time, place, organizer, topics)
def test_set_title(): e = Event(title, description, date, time, place, organizer, topics) assert e.get_title() == title, "Error in title assignment" with pytest.raises(AttributeError): _ = Event(None, description, date, time, place, organizer, topics) with pytest.raises(ValueError): _ = Event("", description, date, time, place, organizer, topics) with pytest.raises(ValueError): _ = Event('a' * 61, description, date, time, place, organizer, topics)
def test_place_type(): e = Event(title, description, date, time, organizer=organizer, topics=topics) e = Event(title, description, date, time, place, organizer, topics) assert e.get_place() == place, "Error in place assignment" with pytest.raises(AttributeError): _ = Event(title, description, date, time, None, organizer, topics) with pytest.raises(ValueError): _ = Event(title, description, date, time, 'a' * 101, organizer, topics)
def save(self, event): # check if that id already exist if event.get_id() in self.__data: # if it does self.__data[event.get_id()] = event return event else: # if it doesn't new_id = list(self.__data.keys())[len(self.__data) - 1] last_number = str(int(new_id.split("a")[-1]) + 1) new_id = new_id[:-len(last_number)] new_id = new_id + last_number new = Event(event.get_title(), event.get_description(), event.get_date(), event.get_time(), event.get_place(), event.get_organizer(), event.get_topics(), new_id) self.__data[new_id] = new return new
def on_put(self, req, resp, id): # Check that the event specified exist try: ev = self._dator.get_by_id(id) except ValueError: resp.status = falcon.HTTP_NOT_FOUND resp.body = json.dumps( {"error": "Resource with specified Id does not exist."}) return resp # Read body of the request with the new event data body = req.bounded_stream.read().decode("utf-8") # Check body is not empty if body is "" or body is None: resp.status = falcon.HTTP_BAD_REQUEST resp.body = json.dumps( {"error": "Data not provided in request body."}) return resp # Load event from body data if it is well formatted. try: ev = json.loads(body, object_hook=event_json_decoder) except: resp.status = falcon.HTTP_422 resp.body = json.dumps( {"error": "Error in data provided in request body."}) return resp # Create the edited event checking that the id is right new_ev = Event(ev.get_title(), ev.get_description(), ev.get_date(), ev.get_time(), ev.get_place(), ev.get_organizer(), ev.get_topics(), id) # Save edited event puted_ev = self._dator.save(new_ev) resp.body = json.dumps(puted_ev.to_json(), default=serialize)
def event_json_decoder(json): id = None topics = "" organizer = "" place = "" if '_id' in json.keys(): id = json['id'] if 'topics' in json.keys(): topics = json['topics'] if 'organizer' in json.keys(): organizer = json['organizer'] if 'place' in json.keys(): place = json['place'] # Can throw (KeyError, IndexError, TypeError) date = json['date'].split('-') time = json['time'].split(':') ev = Event(json['title'], json['description'], dt.date(int(date[0]), int(date[1]), int(date[2])), dt.time(int(time[0]), int(time[1]), int(time[2])), place, organizer, topics, id) return ev
def test_set_date(): e = Event(title, description, date, time, place, organizer, topics) assert e.get_date() == date, "Error in date assignment" with pytest.raises(AttributeError): _ = Event(title, description, None, time, place, organizer, topics)
import sys import os sys.path.append(os.path.abspath('./')) from events_microservice.models.event import Event from events_microservice.models.event_dator import MongoEventDator import datetime as dt import pytest import pymongo from bson.objectid import ObjectId import json ev0 = Event("test title", "test description " + 'a'*60, dt.date.today(), dt.time(10, 10, 10), "test place", organizer="test organizer", topics="test topics", id=None) ev1 = Event("test title1", "test description1 " + 'a'*60, dt.date.today(), dt.time(11, 11, 11), "test place1", organizer="test organizer1", topics="test topics1", id=None) ev2 = Event("test title2", "test description2 " + 'a'*60, dt.date.today(), dt.time(12, 12, 12), "test place2", organizer="test organizer2", topics="test topics2", id=None) ev_t = Event("test titlet", "test descriptiont " + 'a'*60, dt.date.today(), dt.time(12, 12, 12), "test placet", organizer="test organizert", topics="test topicst", id=None) client = pymongo.MongoClient() db = client.EventsMicroservice events_collection = db.Events ids = [] @pytest.fixture(autouse=True, scope='module')
# test_event_dator_mock.py import sys import os sys.path.append(os.path.abspath('./')) from events_microservice.models.event import Event from events_microservice.models.event_dator import EventDatorMock import datetime as dt import pytest ev = Event("test title", "test description " + 'a' * 60, dt.date.today(), dt.time(12, 12, 12), "test place", organizer="test organizer", topics="test topics", id=None) def test_get_events(): ed = EventDatorMock() events = ed.get_all() assert len(events) == 5, "Error in event_dator_ mock, get_all()" assert isinstance(events[0], Event), "Error in event_dator_ mock, get_all()" def test_get_by_id():