<commit_before># -*- encoding: utf-8 -*-
import pytest

from django.core.urlresolvers import reverse

from block.models import BlockError
from block.tests.factories import (
    ImageCategoryFactory,
    ImageFactory,
)
from login.tests.factories import (
    TEST_PASSWORD,
    UserFactory,
)


@pytest.mark.django_db
def test_category_delete(client):
    user = UserFactory(is_staff=True)
    assert client.login(username=user.username, password=<PASSWORD>) is True
    category = ImageCategoryFactory()
    assert category.deleted is False
    # test
    url = reverse('block.image.category.delete', args=[category.pk])
    response = client.post(url)
    # check
    assert 302 == response.status_code
    expect = reverse('block.image.category.list')
    assert expect in response['Location']
    category.refresh_from_db()
    assert category.deleted is True


@pytest.mark.django_db
def test_category_delete_exception(client):
    """Should not delete a category which is in use."""
    user = UserFactory(is_staff=True)
    assert client.login(username=user.username, password=<PASSWORD>) is True
    category = ImageCategoryFactory()
    image = ImageFactory(category=category)
    assert category.deleted is False
    # test
    url = reverse('block.image.category.delete', args=[category.pk])
    with pytest.raises(BlockError) as e:
        client.post(url)
    assert 'Cannot delete an image category which is in use' in str(e.value)


@pytest.mark.django_db
def test_category_update(client):
    user = UserFactory(is_staff=True)
    assert client.login(username=user.username, password=<PASSWORD>) is True
    category = ImageCategoryFactory()
    url = reverse('block.image.category.update', args=[category.pk])
    data = {
        'name': 'Cricket',
    }
    response = client.post(url, data)
    # check
    assert 302 == response.status_code
    expect = reverse('block.image.category.list')
    assert expect in response['Location']
    category.refresh_from_db()
    assert 'Cricket' == category.name


@pytest.mark.django_db
def test_image_update(client):
    user = UserFactory(is_staff=True)
    assert client.login(username=user.username, password=<PASSWORD>) is True
    image = ImageFactory()
    url = reverse('block.image.update', args=[image.pk])
    data = {
        'title': 'Football',
    }
    response = client.post(url, data)
    # check
    assert 302 == response.status_code
    expect = reverse('block.image.list')
    assert expect in response['Location']
    image.refresh_from_db()
    assert 'Football' == image.title<commit_msg>image library - delete #1136<commit_after># -*- encoding: utf-8 -*-
import pytest

from django.core.urlresolvers import reverse

from block.models import (
    BlockError,
    Image,
)
from block.tests.factories import (
    ImageCategoryFactory,
    ImageFactory,
)
from login.tests.factories import (
    TEST_PASSWORD,
    UserFactory,
)


@pytest.mark.django_db
def test_category_delete(client):
    user = UserFactory(is_staff=True)
    assert client.login(username=user.username, password=<PASSWORD>) is True
    category = ImageCategoryFactory()
    assert category.deleted is False
    # test
    url = reverse('block.image.category.delete', args=[category.pk])
    response = client.post(url)
    # check
    assert 302 == response.status_code
    expect = reverse('block.image.category.list')
    assert expect in response['Location']
    category.refresh_from_db()
    assert category.deleted is True


@pytest.mark.django_db
def test_category_delete_exception(client):
    """Should not delete a category which is in use."""
    user = UserFactory(is_staff=True)
    assert client.login(username=user.username, password=<PASSWORD>) is True
    category = ImageCategoryFactory()
    image = ImageFactory(category=category)
    assert category.deleted is False
    # test
    url = reverse('block.image.category.delete', args=[category.pk])
    with pytest.raises(BlockError) as e:
        client.post(url)
    assert 'Cannot delete an image category which is in use' in str(e.value)


@pytest.mark.django_db
def test_category_update(client):
    user = UserFactory(is_staff=True)
    assert client.login(username=user.username, password=<PASSWORD>) is True
    category = ImageCategoryFactory()
    url = reverse('block.image.category.update', args=[category.pk])
    data = {
        'name': 'Cricket',
    }
    response = client.post(url, data)
    # check
    assert 302 == response.status_code
    expect = reverse('block.image.category.list')
    assert expect in response['Location']
    category.refresh_from_db()
    assert 'Cricket' == category.name


@pytest.mark.django_db
def test_image_delete(client):
    user = UserFactory(is_staff=True)
    assert client.login(username=user.username, password=<PASSWORD>) is True
    image_1 = ImageFactory()
    image_2 = ImageFactory()
    image_3 = ImageFactory()
    url = reverse('block.image.list.delete')
    data = {
        'images': [image_1.pk, image_3.pk],
    }
    response = client.post(url, data)
    # check
    assert 302 == response.status_code
    expect = reverse('block.image.list')
    assert expect in response['Location']
    result = [image.pk for image in Image.objects.images()]
    assert [image_2.pk] == result

@pytest.mark.django_db
def test_image_update(client):
    user = UserFactory(is_staff=True)
    assert client.login(username=user.username, password=<PASSWORD>) is True
    image = ImageFactory()
    url = reverse('block.image.update', args=[image.pk])
    data = {
        'title': 'Football',
    }
    response = client.post(url, data)
    # check
    assert 302 == response.status_code
    expect = reverse('block.image.list')
    assert expect in response['Location']
    image.refresh_from_db()
    assert 'Football' == image.title
{'fraction_non_alphanumeric': 0.11537138927097662, 'fraction_numerical': 0.005674002751031637, 'mean_word_length': 3.740831295843521, 'pattern_counts': {'":': 0, '<': 12, '<?xml version=': 0, '>': 12, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5613718', 'n_tokens_mistral': 1748, 'n_tokens_neox': 1732, 'n_words': 405}