<s> import os <EOL> from datetime import date <EOL> from . models import MockObject , ExtendedMockObject <EOL> from postgres_copy import CopyMapping <EOL> from django . test import TestCase <EOL> class PostgresCopyTest ( TestCase ) : <EOL> def setUp ( self ) : <EOL> self . data_dir = os . path . join ( os . path . dirname ( __file__ ) , '<STR_LIT:data>' ) <EOL> self . name_path = os . path . join ( self . data_dir , '<STR_LIT>' ) <EOL> self . foreign_path = os . path . join ( self . data_dir , '<STR_LIT>' ) <EOL> self . pipe_path = os . path . join ( self . data_dir , '<STR_LIT>' ) <EOL> self . null_path = os . path . join ( self . data_dir , '<STR_LIT>' ) <EOL> self . backwards_path = os . path . join ( self . data_dir , '<STR_LIT>' ) <EOL> def tearDown ( self ) : <EOL> MockObject . objects . all ( ) . delete ( ) <EOL> ExtendedMockObject . objects . all ( ) . delete ( ) <EOL> def test_bad_call ( self ) : <EOL> with self . assertRaises ( TypeError ) : <EOL> CopyMapping ( ) <EOL> def test_bad_csv ( self ) : <EOL> with self . assertRaises ( ValueError ) : <EOL> CopyMapping ( <EOL> MockObject , <EOL> '<STR_LIT>' , <EOL> dict ( name = '<STR_LIT>' , number = '<STR_LIT>' , dt = '<STR_LIT>' ) , <EOL> using = '<STR_LIT>' <EOL> ) <EOL> def test_bad_backend ( self ) : <EOL> with self . assertRaises ( TypeError ) : <EOL> CopyMapping ( <EOL> MockObject , <EOL> self . name_path , <EOL> dict ( name = '<STR_LIT>' , number = '<STR_LIT>' , dt = '<STR_LIT>' ) , <EOL> using = '<STR_LIT>' <EOL> ) <EOL> def test_bad_header ( self ) : <EOL> with self . assertRaises ( ValueError ) : <EOL> CopyMapping ( <EOL> MockObject , <EOL> self . name_path , <EOL> dict ( name = '<STR_LIT>' , number = '<STR_LIT>' , dt = '<STR_LIT>' ) , <EOL> ) <EOL> def test_bad_field ( self ) : <EOL> with self . assertRaises ( ValueError ) : <EOL> CopyMapping ( <EOL> MockObject , <EOL> self . name_path , <EOL> dict ( name1 = '<STR_LIT>' , number = '<STR_LIT>' , dt = '<STR_LIT>' ) , <EOL> ) <EOL> def test_simple_save ( self ) : <EOL> c = CopyMapping ( <EOL> MockObject , <EOL> self . name_path , <EOL> dict ( name = '<STR_LIT>' , number = '<STR_LIT>' , dt = '<STR_LIT>' ) , <EOL> ) <EOL> c . save ( ) <EOL> self . assertEqual ( MockObject . objects . count ( ) , <NUM_LIT:3> ) <EOL> self . assertEqual ( MockObject . objects . get ( name = '<STR_LIT>' ) . number , <NUM_LIT:1> ) <EOL> self . assertEqual ( <EOL> MockObject . objects . get ( name = '<STR_LIT>' ) . dt , <EOL> date ( <NUM_LIT> , <NUM_LIT:1> , <NUM_LIT:1> ) <EOL> ) <EOL> def test_save_foreign_key ( self ) : <EOL> c = CopyMapping ( <EOL> MockObject , <EOL> self . foreign_path , <EOL> dict ( name = '<STR_LIT>' , number = '<STR_LIT>' , dt = '<STR_LIT>' , parent = '<STR_LIT>' ) <EOL> ) <EOL> c . save ( ) <EOL> self . assertEqual ( MockObject . objects . count ( ) , <NUM_LIT:3> ) <EOL> self . assertEqual ( MockObject . objects . get ( name = '<STR_LIT>' ) . parent_id , <NUM_LIT:4> ) <EOL> self . assertEqual ( <EOL> MockObject . objects . get ( name = '<STR_LIT>' ) . dt , <EOL> date ( <NUM_LIT> , <NUM_LIT:1> , <NUM_LIT:1> ) <EOL> ) <EOL> def test_silent_save ( self ) : <EOL> c = CopyMapping ( <EOL> MockObject , <EOL> self . name_path , <EOL> dict ( name = '<STR_LIT>' , number = '<STR_LIT>' , dt = '<STR_LIT>' ) , <EOL> ) <EOL> c . save ( silent = True ) <EOL> self . assertEqual ( MockObject . objects . count ( ) , <NUM_LIT:3> ) <EOL> self . assertEqual ( MockObject . objects . get ( name = '<STR_LIT>' ) . number , <NUM_LIT:1> ) <EOL> self . assertEqual ( <EOL> MockObject . objects . get ( name = '<STR_LIT>' ) . dt , <EOL> date ( <NUM_LIT> , <NUM_LIT:1> , <NUM_LIT:1> ) <EOL> ) <EOL> def test_pipe_save ( self ) : <EOL> c = CopyMapping ( <EOL> MockObject , <EOL> self . pipe_path , <EOL> dict ( name = '<STR_LIT>' , number = '<STR_LIT>' , dt = '<STR_LIT>' ) , <EOL> delimiter = "<STR_LIT:|>" , <EOL> ) <EOL> c . save ( ) <EOL> self . assertEqual ( MockObject . objects . count ( ) , <NUM_LIT:3> ) <EOL> self . assertEqual ( MockObject . objects . get ( name = '<STR_LIT>' ) . number , <NUM_LIT:1> ) <EOL> self . assertEqual ( <EOL> MockObject . objects . get ( name = '<STR_LIT>' ) . dt , <EOL> date ( <NUM_LIT> , <NUM_LIT:1> , <NUM_LIT:1> ) <EOL> ) <EOL> def test_null_save ( self ) : <EOL> c = CopyMapping ( <EOL> MockObject , <EOL> self . null_path , <EOL> dict ( name = '<STR_LIT>' , number = '<STR_LIT>' , dt = '<STR_LIT>' ) , <EOL> null = '<STR_LIT>' , <EOL> ) <EOL> c . save ( ) <EOL> self . assertEqual ( MockObject . objects . count ( ) , <NUM_LIT:5> ) <EOL> self . assertEqual ( MockObject . objects . get ( name = '<STR_LIT>' ) . number , <NUM_LIT:1> ) <EOL> self . assertEqual ( MockObject . objects . get ( name = '<STR_LIT>' ) . number , None ) <EOL> self . assertEqual ( <EOL> MockObject . objects . get ( name = '<STR_LIT>' ) . dt , <EOL> date ( <NUM_LIT> , <NUM_LIT:1> , <NUM_LIT:1> ) <EOL> ) <EOL> def test_backwards_save ( self ) : <EOL> c = CopyMapping ( <EOL> MockObject , <EOL> self . backwards_path , <EOL> dict ( name = '<STR_LIT>' , number = '<STR_LIT>' , dt = '<STR_LIT>' ) , <EOL> ) <EOL> c . save ( ) <EOL> self . assertEqual ( MockObject . objects . count ( ) , <NUM_LIT:3> ) <EOL> self . assertEqual ( MockObject . objects . get ( name = '<STR_LIT>' ) . number , <NUM_LIT:1> ) <EOL> self . assertEqual ( <EOL> MockObject . objects . get ( name = '<STR_LIT>' ) . dt , <EOL> date ( <NUM_LIT> , <NUM_LIT:1> , <NUM_LIT:1> ) <EOL> ) <EOL> def test_field_override_save ( self ) : <EOL> c = CopyMapping ( <EOL> MockObject , <EOL> self . null_path , <EOL> dict ( name = '<STR_LIT>' , number = '<STR_LIT>' , dt = '<STR_LIT>' ) , <EOL> ) <EOL> c . save ( ) <EOL> self . assertEqual ( MockObject . objects . count ( ) , <NUM_LIT:5> ) <EOL> self . assertEqual ( MockObject . objects . get ( name = '<STR_LIT>' ) . number , None ) <EOL> self . assertEqual ( <EOL> MockObject . objects . get ( name = '<STR_LIT>' ) . dt , <EOL> date ( <NUM_LIT> , <NUM_LIT:1> , <NUM_LIT:1> ) <EOL> ) <EOL> def test_encoding_save ( self ) : <EOL> c = CopyMapping ( <EOL> MockObject , <EOL> self . null_path , <EOL> dict ( name = '<STR_LIT>' , number = '<STR_LIT>' , dt = '<STR_LIT>' ) , <EOL> encoding = '<STR_LIT>' <EOL> ) <EOL> c . save ( ) <EOL> self . assertEqual ( MockObject . objects . count ( ) , <NUM_LIT:5> ) <EOL> self . assertEqual ( MockObject . objects . get ( name = '<STR_LIT>' ) . number , None ) <EOL> self . assertEqual ( <EOL> MockObject . objects . get ( name = '<STR_LIT>' ) . dt , <EOL> date ( <NUM_LIT> , <NUM_LIT:1> , <NUM_LIT:1> ) <EOL> ) <EOL> def test_static_values ( self ) : <EOL> c = CopyMapping ( <EOL> ExtendedMockObject , <EOL> self . name_path , <EOL> dict ( name = '<STR_LIT>' , number = '<STR_LIT>' , dt = '<STR_LIT>' ) , <EOL> encoding = '<STR_LIT>' , <EOL> static_mapping = { '<STR_LIT>' : <NUM_LIT:1> , '<STR_LIT>' : '<STR_LIT:test>' } <EOL> ) <EOL> c . save ( ) <EOL> self . assertEqual ( <EOL> ExtendedMockObject . objects . filter ( static_val = <NUM_LIT:1> ) . count ( ) , <EOL> <NUM_LIT:3> <EOL> ) <EOL> self . assertEqual ( <EOL> ExtendedMockObject . objects . filter ( static_string = '<STR_LIT:test>' ) . count ( ) , <EOL> <NUM_LIT:3> <EOL> ) <EOL> def test_bad_static_values ( self ) : <EOL> with self . assertRaises ( ValueError ) : <EOL> c = CopyMapping ( <EOL> ExtendedMockObject , <EOL> self . name_path , <EOL> dict ( name = '<STR_LIT>' , number = '<STR_LIT>' , dt = '<STR_LIT>' ) , <EOL> encoding = '<STR_LIT>' , <EOL> static_mapping = { '<STR_LIT>' : <NUM_LIT:1> , } <EOL> ) <EOL> c . save ( ) <EOL> def test_save_foreign_key ( self ) : <EOL> c = CopyMapping ( <EOL> MockObject , <EOL> self . foreign_path , <EOL> dict ( name = '<STR_LIT>' , number = '<STR_LIT>' , dt = '<STR_LIT>' , parent = '<STR_LIT>' ) <EOL> ) <EOL> c . save ( ) <EOL> self . assertEqual ( MockObject . objects . count ( ) , <NUM_LIT:3> ) <EOL> self . assertEqual ( MockObject . objects . get ( name = '<STR_LIT>' ) . parent_id , <NUM_LIT:4> ) <EOL> self . assertEqual ( <EOL> MockObject . objects . get ( name = '<STR_LIT>' ) . dt , <EOL> date ( <NUM_LIT> , <NUM_LIT:1> , <NUM_LIT:1> ) <EOL> ) </s>
