=============================
Edge caes when importing data
=============================

Importing has some edge cases which are demonstrated here:

Set up
======

Create a browser to test the import:

>>> from icemac.addressbook.testing import Browser
>>> browser = Browser()
>>> browser.login('mgr')
>>> browser.open('http://localhost/ab')

Empty import file
=================

When the import file is empty, nothing can get imported:

>>> from StringIO import StringIO
>>> file_data = StringIO()
>>> file_data.write('')
>>> file_data.seek(0)

Upload the file:

>>> from icemac.addressbook.testing import get_messages
>>> browser.getLink('Master data').click()
>>> browser.getLink('Import data').click()
>>> browser.getLink('file').click()
>>> browser.getControl('file').add_file(file_data, 'text/plain', 'empty.csv')
>>> browser.getControl('Add').click()
>>> get_messages(browser)
['"empty.csv" added.']

There is no import file reader, as the CSV reader expects a first line
containing the field names:

>>> browser.getLink('Import').click()
>>> browser.getControl('Import file reader').displayOptions
[]

Editing import file
===================

As the empty import file cannot be imported, it is possible to upload another file by using the `back` button:

>>> browser.getControl('Back').click()
>>> browser.url
'http://localhost/ab/++attribute++importer/File/import/editFile'

Uploading a new file is demonstrated in the next chapter.


Nothing to import
=================

When the import file is not completely empty but contains at least field names a reader can be found but nothing can get imported, too.

>>> file_data = StringIO()
>>> file_data.write('last_name\n')
>>> file_data.seek(0)

To upload the new file we use the step `edit import file`:

>>> browser.getControl('file', index=1).add_file(file_data, 'text/plain', 'header.csv')
>>> browser.getControl('Next').click()
>>> browser.url
'http://localhost/ab/++attribute++importer/File/import/reader'


The original file was really replaced:

>>> browser.getControl('Back').click()
>>> browser.url
'http://localhost/ab/++attribute++importer/File/import/editFile'
>>> browser.getControl('name').value
'header.csv'

Now we can navigate to the `map fields` step and map the fields:

>>> browser.getControl('Next').click()
>>> browser.url
'http://localhost/ab/++attribute++importer/File/import/reader'
>>> browser.getControl('Next').click()
>>> print browser.url
http://localhost/ab/++attribute++importer/File/import/map
>>> browser.getControl('last name').displayValue = ['last_name']
>>> browser.getControl('Next').click()

On the review step a message tells that there is nothing to import:

>>> print browser.url
http://localhost/ab/++attribute++importer/File/import/review
>>> print browser.contents
<!DOCTYPE ...
...There was nothing to import in the import file...

It is possible to go to the complete step as there are no errors:

>>> browser.getControl('Next').click()
>>> browser.url
'http://localhost/ab/++attribute++importer/File/import/complete'
>>> browser.getControl('Complete').click()
>>> browser.url
'http://localhost/ab/++attribute++importer'

The new import file has replaced the old one:

>>> browser.getLink('Master data').click()
>>> browser.getLink('Import data').click()
>>> print browser.contents
<!DOCTYPE ...
<td><a href="http://localhost/ab/++attribute++importer/File">header.csv</a></td>...


Delete persons after import and start new import
================================================

There was a bug which occurred when doing the following steps:

  * importing persons
  * deleting all persons
  * importing the same import file as in the first step

Set up
------

>>> file_data = StringIO()
>>> file_data.write("last_name\n"
...                 "Vipraschil")
>>> file_data.seek(0)


Import persons
--------------

>>> browser.getLink('file').click()
>>> browser.getControl('file').add_file(file_data, 'text/plain', 'del.csv')
>>> browser.getControl('Add').click()
>>> get_messages(browser)
['"del.csv" added.']
>>> browser.url
'http://localhost/ab/++attribute++importer'

>>> browser.getLink('Import').click()
>>> print browser.url
http://localhost/ab/++attribute++importer/File-2/@@import

>>> browser.getControl('Next').click()
>>> print browser.url
http://localhost/ab/++attribute++importer/File-2/import/map

>>> browser.getControl('last name').displayValue = ['last_name']
>>> browser.getControl('Next').click()
>>> browser.url
'http://localhost/ab/++attribute++importer/File-2/import/review'

>>> browser.getControl('Next').click()
>>> browser.url
'http://localhost/ab/++attribute++importer/File-2/import/complete'

>>> browser.getControl('Complete').click()
>>> browser.url
'http://localhost/ab/++attribute++importer'

Delete all persons in address book
----------------------------------

This function can be found on the `Edit address book` form:

>>> browser.open('http://localhost/ab/@@edit.html')
>>> browser.getControl('Delete all persons in address book').click()
>>> browser.url
'http://localhost/ab/@@delete_content.html'
>>> browser.getControl('Yes, delete').click()
>>> get_messages(browser)
['Address book contents deleted.']

Import persons again
--------------------

When importing the same import file, previously an error occurred:

>>> browser.getLink('Master data').click()
>>> browser.getLink('Import data').click()
>>> browser.getLink('Import').click()
>>> print browser.url
http://localhost/ab/++attribute++importer/File-2/@@import
>>> browser.getControl('Next').click()
>>> browser.url
'http://localhost/ab/++attribute++importer/File-2/import/map'
>>> print browser.contents
<!DOCTYPE ...
          <div class="currStep">
            <span>Current step:</span>
            <span>Map fields</span>
          </div>...


