Skip to content

Commit

Permalink
[testapp] added test for issue6
Browse files Browse the repository at this point in the history
  • Loading branch information
thomst committed Aug 2, 2022
1 parent eab8e2a commit 63208ea
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 2 deletions.
16 changes: 16 additions & 0 deletions tests/testapp/admin.py
Expand Up @@ -7,6 +7,7 @@
from .models import ModelB
from .models import ModelC
from .models import ModelD
from .models import Issue6Model


@admin.register(ModelA)
Expand Down Expand Up @@ -43,3 +44,18 @@ class ModelCAdmin(admin.ModelAdmin):
@admin.register(ModelD)
class ModelDAdmin(admin.ModelAdmin):
actions = [csvexport]


@admin.register(Issue6Model)
class Issue6ModelAdmin(admin.ModelAdmin):
actions = [csvexport]
csvexport_export_fields = [
'id',
'model_a.integer_field',
'model_b.integer_field',
]
csvexport_selected_fields = [
'id',
'model_a.integer_field',
'model_b.integer_field',
]
7 changes: 6 additions & 1 deletion tests/testapp/management/commands/testapp.py
Expand Up @@ -7,7 +7,7 @@
from django.contrib.auth.models import User
from django.db.utils import IntegrityError

from ...models import ModelA, ModelB, ModelC, ModelD
from ...models import Issue6Model, ModelA, ModelB, ModelC, ModelD


def create_test_data():
Expand Down Expand Up @@ -40,6 +40,11 @@ def create_test_data():
ma.model_b = mb
ma.model_c = mc
ma.save()
i6m = Issue6Model()
i6m.model_a = ma
i6m.model_b = mb
i6m.save()

mb.model_c = None
mb.save()

Expand Down
37 changes: 37 additions & 0 deletions tests/testapp/migrations/0003_auto_20220801_1850.py
@@ -0,0 +1,37 @@
# Generated by Django 2.2.10 on 2022-08-01 18:50

from django.db import migrations, models
import django.db.models.deletion


class Migration(migrations.Migration):

dependencies = [
('testapp', '0002_auto_20200831_1950'),
]

operations = [
migrations.AlterModelOptions(
name='modelb',
options={'verbose_name': 'ModelB'},
),
migrations.AlterModelOptions(
name='modelc',
options={'verbose_name': 'ModelC'},
),
migrations.AlterModelOptions(
name='modeld',
options={'verbose_name': 'ModelD'},
),
migrations.CreateModel(
name='Issue6Model',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('model_a', models.ForeignKey(null=True, on_delete=django.db.models.deletion.CASCADE, to='testapp.ModelA')),
('model_b', models.ForeignKey(null=True, on_delete=django.db.models.deletion.CASCADE, to='testapp.ModelB')),
],
options={
'verbose_name': 'Issue6Model',
},
),
]
8 changes: 8 additions & 0 deletions tests/testapp/models.py
Expand Up @@ -74,3 +74,11 @@ class ModelA(Base):

class Meta:
verbose_name = _('ModelA')


class Issue6Model(models.Model):
model_a = models.ForeignKey(ModelA, on_delete=models.CASCADE, null=True)
model_b = models.ForeignKey(ModelB, on_delete=models.CASCADE, null=True)

class Meta:
verbose_name = _('Issue6Model')
37 changes: 36 additions & 1 deletion tests/testapp/tests/test_export.py
Expand Up @@ -9,7 +9,7 @@
from csvexport.forms import CSVFieldsForm
from csvexport.forms import CSVFormatForm
from csvexport.forms import UniqueForm
from ..models import ModelA, ModelB, ModelC, ModelD
from ..models import Issue6Model, ModelA, ModelB, ModelC, ModelD
from ..models import UNICODE_STRING
from ..models import BYTE_STRING
from ..admin import ModelBAdmin
Expand Down Expand Up @@ -217,3 +217,38 @@ def test_07_uniq_result(self):
with AlterSettings(CSV_EXPORT_UNIQUE_FORM=True):
resp = self.client.post(self.url_a, post_data)
self.assertEqual(len(resp.content.splitlines()), 3)

def test_08_issue6(self):
# Check if both fields show up in the form.
url = reverse('admin:testapp_issue6model_changelist')
post_data = dict()
post_data['action'] = 'csvexport'
post_data['_selected_action'] = [1]
resp = self.client.post(url, post_data)
self.assertIn('value="model_a.integer_field"', resp.content.decode('utf8'))
self.assertIn('id="id_Issue6Model_ModelA_0"', resp.content.decode('utf8'))
self.assertIn('value="model_b.integer_field"', resp.content.decode('utf8'))
self.assertIn('id="id_Issue6Model_ModelB_0"', resp.content.decode('utf8'))

# Check if the value of both fields show up in the csv-view.
i6m = Issue6Model.objects.get(pk=1)
i6m.model_a.integer_field = 111
i6m.model_a.save()
i6m.model_b.integer_field = 222
i6m.model_b.save()
fields = {
'Issue6Model' : ['id'],
'Issue6Model_ModelA' : ['model_a.integer_field'],
'Issue6Model_ModelB' : ['model_b.integer_field'],
}
post_data = self.post_data.copy()
post_data.update(fields)
post_data['csvexport_view'] = 'View'
post_data['_selected_action'] = [1]
post_data.update(self.csv_format)
resp = self.client.post(url, post_data)
self.assertEqual(resp.status_code, 200)
self.assertIn("text/plain", resp.get('Content-Type'))
self.assertIn(b'111', resp.content)
self.assertIn(b'222', resp.content)

0 comments on commit 63208ea

Please sign in to comment.