#!/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()
[((73, 94), 'sys.path.append', 'sys.path.append', (['""".."""'], {}), "('..')\n", (88, 94), False, 'import sys\n'), ((1809, 1824), 'unittest.main', 'unittest.main', ([], {}), '()\n', (1822, 1824), False, 'import unittest\n'), ((298, 307), 'modules.service.Service', 'Service', ([], {}), '()\n', (305, 307), False, 'from modules.service import Service\n'), ((332, 342), 'modules.database.Database', 'Database', ([], {}), '()\n', (340, 342), False, 'from modules.database import Database\n'), ((596, 647), 'modules.storage.write', 'storage.write', (['"""test"""', 'self.service', '"""test.service"""'], {}), "('test', self.service, 'test.service')\n", (609, 647), False, 'from modules import storage\n'), ((667, 676), 'modules.service.Service', 'Service', ([], {}), '()\n', (674, 676), False, 'from modules.service import Service\n'), ((685, 731), 'modules.storage.read', 'storage.read', (['"""test"""', 'service2', '"""test.service"""'], {}), "('test', service2, 'test.service')\n", (697, 731), False, 'from modules import storage\n'), ((1116, 1163), 'modules.storage.write', 'storage.write', (['"""test"""', 'self.database', '"""test.db"""'], {}), "('test', self.database, 'test.db')\n", (1129, 1163), False, 'from modules import storage\n'), ((1184, 1194), 'modules.database.Database', 'Database', ([], {}), '()\n', (1192, 1194), False, 'from modules.database import Database\n'), ((1203, 1245), 'modules.storage.read', 'storage.read', (['"""test"""', 'database2', '"""test.db"""'], {}), "('test', database2, 'test.db')\n", (1215, 1245), False, 'from modules import storage\n'), ((1016, 1025), 'modules.service.Service', 'Service', ([], {}), '()\n', (1023, 1025), False, 'from modules.service import Service\n'), ((1061, 1070), 'modules.service.Service', 'Service', ([], {}), '()\n', (1068, 1070), False, 'from modules.service import Service\n'), ((1702, 1713), 'os.getcwd', 'os.getcwd', ([], {}), '()\n', (1711, 1713), False, 'import os\n'), ((1751, 1762), 'os.getcwd', 'os.getcwd', ([], {}), '()\n', (1760, 1762), False, 'import os\n')]