# -*- coding: utf-8 -*-
from datetime import datetime
import os
from os.path import dirname,join
import unittest

import django
import pytest

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "demo-project.settings")

try:
    django.setup()
except AttributeError:
   # Setup isn't necessary in Django < 1.7
   pass

from django.conf import settings
from qiniustorage.backends import QiniuStorage, QiniuFile


class QiniuStorageTest(unittest.TestCase):
    def setUp(self):
        self.storage = QiniuStorage()

    def tearDown(self):
        try:
            self.storage.delete(self.file._name)
        except (IOError, AttributeError):
            pass

    def test_init(self):
        self.file = fil = QiniuFile('foo', self.storage, mode='rb')
        assert fil._mode == 'rb'
        assert fil._name == "foo"

    def test_write_to_read_only_file(self):
        with pytest.raises(AttributeError):
            self.file = QiniuFile('foo', self.storage, mode='rb')
            self.file.write("goto fail")

    def test_write_and_delete_file(self):
        ASSET_FILE_NAME = 'jquery-1.11.1.min.js'
        assert self.storage.exists(ASSET_FILE_NAME) == False
        self.file = QiniuFile(ASSET_FILE_NAME, self.storage, mode='wb')

        with open(join(dirname(__file__),'assets', ASSET_FILE_NAME), 'rb') as assset_file:
            content = assset_file.read()

            assset_file.seek(0, os.SEEK_END)
            assset_file_size = assset_file.tell()
            
            self.file.write(content)
            self.storage._save(ASSET_FILE_NAME, self.file)
            
            assert self.storage.exists(ASSET_FILE_NAME)

        assert self.storage.size(ASSET_FILE_NAME) == assset_file_size

        time_delta = datetime.now() - self.storage.modified_time(ASSET_FILE_NAME)
        assert time_delta.total_seconds() < 60

        self.storage.delete(ASSET_FILE_NAME)
        assert self.storage.exists(ASSET_FILE_NAME) == False

    def test_read_file(self):
        ASSET_FILE_NAME = 'bootstrap.min.css'

        with open(join(dirname(__file__),'assets', ASSET_FILE_NAME), 'rb') as assset_file:
            self.storage.save(ASSET_FILE_NAME, assset_file)

        self.file = self.storage.open(ASSET_FILE_NAME, 'r')

        assert self.file._is_read == False

        content = self.file.read()
        assert content.startswith("/*!")

        assert self.file._is_read == True

    def test_dirty_file(self):
        ASSET_FILE_NAME = 'bootstrap.min.css'

        self.file = self.storage.open(ASSET_FILE_NAME, 'rw')

        assert self.file._is_read == False
        assert self.file._is_dirty == False
        assert self.storage.exists(ASSET_FILE_NAME) == False

        with open(join(dirname(__file__),'assets', ASSET_FILE_NAME), 'r') as assset_file:
            content = assset_file.read()
            self.file.write(content)

        assert self.file._is_read == True
        assert self.file._is_dirty == True

        self.file.close()
        assert self.storage.exists(ASSET_FILE_NAME) == True

    def test_listdir(self):
        dirnames = ['', 'foo', 'bar']
        filenames = ['file1', 'file2', 'file3']
        for dirname in dirnames:
            for filename in filenames:
                fil = self.storage.open(join(dirname, filename), 'w')
                fil.write('test text')
                fil.close()

        dirs, files = self.storage.listdir('/')
        assert dirs == ['foo', 'bar']
        assert files == filenames

        dirs, files = self.storage.listdir('foo')
        assert dirs == []
        assert files == filenames

        for dirname in dirnames:
            for filename in filenames:
                self.storage.delete(join(dirname, filename))
   

@@ -105,16 +105,28 @@
 unittest
+%0Aimport uuid
 %0A%0Aimport
@@ -3159,16 +3159,56 @@
 file3'%5D%0A
+        unique_path = str(uuid.uuid4())%0A
         
@@ -3312,24 +3312,37 @@
 e.open(join(
+unique_path, 
 dirname, fil
@@ -3469,11 +3469,19 @@
 dir(
-'/'
+unique_path
 )%0A  
@@ -3598,14 +3598,33 @@
 dir(
+join(unique_path, 
 'foo')
+)
 %0A   
@@ -3790,24 +3790,37 @@
 delete(join(
+unique_path, 
 dirname, fil

Use uuid to avoid conflicts when multiple tests are run by travis at the same time.