Exemple #1
0
class ApiViewTest(TestCase):
    def setUp(self):
        self.user = UserFactory()
        self.user.groups.add(GroupFactory(name='Editor'))

        self.site = LearningSiteFactory()
        self.repository = ArchivalRepositoryFactory()

    def test_anonymous(self):
        # views succeed
        response = self.client.get('/api/site/', {},
                                   HTTP_X_REQUESTED_WITH='XMLHttpRequest')
        self.assertEquals(response.status_code, 200)
        the_json = loads(response.content.decode('utf-8'))
        self.assertEquals(len(the_json['results']), 1)
        self.assertEquals(the_json['results'][0]['id'], self.site.id)

        response = self.client.get('/api/repository/', {},
                                   HTTP_X_REQUESTED_WITH='XMLHttpRequest')
        self.assertEquals(response.status_code, 200)

        the_json = loads(response.content.decode('utf-8'))
        self.assertEquals(the_json['results'][0]['id'], self.repository.id)

        # update fails
        response = self.client.post('/api/site/', {
            'id': self.site.id,
            'title': 'Foo'
        },
                                    HTTP_X_REQUESTED_WITH='XMLHttpRequest')
        self.assertEquals(response.status_code, 403)

    def test_create(self):
        self.client.login(username=self.user.username, password='******')
        data = {
            'title': 'Foo',
            'place': [{
                'title': 'Bar',
                'latlng': {
                    'lat': 5,
                    'lng': 6
                }
            }]
        }
        response = self.client.post('/api/site/',
                                    dumps(data),
                                    content_type="application/json",
                                    HTTP_X_REQUESTED_WITH='XMLHttpRequest')
        self.assertEquals(response.status_code, 201)

        site = LearningSite.objects.get(title='Foo')
        self.assertEquals(site.place.first().title, 'Bar')

    def test_update(self):
        self.client.login(username=self.user.username, password='******')
        data = {
            'id': self.site.id,
            'title': 'Foo',
            'place': [{
                'title': 'Bar',
                'latlng': {
                    'lat': 5,
                    'lng': 6
                }
            }],
            'established': '',
            'defunct': ''
        }
        response = self.client.put('/api/site/{}/'.format(self.site.id),
                                   dumps(data),
                                   content_type="application/json",
                                   HTTP_X_REQUESTED_WITH='XMLHttpRequest')
        self.assertEquals(response.status_code, 200)
        self.site.refresh_from_db()
        self.assertEquals(self.site.title, 'Foo')

    def test_family(self):
        parent = LearningSiteFactory()
        sib = LearningSiteFactory()
        sib2 = LearningSiteFactory()

        LearningSiteRelationshipFactory(site_one=parent, site_two=sib)
        LearningSiteRelationshipFactory(site_one=sib2, site_two=parent)

        family = LearningSiteFamilySerializer().get_family(parent)
        self.assertEquals(len(family), 2)
        self.assertEquals(family[0]['id'], sib.id)
        self.assertEquals(family[0]['relationship'], 'associate')
        self.assertEquals(family[1]['id'], sib2.id)
        self.assertEquals(family[1]['relationship'], 'associate')
Exemple #2
0
    def test_create_or_update(self):
        site = LearningSiteFactory(established=None)

        data = {
            'is_range': False,
            'millenium1': '2',
            'century1': '0',
            'decade1': '1',
            'year1': '0',
            'month1': '2',
            'day1': '28',
            'approximate1': True,
            'uncertain1': True,
            'millenium2': None,
            'century2': None,
            'decade2': None,
            'year2': None,
            'month2': None,
            'day2': None,
            'approximate2': False,
            'uncertain2': False,
        }

        # add established date
        form = ExtendedDateForm(data=data)
        self.assertTrue(form.is_valid())
        form.create_or_update(site, 'established')
        site.refresh_from_db()
        established = site.established
        self.assertEquals(established.edtf_format, '2010-02-28?~')

        # update established date
        data['year1'] = 1
        form = ExtendedDateForm(data=data)
        self.assertTrue(form.is_valid())
        form.create_or_update(site, 'established')
        site.refresh_from_db()
        self.assertEquals(established.id, site.established.id)
        self.assertEquals(established.edtf_format, '2011-02-28?~')

        # remove established date
        data = {
            'is_range': False,
            'millenium1': '',
            'century1': '',
            'decade1': '',
            'year1': '',
            'month1': '',
            'day1': '',
            'approximate1': True,
            'uncertain1': True,
            'millenium2': '',
            'century2': '',
            'decade2': '',
            'year2': '',
            'month2': '',
            'day2': '',
            'approximate2': False,
            'uncertain2': False,
        }
        form = ExtendedDateForm(data=data)
        self.assertTrue(form.is_valid())
        form.create_or_update(site, 'established')
        site.refresh_from_db()
        self.assertIsNone(site.established)

        with self.assertRaises(ExtendedDate.DoesNotExist):
            ExtendedDate.objects.get(id=established.id)