Exemple #1
0
    def run(self, tracker, school, data_type, options):
        """Run the command."""
        tracker.school = school
        tracker.mode = "validating"
        if options["display_progress_bar"]:
            tracker.add_viewer(
                StatProgressBar("{valid}/{total}", statistics=self.stat_view),
                name="progressbar",
            )
        logger = logging.getLogger("parsing.schools." + school)
        logger.debug("Digest command options:" + str(options))

        # Load config file to dictionary.
        if isinstance(options["config"], str):
            with open(options["config"].format(school=school, type=data_type),
                      "r") as file:
                options["config"] = json.load(file)

        try:
            Validator(options["config"],
                      tracker=tracker).validate_self_contained(
                          options["data"].format(school=school,
                                                 type=data_type),
                          break_on_error=True,
                          break_on_warning=options.get("break_on_warning"),
                          display_progress_bar=options["display_progress_bar"],
                      )
        except (ValidationError, ValidationWarning, Exception):
            logging.exception("Failed validation before digestion")
            return  # Skip digestion for this school.

        if options["display_progress_bar"]:
            tracker.remove_viewer("progressbar")
            tracker.add_viewer(ETAProgressBar(), name="progressbar")
        tracker.mode = "digesting"

        with open(options["data"].format(school=school, type=data_type),
                  "r") as file:
            data = json.load(file)

        try:
            Digestor(school, meta=data["$meta"], tracker=tracker).digest(
                data["$data"],
                diff=options["diff"],
                load=options["load"],
                output=options["output_diff"].format(school=school,
                                                     type=data_type),
            )

        except DigestionError:
            logging.exception("Failed digestion")
        except PipelineException:
            logging.expection("Failed digestion w/in pipeline")
        except Exception:
            logging.exception("Failed digestion with uncaught exception")

        logging.info("Digestion overview for " + school + ": " +
                     str(self.stat_view.report()))
Exemple #2
0
    def run(self, tracker, school, data_type, options):
        """Run the command."""
        tracker.school = school
        tracker.mode = 'validating'
        if options['display_progress_bar']:
            tracker.add_viewer(StatProgressBar('{valid}/{total}',
                                               statistics=self.stat_view),
                               name='progressbar')
        logger = logging.getLogger('parsing.schools.' + school)
        logger.debug('Digest command options:' + str(options))

        # Load config file to dictionary.
        if isinstance(options['config'], str):
            with open(options['config'].format(school=school, type=data_type),
                      'r') as file:
                options['config'] = json.load(file)

        try:
            Validator(options['config'],
                      tracker=tracker).validate_self_contained(
                          options['data'].format(school=school,
                                                 type=data_type),
                          break_on_error=True,
                          break_on_warning=options.get('break_on_warning'),
                          display_progress_bar=options['display_progress_bar'])
        except (ValidationError, ValidationWarning, Exception):
            logging.exception('Failed validation before digestion')
            return  # Skip digestion for this school.

        if options['display_progress_bar']:
            tracker.remove_viewer('progressbar')
            tracker.add_viewer(ETAProgressBar(), name='progressbar')
        tracker.mode = 'digesting'

        with open(options['data'].format(school=school, type=data_type),
                  'r') as file:
            data = json.load(file)

        try:
            Digestor(school, meta=data['$meta'], tracker=tracker).digest(
                data['$data'],
                diff=options['diff'],
                load=options['load'],
                output=options['output_diff'].format(school=school,
                                                     type=data_type))

        except DigestionError:
            logging.exception('Failed digestion')
        except PipelineException:
            logging.expection('Failed digestion w/in pipeline')
        except Exception:
            logging.exception('Failed digestion with uncaught exception')

        logging.info('Digestion overview for ' + school + ': ' +
                     str(self.stat_view.report()))
Exemple #3
0
    def run(self, tracker, school, data_type, options):
        tracker.school = school

        tracker.mode = 'validating'

        try:
            Validator(options['config_file'].format(school=school,
                                                    type=data_type),
                      tracker=tracker).validate_self_contained(
                          options['data'].format(school=school,
                                                 type=data_type),
                          break_on_error=True,
                          break_on_warning=options.get('break_on_warning'),
                          output_error=options.get('output_error').format(
                              school=school, type=data_type),
                          display_progress_bar=options['display_progress_bar'])
        except JsonException:
            print('FAILED VALIDATION', file=sys.stderr)
            return  # Skip digestion for this school.

        tracker.mode = 'digesting'

        try:
            Digestor(school,
                     data=options['data'].format(school=school,
                                                 type=data_type),
                     output=options['output_diff'].format(school=school,
                                                          type=data_type),
                     diff=options['diff'],
                     load=options['load'],
                     display_progress_bar=options['display_progress_bar'],
                     tracker=tracker).digest()

        except DigestionError as e:
            self.stderr.write(self.style.ERROR('FAILED: digestion'))
            self.stderr.write(str(e))
            tracker.see_error(str(e) + '\n' + traceback.format_exc())
        except Exception as e:
            self.stderr.write(self.style.ERROR('FAILED: digestion'))
            self.stderr.write(traceback.format_exc())
            tracker.see_error(traceback.format_exc())
Exemple #4
0
    def test_digest_nested(self):
        meta = {
            "$schools": {
                "salisbury": {
                    "2017": [
                        "Summer"
                    ]
                }
            },
            "$timestamp": 1502836183.235978
        }
        digestor = Digestor('test', meta)

        nested_course = {
            'kind': 'course',
            'school': {
                'code': 'test'
            },
            'code': 'ABC',
            'name': 'Alphabet',
            'department': {
                'code': 'GHI',
                'name': 'English'
            },
            'credits': 3.,
            'prerequisites': ['ABC', 'DEF'],
            'corequisites': ['A', 'AB', 'BC', 'B', 'C'],
            'homepage': 'www.google.com',
            'same_as': ['ABD'],
            'description': 'Um, hi hello',
            'sections': [
                {
                    'code': '001',
                    'term': 'Bar',
                    'year': '2017',
                    'instructors': [
                        {
                            'name': {
                                'first': 'Sem',
                                'last': 'Ly'
                            }
                        },
                        {
                            'name': 'Semesterly'
                        }
                    ],
                    'capacity': 42,
                    'enrollment': 41,
                    'waitlist': 0,
                    'waitlist_size': 100,
                    'type': 'Lecture',
                    'fees': 50.,
                },
                {
                    'code': '002',
                    'term': 'Bar',
                    'year': '2017',
                    'instructors': [
                        {
                            'name': 'Semesterly'
                        }
                    ],
                    'capacity': 40,
                    'enrollment': 36,
                    'waitlist': 0,
                    'waitlist_size': 100,
                    'type': 'Lecture',
                    'fees': 50.,
                    'meetings': [
                        {
                            'days': ['M', 'F'],
                            'time': {
                                'start': '14:00',
                                'end': '14:50'
                            },
                            'location': {
                                'campus': 'Homewood',
                                'building': 'Malone',
                                'room': 'Ugrad'
                            }
                        },
                        {
                            'days': ['W'],
                            'time': {
                                'start': '10:00',
                                'end': '12:15'
                            }
                        }
                    ]
                }
            ]
        }

        output = StringIO.StringIO()
        digestor.digest(nested_course, diff=True, load=True, output=output)

        diff = [
            {
                "$context": {},
                "$new": {
                    "code": "ABC",
                    "corequisites": "A, AB, BC, B, C",
                    "department": "English",
                    "description": "Um, hi hello",
                    "name": "Alphabet",
                    "num_credits": 3.0,
                    "prerequisites": "Pre: ABC, DEF Co: A, AB, BC, B, C",
                    "school": "test"
                }
            },
            {
                "$context": {
                    "course": "ABC: Alphabet",
                    "semester": "Bar 2017"
                },
                "$new": {
                    "enrolment": 41,
                    "instructors": "Sem LySemesterly",
                    "meeting_section": "001",
                    "section_type": "L",
                    "size": 42,
                    "waitlist": 0,
                    "waitlist_size": 100
                }
            },
            {
                "$context": {
                    "course": "ABC: Alphabet",
                    "semester": "Bar 2017"
                },
                "$new": {
                    "enrolment": 36,
                    "instructors": "Semesterly",
                    "meeting_section": "002",
                    "section_type": "L",
                    "size": 40,
                    "waitlist": 0,
                    "waitlist_size": 100
                }
            },
            {
                "$context": {
                    "section": "Course: ABC: Alphabet; Section: ABC: Alphabet; Semester: ABC: Alphabet"
                },
                "$new": {
                    "day": "M",
                    "location": "Malone Ugrad",
                    "time_end": "14:50",
                    "time_start": "14:00"
                }
            },
            {
                "$context": {
                    "section": "Course: ABC: Alphabet; Section: ABC: Alphabet; Semester: ABC: Alphabet"
                },
                "$new": {
                    "day": "F",
                    "location": "Malone Ugrad",
                    "time_end": "14:50",
                    "time_start": "14:00"
                }
            },
            {
                "$context": {
                    "section": "Course: ABC: Alphabet; Section: ABC: Alphabet; Semester: ABC: Alphabet"
                },
                "$new": {
                    "day": "W",
                    "location": " ",
                    "time_end": "12:15",
                    "time_start": "10:00"
                }
            }
        ]
        self.assertEqual(
            json.dumps(diff, sort_keys=True, indent=2, separators=(',', ': ')),
            output.getvalue()
        )
Exemple #5
0
    def test_digest_flat(self):
        meta = {
            "$schools": {
                "salisbury": {
                    "2017": [
                        "Summer"
                    ]
                }
            },
            "$timestamp": 1502836183.235978
        }
        digestor = Digestor('test', meta)

        course = {
            'kind': 'course',
            'school': {
                'code': 'test'
            },
            'code': 'ABC',
            'name': 'Alphabet',
            'department': {
                'code': 'GHI',
                'name': 'English'
            },
            'credits': 3.,
            'prerequisites': ['ABC', 'DEF'],
            'corequisites': ['A', 'AB', 'BC', 'B', 'C'],
            'homepage': 'www.google.com',
            'description': 'Um, hi hello',
        }

        output = StringIO.StringIO()
        digestor.digest(course, diff=True, load=False, output=output)
        diff = [
            {
                "$context": {},
                "$new": {
                    "code": "ABC",
                    "corequisites": "A, AB, BC, B, C",
                    "department": "English",
                    "description": "Um, hi hello",
                    "name": "Alphabet",
                    "num_credits": 3.0,
                    "prerequisites": "Pre: ABC, DEF Co: A, AB, BC, B, C",
                    "school": "test"
                }
            }
        ]
        self.assertEqual(
            json.dumps(diff, sort_keys=True, indent=2, separators=(',', ': ')),
            output.getvalue()
        )

        digestor.digest(course, diff=False, load=True)
        course_model = Course.objects.get(school='test',
                                          code='ABC',
                                          name='Alphabet')
        self.assertEqual(course_model.num_credits, 3.)
        self.assertEqual(course_model.department, 'English')
        self.assertEqual(course_model.corequisites, 'A, AB, BC, B, C')
        self.assertEqual(course_model.description, 'Um, hi hello')
        self.assertEqual(course_model.prerequisites,
                         'Pre: ABC, DEF Co: A, AB, BC, B, C')

        output = StringIO.StringIO()
        digestor.digest(course, diff=True, load=True, output=output)
        self.assertEqual(len(eval(output.getvalue())), 0)

        course2 = {
            'kind': 'course',
            'school': {
                'code': 'test'
            },
            'code': 'ABD',
            'name': 'The second course',
            'department': {
                'name': 'Where'
            },
            'credits': 3.5,
            'same_as': ['ABC'],
        }
        digestor.digest(course2, diff=False, load=True)
        course2_model = Course.objects.get(school='test',
                                           code='ABD')
        self.assertEqual(course2_model.same_as, course_model)

        section = {
            'kind': 'section',
            'course': {
                'code': 'ABC',
            },
            'code': '001',
            'term': 'Bar',
            'year': '2017',
            'instructors': [
                {
                    'name': {
                        'first': 'Sem',
                        'last': 'Ly'
                    }
                },
                {
                    'name': 'Semesterly'
                }
            ],
            'capacity': 42,
            'enrollment': 41,
            'waitlist': 0,
            'waitlist_size': 100,
            'type': 'Lecture',
            'fees': 50.,
        }

        output = StringIO.StringIO()
        digestor.digest(section, diff=True, load=True, output=output)
        diff = [
            {
                "$context": {
                    "course": "ABC: Alphabet",
                    "semester": "Bar 2017"
                },
                "$new": {
                    "enrolment": 41,
                    "instructors": "Sem LySemesterly",
                    "meeting_section": "001",
                    "section_type": "L",
                    "size": 42,
                    "waitlist": 0,
                    "waitlist_size": 100
                }
            }
        ]
        self.assertEqual(
            json.dumps(diff, sort_keys=True, indent=2, separators=(',', ': ')),
            output.getvalue()
        )
        section_model = Section.objects.get(
            course__school='test',
            course__code=section['course']['code'],
            meeting_section=section['code'],
            semester__year=section['year'],
            semester__name=section['term']
        )
        Semester.objects.get(year='2017', name='Bar')
        self.assertEqual(section_model.course, course_model)
        self.assertEqual(section_model.size, section['capacity'])
        self.assertEqual(section_model.waitlist, section['waitlist'])
        self.assertEqual(section_model.waitlist_size, section['waitlist_size'])
        self.assertEqual(section_model.section_type, 'L')
        self.assertEqual(section_model.enrolment, section['enrollment'])

        meeting = {
            'kind': 'meeting',
            'course': {
                'code': 'ABC'
            },
            'section': {
                'code': '001',
                'year': '2017',
                'term': 'Bar'
            },
            'days': ['M', 'W', 'F'],
            'time': {
                'start': '14:00',
                'end': '14:50'
            },
            'location': {
                'campus': 'Homewood',
                'building': 'Malone',
                'room': 'Ugrad'
            }
        }

        output = StringIO.StringIO()
        digestor.digest(meeting, diff=True, load=True, output=output)
        diff = [
            {
                "$context": {
                    "section": "Course: ABC: Alphabet; Section: ABC: Alphabet; Semester: ABC: Alphabet"
                },
                "$new": {
                    "day": "M",
                    "location": "Malone Ugrad",
                    "time_end": "14:50",
                    "time_start": "14:00"
                }
            },
            {
                "$context": {
                    "section": "Course: ABC: Alphabet; Section: ABC: Alphabet; Semester: ABC: Alphabet"
                },
                "$new": {
                    "day": "W",
                    "location": "Malone Ugrad",
                    "time_end": "14:50",
                    "time_start": "14:00"
                }
            },
            {
                "$context": {
                    "section": "Course: ABC: Alphabet; Section: ABC: Alphabet; Semester: ABC: Alphabet"
                },
                "$new": {
                    "day": "F",
                    "location": "Malone Ugrad",
                    "time_end": "14:50",
                    "time_start": "14:00"
                }
            }
        ]
        self.assertEqual(
            json.dumps(diff, sort_keys=True, indent=2, separators=(',', ': ')),
            output.getvalue()
        )
        self.assertEqual(
            len(Offering.objects.filter(section=section_model)),
            3
        )
Exemple #6
0
    def test_digest_nested(self):
        meta = {
            "$schools": {
                "salisbury": {
                    "2017": [
                        "Summer"
                    ]
                }
            },
            "$timestamp": 1502836183.235978
        }
        digestor = Digestor('test', meta)

        nested_course = {
            'kind': 'course',
            'school': {
                'code': 'test'
            },
            'code': 'ABC',
            'name': 'Alphabet',
            'department': {
                'code': 'GHI',
                'name': 'English'
            },
            'credits': 3.,
            'prerequisites': ['ABC', 'DEF'],
            'corequisites': ['A', 'AB', 'BC', 'B', 'C'],
            'homepage': 'www.google.com',
            'same_as': ['ABD'],
            'description': 'Um, hi hello',
            'sections': [
                {
                    'code': '001',
                    'term': 'Bar',
                    'year': '2017',
                    'instructors': [
                        {
                            'name': {
                                'first': 'Sem',
                                'last': 'Ly'
                            }
                        },
                        {
                            'name': 'Semesterly'
                        }
                    ],
                    'capacity': 42,
                    'enrollment': 41,
                    'waitlist': 0,
                    'waitlist_size': 100,
                    'type': 'Lecture',
                    'fees': 50.,
                },
                {
                    'code': '002',
                    'term': 'Bar',
                    'year': '2017',
                    'instructors': [
                        {
                            'name': 'Semesterly'
                        }
                    ],
                    'capacity': 40,
                    'enrollment': 36,
                    'waitlist': 0,
                    'waitlist_size': 100,
                    'type': 'Lecture',
                    'fees': 50.,
                    'meetings': [
                        {
                            'days': ['M', 'F'],
                            'time': {
                                'start': '14:00',
                                'end': '14:50'
                            },
                            'location': {
                                'campus': 'Homewood',
                                'building': 'Malone',
                                'room': 'Ugrad'
                            }
                        },
                        {
                            'days': ['W'],
                            'time': {
                                'start': '10:00',
                                'end': '12:15'
                            }
                        }
                    ]
                }
            ]
        }

        output = StringIO.StringIO()
        digestor.digest(nested_course, diff=True, load=True, output=output)

        diff = [
            {
                "$context": {},
                "$new": {
                    "code": "ABC",
                    "corequisites": "A, AB, BC, B, C",
                    "department": "English",
                    "description": "Um, hi hello",
                    "name": "Alphabet",
                    "num_credits": 3.0,
                    "prerequisites": "Pre: ABC, DEF Co: A, AB, BC, B, C",
                    "school": "test"
                }
            },
            {
                "$context": {
                    "course": "ABC: Alphabet",
                    "semester": "Bar 2017"
                },
                "$new": {
                    "enrolment": 41,
                    "instructors": "Sem LySemesterly",
                    "meeting_section": "001",
                    "section_type": "L",
                    "size": 42,
                    "waitlist": 0,
                    "waitlist_size": 100
                }
            },
            {
                "$context": {
                    "course": "ABC: Alphabet",
                    "semester": "Bar 2017"
                },
                "$new": {
                    "enrolment": 36,
                    "instructors": "Semesterly",
                    "meeting_section": "002",
                    "section_type": "L",
                    "size": 40,
                    "waitlist": 0,
                    "waitlist_size": 100
                }
            },
            {
                "$context": {
                    "section": "Course: ABC: Alphabet; Section: ABC: Alphabet; Semester: ABC: Alphabet"
                },
                "$new": {
                    "day": "M",
                    "location": "",
                    "time_end": "14:50",
                    "time_start": "14:00"
                }
            },
            {
                "$context": {
                    "section": "Course: ABC: Alphabet; Section: ABC: Alphabet; Semester: ABC: Alphabet"
                },
                "$new": {
                    "day": "F",
                    "location": "",
                    "time_end": "14:50",
                    "time_start": "14:00"
                }
            },
            {
                "$context": {
                    "section": "Course: ABC: Alphabet; Section: ABC: Alphabet; Semester: ABC: Alphabet"
                },
                "$new": {
                    "day": "W",
                    "location": "",
                    "time_end": "12:15",
                    "time_start": "10:00"
                }
            }
        ]
        self.assertEqual(
            json.dumps(diff, sort_keys=True, indent=2, separators=(',', ': ')),
            output.getvalue()
        )
Exemple #7
0
    def test_digest_flat(self):
        meta = {
            "$schools": {
                "salisbury": {
                    "2017": [
                        "Summer"
                    ]
                }
            },
            "$timestamp": 1502836183.235978
        }
        digestor = Digestor('test', meta)

        course = {
            'kind': 'course',
            'school': {
                'code': 'test'
            },
            'code': 'ABC',
            'name': 'Alphabet',
            'department': {
                'code': 'GHI',
                'name': 'English'
            },
            'credits': 3.,
            'prerequisites': ['ABC', 'DEF'],
            'corequisites': ['A', 'AB', 'BC', 'B', 'C'],
            'homepage': 'www.google.com',
            'description': 'Um, hi hello',
        }

        output = StringIO.StringIO()
        digestor.digest(course, diff=True, load=False, output=output)
        diff = [
            {
                "$context": {},
                "$new": {
                    "code": "ABC",
                    "corequisites": "A, AB, BC, B, C",
                    "department": "English",
                    "description": "Um, hi hello",
                    "name": "Alphabet",
                    "num_credits": 3.0,
                    "prerequisites": "Pre: ABC, DEF Co: A, AB, BC, B, C",
                    "school": "test"
                }
            }
        ]
        self.assertEqual(
            json.dumps(diff, sort_keys=True, indent=2, separators=(',', ': ')),
            output.getvalue()
        )

        digestor.digest(course, diff=False, load=True)
        course_model = Course.objects.get(school='test',
                                          code='ABC',
                                          name='Alphabet')
        self.assertEqual(course_model.num_credits, 3.)
        self.assertEqual(course_model.department, 'English')
        self.assertEqual(course_model.corequisites, 'A, AB, BC, B, C')
        self.assertEqual(course_model.description, 'Um, hi hello')
        self.assertEqual(course_model.prerequisites,
                         'Pre: ABC, DEF Co: A, AB, BC, B, C')

        output = StringIO.StringIO()
        digestor.digest(course, diff=True, load=True, output=output)
        self.assertEqual(len(eval(output.getvalue())), 0)

        course2 = {
            'kind': 'course',
            'school': {
                'code': 'test'
            },
            'code': 'ABD',
            'name': 'The second course',
            'department': {
                'name': 'Where'
            },
            'credits': 3.5,
            'same_as': ['ABC'],
        }
        digestor.digest(course2, diff=False, load=True)
        course2_model = Course.objects.get(school='test',
                                           code='ABD')
        self.assertEqual(course2_model.same_as, course_model)

        section = {
            'kind': 'section',
            'course': {
                'code': 'ABC',
            },
            'code': '001',
            'term': 'Bar',
            'year': '2017',
            'instructors': [
                {
                    'name': {
                        'first': 'Sem',
                        'last': 'Ly'
                    }
                },
                {
                    'name': 'Semesterly'
                }
            ],
            'capacity': 42,
            'enrollment': 41,
            'waitlist': 0,
            'waitlist_size': 100,
            'type': 'Lecture',
            'fees': 50.,
        }

        output = StringIO.StringIO()
        digestor.digest(section, diff=True, load=True, output=output)
        diff = [
            {
                "$context": {
                    "course": "ABC: Alphabet",
                    "semester": "Bar 2017"
                },
                "$new": {
                    "enrolment": 41,
                    "instructors": "Sem LySemesterly",
                    "meeting_section": "001",
                    "section_type": "L",
                    "size": 42,
                    "waitlist": 0,
                    "waitlist_size": 100
                }
            }
        ]
        self.assertEqual(
            json.dumps(diff, sort_keys=True, indent=2, separators=(',', ': ')),
            output.getvalue()
        )
        section_model = Section.objects.get(
            course__school='test',
            course__code=section['course']['code'],
            meeting_section=section['code'],
            semester__year=section['year'],
            semester__name=section['term']
        )
        Semester.objects.get(year='2017', name='Bar')
        self.assertEqual(section_model.course, course_model)
        self.assertEqual(section_model.size, section['capacity'])
        self.assertEqual(section_model.waitlist, section['waitlist'])
        self.assertEqual(section_model.waitlist_size, section['waitlist_size'])
        self.assertEqual(section_model.section_type, 'L')
        self.assertEqual(section_model.enrolment, section['enrollment'])

        meeting = {
            'kind': 'meeting',
            'course': {
                'code': 'ABC'
            },
            'section': {
                'code': '001',
                'year': '2017',
                'term': 'Bar'
            },
            'days': ['M', 'W', 'F'],
            'time': {
                'start': '14:00',
                'end': '14:50'
            },
            'location': {
                'campus': 'Homewood',
                'building': 'Malone',
                'room': 'Ugrad'
            }
        }

        output = StringIO.StringIO()
        digestor.digest(meeting, diff=True, load=True, output=output)
        diff = [
            {
                "$context": {
                    "section": "Course: ABC: Alphabet; Section: ABC: Alphabet; Semester: ABC: Alphabet"
                },
                "$new": {
                    "day": "M",
                    "location": "",
                    "time_end": "14:50",
                    "time_start": "14:00"
                }
            },
            {
                "$context": {
                    "section": "Course: ABC: Alphabet; Section: ABC: Alphabet; Semester: ABC: Alphabet"
                },
                "$new": {
                    "day": "W",
                    "location": "",
                    "time_end": "14:50",
                    "time_start": "14:00"
                }
            },
            {
                "$context": {
                    "section": "Course: ABC: Alphabet; Section: ABC: Alphabet; Semester: ABC: Alphabet"
                },
                "$new": {
                    "day": "F",
                    "location": "",
                    "time_end": "14:50",
                    "time_start": "14:00"
                }
            }
        ]
        self.assertEqual(
            json.dumps(diff, sort_keys=True, indent=2, separators=(',', ': ')),
            output.getvalue()
        )
        self.assertEqual(
            len(Offering.objects.filter(section=section_model)),
            3
        )
Exemple #8
0
    def test_digest_nested(self):
        meta = {
            "$schools": {
                "salisbury": {
                    "2017": ["Summer"]
                }
            },
            "$timestamp": 1502836183.235978,
        }
        digestor = Digestor("test", meta)

        nested_course = {
            "kind":
            "course",
            "school": {
                "code": "test"
            },
            "code":
            "ABC",
            "name":
            "Alphabet",
            "department": {
                "code": "GHI",
                "name": "English"
            },
            "credits":
            3.0,
            "prerequisites": ["ABC", "DEF"],
            "corequisites": ["A", "AB", "BC", "B", "C"],
            "homepage":
            "www.google.com",
            "same_as": ["ABD"],
            "description":
            "Um, hi hello",
            "sections": [
                {
                    "code":
                    "001",
                    "term":
                    "Bar",
                    "year":
                    "2017",
                    "instructors": [
                        {
                            "name": {
                                "first": "Sem",
                                "last": "Ly"
                            }
                        },
                        {
                            "name": "Semesterly"
                        },
                    ],
                    "capacity":
                    42,
                    "enrollment":
                    41,
                    "waitlist":
                    0,
                    "waitlist_size":
                    100,
                    "type":
                    "Lecture",
                    "fees":
                    50.0,
                },
                {
                    "code":
                    "002",
                    "term":
                    "Bar",
                    "year":
                    "2017",
                    "instructors": [{
                        "name": "Semesterly"
                    }],
                    "capacity":
                    40,
                    "enrollment":
                    36,
                    "waitlist":
                    0,
                    "waitlist_size":
                    100,
                    "type":
                    "Lecture",
                    "fees":
                    50.0,
                    "meetings": [
                        {
                            "days": ["M", "F"],
                            "dates": {
                                "start": "08-29-2017",
                                "end": "12-10-2017",
                            },
                            "time": {
                                "start": "14:00",
                                "end": "14:50"
                            },
                            "location": {
                                "campus": "Homewood",
                                "building": "Malone",
                                "room": "Ugrad",
                            },
                            "is_short_course": False,
                        },
                        {
                            "days": ["W"],
                            "dates": {
                                "start": "08-29-2017",
                                "end": "12-10-2017",
                            },
                            "time": {
                                "start": "10:00",
                                "end": "12:15"
                            },
                            "is_short_course": False,
                        },
                    ],
                },
            ],
        }

        output = io.StringIO()
        digestor.digest(nested_course, diff=True, load=True, output=output)

        diff = [
            {
                "$context": {},
                "$new": {
                    "code": "ABC",
                    "corequisites": "A, AB, BC, B, C",
                    "department": "English",
                    "description": "Um, hi hello",
                    "name": "Alphabet",
                    "num_credits": 3.0,
                    "prerequisites": "Pre: ABC, DEF Co: A, AB, BC, B, C",
                    "school": "test",
                },
            },
            {
                "$context": {
                    "course": "ABC: Alphabet",
                    "semester": "Bar 2017"
                },
                "$new": {
                    "enrolment": 41,
                    "instructors": "Sem LySemesterly",
                    "meeting_section": "001",
                    "section_type": "L",
                    "size": 42,
                    "waitlist": 0,
                    "waitlist_size": 100,
                },
            },
            {
                "$context": {
                    "course": "ABC: Alphabet",
                    "semester": "Bar 2017"
                },
                "$new": {
                    "enrolment": 36,
                    "instructors": "Semesterly",
                    "meeting_section": "002",
                    "section_type": "L",
                    "size": 40,
                    "waitlist": 0,
                    "waitlist_size": 100,
                },
            },
            {
                "$context": {
                    "section":
                    "Course: ABC: Alphabet; Section: ABC: Alphabet; Semester: ABC: Alphabet"
                },
                "$new": {
                    "day": "M",
                    "location": "Malone Ugrad",
                    "date_start": "08-29-2017",
                    "date_end": "12-10-2017",
                    "time_end": "14:50",
                    "time_start": "14:00",
                    "is_short_course": False,
                },
            },
            {
                "$context": {
                    "section":
                    "Course: ABC: Alphabet; Section: ABC: Alphabet; Semester: ABC: Alphabet"
                },
                "$new": {
                    "day": "F",
                    "location": "Malone Ugrad",
                    "date_start": "08-29-2017",
                    "date_end": "12-10-2017",
                    "time_end": "14:50",
                    "time_start": "14:00",
                    "is_short_course": False,
                },
            },
            {
                "$context": {
                    "section":
                    "Course: ABC: Alphabet; Section: ABC: Alphabet; Semester: ABC: Alphabet"
                },
                "$new": {
                    "day": "W",
                    "location": " ",
                    "date_start": "08-29-2017",
                    "date_end": "12-10-2017",
                    "time_end": "12:15",
                    "time_start": "10:00",
                    "is_short_course": False,
                },
            },
        ]
        self.assertEqual(
            json.dumps(diff, sort_keys=True, indent=2, separators=(",", ": ")),
            output.getvalue(),
        )
Exemple #9
0
    def test_digest_flat(self):
        meta = {
            "$schools": {
                "salisbury": {
                    "2017": ["Summer"]
                }
            },
            "$timestamp": 1502836183.235978,
        }
        digestor = Digestor("test", meta)

        course = {
            "kind": "course",
            "school": {
                "code": "test"
            },
            "code": "ABC",
            "name": "Alphabet",
            "department": {
                "code": "GHI",
                "name": "English"
            },
            "credits": 3.0,
            "prerequisites": ["ABC", "DEF"],
            "corequisites": ["A", "AB", "BC", "B", "C"],
            "homepage": "www.google.com",
            "description": "Um, hi hello",
        }

        output = io.StringIO()
        digestor.digest(course, diff=True, load=False, output=output)
        diff = [{
            "$context": {},
            "$new": {
                "code": "ABC",
                "corequisites": "A, AB, BC, B, C",
                "department": "English",
                "description": "Um, hi hello",
                "name": "Alphabet",
                "num_credits": 3.0,
                "prerequisites": "Pre: ABC, DEF Co: A, AB, BC, B, C",
                "school": "test",
            },
        }]
        self.assertEqual(
            json.dumps(diff, sort_keys=True, indent=2, separators=(",", ": ")),
            output.getvalue(),
        )

        digestor.digest(course, diff=False, load=True)
        course_model = Course.objects.get(school="test",
                                          code="ABC",
                                          name="Alphabet")
        self.assertEqual(course_model.num_credits, 3.0)
        self.assertEqual(course_model.department, "English")
        self.assertEqual(course_model.corequisites, "A, AB, BC, B, C")
        self.assertEqual(course_model.description, "Um, hi hello")
        self.assertEqual(course_model.prerequisites,
                         "Pre: ABC, DEF Co: A, AB, BC, B, C")

        output = io.StringIO()
        digestor.digest(course, diff=True, load=True, output=output)
        self.assertEqual(len(eval(output.getvalue())), 0)

        course2 = {
            "kind": "course",
            "school": {
                "code": "test"
            },
            "code": "ABD",
            "name": "The second course",
            "department": {
                "name": "Where"
            },
            "credits": 3.5,
            "same_as": ["ABC"],
        }
        digestor.digest(course2, diff=False, load=True)
        course2_model = Course.objects.get(school="test", code="ABD")
        self.assertEqual(course2_model.same_as, course_model)

        section = {
            "kind":
            "section",
            "course": {
                "code": "ABC",
            },
            "code":
            "001",
            "term":
            "Bar",
            "year":
            "2017",
            "instructors": [
                {
                    "name": {
                        "first": "Sem",
                        "last": "Ly"
                    }
                },
                {
                    "name": "Semesterly"
                },
            ],
            "capacity":
            42,
            "enrollment":
            41,
            "waitlist":
            0,
            "waitlist_size":
            100,
            "type":
            "Lecture",
            "fees":
            50.0,
        }

        output = io.StringIO()
        digestor.digest(section, diff=True, load=True, output=output)
        diff = [{
            "$context": {
                "course": "ABC: Alphabet",
                "semester": "Bar 2017"
            },
            "$new": {
                "enrolment": 41,
                "instructors": "Sem LySemesterly",
                "meeting_section": "001",
                "section_type": "L",
                "size": 42,
                "waitlist": 0,
                "waitlist_size": 100,
            },
        }]
        self.assertEqual(
            json.dumps(diff, sort_keys=True, indent=2, separators=(",", ": ")),
            output.getvalue(),
        )
        section_model = Section.objects.get(
            course__school="test",
            course__code=section["course"]["code"],
            meeting_section=section["code"],
            semester__year=section["year"],
            semester__name=section["term"],
        )
        Semester.objects.get(year="2017", name="Bar")
        self.assertEqual(section_model.course, course_model)
        self.assertEqual(section_model.size, section["capacity"])
        self.assertEqual(section_model.waitlist, section["waitlist"])
        self.assertEqual(section_model.waitlist_size, section["waitlist_size"])
        self.assertEqual(section_model.section_type, "L")
        self.assertEqual(section_model.enrolment, section["enrollment"])

        meeting = {
            "kind": "meeting",
            "course": {
                "code": "ABC"
            },
            "section": {
                "code": "001",
                "year": "2017",
                "term": "Bar"
            },
            "days": ["M", "W", "F"],
            "dates": {
                "start": "08-29-2017",
                "end": "12-10-2017",
            },
            "time": {
                "start": "14:00",
                "end": "14:50"
            },
            "location": {
                "campus": "Homewood",
                "building": "Malone",
                "room": "Ugrad"
            },
            "is_short_course": False,
        }

        output = io.StringIO()
        digestor.digest(meeting, diff=True, load=True, output=output)
        diff = [
            {
                "$context": {
                    "section":
                    "Course: ABC: Alphabet; Section: ABC: Alphabet; Semester: ABC: Alphabet"
                },
                "$new": {
                    "day": "M",
                    "location": "Malone Ugrad",
                    "date_start": "08-29-2017",
                    "date_end": "12-10-2017",
                    "time_end": "14:50",
                    "time_start": "14:00",
                    "is_short_course": False,
                },
            },
            {
                "$context": {
                    "section":
                    "Course: ABC: Alphabet; Section: ABC: Alphabet; Semester: ABC: Alphabet"
                },
                "$new": {
                    "day": "W",
                    "location": "Malone Ugrad",
                    "date_start": "08-29-2017",
                    "date_end": "12-10-2017",
                    "time_end": "14:50",
                    "time_start": "14:00",
                    "is_short_course": False,
                },
            },
            {
                "$context": {
                    "section":
                    "Course: ABC: Alphabet; Section: ABC: Alphabet; Semester: ABC: Alphabet"
                },
                "$new": {
                    "day": "F",
                    "location": "Malone Ugrad",
                    "date_start": "08-29-2017",
                    "date_end": "12-10-2017",
                    "time_end": "14:50",
                    "time_start": "14:00",
                    "is_short_course": False,
                },
            },
        ]
        self.assertEqual(
            json.dumps(diff, sort_keys=True, indent=2, separators=(",", ": ")),
            output.getvalue(),
        )
        self.assertEqual(len(Offering.objects.filter(section=section_model)),
                         3)