import json
import os
import sys
from tempfile import NamedTemporaryFile
import unittest

sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
from pysswords.db import Database, Credential
from pysswords.crypt import CryptOptions


class PysswordsTests(unittest.TestCase):
    def setUp(self):
        self.db_file = NamedTemporaryFile(mode='w', delete=False)
        self.db_path = self.db_file.name
        self.db_file.close()
        self.password = "=Sup3rh4rdp4ssw0rdt0cr4ck"
        self.default_iterations = 1000

        self.crypt_options = CryptOptions(
            password=self.password,
            salt=None,
            iterations=1000
        )

        self.db = Database.create(
            path=self.db_path,
            crypt_options=self.crypt_options
        )

    def tearDown(self):
        os.remove(self.db_path)

    def test_database_default_content(self):
        expected_content = json.dumps([{}])
        self.assertEqual(Database.DEFAULT_CONTENT, expected_content)

    def test_create_credential_database(self):
        database = Database.create(
            path=self.db_path,
            crypt_options=self.crypt_options
        )
        self.assertIsInstance(database, Database)
        self.assertEqual(len(database.credentials), 0)

    def test_add_new_credential(self):
        credential = Credential(
            name="example",
            login="john",
            password="my-great-password",
            login_url="http://example.org/login",
            description="This is login credentials for example"
        )
        self.assertEqual(len(self.db.credentials), 0)
        self.db.add_credential(credential)
        self.assertEqual(len(self.db.credentials), 1)

    def test_delete_credential_by_name(self):
        credential = Credential(
            name="example",
            login="john",
            password="my-great-password",
            login_url="http://example.org/login",
            description="This is login credentials for example"
        )
        self.db.add_credential(credential)
        self.db.delete_credential(name="example")
        self.assertEqual(len(self.db.credentials), 0)

    def test_delete_credential_by_login(self):
        credential = Credential(
            name="example",
            login="john",
            password="my-great-password",
            login_url="http://example.org/login",
            description="This is login credentials for example"
        )
        self.db.add_credential(credential)
        self.db.delete_credential(login="john")
        self.assertEqual(len(self.db.credentials), 0)

    def test_delete_credential_by_name_and_login(self):
        credential = Credential(
            name="example",
            login="john",
            password="my-great-password",
            login_url="http://example.org/login",
            description="This is login credentials for example"
        )

        credential2 = Credential(
            name="github",
            login="john",
            password="my-great-password",
            login_url="http://example.org/login",
            description="This is login credentials for example"
        )

        self.db.add_credential(credential)
        self.db.add_credential(credential2)

        self.db.delete_credential(name=credential2.name,
                                  login=credential2.login)

        self.assertIn(credential, self.db.credentials)
        self.assertNotIn(credential2, self.db.credentials)



if __name__ == "__main__":
    unittest.main()

@@ -801,24 +801,431 @@
 %0A        )%0A%0A
+    def some_credential(self, **kwargs):%0A        return Credential(%0A            name=kwargs.get(%22name%22, %22example%22),%0A            login=kwargs.get(%22login%22, %22john%22),%0A            password=kwargs.get(%22password%22, %22my-great-password%22),%0A            login_url=kwargs.get(%22login_url%22, %22http://example.org/login%22),%0A            description=kwargs.get(%22description%22, %22This is login credentials for example%22),%0A        )%0A%0A
     def tear
@@ -1746,261 +1746,8 @@
 f):%0A
-        credential = Credential(%0A            name=%22example%22,%0A            login=%22john%22,%0A            password=%22my-great-password%22,%0A            login_url=%22http://example.org/login%22,%0A            description=%22This is login credentials for example%22%0A        )%0A
     
@@ -1819,32 +1819,42 @@
 .add_credential(
+self.some_
 credential)%0A    
@@ -1839,32 +1839,34 @@
 .some_credential
+()
 )%0A        self.a
@@ -1964,218 +1964,41 @@
     
-credential = Credential(%0A            name=%22example%22,%0A            login=%22john%22,%0A            password=%22my-great-password%22,%0A            login_url=%22http://example.org/login%22,%0A            description=%22This is login 
+self.db.add_credential(self.some_
 cred
@@ -1999,38 +1999,39 @@
 e_credential
-s for 
+(name=%22
 example%22
 %0A        )%0A 
@@ -2022,60 +2022,9 @@
 ple%22
-%0A        )%0A        self.db.add_credential(credential
+)
 )%0A  
@@ -2185,294 +2185,65 @@
     
-credential = Credential(%0A            name=%22example%22,%0A            login=%22john%22,%0A            password=%22my-great-password%22,%0A            login_url=%22http://example.org/login%22,%0A            description=%22This is login credentials for example%22%0A        )%0A        self.db.add_credential(credential
+self.db.add_credential(self.some_credential(login=%22john%22)
 )%0A  
@@ -2332,32 +2332,62 @@
 .credentials), 0
+, %22Couldn't delete credential%22
 )%0A%0A    def test_
@@ -2454,287 +2454,104 @@
 l = 
-Credential(%0A            name=%22example%22,%0A            login=%22john%22,%0A            password=%22my-great-password%22,%0A            login_url=%22http://example.org/login%22,%0A            description=%22This is login credentials for example%22%0A        )%0A%0A        credential2 = Credential(%0A            
+self.some_credential(name=%22github%22, login=%22example%22)%0A        credential2 = self.some_credential(
 name
@@ -2560,28 +2560,16 @@
 github%22,
-%0A           
  login=%22
@@ -2577,174 +2577,8 @@
 ohn%22
-,%0A            password=%22my-great-password%22,%0A            login_url=%22http://example.org/login%22,%0A            description=%22This is login credentials for example%22%0A        
 )%0A%0A 
@@ -2898,16 +2898,18 @@
 als)%0A%0A%0A%0A
+%0A%0A
 if __nam

Remove duplicated code of credentials in test.py