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)
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)
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' })
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)
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')
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, '')
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.'])
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)
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])
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)
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.'])
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)
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)
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])