<s> from __future__ import unicode_literals <EOL> import tablib <EOL> from copy import deepcopy <EOL> from datetime import date <EOL> from decimal import Decimal <EOL> from unittest import skip , skipUnless <EOL> from django . conf import settings <EOL> from django . contrib . auth . models import User <EOL> from django . db import IntegrityError <EOL> from django . db . models import Count <EOL> from django . db . models . fields import FieldDoesNotExist <EOL> from django . test import TestCase , TransactionTestCase , skipUnlessDBFeature <EOL> from django . utils . html import strip_tags <EOL> from import_export import fields , resources , results , widgets <EOL> from import_export . instance_loaders import ModelInstanceLoader <EOL> from . . models import ( <EOL> Author , Book , Category , Entry , Profile , WithDefault , WithDynamicDefault , <EOL> WithFloatField , <EOL> ) <EOL> try : <EOL> from django . utils . encoding import force_text <EOL> except ImportError : <EOL> from django . utils . encoding import force_unicode as force_text <EOL> class MyResource ( resources . Resource ) : <EOL> name = fields . Field ( ) <EOL> email = fields . Field ( ) <EOL> extra = fields . Field ( ) <EOL> class Meta : <EOL> export_order = ( '<STR_LIT:email>' , '<STR_LIT:name>' ) <EOL> class ResourceTestCase ( TestCase ) : <EOL> def setUp ( self ) : <EOL> self . my_resource = MyResource ( ) <EOL> def test_fields ( self ) : <EOL> fields = self . my_resource . fields <EOL> self . assertIn ( '<STR_LIT:name>' , fields ) <EOL> def test_field_column_name ( self ) : <EOL> field = self . my_resource . fields [ '<STR_LIT:name>' ] <EOL> self . assertIn ( field . column_name , '<STR_LIT:name>' ) <EOL> def test_meta ( self ) : <EOL> self . assertIsInstance ( self . my_resource . _meta , <EOL> resources . ResourceOptions ) <EOL> def test_get_export_order ( self ) : <EOL> self . assertEqual ( self . my_resource . get_export_headers ( ) , <EOL> [ '<STR_LIT:email>' , '<STR_LIT:name>' , '<STR_LIT>' ] ) <EOL> def test_inheritance ( self ) : <EOL> class A ( MyResource ) : <EOL> inherited = fields . Field ( ) <EOL> class Meta : <EOL> import_id_fields = ( '<STR_LIT:email>' , ) <EOL> class B ( A ) : <EOL> local = fields . Field ( ) <EOL> class Meta : <EOL> export_order = ( '<STR_LIT:email>' , '<STR_LIT>' ) <EOL> resource = B ( ) <EOL> self . assertIn ( '<STR_LIT:name>' , resource . fields ) <EOL> self . assertIn ( '<STR_LIT>' , resource . fields ) <EOL> self . assertIn ( '<STR_LIT>' , resource . fields ) <EOL> self . assertEqual ( resource . get_export_headers ( ) , <EOL> [ '<STR_LIT:email>' , '<STR_LIT>' , '<STR_LIT:name>' , '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> self . assertEqual ( resource . _meta . import_id_fields , ( '<STR_LIT:email>' , ) ) <EOL> def test_inheritance_with_custom_attributes ( self ) : <EOL> class A ( MyResource ) : <EOL> inherited = fields . Field ( ) <EOL> class Meta : <EOL> import_id_fields = ( '<STR_LIT:email>' , ) <EOL> custom_attribute = True <EOL> class B ( A ) : <EOL> local = fields . Field ( ) <EOL> resource = B ( ) <EOL> self . assertEqual ( resource . _meta . custom_attribute , True ) <EOL> class AuthorResource ( resources . ModelResource ) : <EOL> books = fields . Field ( <EOL> column_name = '<STR_LIT>' , <EOL> attribute = '<STR_LIT>' , <EOL> readonly = True , <EOL> ) <EOL> class Meta : <EOL> model = Author <EOL> export_order = ( '<STR_LIT:name>' , '<STR_LIT>' ) <EOL> class BookResource ( resources . ModelResource ) : <EOL> published = fields . Field ( column_name = '<STR_LIT>' ) <EOL> class Meta : <EOL> model = Book <EOL> exclude = ( '<STR_LIT>' , ) <EOL> class ProfileResource ( resources . ModelResource ) : <EOL> class Meta : <EOL> model = Profile <EOL> exclude = ( '<STR_LIT:user>' , ) <EOL> class WithDefaultResource ( resources . ModelResource ) : <EOL> class Meta : <EOL> model = WithDefault <EOL> fields = ( '<STR_LIT:name>' , ) <EOL> class ModelResourceTest ( TestCase ) : <EOL> def setUp ( self ) : <EOL> self . resource = BookResource ( ) <EOL> self . book = Book . objects . create ( name = "<STR_LIT>" ) <EOL> self . dataset = tablib . Dataset ( headers = [ '<STR_LIT:id>' , '<STR_LIT:name>' , '<STR_LIT>' , <EOL> '<STR_LIT>' ] ) <EOL> row = [ self . book . pk , '<STR_LIT>' , '<STR_LIT>' , "<STR_LIT>" ] <EOL> self . dataset . append ( row ) <EOL> def test_default_instance_loader_class ( self ) : <EOL> self . assertIs ( self . resource . _meta . instance_loader_class , <EOL> ModelInstanceLoader ) <EOL> def test_fields ( self ) : <EOL> fields = self . resource . fields <EOL> self . assertIn ( '<STR_LIT:id>' , fields ) <EOL> self . assertIn ( '<STR_LIT:name>' , fields ) <EOL> self . assertIn ( '<STR_LIT>' , fields ) <EOL> self . assertIn ( '<STR_LIT>' , fields ) <EOL> def test_fields_foreign_key ( self ) : <EOL> fields = self . resource . fields <EOL> self . assertIn ( '<STR_LIT>' , fields ) <EOL> widget = fields [ '<STR_LIT>' ] . widget <EOL> self . assertIsInstance ( widget , widgets . ForeignKeyWidget ) <EOL> self . assertEqual ( widget . model , Author ) <EOL> def test_fields_m2m ( self ) : <EOL> fields = self . resource . fields <EOL> self . assertIn ( '<STR_LIT>' , fields ) <EOL> def test_excluded_fields ( self ) : <EOL> self . assertNotIn ( '<STR_LIT>' , self . resource . fields ) <EOL> def test_init_instance ( self ) : <EOL> instance = self . resource . init_instance ( ) <EOL> self . assertIsInstance ( instance , Book ) <EOL> def test_default ( self ) : <EOL> self . assertEquals ( WithDefaultResource . fields [ '<STR_LIT:name>' ] . clean ( { '<STR_LIT:name>' : '<STR_LIT>' } ) , '<STR_LIT>' ) <EOL> def test_get_instance ( self ) : <EOL> instance_loader = self . resource . _meta . instance_loader_class ( <EOL> self . resource ) <EOL> self . resource . _meta . import_id_fields = [ '<STR_LIT:id>' ] <EOL> instance = self . resource . get_instance ( instance_loader , <EOL> self . dataset . dict [ <NUM_LIT:0> ] ) <EOL> self . assertEqual ( instance , self . book ) <EOL> def test_get_instance_import_id_fields ( self ) : <EOL> class BookResource ( resources . ModelResource ) : <EOL> name = fields . Field ( attribute = '<STR_LIT:name>' , widget = widgets . CharWidget ( ) ) <EOL> class Meta : <EOL> model = Book <EOL> import_id_fields = [ '<STR_LIT:name>' ] <EOL> resource = BookResource ( ) <EOL> instance_loader = resource . _meta . instance_loader_class ( resource ) <EOL> instance = resource . get_instance ( instance_loader , self . dataset . dict [ <NUM_LIT:0> ] ) <EOL> self . assertEqual ( instance , self . book ) <EOL> def test_get_instance_with_missing_field_data ( self ) : <EOL> instance_loader = self . resource . _meta . instance_loader_class ( <EOL> self . resource ) <EOL> dataset = tablib . Dataset ( headers = [ '<STR_LIT:name>' , '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> dataset . append ( [ '<STR_LIT>' , '<STR_LIT>' , "<STR_LIT>" ] ) <EOL> with self . assertRaises ( KeyError ) as cm : <EOL> self . resource . get_instance ( instance_loader , dataset . dict [ <NUM_LIT:0> ] ) <EOL> self . assertEqual ( u"<STR_LIT>" <EOL> "<STR_LIT>" % [ u'<STR_LIT:name>' , u'<STR_LIT>' , u'<STR_LIT>' ] , <EOL> cm . exception . args [ <NUM_LIT:0> ] ) <EOL> def test_get_export_headers ( self ) : <EOL> headers = self . resource . get_export_headers ( ) <EOL> self . assertEqual ( headers , [ '<STR_LIT>' , '<STR_LIT:id>' , '<STR_LIT:name>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , ] ) <EOL> def test_export ( self ) : <EOL> dataset = self . resource . export ( Book . objects . all ( ) ) <EOL> self . assertEqual ( len ( dataset ) , <NUM_LIT:1> ) <EOL> def test_export_iterable ( self ) : <EOL> dataset = self . resource . export ( list ( Book . objects . all ( ) ) ) <EOL> self . assertEqual ( len ( dataset ) , <NUM_LIT:1> ) <EOL> def test_get_diff ( self ) : <EOL> book2 = Book ( name = "<STR_LIT>" ) <EOL> diff = self . resource . get_diff ( self . book , book2 ) <EOL> headers = self . resource . get_export_headers ( ) <EOL> self . assertEqual ( diff [ headers . index ( '<STR_LIT:name>' ) ] , <EOL> u'<STR_LIT>' <EOL> u'<STR_LIT>' ) <EOL> self . assertFalse ( diff [ headers . index ( '<STR_LIT>' ) ] ) <EOL> @ skip ( "<STR_LIT>" ) <EOL> def test_get_diff_with_callable_related_manager ( self ) : <EOL> resource = AuthorResource ( ) <EOL> author = Author ( name = "<STR_LIT>" ) <EOL> author . save ( ) <EOL> author2 = Author ( name = "<STR_LIT>" ) <EOL> self . book . author = author <EOL> self . book . save ( ) <EOL> diff = resource . get_diff ( author2 , author ) <EOL> headers = resource . get_export_headers ( ) <EOL> self . assertEqual ( diff [ headers . index ( '<STR_LIT>' ) ] , <EOL> '<STR_LIT>' ) <EOL> def test_import_data ( self ) : <EOL> result = self . resource . import_data ( self . dataset , raise_errors = True ) <EOL> self . assertFalse ( result . has_errors ( ) ) <EOL> self . assertEqual ( len ( result . rows ) , <NUM_LIT:1> ) <EOL> self . assertTrue ( result . rows [ <NUM_LIT:0> ] . diff ) <EOL> self . assertEqual ( result . rows [ <NUM_LIT:0> ] . import_type , <EOL> results . RowResult . IMPORT_TYPE_UPDATE ) <EOL> instance = Book . objects . get ( pk = self . book . pk ) <EOL> self . assertEqual ( instance . author_email , '<STR_LIT>' ) <EOL> self . assertEqual ( instance . price , Decimal ( "<STR_LIT>" ) ) <EOL> def test_import_data_value_error_includes_field_name ( self ) : <EOL> class AuthorResource ( resources . ModelResource ) : <EOL> class Meta : <EOL> model = Author <EOL> resource = AuthorResource ( ) <EOL> dataset = tablib . Dataset ( headers = [ '<STR_LIT:id>' , '<STR_LIT:name>' , '<STR_LIT>' ] ) <EOL> dataset . append ( [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> result = resource . import_data ( dataset , raise_errors = False ) <EOL> self . assertTrue ( result . has_errors ( ) ) <EOL> self . assertTrue ( result . rows [ <NUM_LIT:0> ] . errors ) <EOL> msg = ( "<STR_LIT>" ) <EOL> actual = result . rows [ <NUM_LIT:0> ] . errors [ <NUM_LIT:0> ] . error <EOL> self . assertIsInstance ( actual , ValueError ) <EOL> self . assertEqual ( msg , str ( actual ) ) <EOL> def test_import_data_error_saving_model ( self ) : <EOL> row = list ( self . dataset . pop ( ) ) <EOL> row [ <NUM_LIT:0> ] = '<STR_LIT:foo>' <EOL> self . dataset . append ( row ) <EOL> result = self . resource . import_data ( self . dataset , raise_errors = False ) <EOL> self . assertTrue ( result . has_errors ( ) ) <EOL> self . assertTrue ( result . rows [ <NUM_LIT:0> ] . errors ) <EOL> actual = result . rows [ <NUM_LIT:0> ] . errors [ <NUM_LIT:0> ] . error <EOL> self . assertIsInstance ( actual , ValueError ) <EOL> self . assertIn ( "<STR_LIT>" , <EOL> str ( actual ) ) <EOL> def test_import_data_delete ( self ) : <EOL> class B ( BookResource ) : <EOL> delete = fields . Field ( widget = widgets . BooleanWidget ( ) ) <EOL> def for_delete ( self , row , instance ) : <EOL> return self . fields [ '<STR_LIT>' ] . clean ( row ) <EOL> row = [ self . book . pk , self . book . name , '<STR_LIT:1>' ] <EOL> dataset = tablib . Dataset ( * [ row ] , headers = [ '<STR_LIT:id>' , '<STR_LIT:name>' , '<STR_LIT>' ] ) <EOL> result = B ( ) . import_data ( dataset , raise_errors = True ) <EOL> self . assertFalse ( result . has_errors ( ) ) <EOL> self . assertEqual ( result . rows [ <NUM_LIT:0> ] . import_type , <EOL> results . RowResult . IMPORT_TYPE_DELETE ) <EOL> self . assertFalse ( Book . objects . filter ( pk = self . book . pk ) ) <EOL> def test_relationships_fields ( self ) : <EOL> class B ( resources . ModelResource ) : <EOL> class Meta : <EOL> model = Book <EOL> fields = ( '<STR_LIT>' , ) <EOL> author = Author . objects . create ( name = "<STR_LIT>" ) <EOL> self . book . author = author <EOL> resource = B ( ) <EOL> result = resource . fields [ '<STR_LIT>' ] . export ( self . book ) <EOL> self . assertEqual ( result , author . name ) <EOL> def test_dehydrating_fields ( self ) : <EOL> class B ( resources . ModelResource ) : <EOL> full_title = fields . Field ( column_name = "<STR_LIT>" ) <EOL> class Meta : <EOL> model = Book <EOL> fields = ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> def dehydrate_full_title ( self , obj ) : <EOL> return '<STR_LIT>' % ( obj . name , obj . author . name ) <EOL> author = Author . objects . create ( name = "<STR_LIT>" ) <EOL> self . book . author = author <EOL> resource = B ( ) <EOL> full_title = resource . export_field ( resource . get_fields ( ) [ <NUM_LIT:0> ] , self . book ) <EOL> self . assertEqual ( full_title , '<STR_LIT>' % ( self . book . name , <EOL> self . book . author . name ) ) <EOL> def test_widget_fomat_in_fk_field ( self ) : <EOL> class B ( resources . ModelResource ) : <EOL> class Meta : <EOL> model = Book <EOL> fields = ( '<STR_LIT>' , ) <EOL> widgets = { <EOL> '<STR_LIT>' : { '<STR_LIT>' : '<STR_LIT>' } , <EOL> } <EOL> author = Author . objects . create ( name = "<STR_LIT>" ) <EOL> self . book . author = author <EOL> resource = B ( ) <EOL> result = resource . fields [ '<STR_LIT>' ] . export ( self . book ) <EOL> self . assertEqual ( result , str ( date . today ( ) ) ) <EOL> def test_widget_kwargs_for_field ( self ) : <EOL> class B ( resources . ModelResource ) : <EOL> class Meta : <EOL> model = Book <EOL> fields = ( '<STR_LIT>' , ) <EOL> widgets = { <EOL> '<STR_LIT>' : { '<STR_LIT>' : '<STR_LIT>' } , <EOL> } <EOL> resource = B ( ) <EOL> self . book . published = date ( <NUM_LIT> , <NUM_LIT:8> , <NUM_LIT> ) <EOL> result = resource . fields [ '<STR_LIT>' ] . export ( self . book ) <EOL> self . assertEqual ( result , "<STR_LIT>" ) <EOL> def test_foreign_keys_export ( self ) : <EOL> author1 = Author . objects . create ( name = '<STR_LIT>' ) <EOL> self . book . author = author1 <EOL> self . book . save ( ) <EOL> dataset = self . resource . export ( Book . objects . all ( ) ) <EOL> self . assertEqual ( dataset . dict [ <NUM_LIT:0> ] [ '<STR_LIT>' ] , author1 . pk ) <EOL> def test_foreign_keys_import ( self ) : <EOL> author2 = Author . objects . create ( name = '<STR_LIT>' ) <EOL> headers = [ '<STR_LIT:id>' , '<STR_LIT:name>' , '<STR_LIT>' ] <EOL> row = [ None , '<STR_LIT>' , author2 . pk ] <EOL> dataset = tablib . Dataset ( row , headers = headers ) <EOL> self . resource . import_data ( dataset , raise_errors = True ) <EOL> book = Book . objects . get ( name = '<STR_LIT>' ) <EOL> self . assertEqual ( book . author , author2 ) <EOL> def test_m2m_export ( self ) : <EOL> cat1 = Category . objects . create ( name = '<STR_LIT>' ) <EOL> cat2 = Category . objects . create ( name = '<STR_LIT>' ) <EOL> self . book . categories . add ( cat1 ) <EOL> self . book . categories . add ( cat2 ) <EOL> dataset = self . resource . export ( Book . objects . all ( ) ) <EOL> self . assertEqual ( dataset . dict [ <NUM_LIT:0> ] [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' % ( cat1 . pk , cat2 . pk ) ) <EOL> def test_m2m_import ( self ) : <EOL> cat1 = Category . objects . create ( name = '<STR_LIT>' ) <EOL> headers = [ '<STR_LIT:id>' , '<STR_LIT:name>' , '<STR_LIT>' ] <EOL> row = [ None , '<STR_LIT>' , "<STR_LIT:%s>" % cat1 . pk ] <EOL> dataset = tablib . Dataset ( row , headers = headers ) <EOL> self . resource . import_data ( dataset , raise_errors = True ) <EOL> book = Book . objects . get ( name = '<STR_LIT>' ) <EOL> self . assertIn ( cat1 , book . categories . all ( ) ) <EOL> def test_m2m_options_import ( self ) : <EOL> cat1 = Category . objects . create ( name = '<STR_LIT>' ) <EOL> cat2 = Category . objects . create ( name = '<STR_LIT>' ) <EOL> headers = [ '<STR_LIT:id>' , '<STR_LIT:name>' , '<STR_LIT>' ] <EOL> row = [ None , '<STR_LIT>' , "<STR_LIT>" ] <EOL> dataset = tablib . Dataset ( row , headers = headers ) <EOL> class BookM2MResource ( resources . ModelResource ) : <EOL> categories = fields . Field ( <EOL> attribute = '<STR_LIT>' , <EOL> widget = widgets . ManyToManyWidget ( Category , field = '<STR_LIT:name>' , <EOL> separator = '<STR_LIT:|>' ) <EOL> ) <EOL> class Meta : <EOL> model = Book <EOL> resource = BookM2MResource ( ) <EOL> resource . import_data ( dataset , raise_errors = True ) <EOL> book = Book . objects . get ( name = '<STR_LIT>' ) <EOL> self . assertIn ( cat1 , book . categories . all ( ) ) <EOL> self . assertIn ( cat2 , book . categories . all ( ) ) <EOL> def test_related_one_to_one ( self ) : <EOL> user = User . objects . create ( username = '<STR_LIT:foo>' ) <EOL> profile = Profile . objects . create ( user = user ) <EOL> Entry . objects . create ( user = user ) <EOL> Entry . objects . create ( user = User . objects . create ( username = '<STR_LIT:bar>' ) ) <EOL> class EntryResource ( resources . ModelResource ) : <EOL> class Meta : <EOL> model = Entry <EOL> fields = ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> resource = EntryResource ( ) <EOL> dataset = resource . export ( Entry . objects . all ( ) ) <EOL> self . assertEqual ( dataset . dict [ <NUM_LIT:0> ] [ '<STR_LIT>' ] , profile . pk ) <EOL> self . assertEqual ( dataset . dict [ <NUM_LIT:0> ] [ '<STR_LIT>' ] , '<STR_LIT:1>' ) <EOL> self . assertEqual ( dataset . dict [ <NUM_LIT:1> ] [ '<STR_LIT>' ] , '<STR_LIT>' ) <EOL> self . assertEqual ( dataset . dict [ <NUM_LIT:1> ] [ '<STR_LIT>' ] , '<STR_LIT>' ) <EOL> def test_empty_get_queryset ( self ) : <EOL> dataset = self . resource . export ( Book . objects . none ( ) ) <EOL> self . assertEqual ( len ( dataset ) , <NUM_LIT:0> ) <EOL> def test_import_data_skip_unchanged ( self ) : <EOL> def attempted_save ( instance , real_dry_run ) : <EOL> self . fail ( '<STR_LIT>' ) <EOL> cat1 = Category . objects . create ( name = '<STR_LIT>' ) <EOL> cat2 = Category . objects . create ( name = '<STR_LIT>' ) <EOL> self . book . categories . add ( cat1 ) <EOL> self . book . categories . add ( cat2 ) <EOL> dataset = self . resource . export ( ) <EOL> resource = deepcopy ( self . resource ) <EOL> resource . _meta . skip_unchanged = True <EOL> resource . save_instance = attempted_save <EOL> result = resource . import_data ( dataset , raise_errors = True ) <EOL> self . assertFalse ( result . has_errors ( ) ) <EOL> self . assertEqual ( len ( result . rows ) , len ( dataset ) ) <EOL> self . assertTrue ( result . rows [ <NUM_LIT:0> ] . diff ) <EOL> self . assertEqual ( result . rows [ <NUM_LIT:0> ] . import_type , <EOL> results . RowResult . IMPORT_TYPE_SKIP ) <EOL> resource . _meta . report_skipped = False <EOL> result = resource . import_data ( dataset , raise_errors = True ) <EOL> self . assertFalse ( result . has_errors ( ) ) <EOL> self . assertEqual ( len ( result . rows ) , <NUM_LIT:0> ) <EOL> def test_before_import_access_to_kwargs ( self ) : <EOL> class B ( BookResource ) : <EOL> def before_import ( self , dataset , dry_run , ** kwargs ) : <EOL> if '<STR_LIT>' in kwargs : <EOL> dataset . headers [ dataset . headers . index ( '<STR_LIT>' ) ] = '<STR_LIT>' <EOL> dataset . insert_col ( <NUM_LIT:0> , <EOL> lambda row : kwargs [ '<STR_LIT>' ] , <EOL> header = '<STR_LIT>' ) <EOL> resource = B ( ) <EOL> result = resource . import_data ( self . dataset , raise_errors = True , <EOL> extra_arg = '<STR_LIT>' ) <EOL> self . assertFalse ( result . has_errors ( ) ) <EOL> self . assertEqual ( len ( result . rows ) , <NUM_LIT:1> ) <EOL> instance = Book . objects . get ( pk = self . book . pk ) <EOL> self . assertEqual ( instance . author_email , '<STR_LIT>' ) <EOL> def test_link_to_nonexistent_field ( self ) : <EOL> with self . assertRaises ( FieldDoesNotExist ) as cm : <EOL> class BrokenBook1 ( resources . ModelResource ) : <EOL> class Meta : <EOL> model = Book <EOL> fields = ( '<STR_LIT>' , ) <EOL> self . assertEqual ( "<STR_LIT>" , <EOL> cm . exception . args [ <NUM_LIT:0> ] ) <EOL> with self . assertRaises ( FieldDoesNotExist ) as cm : <EOL> class BrokenBook2 ( resources . ModelResource ) : <EOL> class Meta : <EOL> model = Book <EOL> fields = ( '<STR_LIT>' , ) <EOL> self . assertEqual ( "<STR_LIT>" <EOL> "<STR_LIT>" , cm . exception . args [ <NUM_LIT:0> ] ) <EOL> def test_link_to_nonrelation_field ( self ) : <EOL> with self . assertRaises ( KeyError ) as cm : <EOL> class BrokenBook1 ( resources . ModelResource ) : <EOL> class Meta : <EOL> model = Book <EOL> fields = ( '<STR_LIT>' , ) <EOL> self . assertEqual ( "<STR_LIT>" , <EOL> cm . exception . args [ <NUM_LIT:0> ] ) <EOL> with self . assertRaises ( KeyError ) as cm : <EOL> class BrokenBook2 ( resources . ModelResource ) : <EOL> class Meta : <EOL> model = Book <EOL> fields = ( '<STR_LIT>' , ) <EOL> self . assertEqual ( "<STR_LIT>" , <EOL> cm . exception . args [ <NUM_LIT:0> ] ) <EOL> def test_override_field_construction_in_resource ( self ) : <EOL> class B ( resources . ModelResource ) : <EOL> class Meta : <EOL> model = Book <EOL> fields = ( '<STR_LIT>' , ) <EOL> @ classmethod <EOL> def field_from_django_field ( self , field_name , django_field , <EOL> readonly ) : <EOL> if field_name == '<STR_LIT>' : <EOL> return { '<STR_LIT>' : '<STR_LIT>' } <EOL> B ( ) <EOL> self . assertEqual ( { '<STR_LIT>' : '<STR_LIT>' } , B . fields [ '<STR_LIT>' ] ) <EOL> def test_readonly_annotated_field_import_and_export ( self ) : <EOL> class B ( BookResource ) : <EOL> total_categories = fields . Field ( '<STR_LIT>' , readonly = True ) <EOL> class Meta : <EOL> model = Book <EOL> skip_unchanged = True <EOL> cat1 = Category . objects . create ( name = '<STR_LIT>' ) <EOL> self . book . categories . add ( cat1 ) <EOL> resource = B ( ) <EOL> dataset = resource . export ( <EOL> Book . objects . annotate ( total_categories = Count ( '<STR_LIT>' ) ) ) <EOL> self . assertEqual ( int ( dataset . dict [ <NUM_LIT:0> ] [ '<STR_LIT>' ] ) , <NUM_LIT:1> ) <EOL> result = resource . import_data ( dataset , raise_errors = True ) <EOL> self . assertFalse ( result . has_errors ( ) ) <EOL> self . assertEqual ( len ( result . rows ) , len ( dataset ) ) <EOL> self . assertEqual ( <EOL> result . rows [ <NUM_LIT:0> ] . import_type , results . RowResult . IMPORT_TYPE_SKIP ) <EOL> def test_follow_relationship_for_modelresource ( self ) : <EOL> class EntryResource ( resources . ModelResource ) : <EOL> username = fields . Field ( attribute = '<STR_LIT>' , readonly = False ) <EOL> class Meta : <EOL> model = Entry <EOL> fields = ( '<STR_LIT:id>' , ) <EOL> def after_save_instance ( self , instance , dry_run ) : <EOL> if not dry_run : <EOL> instance . user . save ( ) <EOL> user = User . objects . create ( username = '<STR_LIT:foo>' ) <EOL> entry = Entry . objects . create ( user = user ) <EOL> row = [ <EOL> entry . pk , <EOL> '<STR_LIT:bar>' , <EOL> ] <EOL> self . dataset = tablib . Dataset ( headers = [ '<STR_LIT:id>' , '<STR_LIT:username>' ] ) <EOL> self . dataset . append ( row ) <EOL> result = EntryResource ( ) . import_data ( <EOL> self . dataset , raise_errors = True , dry_run = False ) <EOL> self . assertFalse ( result . has_errors ( ) ) <EOL> self . assertEquals ( User . objects . get ( pk = user . pk ) . username , '<STR_LIT:bar>' ) <EOL> def test_import_data_dynamic_default_callable ( self ) : <EOL> class DynamicDefaultResource ( resources . ModelResource ) : <EOL> class Meta : <EOL> model = WithDynamicDefault <EOL> fields = ( '<STR_LIT:id>' , '<STR_LIT:name>' , ) <EOL> self . assertTrue ( callable ( DynamicDefaultResource . fields [ '<STR_LIT:name>' ] . default ) ) <EOL> resource = DynamicDefaultResource ( ) <EOL> dataset = tablib . Dataset ( headers = [ '<STR_LIT:id>' , '<STR_LIT:name>' , ] ) <EOL> dataset . append ( [ <NUM_LIT:1> , None ] ) <EOL> dataset . append ( [ <NUM_LIT:2> , None ] ) <EOL> resource . import_data ( dataset , raise_errors = False ) <EOL> objs = WithDynamicDefault . objects . all ( ) <EOL> self . assertNotEqual ( objs [ <NUM_LIT:0> ] . name , objs [ <NUM_LIT:1> ] . name ) <EOL> def test_float_field ( self ) : <EOL> class R ( resources . ModelResource ) : <EOL> class Meta : <EOL> model = WithFloatField <EOL> resource = R ( ) <EOL> dataset = tablib . Dataset ( headers = [ '<STR_LIT:id>' , '<STR_LIT:f>' , ] ) <EOL> dataset . append ( [ None , None ] ) <EOL> dataset . append ( [ None , '<STR_LIT>' ] ) <EOL> resource . import_data ( dataset , raise_errors = True ) <EOL> self . assertEqual ( WithFloatField . objects . all ( ) [ <NUM_LIT:0> ] . f , None ) <EOL> self . assertEqual ( WithFloatField . objects . all ( ) [ <NUM_LIT:1> ] . f , None ) <EOL> class ModelResourceTransactionTest ( TransactionTestCase ) : <EOL> @ skipUnlessDBFeature ( '<STR_LIT>' ) <EOL> def test_m2m_import_with_transactions ( self ) : <EOL> resource = BookResource ( ) <EOL> cat1 = Category . objects . create ( name = '<STR_LIT>' ) <EOL> headers = [ '<STR_LIT:id>' , '<STR_LIT:name>' , '<STR_LIT>' ] <EOL> row = [ None , '<STR_LIT>' , "<STR_LIT:%s>" % cat1 . pk ] <EOL> dataset = tablib . Dataset ( row , headers = headers ) <EOL> result = resource . import_data ( <EOL> dataset , dry_run = True , use_transactions = True <EOL> ) <EOL> row_diff = result . rows [ <NUM_LIT:0> ] . diff <EOL> fields = resource . get_fields ( ) <EOL> id_field = resource . fields [ '<STR_LIT:id>' ] <EOL> id_diff = row_diff [ fields . index ( id_field ) ] <EOL> self . assertTrue ( id_diff ) <EOL> category_field = resource . fields [ '<STR_LIT>' ] <EOL> categories_diff = row_diff [ fields . index ( category_field ) ] <EOL> self . assertEqual ( strip_tags ( categories_diff ) , force_text ( cat1 . pk ) ) <EOL> self . assertFalse ( Book . objects . filter ( name = '<STR_LIT>' ) ) <EOL> @ skipUnlessDBFeature ( '<STR_LIT>' ) <EOL> def test_m2m_import_with_transactions_error ( self ) : <EOL> resource = ProfileResource ( ) <EOL> headers = [ '<STR_LIT:id>' , '<STR_LIT:user>' ] <EOL> row = [ None , None ] <EOL> dataset = tablib . Dataset ( row , headers = headers ) <EOL> result = resource . import_data ( <EOL> dataset , dry_run = True , use_transactions = True <EOL> ) <EOL> self . assertTrue ( result . has_errors ( ) ) <EOL> self . assertEqual ( Profile . objects . count ( ) , <NUM_LIT:0> ) <EOL> class ModelResourceFactoryTest ( TestCase ) : <EOL> def test_create ( self ) : <EOL> BookResource = resources . modelresource_factory ( Book ) <EOL> self . assertIn ( '<STR_LIT:id>' , BookResource . fields ) <EOL> self . assertEqual ( BookResource . _meta . model , Book ) <EOL> @ skipUnless ( <EOL> '<STR_LIT>' in settings . DATABASES [ '<STR_LIT:default>' ] [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' ) <EOL> class PostgresTests ( TransactionTestCase ) : <EOL> reset_sequences = True <EOL> def test_create_object_after_importing_dataset_with_id ( self ) : <EOL> dataset = tablib . Dataset ( headers = [ '<STR_LIT:id>' , '<STR_LIT:name>' ] ) <EOL> dataset . append ( [ <NUM_LIT:1> , '<STR_LIT>' ] ) <EOL> resource = BookResource ( ) <EOL> result = resource . import_data ( dataset ) <EOL> self . assertFalse ( result . has_errors ( ) ) <EOL> try : <EOL> Book . objects . create ( name = '<STR_LIT>' ) <EOL> except IntegrityError : <EOL> self . fail ( '<STR_LIT>' ) </s>
