def handle(self, *args, **options): KHARKOV = [50.040156499999995, 36.3003934] SMELA = [49.2391097, 31.859249100000003] CITIES = [KHARKOV, SMELA] print("COMMAND STARTED") unique_categories = list() while len(unique_categories) < 5: unique_categories.append(fake.word()) unique_categories = list(set(unique_categories)) unique_emails = list() while len(unique_emails) < 100: unique_emails.append(fake.email()) unique_emails = list(set(unique_emails)) print("CREATING OF CATEGORIES") categories = [ CategoryFactory(name=category) for category in unique_categories ] print("CREATING OF USERS-PROFILES-POSTS") iteration = 0 for email in unique_emails: coords = random.choice(CITIES) try: user = UserFactory(email=email, latitude=coords[0], longitude=coords[1], full_name=fake.name()) user.set_password('qweqweqwe') user.save() except IntegrityError: print('EMAIL SKIPPED') continue try: image_url = fake.image_url() while "lorempixel" in image_url: image_url = fake.image_url() image_path = load_image_by_url(image_url) image = Image.objects.create(user=user, image=image_path) except Exception as e: print(e) continue profile = ProfileFactory(user=user, avatar=image, age=random.randint(18, 55)) post = PostFactory(profile=profile, category=random.choice(categories), looking_for=random.choice(GENDERS)) post.photos.add(image) post.save() print('{}/{}'.format(iteration, len(unique_emails))) iteration += 1 print("COMMAND FINISHED")
def test_update_profile(self): """ Test updating profile """ profile = ProfileFactory() url = reverse('accounts:profiles-detail', kwargs={'pk': profile.pk}) response = self.client.patch(url, data={'first_name': 'New name'}) self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertNotEqual(profile.first_name, response.data['first_name'])
def test_set_avatar_mutation_1(snapshot, request): """ Test a successful set avatar mutation""" upload = "server/media/avatars/example_pic.jpg" user = UserFactory() profile = ProfileFactory(user=user) request.user = user source_image = SourceImage.objects.create(image=upload, user=user) image = Image.objects.create(user=user, image=upload, source=source_image) client = Client(schema, context=request) executed = client.execute(''' mutation setAvatar($profileId: ID!, $upload: Upload!, $galleryImageId: ID!) { setAvatar( profileId: $profileId upload: $upload galleryImageId: $galleryImageId ) { errors success } } ''', variables={"upload": upload, "galleryImageId": image.id, "profileId": profile.id}) snapshot.assert_match(executed)
def test_login_success(self): """ Test successfull Login """ user = UserFactory() profile = ProfileFactory(user=user) data = {"email": user.email, "password": '******'} response = self.client.post(reverse('accounts:login'), data) self.assertEqual(response.status_code, status.HTTP_200_OK)
def test_me_query_2(snapshot, request): """ Test a me query with profile""" user = UserFactory() ProfileFactory(user=user) request.user = user client = Client(schema, context=request) executed = client.execute(''' query me { me { categories { id name posts { id } } id geoLocationAllowed email isVerified lastVerificationCode fullName gallery { id image } profiles { id name avatar { id image source { id image } } age gender isActive user { id fullName email } } } } ''', variables={}) snapshot.assert_match(executed)
def setUp(self): """ Initial settings (create user, profile, token, genre, book) """ self.profile = ProfileFactory() self.token = Token.objects.first().key self.client.credentials(HTTP_AUTHORIZATION='Token ' + self.token) self.book = BookFactory() self.test_comment_data = { 'profile': self.profile.pk, 'book': self.book.pk, 'text': 'Test text', }
def fake_db_for_tester(user): EXAMPLE_PIC_PATH = 'server/media/gallery/example_pic.jpg' first_image = shutil.copy( EXAMPLE_PIC_PATH, 'server/media/gallery/example_pic_%d.jpg' % user.id) source = shutil.copy(EXAMPLE_PIC_PATH, 'server/media/source/example_pic_%d.jpg' % user.id) source_image = SourceImage.objects.create(user=user, image=source[13:]) image = Image.objects.create(user=user, image=first_image[12:], source=source_image) emails = generate_fake_emails(50) if Category.objects.all().count() < 3: Category.objects.create(name='Sport') Category.objects.create(name='Dating') Category.objects.create(name='Food') categories = Category.objects.all() for email in emails: try: user = UserFactory( email=email, latitude=user.latitude, longitude=user.longitude, full_name=fake.name(), ) user.set_password('qweqweqwe') user.save() except IntegrityError: print('EMAIL SKIPPED') continue new_image = shutil.copy( EXAMPLE_PIC_PATH, 'server/media/gallery/example_pic_%d.jpg' % user.id) try: image = Image.objects.create(user=user, image=new_image[12:]) avatar = Avatar.objects.create(user=user, image=new_image[12:], source=source_image) except Exception as e: print(e) continue profile = ProfileFactory(user=user, avatar=avatar, age=random.randint(18, 55), name=fake.name()) post = PostFactory(profile=profile, category=random.choice(categories), looking_for=random.choice(GENDERS), text=fake.text()[:random.randint(0, 500)]) post = PostFactory(profile=profile, category=random.choice(categories), looking_for=random.choice(GENDERS), text=fake.text()[:random.randint(0, 500)], is_matches=True) images = Image.objects.all() asd = "1.jpeg" # str(uuid.uuid1()) post_image_path = copy_image( "server/media/gallery/%s" % image.file_name, "server/media/gallery_edited/%s" % asd) post_image_1 = PostImage.objects.create(image='gallery_edited/1.jpeg', source=source_image) post_image_2 = PostImage.objects.create(image='gallery_edited/1.jpeg', source=source_image) post_image_3 = PostImage.objects.create(image='gallery_edited/1.jpeg', source=source_image) post.photos.add(post_image_1) post.photos.add(post_image_2) post.photos.add(post_image_3) post.save()
def create_user(self, **kwargs): kwargs.setdefault('is_superuser', False) user = UserFactory(**kwargs) user.profile = ProfileFactory(user=user) return user
def test_retrieve_profile(self): """ Test retrieving detail profile """ profile = ProfileFactory() url = reverse('accounts:profiles-detail', kwargs={'pk': profile.pk}) response = self.client.get(url) self.assertEqual(response.status_code, status.HTTP_200_OK)
def test_delete_profile(self): """ Test deleting profile """ profile = ProfileFactory() url = reverse('accounts:profiles-detail', kwargs={'pk': profile.pk}) response = self.client.delete(url) self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT)
def test_get_profiles(self): """ Test getting profile queryset """ ProfileFactory() response = self.client.get(reverse('accounts:profiles-list')) self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertTrue(response.data)