Ejemplo n.º 1
0
 def test_save_contact(self):
     with ContactManager() as cm:
         user0 = User("Mike", "13800000000")
         cm.new_contact(user0)
     with ContactManager() as other_cm:
         matches = other_cm.search_contact("mike")
         self.assertEqual(len(matches), 1)
         self.assertEqual(matches[0]["username"].lower(), "mike")
Ejemplo n.º 2
0
 def test_delete_all_contacts(self):
     with ContactManager() as cm:
         user0 = User("Mike", "13800000000")
         user1 = User("Mike", "13800000001")
         cm.new_contact(user0).new_contact(user1)
         self.assertEqual(cm.size(), 2)
     with ContactManager() as cm:
         cm.delete_all_contacts()
         self.assertEqual(cm.size(), 0)
Ejemplo n.º 3
0
def search_contact(args):
    with ContactManager(args.database) as cm:
        matchs = cm.search_contact(args.username)
        messages = "List all matchers as below\n-------------\n"
        for match in matchs:
            messages = messages + cm.show_contact(match)
        print(messages)
Ejemplo n.º 4
0
 def test_delete_user_with_out_of_index(self):
     with ContactManager() as cm:
         user0 = User("Mike", "13800000000")
         cm.new_contact(user0)
         matches = cm.search_contact("Mike")
         self.assertEqual(len(matches), 1)
         with self.assertRaises(IndexOutofRangeException):
             matches = cm.delete_user(10)
Ejemplo n.º 5
0
def delete_all_contacts(args):
    confirm = raw_input("Are you sure to clean all data [yes/no]?:")
    if confirm.lower() == "yes":
        with ContactManager(args.database) as cm:
            cm.delete_all_contacts()
        print("Clean success")
    else:
        print("Cancle clean")
Ejemplo n.º 6
0
 def test_delete_user_with_index(self):
     with ContactManager() as cm:
         user0 = User("Mike", "13800000000")
         cm.new_contact(user0)
         matches = cm.search_contact("Mike")
         self.assertEqual(len(matches), 1)
         matches = cm.delete_user(0)
         matches = cm.search_contact("Mike")
         self.assertEqual(len(matches), 0)
Ejemplo n.º 7
0
 def test_new_contact_with_user_object(self):
     with ContactManager() as cm:
         user = User("Mike", "13800000000")
         cm.new_contact(user)
         self.assertEqual(len(cm.contacts["data"]), 1)
         self.assertDictEqual(cm.contacts["data"][0], {
             "index": 0,
             "username": user.username,
             "phones": user.phones
         })
Ejemplo n.º 8
0
 def test_delete_new_phone_with_user_index(self):
     with ContactManager() as cm:
         user0 = User("Mike", "13800000000")
         cm.new_contact(user0)
         cm.delete_phone_with_user_index(0, "13810000000")
         phones = cm.contacts["data"][0]["phones"]
         self.assertEqual(len(phones), 1)
         self.assertIn("13800000000", phones)
         cm.delete_phone_with_user_index(0, "13800000000")
         phones = cm.contacts["data"][0]["phones"]
         self.assertEqual(len(phones), 0)
Ejemplo n.º 9
0
 def test_search_contact_with_user_name(self):
     with ContactManager() as cm:
         user0 = User("Mike", "13800000000")
         user1 = User("Alice", "13100000000")
         cm.new_contact(user0)
         cm.new_contact(user1)
         matches = cm.search_contact(user0.username)
         self.assertEqual(len(matches), 1)
         self.assertEqual(matches[0]["username"], user0.username)
         matches = cm.search_contact("MIKE")
         self.assertEqual(len(matches), 1)
         self.assertEqual(matches[0]["username"].lower(), "MIKE".lower())
Ejemplo n.º 10
0
    def test_many_ops_with_in_single_process(self):
        with ContactManager() as cm:
            user0 = User("Mike", "13800000000")
            user1 = User("Alice", "13810000000")
            user0_update = User("Mike", "13820000000")
            cm.new_contact(user0).new_contact(user1).update_contact(
                0, user0_update)
            current_contacts = cm.list_contacts()
            self.assertEqual(len(current_contacts), 2)
            self.assertEqual(current_contacts[0]["username"], "Mike")
            self.assertEqual(current_contacts[0]["phones"], ["13820000000"])
            self.assertEqual(2, cm.size())

        with ContactManager() as cm:
            matches = cm.search_contact("Mike")
            self.assertEqual(len(matches), 1)
            self.assertEqual(matches[0]["username"], "Mike")

        with ContactManager() as cm:
            cm.delete_user(0)
            self.assertEqual(1, cm.size())
Ejemplo n.º 11
0
 def test_append_new_phone_with_user_index(self):
     with ContactManager() as cm:
         user0 = User("Mike", "13800000000")
         cm.new_contact(user0)
         cm.append_new_phone_with_user_index(0, "13810000000")
         phones = cm.contacts["data"][0]["phones"]
         self.assertEqual(len(phones), 2)
         self.assertIn("13800000000", phones)
         self.assertIn("13810000000", phones)
         cm.append_new_phone_with_user_index(0, "13810000000")
         phones = cm.contacts["data"][0]["phones"]
         self.assertEqual(len(phones), 2)
         with self.assertRaises(PhoneNotValidException):
             cm.append_new_phone_with_user_index(0, "not valid phone")
Ejemplo n.º 12
0
def delete_contact(args):
    with ContactManager(args.database) as cm:
        cm.update_contact(args.index)
    print("Delete success")
Ejemplo n.º 13
0
 def test_new_contact_with_wrong_correct(self):
     with ContactManager() as cm:
         user = "******"
         with self.assertRaises(UserNotValidException):
             cm.new_contact(user)
Ejemplo n.º 14
0
 def test_read_init_db_file_return_zero_size(self):
     with ContactManager() as cm:
         size = cm.size()
         self.assertEqual(size, 0)
Ejemplo n.º 15
0
 def test_read_init_db_file_return_correct_version(self):
     with ContactManager() as cm:
         version = cm.version()
         self.assertEqual(version, Config.DB_VERSION)
Ejemplo n.º 16
0
 def test_read_init_db_file_return_empty_list(self):
     with ContactManager() as cm:
         contacts = cm.list_contacts()
         self.assertEquals(contacts, [])
Ejemplo n.º 17
0
 def test_update_user_with_no_correct_user_type(self):
     with ContactManager() as cm:
         user0 = User("Mike", "13800000000")
         cm.new_contact(user0)
         with self.assertRaises(UserNotValidException):
             _ = cm.update_contact(index=0, user="******")
Ejemplo n.º 18
0
 def test_read_db_file_with_right_format(self):
     with ContactManager(db_file="./test_data/contact.db") as cm:
         pass
     self.assertEqual(cm.db_file, "./test_data/contact.db")
Ejemplo n.º 19
0
 def test_init_db_file_with_default(self):
     with ContactManager() as cm:
         pass
     isDBFileExist = os.path.isfile(Config.DEFAULT_DB_FILE)
     self.assertTrue(isDBFileExist)
Ejemplo n.º 20
0
def update_contact(args):
    with ContactManager(args.database) as cm:
        user = User(username=args.username, phones=args.phone)
        cm.update_contact(args.index, user)
    print("Update success")
Ejemplo n.º 21
0
 def test_init_db_file_with_filename(self):
     with ContactManager(db_file="test.db") as cm:
         pass
     isDBFileExist = os.path.isfile("test.db")
     self.assertTrue(isDBFileExist)
     os.remove("test.db")
Ejemplo n.º 22
0
def list_contacts(args):
    with ContactManager(args.database) as cm:
        messages = "List all contacts as below\n-------------\n"
        for contact in cm.list_contacts():
            messages = messages + cm.show_contact(contact)
        print(messages)
Ejemplo n.º 23
0
 def test_search_contact_with_not_exist_user(self):
     with ContactManager() as cm:
         user0 = User("Mike", "13800000000")
         cm.new_contact(user0)
         matches = cm.search_contact("alice")
         self.assertEqual(len(matches), 0)
Ejemplo n.º 24
0
 def test_read_db_file_with_fail_format(self):
     with self.assertRaises(ConfigFileParseException):
         with ContactManager(db_file="./test_data/failed_contact.db") as cm:
             pass
Ejemplo n.º 25
0
def new_contact(args):
    with ContactManager(args.database) as cm:
        user = User(username=args.username, phones=args.phone)
        cm.new_contact(user)
    print("Create success")
Ejemplo n.º 26
0
 def test_update_user_with_no_index(self):
     with ContactManager() as cm:
         user0 = User("Mike", "13800000000")
         cm.new_contact(user0)
         with self.assertRaises(IndexNotGivenException):
             _ = cm.update_contact()