def test_registrations(self):
        # create new students
        rv, json = self.client.post('/api/v1.0/students/',
                                    data={'name': 'susan'})
        self.assertTrue(rv.status_code == 201)
        susan_url = rv.headers['Location']

        rv, json = self.client.post('/api/v1.0/students/',
                                    data={'name': 'david'})
        self.assertTrue(rv.status_code == 201)
        david_url = rv.headers['Location']

        # create new classes
        rv, json = self.client.post('/api/v1.0/classes/',
                                    data={'name': 'algebra'})
        self.assertTrue(rv.status_code == 201)
        algebra_url = rv.headers['Location']

        rv, json = self.client.post('/api/v1.0/classes/',
                                    data={'name': 'lit'})
        self.assertTrue(rv.status_code == 201)
        lit_url = rv.headers['Location']

        # register students to classes
        rv, json = self.client.post('/api/v1.0/registrations/',
                                    data={'student': susan_url,
                                          'class': algebra_url})
        self.assertTrue(rv.status_code == 201)
        susan_in_algebra_url = rv.headers['Location']

        rv, json = self.client.post('/api/v1.0/registrations/',
                                    data={'student': susan_url,
                                          'class': lit_url})
        self.assertTrue(rv.status_code == 201)
        susan_in_lit_url = rv.headers['Location']

        rv, json = self.client.post('/api/v1.0/registrations/',
                                    data={'student': david_url,
                                          'class': algebra_url})
        self.assertTrue(rv.status_code == 201)
        david_in_algebra_url = rv.headers['Location']

        # get registration
        rv, json = self.client.get(susan_in_lit_url)
        self.assertTrue(rv.status_code == 200)
        self.assertTrue(json['student'] == susan_url)
        self.assertTrue(json['class'] == lit_url)

        # get collection
        rv, json = self.client.get('/api/v1.0/registrations/')
        self.assertTrue(rv.status_code == 200)
        self.assertTrue(susan_in_algebra_url in json['urls'])
        self.assertTrue(susan_in_lit_url in json['urls'])
        self.assertTrue(david_in_algebra_url in json['urls'])
        self.assertTrue(len(json['urls']) == 3)

        # bad registrations
        rv,json = self.client.post('/api/v1.0/registrations/', data={})
        self.assertTrue(rv.status_code == 400)

        self.assertRaises(ValidationError, lambda:
            self.client.post('/api/v1.0/registrations/',
                             data={'student': david_url}))

        self.assertRaises(ValidationError, lambda:
            self.client.post('/api/v1.0/registrations/',
                             data={'class': algebra_url}))

        self.assertRaises(ValidationError, lambda:
            self.client.post('/api/v1.0/registrations/',
                             data={'student': david_url, 'class': 'bad-url'}))

        self.assertRaises(ValidationError, lambda:
            self.client.post('/api/v1.0/registrations/',
                             data={'student': david_url,
                                   'class': algebra_url + '1'}))
        db.session.remove()

        # get classes from each student
        rv, json = self.client.get(susan_url)
        self.assertTrue(rv.status_code == 200)
        susans_reg_url = json['registrations']
        rv, json = self.client.get(susans_reg_url)
        self.assertTrue(rv.status_code == 200)
        self.assertTrue(susan_in_algebra_url in json['urls'])
        self.assertTrue(susan_in_lit_url in json['urls'])
        self.assertTrue(len(json['urls']) == 2)

        rv, json = self.client.get(david_url)
        self.assertTrue(rv.status_code == 200)
        davids_reg_url = json['registrations']
        rv, json = self.client.get(davids_reg_url)
        self.assertTrue(rv.status_code == 200)
        self.assertTrue(david_in_algebra_url in json['urls'])
        self.assertTrue(len(json['urls']) == 1)

        # get students for each class
        rv, json = self.client.get(algebra_url)
        self.assertTrue(rv.status_code == 200)
        algebras_reg_url = json['registrations']
        rv, json = self.client.get(algebras_reg_url)
        self.assertTrue(rv.status_code == 200)
        self.assertTrue(json['urls'] in susan_in_algebra_url)
        self.assertTrue(david_in_algebra_url in json['urls'])
        self.assertTrue(len(json['urls']) == 2)

        rv, json = self.client.get(lit_url)
        self.assertTrue(rv.status_code == 200)
        lits_reg_url = json['registrations']
        rv, json = self.client.get(lits_reg_url)
        self.assertTrue(rv.status_code == 200)
        self.assertTrue(susan_in_lit_url in json['urls'])
        self.assertTrue(len(json['urls']) == 1)

        # unregister students
        rv, json = self.client.delete(susan_in_algebra_url)
        self.assertTrue(rv.status_code == 200)

        rv, json = self.client.delete(david_in_algebra_url)
        self.assertTrue(rv.status_code == 200)

        # get collection
        rv, json = self.client.get('/api/v1.0/registrations/')
        self.assertTrue(rv.status_code == 200)
        self.assertFalse(susan_in_algebra_url in json['urls'])
        self.assertTrue(susan_in_lit_url in json['urls'])
        self.assertFalse(david_in_algebra_url in json['urls'])
        self.assertTrue(len(json['urls']) == 1)

        # delete student
        rv, json = self.client.delete(susan_url)
        self.assertTrue(rv.status_code == 200)

        # get collection
        rv, json = self.client.get('/api/v1.0/registrations/')
        self.assertTrue(rv.status_code == 200)
        self.assertTrue(len(json['urls']) == 0)
dataset/ETHPy150Open miguelgrinberg/api-pycon2014/tests/test_api.py/TestAPI.test_registrations/swapped operands of `in`