def test_get_all(self): io = IO('test.json') io.set([]) repo = EventRepository('test.json') content = repo.get_all() self.assertEqual(content, []) event1 = Event('14/01/2015', '14:00', 'Colectiv') event2 = Event('14/01/2015', '14:00', 'Colectiv') repo.insert(event1) repo.insert(event2) content = repo.get_all() self.assertIs(type(content), list) self.assertEqual(len(content), 2) stored_event1 = content[0] stored_event2 = content[1] self.assertEqual(stored_event1.get_date(), '14/01/2015') self.assertEqual(stored_event1.get_time(), '14:00') self.assertEqual(stored_event1.get_description(), 'Colectiv') self.assertEqual(stored_event2.get_date(), '14/01/2015') self.assertEqual(stored_event2.get_time(), '14:00') self.assertEqual(stored_event2.get_description(), 'Colectiv')
def validate(person_id, event_id, date): ''' Validates PersonEvent params :param person_id: :param event_id: :param date: :return: None :raise AttributeError: :raise ValueError: ''' if person_id is None or event_id is None: raise AttributeError('Please provide all fields') event_repository = EventRepository() person_repository = PersonRepository() try: event = event_repository.get(event_id) person = person_repository.get(person_id) except ValueError: raise ValueError('Invalid IDs! Please provide valid IDs') if date is not None: try: datetime.strptime(date, '%d/%m/%Y') except ValueError: raise ValueError("Date doesn't have the right format")
def __init__(self): ''' EventController constructor :return: object ''' self.repository = EventRepository() self.person_event_repository = PersonEventRepository() self.person_repository = PersonRepository()
def test_get(self): Event.set_class_id(0) io = IO('test.json') io.set([]) repo = EventRepository('test.json') event1 = Event('14/01/2015', '14:00', 'Colectiv') event2 = Event('14/01/2015', '14:00', 'Colectiv') repo.insert(event1) repo.insert(event2) content = repo.get(0) self.assertIs(type(content), Event) self.assertEqual(content.get_date(), '14/01/2015') self.assertEqual(content.get_time(), '14:00') self.assertEqual(content.get_description(), 'Colectiv') with self.assertRaisesRegex(ValueError, 'Event not found!'): content = repo.get(10)
def test_get_max_id(self): Event.set_class_id(0) io = IO('test.json') io.set([]) repo = EventRepository('test.json') self.assertEqual(repo.get_max_id(), 0) content = repo.get_all() self.assertEqual(content, []) event1 = Event('14/01/2015', '14:00', 'Colectiv') event2 = Event('14/01/2015', '14:00', 'Colectiv') repo.insert(event1) repo.insert(event2) content = repo.get_all() self.assertEqual(repo.get_max_id(), 2)
def test_delete(self): Event.set_class_id(0) io = IO('test.json') io.set([]) repo = EventRepository('test.json') event1 = Event('14/01/2015', '14:00', 'Colectiv') event2 = Event('14/01/2015', '14:00', 'Colectiv') repo.insert(event1) repo.insert(event2) event = repo.get(0) self.assertIs(type(event), Event) repo.delete(event) with self.assertRaisesRegex(ValueError, 'Event not found!'): updated_event = repo.get(0)
def test_update(self): Event.set_class_id(0) io = IO('test.json') io.set([]) repo = EventRepository('test.json') event1 = Event('14/01/2015', '14:00', 'Colectiv') event2 = Event('14/01/2015', '14:00', 'Colectiv') repo.insert(event1) repo.insert(event2) event = repo.get(0) self.assertIs(type(event), Event) event.update('15/01/2015', '15:00', 'Untold festival') repo.update(event) updated_event = repo.get(0) self.assertEqual(updated_event.get_date(), '15/01/2015') self.assertEqual(updated_event.get_time(), '15:00') self.assertEqual(updated_event.get_description(), 'Untold festival')
class EventController(): def __init__(self): ''' EventController constructor :return: object ''' self.repository = EventRepository() self.person_event_repository = PersonEventRepository() self.person_repository = PersonRepository() def index(self): ''' Return all events in one string ready to display :return: string ''' events = self.repository.get_all() output = "" for event in events: output += str(event.get_id()) + "\t" + event.get_date() + "\t" + event.get_time() + "\t" + event.get_description() + "\n" return output def show(self, id): ''' Return a found event ready to display :param id: :return: string ''' try: id = int(id) except ValueError: return "Please enter an integer for ID" try: event = self.repository.get(id) return str(event.get_id()) + "\t" + event.get_date() + "\t" + event.get_time() + "\t" + event.get_description() + "\n" except (ValueError, AttributeError) as e: return e def insert(self, date = None, time = None, description = None): ''' Insert an event :param date: :param time: :param description: :return: string ''' try: event = Event(date, time, description) except (AttributeError, ValueError) as e: return e self.repository.insert(event) return "Event inserted successfully" def update(self, id, date = None, time = None, description = None): ''' Update an event :param id: :param date: :param time: :param description: :return: string ''' try: id = int(id) except ValueError: return "Please insert a valid ID" try: event = self.repository.get(id) except ValueError as e: return e if date == '': date = None if time == '': time = None if description == '': description = None try: event.update(date, time, description) self.repository.update(event) except (ValueError, AttributeError) as e: return e return "Event updated successfully!" def delete(self, id): ''' Delete an event :param id: :return: string ''' try: id = int(id) except ValueError: return "Please enter an integer for ID" try: event = self.repository.get(id) self.repository.delete(event) except (ValueError, AttributeError) as e: return e return "Event was deleted successfully" def get_top_events(self): ''' Show top events with most visitors :return: string ''' relations = self.get_instantiated_relation() occurences = self.determine_occurences(relations) relations.sort( key=lambda rel: EventController.find_dict2(occurences, rel[0].get_id())[rel[0].get_id()], reverse=True ) # Unique relations unique_relations = [] actual_event = -1 for rel in relations: if rel[0].get_id() != actual_event: actual_event = rel[0].get_id() unique_relations.append(rel) output = "" for rel in unique_relations: number_events = EventController.find_dict2(occurences, rel[0].get_id())[rel[0].get_id()] output += str(rel[0].get_id()) + "\t" + rel[0].get_description() + "\tparticipa \t" + str(number_events) + " persoane\n" return output def get_instantiated_relation(self, person_id = None, event_id = None): ''' Instantiate a Repository Many-to-Many relation :param person_id: :param event_id: :return: array ''' if person_id is None and event_id is None: person_events = self.person_event_repository.get_all() else: person_events = self.person_event_repository.get_by_id(person_id, event_id) result = [] for relation in person_events: person = self.person_repository.get(relation.get_person_id()) event = self.repository.get(relation.get_event_id()) date = relation.get_date() if date is None: date = "Doesn't exist" result.append((event, person, date)) return result @staticmethod def determine_occurences(relations): ''' Find occurences in multiple tuples of realtions :param relations: :return: array ''' occurences = [] for relation in relations: event_id = relation[0].get_id() try: occurence = EventController.find_dict2(occurences, event_id) except: occurences.append({ event_id: 0 }) occurence = occurences[-1] finally: occurence[event_id] += 1 return occurences @staticmethod def find_dict(array, id): ''' Find dictionary with given id :param array: :param id: :return: dictionary :raise: ValueError if id is not found ''' for elem in array: try: elem[id] return elem except KeyError: continue raise ValueError @staticmethod def find_dict2(array, id): ''' Find dictionary with given id :param array: :param id: :return: dictionary :raise: ValueError if id is not found ''' if not len(array): raise ValueError try: array[0][id] return array[0] finally: return find_dict2(array[1:], id)
class PersonController(): def __init__(self): ''' PersonController constructor :return: object ''' self.repository = PersonRepository() self.person_event_repository = PersonEventRepository() self.event_repository = EventRepository() def index(self): ''' Return all persons :return: string ''' output = "" for person in self.repository.get_all(): output += str(person.get_id()) + "\t" + person.get_name() + "\t" + person.get_address() + "\n" return output def show(self, id): ''' Return a found person :param id: :return: string ''' try: id = int(id) except ValueError: return "Please enter an integer for ID" try: person = self.repository.get(id) return str(person.get_id()) + "\t" + person.get_name() + "\t" + person.get_address() + "\n" except (ValueError, AttributeError) as e: return e def insert(self, name = None, address = None, event = None): ''' Insert a person :param date: :param time: :param description: :return: string ''' try: person = Person(name, address, event) self.repository.insert(person) except (ValueError, AttributeError) as e: return e return "Inserted successfully" def update(self, id, first_name = None, last_name = None, address = None): ''' Update an event :param id: :param date: :param time: :param description: :return: string ''' try: id = int(id) except ValueError: return "Please insert a valid ID" try: person = self.repository.get(id) except ValueError as e: return e if first_name == '': first_name = None if last_name == '': last_name = None if address == '': address = None try: person.update(first_name, last_name, address) self.repository.update(person) except (ValueError, AttributeError) as e: return e return "Person updated successfully!" def delete(self, id): ''' Delete an event from instances array :param id: :return: string ''' try: id = int(id) except ValueError: return "Please enter an integer for ID" try: person = self.repository.get(id) self.repository.delete(person) except (ValueError, AttributeError) as e: return e return "Person was deleted successfully" def associate(self, person_id, event_id, data): ''' Associate a person with an event :param person_id: :param event_id: :param data: :return: string ''' try: person_id = int(person_id) event_id = int(event_id) except ValueError: return "Please enter an integer for ID" try: person_event = PersonEvent(person_id, event_id, data) self.person_event_repository.insert(person_event) except (ValueError, AttributeError) as e: return e return "Association created successfully" def search(self, term): ''' Search people by term :param term: :return: string ''' found_people = [] for person in self.repository.get_all(): person_criteria = person.get_name() + person.get_address() if (re.match(term.lower(), person_criteria.lower()) is not None): found_people.append(person) output = "" for person in found_people: output += str(person.get_id()) + "\t" + person.get_name() + "\t" + person.get_address() + "\n" return output def get_events(self, id): ''' Return events that a person has participated in :param id: :return: string ''' try: id = int(id) except ValueError: return "Please enter an integer for ID" output = "" try: relations = self.get_instantiated_relation(id) except: print("The id doesn't exist") relations.sort(key=lambda rel: (rel[0].get_description(), rel[0].get_date())) for relation in relations: event = relation[0] person = relation[1] date = relation[2] output += str(event.get_id()) + "\t" + event.get_description() + "\t Inscreire in data: " + str(date) + "\n" return output def get_top_persons(self): ''' Return top people with most visits :return: string ''' relations = self.get_instantiated_relation() occurences = self.determine_occurences(relations) relations.sort( key=lambda rel: PersonController.find_dict(occurences, rel[1].get_id())[rel[1].get_id()], reverse=True ) # Unique relations unique_relations = [] actual_person = -1 for rel in relations: if rel[1].get_id() != actual_person: actual_person = rel[1].get_id() unique_relations.append(rel) output = "" for rel in unique_relations: number_events = PersonController.find_dict(occurences, rel[1].get_id())[rel[1].get_id()] output += rel[1].get_name() + "\tparticipa la\t" + str(number_events) + "\n" return output def get_instantiated_relation(self, person_id = None, event_id = None): ''' Reinstantiate a relation between person and event from repository :param person_id: :param event_id: :return: string ''' if person_id is None and event_id is None: person_events = self.person_event_repository.get_all() else: person_events = self.person_event_repository.get_by_id(person_id, event_id) result = [] for relation in person_events: event = self.event_repository.get(relation.get_event_id()) person = self.repository.get(relation.get_person_id()) date = relation.get_date() if date is None: date = "Doesn't exist" result.append((event, person, date)) return result @staticmethod def determine_occurences(relations): ''' Find occurences in multiple tuples of realtions :param relations: :return: array ''' occurences = [] for relation in relations: person_id = relation[1].get_id() try: occurence = PersonController.find_dict(occurences, person_id) except: occurences.append({ person_id: 0 }) occurence = occurences[-1] finally: occurence[person_id] += 1 return occurences @staticmethod def find_dict(array, id): ''' Find dictionary with given id :param array: :param id: :return: dictionary :raise: ValueError if id is not found ''' for elem in array: try: elem[id] return elem except KeyError: continue raise ValueError