Пример #1
0
    def test_set_values(self):
        user = UserF(username='******', password='******')
        attr1 = AttributeF.create(id=1, key='test')
        attr2 = AttributeF.create(id=2, key='osm')

        dom = DomainSpecification2AF.create(name='a domain',
                                            spec1__attribute=attr1,
                                            spec2__attribute=attr2)

        chgset = ChangesetF.create(social_user=user)

        locality = LocalityF.create(pk=1, domain=dom, changeset=chgset)

        value_map = {'osm': 'osm val', 'test': 'test val'}
        chg_values = locality.set_values(value_map, social_user=user)

        self.assertEqual(len(chg_values), 2)

        # both attributes are created
        self.assertEqual([val[1] for val in chg_values], [True, True])

        # changesets should be the same for all changed values
        self.assertEqual(chg_values[0][0].changeset,
                         chg_values[1][0].changeset)
        value_map = {'osm': 'new osm val'}
        chg_values = locality.set_values(value_map, social_user=user)

        # attribute has been updated
        self.assertEqual(chg_values[0][1], False)
Пример #2
0
    def test_locality_api_view(self):
        user = UserF.create(id=1, username='******')
        chgset = ChangesetF.create(id=1,
                                   social_user=user,
                                   created=datetime.datetime(
                                       2014, 11, 23, 12, 0))
        dom = DomainF.create(name='test_domain', changeset=chgset)

        attr = AttributeF.create(key='test', changeset=chgset)

        spec = SpecificationF.create(domain=dom,
                                     attribute=attr,
                                     changeset=chgset)

        loc = LocalityF.create(geom='POINT(16.9 45.4)',
                               uuid='35570d8b22494bb6a88487a8108ffd69',
                               changeset=chgset,
                               domain=dom)

        ValueF.create(changeset=chgset,
                      specification=spec,
                      locality=loc,
                      data='test val')

        resp = self.client.get(
            reverse('api_locality',
                    kwargs={'uuid': '35570d8b22494bb6a88487a8108ffd69'}))

        self.assertEqual(resp.status_code, 200)

        self.assertEqual(
            resp.content,
            u'{"geom": [16.9, 45.4], "version": 1, "values": {"test": "test va'
            u'l"}, "uuid": "35570d8b22494bb6a88487a8108ffd69", "changeset": 1}'
        )
Пример #3
0
    def test_localities_api_view(self):
        user = UserF.create(id=1, username='******')
        chgset = ChangesetF.create(
            social_user=user, created=datetime.datetime(2014, 11, 23, 12, 0)
        )
        dom = DomainF.create(name='test_domain', changeset=chgset)
        LocalityF.create(
            geom='POINT(16.9 45.4)', uuid='35570d8b22494bb6a88487a8108ffd69',
            changeset=chgset, domain=dom
        )

        LocalityF.create(
            geom='POINT(16 45)', uuid='35570d8b22494bb6a88487a8108ffd68',
            changeset=chgset, domain=dom
        )

        resp = self.client.get(
            reverse('api_localities'), {'bbox': '-180,-90,180,90'}
        )

        self.assertEqual(resp.status_code, 200)

        self.assertEqual(
            resp.content,
            u'[{"version": 1, "user_id": 1, "uuid": "35570d8b22494bb6a88487a81'
            u'08ffd69", "lnglat": "16.9,45.4"}, {"version": 1, "user_id": 1, "'
            u'uuid": "35570d8b22494bb6a88487a8108ffd68", "lnglat": "16,45"}]'
        )
Пример #4
0
    def test_repr_dict_method(self):
        user = UserF(username='******', password='******')
        chgset = ChangesetF.create(id=1, social_user=user)
        attr1 = AttributeF.create(key='test')
        attr2 = AttributeF.create(key='osm')

        dom = DomainSpecification2AF.create(name='a domain',
                                            spec1__attribute=attr1,
                                            spec2__attribute=attr2)

        # this domain should not be in results
        attr3 = AttributeF.create(key='osm2')
        DomainSpecification1AF.create(name='a new domain',
                                      spec1__attribute=attr3)

        locality = LocalityF.create(pk=1,
                                    domain=dom,
                                    uuid='93b7e8c4621a4597938dfd3d27659162',
                                    geom='POINT (16 45)',
                                    changeset=chgset)

        value_map = {'osm': 'osm val', 'test': 'test val'}
        locality.set_values(value_map, social_user=user)

        self.assertDictEqual(
            locality.repr_dict(), {
                u'geom': (16.0, 45.0),
                u'version': 1,
                u'changeset': 1,
                u'values': {
                    u'test': u'test val',
                    u'osm': u'osm val'
                },
                u'uuid': '93b7e8c4621a4597938dfd3d27659162'
            })
Пример #5
0
    def test_localitiesCreate_form_post(self):
        UserF(username='******', password='******')
        test_attr = AttributeF.create(key='test')
        DomainSpecification1AF(name='test', spec1__attribute=test_attr)

        self.client.login(username='******', password='******')
        resp = self.client.post(
            reverse('locality-create', kwargs={'domain': 'test'}), {
                'test': 'new_osm',
                'lon': 10,
                'lat': 35
            })

        self.assertEqual(resp.status_code, 200)

        # check if got back an id, can be parsed as int
        self.assertTrue(int(resp.content) != 0)

        loc = Locality.objects.get()

        self.assertEqual(loc.geom.x, 10.0)
        self.assertEqual(loc.geom.y, 35.0)

        self.assertListEqual([val.data for val in loc.value_set.all()],
                             ['new_osm'])

        # test version
        self.assertEqual(loc.version, 1)
Пример #6
0
    def test_localitiesUpdate_form_post_partial_data_update_locality(self):
        UserF(username='******', password='******')

        test_attr = AttributeF.create(key='test')
        chgset = ChangesetF.create(id=1)

        dom = DomainSpecification1AF(spec1__attribute=test_attr)

        spec = dom.specification_set.all()[0]

        org_loc = LocalityValue1F.create(
            geom='POINT(16 45)',
            val1__data='test_osm',
            domain=dom,
            uuid='93b7e8c4621a4597938dfd3d27659162',
            val1__specification=spec,
            changeset=chgset,
            val1__changeset=chgset)

        org_loc_version = org_loc.version
        org_value_versions = [
            org_val.version for org_val in org_loc.value_set.all()
        ]

        self.client.login(username='******', password='******')
        resp = self.client.post(
            reverse('locality-update',
                    kwargs={'uuid': '93b7e8c4621a4597938dfd3d27659162'}), {
                        'test': 'test_osm',
                        'lon': 16,
                        'lat': 10
                    })

        self.assertEqual(resp.status_code, 200)

        self.assertEqual(resp.content, 'OK')

        loc = Locality.objects.get(uuid='93b7e8c4621a4597938dfd3d27659162')

        self.assertEqual(loc.geom.x, 16.0)
        self.assertEqual(loc.geom.y, 10.0)

        self.assertListEqual([val.data for val in loc.value_set.all()],
                             ['test_osm'])

        # check if we got a new changeset (data CHANGE)
        self.assertNotEqual(loc.changeset.id, chgset.id)

        # value changeset should NOT change
        self.assertTrue(
            all(True for val in loc.value_set.all()
                if val.changeset.id == chgset.id))

        # test version, should CHANGE
        self.assertNotEqual(loc.version, org_loc_version)

        # test values version, should NOT CHANGE
        self.assertListEqual([val.version for val in loc.value_set.all()],
                             org_value_versions)
Пример #7
0
    def test_new_user_replaces_old(self):
        user = UserF(username='******', password='******')
        UserF(username='******', password='******')

        with mock.patch(
                'social_users.middleware.profile_picture_url') as mock_ppurl:
            mock_ppurl.return_value = None

            MockBackend = mock.Mock()

            kwargs = {'details': {'username': '******'}}
            save_profile(MockBackend, user, {}, is_new=True, **kwargs)

        users = User.objects.all()

        self.assertEqual(len(users), 1)
        self.assertEqual(users[0].username, 'test2')
Пример #8
0
    def test_no_backend_no_profile(self):
        user = UserF(username='******', password='******')

        with mock.patch(
                'social_users.middleware.profile_picture_url') as mock_ppurl:
            mock_ppurl.return_value = None

            MockBackend = mock.Mock()

            save_profile(MockBackend, user, {}, is_new=False)

        self.assertEqual(user.profile.profile_picture, '')
Пример #9
0
    def test_localitiesCreate_form_post_fail(self):
        UserF(username='******', password='******')

        test_attr = AttributeF.create(key='test')
        DomainSpecification1AF(name='test', spec1__attribute=test_attr)

        self.client.login(username='******', password='******')
        resp = self.client.post(
            reverse('locality-create', kwargs={'domain': 'test'}),
            {'test': 'new_osm'})

        self.assertEqual(resp.status_code, 200)

        self.assertFormError(resp, 'form', 'lat', [u'This field is required.'])
        self.assertFormError(resp, 'form', 'lon', [u'This field is required.'])
Пример #10
0
    def test_set_values_bad_key(self):
        user = UserF(username='******', password='******')
        attr1 = AttributeF.create(id=1, key='test')
        attr2 = AttributeF.create(id=2, key='osm')

        chgset = ChangesetF.create(social_user=user)

        dom = DomainSpecification2AF.create(name='a domain',
                                            spec1__attribute=attr1,
                                            spec2__attribute=attr2)

        locality = LocalityF.create(pk=1, domain=dom, changeset=chgset)

        value_map = {'osm2': 'bad key', 'test': 'test val'}
        chg_values = locality.set_values(value_map, social_user=user)

        self.assertEqual(len(chg_values), 1)
Пример #11
0
    def test_set_values_partial(self):
        user = UserF(username='******', password='******')
        attr1 = AttributeF.create(id=1, key='test')
        attr2 = AttributeF.create(id=2, key='osm')

        dom = DomainSpecification2AF.create(name='a domain',
                                            spec1__attribute=attr1,
                                            spec2__attribute=attr2)

        chgset = ChangesetF.create(social_user=user)

        locality = LocalityF.create(pk=1, domain=dom, changeset=chgset)

        value_map = {'osm': 'osm val'}
        chg_values = locality.set_values(value_map, social_user=user)
        self.assertEqual(len(chg_values), 1)

        # is attribute created
        self.assertEqual([val[1] for val in chg_values], [True])
Пример #12
0
 def setUp(self):
     self.user = UserF.create(**{'username': '******', 'is_staff': True})
     self.user.set_password('password')
     self.user.save()
     self.attr = AttributeF.create(key='facility')
     self.attr2 = AttributeF.create(key='staff')
     self.attr3 = AttributeF.create(key='inpatient_service')
     self.attr4 = AttributeF.create(key='scope_of_service')
     self.locality = LocalityValue4F.create(
         name='locality test',
         val1__data='building',
         val1__specification__attribute=self.attr,
         val2__data='15|18',
         val2__specification__attribute=self.attr2,
         val3__data='5|7',
         val3__specification__attribute=self.attr3,
         val4__data='specialized care|'
         'general acute care|rehabilitation care|',
         val4__specification__attribute=self.attr4,
     )
Пример #13
0
    def test_response_with_profile(self):
        user = UserF(username='******', password='******')
        profile = ProfileF(user=user)

        with mock.patch(
                'social_users.middleware.profile_picture_url') as mock_ppurl:
            mock_ppurl.return_value = 'picture?type=large'

            MockBackend = mock.Mock()

            user_obj = save_profile(MockBackend, user, {}, is_new=False)

        profile.refresh_from_db()

        self.assertEqual(profile.profile_picture, 'picture?type=large')

        self.assertEqual(user.profile.id, profile.id)

        self.assertTrue('user' in user_obj)

        self.assertTrue(user_obj['user'].id == user.id)
Пример #14
0
    def test_localitiesUpdate_form_post_fail(self):
        UserF(username='******', password='******')
        test_attr = AttributeF.create(key='test')

        dom = DomainSpecification1AF(spec1__attribute=test_attr)

        LocalityValue1F.create(geom='POINT(16 45)',
                               val1__data='osm',
                               domain=dom,
                               uuid='93b7e8c4621a4597938dfd3d27659162',
                               val1__specification__attribute=test_attr)

        self.client.login(username='******', password='******')
        resp = self.client.post(
            reverse('locality-update',
                    kwargs={'uuid': '93b7e8c4621a4597938dfd3d27659162'}),
            {'test': 'new_osm'})

        self.assertEqual(resp.status_code, 200)

        self.assertFormError(resp, 'form', 'lat', [u'This field is required.'])
        self.assertFormError(resp, 'form', 'lon', [u'This field is required.'])
Пример #15
0
    def test_locality_api_view(self):
        user = UserF.create(id=1, username='******')
        chgset = ChangesetF.create(
            id=1, social_user=user,
            created=datetime.datetime(2014, 11, 23, 12, 0)
        )
        dom = DomainF.create(name='test_domain', changeset=chgset)

        attr = AttributeF.create(key='test', changeset=chgset)

        spec = SpecificationF.create(
            domain=dom, attribute=attr, changeset=chgset
        )

        loc = LocalityF.create(
            geom='POINT(16.9 45.4)', uuid='35570d8b22494bb6a88487a8108ffd69',
            changeset=chgset, domain=dom
        )

        ValueF.create(
            changeset=chgset, specification=spec, locality=loc,
            data='test val'
        )

        resp = self.client.get(
            reverse(
                'api_locality',
                kwargs={'uuid': '35570d8b22494bb6a88487a8108ffd69'}
            )
        )

        self.assertEqual(resp.status_code, 200)

        self.assertEqual(
            resp.content,
            u'{"geom": [16.9, 45.4], "version": 1, "values": {"test": "test va'
            u'l"}, "uuid": "35570d8b22494bb6a88487a8108ffd69", "changeset": 1}'
        )
Пример #16
0
    def test_localitiesUpdate_form_get(self):
        UserF(username='******', password='******')

        test_attr = AttributeF.create(key='test')

        dom = DomainSpecification1AF(spec1__attribute=test_attr)

        LocalityValue1F.create(geom='POINT(16 45)',
                               val1__data='osm',
                               domain=dom,
                               uuid='93b7e8c4621a4597938dfd3d27659162',
                               val1__specification__attribute=test_attr)

        self.client.login(username='******', password='******')
        resp = self.client.get(
            reverse('locality-update',
                    kwargs={'uuid': '93b7e8c4621a4597938dfd3d27659162'}))

        self.assertEqual(resp.status_code, 200)

        self.assertEqual(resp['Content-Type'], 'text/html; charset=utf-8')

        self.assertContains(
            resp,
            '<input id="id_lon" name="lon" step="any" type="number" value="16.'
            '0" />',
            html=True)
        self.assertContains(
            resp,
            '<input id="id_lat" name="lat" step="any" type="number" value="45.'
            '0" />',
            html=True)
        self.assertContains(
            resp,
            '<input class="form-control" id="id_test" name="test" type="text" '
            'value="osm" />',
            html=True)
Пример #17
0
    def test_localitiesCreate_form_get(self):
        UserF(username='******', password='******')
        test_attr = AttributeF.create(key='test')
        DomainSpecification1AF(name='test', spec1__attribute=test_attr)

        self.client.login(username='******', password='******')
        resp = self.client.get(
            reverse('locality-create', kwargs={'domain': 'test'}))

        self.assertEqual(resp.status_code, 200)

        self.assertEqual(resp['Content-Type'], 'text/html; charset=utf-8')

        self.assertContains(
            resp,
            '<input id="id_lon" name="lon" step="any" type="number" />',
            html=True)
        self.assertContains(
            resp,
            '<input id="id_lat" name="lat" step="any" type="number" />',
            html=True)
        self.assertContains(resp,
                            '<input id="id_test" name="test" type="text" />',
                            html=True)
Пример #18
0
 def setUp(self):
     UserF.create(id=-1, username='******')
Пример #19
0
    def test_localitiesUpdate_form_post_partial_data_update_values(self):
        UserF(username='******', password='******')
        test_attr = AttributeF.create(key='test')
        test_attr2 = AttributeF.create(key='other_test')
        chgset = ChangesetF.create(id=1)

        dom = DomainSpecification2AF(spec1__attribute=test_attr,
                                     spec2__attribute=test_attr2)

        spec = [spec for spec in dom.specification_set.all()]

        org_loc = LocalityValue2F.create(
            geom='POINT(16 45)',
            domain=dom,
            changeset=chgset,
            uuid='93b7e8c4621a4597938dfd3d27659162',
            val1__data='test_osm',
            val1__specification=spec[0],
            val1__changeset=chgset,
            val2__data='other_osm',
            val2__specification=spec[1],
            val2__changeset=chgset)

        org_loc_version = org_loc.version
        org_value_versions = [
            org_val.version for org_val in org_loc.value_set.all()
        ]

        self.client.login(username='******', password='******')
        resp = self.client.post(
            reverse('locality-update',
                    kwargs={'uuid': '93b7e8c4621a4597938dfd3d27659162'}), {
                        'test': 'new_test_osm',
                        'other_test': 'other_osm',
                        'lon': 16,
                        'lat': 45
                    })

        self.assertEqual(resp.status_code, 200)

        self.assertEqual(resp.content, 'OK')

        loc = Locality.objects.get(uuid='93b7e8c4621a4597938dfd3d27659162')

        self.assertEqual(loc.geom.x, 16.0)
        self.assertEqual(loc.geom.y, 45.0)

        self.assertListEqual([val.data for val in loc.value_set.all()],
                             [u'other_osm', u'new_test_osm'])

        # check if we got the SAME changeset (NO data CHANGE)
        self.assertEqual(loc.changeset.id, chgset.id)

        # value changeset of one attribute should CHANGE
        self.assertListEqual(
            [val.changeset.id == chgset.id for val in loc.value_set.all()],
            [True, False])

        # test version, should NOT CHANGE
        self.assertEqual(loc.version, org_loc_version)

        # test values version, should CHANGE
        self.assertListEqual([
            val.version == org_value_versions[idx]
            for idx, val in enumerate(loc.value_set.all())
        ], [True, False])