Python django.core.serializers.json.Serializer() Examples

The following are 7 code examples of django.core.serializers.json.Serializer(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module django.core.serializers.json , or try the search function .
Example #1
Source File: test_load_program_fixture.py    From course-discovery with GNU Affero General Public License v3.0 6 votes vote down vote up
def test_existing_seat_types(self):

        fixture = json_serializer.Serializer().serialize([
            self.organization,
            self.seat_type_verified,
            self.program_type_masters,
            self.program,
        ])
        self._mock_fixture_response(fixture)
        self.reset_db_state()

        # create existing verified seat with different pk than fixture and
        # a second seat type with the same pk but different values
        new_pk = self.seat_type_verified.id + 1
        SeatType.objects.create(id=new_pk, name='Verified', slug='verified')
        SeatType.objects.create(id=self.seat_type_verified.id, name='Test', slug='test')
        self._call_load_program_fixture([str(self.program.uuid)])

        stored_program = Program.objects.get(uuid=self.program.uuid)
        stored_seat_type = stored_program.type.applicable_seat_types.first()

        self.assertEqual(stored_seat_type.id, new_pk)
        self.assertEqual(stored_seat_type.name, self.seat_type_verified.name) 
Example #2
Source File: test_load_program_fixture.py    From course-discovery with GNU Affero General Public License v3.0 6 votes vote down vote up
def test_fail_on_save_error(self):

        fixture = json_serializer.Serializer().serialize([
            self.organization,
        ])

        #  Should not be able to save an organization without uuid
        fixture_json = json.loads(fixture)
        fixture_json[0]['fields']['uuid'] = None
        fixture = json.dumps(fixture_json)

        self._mock_fixture_response(fixture)
        self.reset_db_state()

        with pytest.raises(IntegrityError) as err:
            self._call_load_program_fixture([str(self.program.uuid)])
        expected_msg = r'Failed to save course_metadata.Organization\(pk={pk}\):'.format(pk=self.organization.id)
        assert re.match(expected_msg, str(err.value)) 
Example #3
Source File: test_load_program_fixture.py    From course-discovery with GNU Affero General Public License v3.0 6 votes vote down vote up
def test_ignore_program_external_key(self):
        fixture = json_serializer.Serializer().serialize([
            self.organization,
            self.seat_type_verified,
            self.program_type_masters,
            self.program,
        ])
        self._mock_fixture_response(fixture)
        self.reset_db_state()

        self._call_load_program_fixture([
            '{uuid}:{external_key}'.format(
                uuid=str(self.program.uuid),
                external_key='CS-104-FALL-2019'
            )
        ])

        Program.objects.get(uuid=self.program.uuid) 
Example #4
Source File: views.py    From course-discovery with GNU Affero General Public License v3.0 5 votes vote down vote up
def get(self, request):
        uuids_string = self.request.GET.get(self.QUERY_PARAM)
        if not uuids_string:
            return HttpResponse(self.HELP_STRING, status=404)
        uuids_split = uuids_string.split(',')
        try:
            uuids = {UUID(uuid_str) for uuid_str in uuids_split}
        except ValueError:
            return HttpResponse(self.HELP_STRING, status=404)
        if len(uuids) > self.MAX_REQUESTED_PROGRAMS:
            return HttpResponse(
                'Too many programs requested, only {} allowed.'.format(self.MAX_REQUESTED_PROGRAMS),
                status=422,
            )
        programs = use_read_replica_if_available(
            Program.objects.filter(uuid__in=list(uuids))
        )
        loaded_uuids = {program.uuid for program in programs}
        bad_uuids = uuids - loaded_uuids
        if bad_uuids:
            return HttpResponse(
                "Could not load programs from UUIDs: [{}]".format(
                    ",".join(str(uuid) for uuid in bad_uuids)
                ),
                status=404,
            )
        objects = load_program_fixture(programs)
        json_text = json.Serializer().serialize(objects)
        return HttpResponse(json_text, content_type='text/json') 
Example #5
Source File: test_load_program_fixture.py    From course-discovery with GNU Affero General Public License v3.0 5 votes vote down vote up
def test_update_existing_program_type(self):

        fixture = json_serializer.Serializer().serialize([
            self.organization,
            self.seat_type_verified,
            self.program_type_masters,
            self.program,
        ])
        self._mock_fixture_response(fixture)
        self.reset_db_state()

        # set DB to have a conflicting program type on load
        seat_type = SeatTypeFactory(
            name='Something',
            slug='something',
        )
        existing_program_type = ProgramTypeFactory(
            name='Masters',
            name_t='Masters',
            slug='masters',
            applicable_seat_types=[seat_type]
        )

        self._call_load_program_fixture([str(self.program.uuid)])

        stored_program = Program.objects.get(uuid=self.program.uuid)

        # assert existing DB value is used
        stored_program_type = stored_program.type
        self.assertEqual(stored_program_type, existing_program_type)

        # assert existing DB value is updated to match fixture
        stored_seat_types = list(stored_program_type.applicable_seat_types.all())
        self.assertEqual(len(stored_seat_types), 1)
        self.assertEqual(stored_seat_types[0].name, self.seat_type_verified.name) 
Example #6
Source File: test_load_program_fixture.py    From course-discovery with GNU Affero General Public License v3.0 4 votes vote down vote up
def test_load_programs(self):

        fixture = json_serializer.Serializer().serialize([
            self.program_type_masters,
            self.program_type_mm,
            self.organization,
            self.seat_type_verified,
            self.program,
            self.program_2,
            self.program_mm,
            self.curriculum_program_membership,
            self.curriculum_course_membership,
            self.curriculum,
            self.course,
            self.course_mm,
            self.course_run,
            self.course_run_mm,
        ])
        self._mock_fixture_response(fixture)

        requested_programs = [
            str(self.program.uuid),
            str(self.program_2.uuid),
        ]
        self.reset_db_state()
        self._call_load_program_fixture(requested_programs)

        # walk through program structure to validate correct
        # objects have been created
        stored_program = Program.objects.get(uuid=self.program.uuid)
        stored_program_2 = Program.objects.get(uuid=self.program_2.uuid)
        self.assertEqual(stored_program.title, self.program.title)
        self.assertEqual(stored_program_2.title, self.program_2.title)

        stored_organization = stored_program.authoring_organizations.first()
        self.assertEqual(stored_organization.name, self.organization.name)

        # partner should use existing edx value
        self.assertEqual(stored_program.partner, self.default_partner)
        self.assertEqual(stored_organization.partner, self.default_partner)

        stored_program_type = stored_program.type
        self.assertEqual(stored_program_type.name_t, self.program_type_masters.name)

        stored_seat_type = stored_program_type.applicable_seat_types.first()
        self.assertEqual(stored_seat_type.name, self.seat_type_verified.name)

        stored_curriculum = stored_program.curricula.first()
        self.assertEqual(stored_curriculum.uuid, self.curriculum.uuid)

        stored_course = stored_curriculum.course_curriculum.first()
        self.assertEqual(stored_course.key, self.course.key)

        stored_mm = stored_curriculum.program_curriculum.first()
        self.assertEqual(stored_mm.uuid, self.program_mm.uuid)

        stored_course_run = stored_course.course_runs.first()
        self.assertEqual(stored_course_run.key, self.course_run.key) 
Example #7
Source File: test_load_program_fixture.py    From course-discovery with GNU Affero General Public License v3.0 4 votes vote down vote up
def test_update_existing_data(self):

        fixture = json_serializer.Serializer().serialize([
            self.organization,
            self.seat_type_verified,
            self.program_type_masters,
            self.program,
            self.curriculum,
            self.course,
            self.course_run,
            self.curriculum_course_membership,
        ])
        self._mock_fixture_response(fixture)
        self._call_load_program_fixture([str(self.program.uuid)])

        self.program.title = 'program-title-modified'
        self.course.title = 'course-title-modified'
        new_course = CourseFactory(partner=self.partner, authoring_organizations=[self.organization])
        new_course_run = CourseRunFactory(course=new_course)
        new_course_membership = CurriculumCourseMembershipFactory(course=new_course, curriculum=self.curriculum)

        fixture = json_serializer.Serializer().serialize([
            self.organization,
            self.seat_type_verified,
            self.program_type_masters,
            self.program,
            self.curriculum,
            self.course,
            self.course_run,
            self.curriculum_course_membership,
            new_course_membership,
            new_course,
            new_course_run,
        ])
        responses.reset()
        self._mock_oauth_request()
        self._mock_fixture_response(fixture)
        self.reset_db_state()
        self._call_load_program_fixture([str(self.program.uuid)])

        stored_program = Program.objects.get(uuid=self.program.uuid)
        self.assertEqual(stored_program.title, 'program-title-modified')

        stored_program_courses = stored_program.curricula.first().course_curriculum.all()
        modified_existing_course = stored_program_courses.get(uuid=self.course.uuid)
        stored_new_course = stored_program_courses.get(uuid=new_course.uuid)

        self.assertEqual(len(stored_program_courses), 2)
        self.assertEqual(modified_existing_course.title, 'course-title-modified')
        self.assertEqual(stored_new_course.key, new_course.key)