#!/usr/bin/python3.4
__author__ = "Jeremy Rabasco"

import sys
import os
sys.path.append("..")
import unittest
from modules import storage
from modules.service import Service
from modules.database import Database


class TestStorage(unittest.TestCase):

    def setUp(self):
        self.service = Service()
        self.database = Database()
        open("test.service", "w+").close()
        open("test.db", "w+").close()

    def test_write_read_service(self):
        self.service.service_name = "Hello"
        self.service.username = "This"
        self.service.password = "Works"

        storage.write("test", self.service, "test.service")
        service2 = Service()
        storage.read("test", service2, "test.service")
        self.assertEqual(service2.service_name, self.service.service_name)
        self.assertEqual(service2.username, self.service.username)
        self.assertEqual(service2.password, self.service.password)

    def test_write_read_database(self):
        self.database.add_service(Service())
        self.database.add_service(Service())
        self.database.name = "Hey"

        storage.write("test", self.database, "test.db")
        database2 = Database()
        storage.read("test", database2, "test.db")
        self.assertEqual(database2.name, self.database.name)
        for i in range(len(self.database.services)):
            self.assertEqual(database2.services[i].service_name, self.database.services[i].service_name)
            self.assertEqual(database2.services[i].username, self.database.services[i].username)
            self.assertEqual(database2.services[i].password, self.database.services[i].password)

    def tearDown(self):
        os.remove(os.getcwd() + "/test.service")
        os.remove(os.getcwd() + "/test.db")

if __name__ == "__main__":
    unittest.main()
[((6, 0, 6, 21), 'sys.path.append', 'sys.path.append', ({(6, 16, 6, 20): '""".."""'}, {}), "('..')", False, 'import sys\n'), ((52, 4, 52, 19), 'unittest.main', 'unittest.main', ({}, {}), '()', False, 'import unittest\n'), ((16, 23, 16, 32), 'modules.service.Service', 'Service', ({}, {}), '()', False, 'from modules.service import Service\n'), ((17, 24, 17, 34), 'modules.database.Database', 'Database', ({}, {}), '()', False, 'from modules.database import Database\n'), ((26, 8, 26, 59), 'modules.storage.write', 'storage.write', ({(26, 22, 26, 28): '"""test"""', (26, 30, 26, 42): 'self.service', (26, 44, 26, 58): '"""test.service"""'}, {}), "('test', self.service, 'test.service')", False, 'from modules import storage\n'), ((27, 19, 27, 28), 'modules.service.Service', 'Service', ({}, {}), '()', False, 'from modules.service import Service\n'), ((28, 8, 28, 54), 'modules.storage.read', 'storage.read', ({(28, 21, 28, 27): '"""test"""', (28, 29, 28, 37): 'service2', (28, 39, 28, 53): '"""test.service"""'}, {}), "('test', service2, 'test.service')", False, 'from modules import storage\n'), ((38, 8, 38, 55), 'modules.storage.write', 'storage.write', ({(38, 22, 38, 28): '"""test"""', (38, 30, 38, 43): 'self.database', (38, 45, 38, 54): '"""test.db"""'}, {}), "('test', self.database, 'test.db')", False, 'from modules import storage\n'), ((39, 20, 39, 30), 'modules.database.Database', 'Database', ({}, {}), '()', False, 'from modules.database import Database\n'), ((40, 8, 40, 50), 'modules.storage.read', 'storage.read', ({(40, 21, 40, 27): '"""test"""', (40, 29, 40, 38): 'database2', (40, 40, 40, 49): '"""test.db"""'}, {}), "('test', database2, 'test.db')", False, 'from modules import storage\n'), ((34, 34, 34, 43), 'modules.service.Service', 'Service', ({}, {}), '()', False, 'from modules.service import Service\n'), ((35, 34, 35, 43), 'modules.service.Service', 'Service', ({}, {}), '()', False, 'from modules.service import Service\n'), ((48, 18, 48, 29), 'os.getcwd', 'os.getcwd', ({}, {}), '()', False, 'import os\n'), ((49, 18, 49, 29), 'os.getcwd', 'os.getcwd', ({}, {}), '()', False, 'import os\n')]