<s> from StringIO import StringIO <EOL> import json <EOL> import os <EOL> from django . core . urlresolvers import reverse <EOL> from django . test import TestCase <EOL> from corehq . apps . domain . shortcuts import create_domain <EOL> from corehq . apps . reports . models import FormExportSchema <EOL> from corehq . apps . users . models import CommCareUser <EOL> from couchexport . models import Format <EOL> from django_digest . test import Client <EOL> XMLNS = '<STR_LIT>' <EOL> XFORM_ID = '<STR_LIT>' <EOL> XML_DATA = """<STR_LIT>""" <EOL> class FormExportTest ( TestCase ) : <EOL> def setUp ( self ) : <EOL> self . app_id = '<STR_LIT>' <EOL> self . domain_name = '<STR_LIT>' <EOL> self . domain = create_domain ( self . domain_name ) <EOL> self . username = '<STR_LIT>' <EOL> self . couch_user = CommCareUser . create ( self . domain_name , self . username , <EOL> password = '<STR_LIT>' ) <EOL> self . couch_user . save ( ) <EOL> self . client = Client ( ) <EOL> self . client . login ( username = self . couch_user . username , password = '<STR_LIT>' ) <EOL> self . url = reverse ( "<STR_LIT>" , <EOL> args = [ self . domain_name , self . app_id ] ) <EOL> self . custom_export = FormExportSchema . wrap ( { <EOL> '<STR_LIT:type>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : self . app_id , <EOL> '<STR_LIT>' : Format . JSON , <EOL> '<STR_LIT:index>' : json . dumps ( [ self . domain_name , XMLNS ] ) , <EOL> '<STR_LIT>' : [ { <EOL> '<STR_LIT:index>' : '<STR_LIT:#>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : [ { '<STR_LIT:index>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT:Name>' } ] , <EOL> } ] <EOL> } ) <EOL> def tearDown ( self ) : <EOL> self . couch_user . delete ( ) <EOL> def post_it ( self , user_id = None , form_id = XFORM_ID ) : <EOL> user_id = user_id or self . couch_user . _id <EOL> f = StringIO ( XML_DATA . format ( <EOL> user_id = user_id , <EOL> xmlns = XMLNS , <EOL> xform_id = form_id , <EOL> ) ) <EOL> f . name = '<STR_LIT>' <EOL> return self . client . post ( self . url , { '<STR_LIT>' : f } ) <EOL> def test_include_duplicates ( self ) : <EOL> self . post_it ( ) <EOL> self . post_it ( ) <EOL> self . custom_export . include_errors = True <EOL> files = self . custom_export . get_export_files ( ) <EOL> data = json . loads ( files . file . payload ) <EOL> self . assertEqual ( data [ '<STR_LIT>' ] [ '<STR_LIT>' ] , [ '<STR_LIT:Name>' ] ) <EOL> self . assertEqual ( len ( data [ '<STR_LIT>' ] [ '<STR_LIT>' ] ) , <NUM_LIT:2> ) <EOL> self . custom_export . include_errors = False <EOL> files = self . custom_export . get_export_files ( ) <EOL> data = json . loads ( files . file . payload ) <EOL> self . assertEqual ( data [ '<STR_LIT>' ] [ '<STR_LIT>' ] , [ '<STR_LIT:Name>' ] ) <EOL> self . assertEqual ( len ( data [ '<STR_LIT>' ] [ '<STR_LIT>' ] ) , <NUM_LIT:1> ) <EOL> def test_exclude_unknown_users ( self ) : <EOL> self . post_it ( form_id = '<STR_LIT>' , user_id = self . couch_user . _id ) <EOL> files = self . custom_export . get_export_files ( ) <EOL> data = json . loads ( files . file . payload ) <EOL> self . assertEqual ( len ( data [ '<STR_LIT>' ] [ '<STR_LIT>' ] ) , <NUM_LIT:1> ) <EOL> self . post_it ( form_id = '<STR_LIT>' , user_id = '<STR_LIT>' ) <EOL> files = self . custom_export . get_export_files ( ) <EOL> data = json . loads ( files . file . payload ) <EOL> self . assertEqual ( len ( data [ '<STR_LIT>' ] [ '<STR_LIT>' ] ) , <NUM_LIT:1> ) <EOL> self . post_it ( form_id = '<STR_LIT>' , user_id = self . couch_user . _id ) <EOL> files = self . custom_export . get_export_files ( ) <EOL> data = json . loads ( files . file . payload ) <EOL> self . assertEqual ( len ( data [ '<STR_LIT>' ] [ '<STR_LIT>' ] ) , <NUM_LIT:2> ) </s>
