def test_json_export(self): # Check that file with JSON data exists after export # ToDO validate correctness of data in output file set_run_info(export_json_path='json/test.json') data = test_data1 Exchanger.export_data(data, ExchangeFormat.JSON) os.stat(config['export_json_path'])
def downloadSchedule(request, number): try: sch = GeneratedSchedules.objects.filter(id=number)[0] except: return HttpResponseRedirect('/upload/') ftemp = f'files/{str(request.user.id)}_{str(number)}' # a little bit unefficient Exchanger.json_to_csv(sch.location, ftemp) data = ''.join(i for i in open(ftemp, 'r')) file_to_send = ContentFile(data) response = HttpResponse(file_to_send, 'application/csv') response['Content-Length'] = file_to_send.size response['Content-Disposition'] = 'attachment; filename="schedule.csv"' os.remove(ftemp) return response
def test_json_import(self): # Check that data imported from JSON file has correct structure set_run_info(import_json_path='json/schedule_data.json') data = Exchanger.import_data(ExchangeFormat.JSON) self.assertGreater(len(data), 0) self.assertGreater(len(data['lessons']), 0) self.assertGreater(len(data['days']), 0) self.assertGreater(len(data['time_slots']), 0) self.assertGreater(len(data['auditoriums']), 0) self.assertGreater(len(data['student_groups']), 0)
def test_csv_import(self): # Check that data imported from CSV file has correct structure set_run_info(import_csv_path='csv/') data = Exchanger.import_data(ExchangeFormat.CSV) self.assertGreater(len(data), 0) self.assertGreater(len(data['lessons']), 0) self.assertGreater(len(data['days']), 0) self.assertGreater(len(data['time_slots']), 0) self.assertGreater(len(data['auditoriums']), 0) self.assertGreater(len(data['student_groups']), 0)
def get_initial_population(data=None) -> (Population, dict): """Make initial population based on data from CSV/JSON (at the moment)""" if data is None: data = Exchanger.import_data(config['exchange_format']) data, uni_conf = DataProcessor.enumerate_data(data) initial_population = Population() for i in range(config['size_of_population']): lessons_list = [] for lesson in data['lessons']: day = choice(data['days']) time_slot = choice(data['time_slots']) auditorium = choice(data['auditoriums']) slot = Lesson(time_slot, (lesson[0], lesson[1]), lesson[2], day, auditorium, lesson[3]) lessons_list.append(slot) initial_population.add(individ=Schedule(lessons_list)) return initial_population, uni_conf
def test_csv_to_json(self): # Check that file with JSON data exists after transformation set_run_info(import_json_path='json/imported_schedule_data.json') Exchanger.csv_to_json() os.stat(config['import_json_path'])
def test_csv_export(self): set_run_info(export_json_path='json/test.json') data = test_data1 Exchanger.export_data(data, ExchangeFormat.CSV) os.stat(config['export_csv_path'])
def test_json_to_csv(self): set_run_info(import_json_path='json/test.json', export_csv_path='csv/schedule.csv') Exchanger.json_to_csv(config['import_json_path'], config['export_csv_path']) os.stat(config['import_csv_path'])