def __load_from_file(self):
     try:
         with open(self.__filename, "r") as f_read:
             saved_cards = json.load(f_read)
             self.__customer_card_storage.clear()
             for saved_card in saved_cards:
                 card = CustomerCard(*saved_card)
                 self.__customer_card_storage[card.get_id_entity()] = card
     except FileNotFoundError:
         self.__customer_card_storage = {}
 def test_getter(self):
     card_one = CustomerCard(1, 'Adam', 'Andrei', 5010306015562,
                             '06/03/2001', '01/10/2019', False)
     self.assertEqual(card_one.get_id_entity(), 1)
     self.assertEqual(card_one.get_customer_name(), 'Adam')
     self.assertEqual(card_one.get_customer_first_name(), 'Andrei')
     self.assertEqual(card_one.get_customer_cnp(), 5010306015562)
     self.assertEqual(card_one.get_birth_date(), '06/03/2001')
     self.assertEqual(card_one.get_registration_date(), '01/10/2019')
Beispiel #3
0
 def update_customer_card(self, customer_card_id, new_customer_name,
                          new_customer_first_name, new_customer_cnp,
                          new_birth_date, new_registration_date,
                          new_is_removed):
     customer_card = CustomerCard(customer_card_id, new_customer_name,
                                  new_customer_first_name, new_customer_cnp,
                                  new_birth_date, new_registration_date,
                                  new_is_removed)
     self.__customer_card_validator.validate_date(new_birth_date)
     self.__customer_card_validator.validate_date(new_registration_date)
     self.__customer_card_repository.ensure_unique_cnp(customer_card)
     self.__customer_card_validator.validate_customer_card(customer_card)
     self.__customer_card_repository.update(customer_card)
Beispiel #4
0
 def add_customer_card(self, customer_card_id, customer_name,
                       customer_first_name, customer_cnp, birth_date,
                       registration_date, is_removed):
     """
     Function creates a card
     :param is_removed:
     :param customer_card_id: int
     :param customer_name: string
     :param customer_first_name: string
     :param customer_cnp: int
     :param birth_date: string
     :param registration_date: string
     """
     customer_card = CustomerCard(customer_card_id, customer_name,
                                  customer_first_name, customer_cnp,
                                  birth_date, registration_date, is_removed)
     self.__customer_card_validator.validate_date(birth_date)
     self.__customer_card_validator.validate_date(registration_date)
     self.__customer_card_repository.ensure_unique_cnp(customer_card)
     self.__customer_card_validator.validate_customer_card(customer_card)
     self.__customer_card_repository.create(customer_card)
Beispiel #5
0
 def populate_cards(self, n):
     list_of_id = []
     list_of_cnp = []
     for card in self.__customer_card_repository.read_all():
         card_id = card.get_id_entity()
         list_of_id.append(card_id)
         card_cnp = card.get_customer_cnp()
         list_of_cnp.append(card_cnp)
     sorted_list = sorted(list_of_id)
     start_id = sorted_list[-1] + 1
     cnp_sorted_list = sorted(list_of_cnp)
     start_cnp = cnp_sorted_list[-1] + 1
     letters = string.ascii_letters
     for i in range(n):
         customer_name = ''.join(random.choice(letters) for i in range(15))
         customer_first_name = ''.join(
             random.choice(letters) for i in range(15))
         cnp = int(random.randint(1000000000000, 9999999999999))
         if cnp not in list_of_cnp:
             good_cnp = cnp
         else:
             good_cnp = start_cnp
         day = int(random.randint(1, 31))
         month = int(random.randint(1, 12))
         year = int(random.randint(2000, 2019))
         birth_date = "{}/{}/{}".format(day, month, year)
         reg_day = int(random.randint(1, 31))
         reg_month = int(random.randint(1, 12))
         reg_year = int(random.randint(2000, 2019))
         registration_date = "{}/{}/{}".format(reg_day, reg_month, reg_year)
         is_removed = random.choice([True, False])
         customer_card = CustomerCard(start_id, customer_name,
                                      customer_first_name, good_cnp,
                                      birth_date, registration_date,
                                      is_removed)
         self.__customer_card_validator.validate_customer_card(
             customer_card)
         self.__customer_card_repository.create(customer_card)
         start_id += 1
     return self.__customer_card_repository.read_all()
 def test_equal(self):
     card_one = CustomerCard(1, 'Adam', 'Andrei', 5010306015562,
                             '06/03/2001', '01/10/2019', False)
     card_two = CustomerCard(1, 'Adam', 'Andrei', 5010306015562,
                             '06/03/2001', '01/10/2019', False)
     self.assertEqual(card_one, card_two)
 def test_get_text_format(self):
     card_one = CustomerCard(1, 'Adam', 'Andrei', 5010306015562,
                             '06/03/2001', '01/10/2019', False)
     card_string = card_one.get_text_format()
     self.assertEqual(card_string,
                      "1,Adam,Andrei,5010306015562,06/03/2001,01/10/2019")
Beispiel #8
0
 def validate_customer_card(customer_card: CustomerCard):
     """
     Function verify if an object respects some conditions and if it finds an irregularity raise an exception
     :param customer_card: an CustomerCard object
     :return: exceptions
     """
     if not isinstance(customer_card.get_id_entity(), int):
         raise InvalidCustomerCardException("ID = {} must be an int".format(
             customer_card.get_id_entity()))
     if len("{}".format(customer_card.get_customer_cnp())) != 13:
         raise InvalidCNPException("CNP {} must be made out of 13"
                                   " characters".format(
                                       customer_card.get_customer_cnp()))
     if len("{}".format(customer_card.get_customer_name())) == 0:
         raise InvalidCustomerCardException("Customer has to get a name ")
     if len("{}".format(customer_card.get_customer_first_name())) == 0:
         raise InvalidCustomerCardException("Customer has to get a name ")
     if not isinstance(customer_card.get_customer_cnp(), int):
         raise InvalidCustomerCardException("CNP {} must be int".format(
             customer_card.get_customer_cnp()))
     if "/" not in customer_card.get_birth_date():
         raise InvalidCustomerCardException("please use / for writing date")
     if "/" not in customer_card.get_registration_date():
         raise InvalidCustomerCardException("please use / for writing date")
     if len("{}".format(customer_card.get_birth_date())) < 5:
         raise InvalidCustomerCardException("Date is not valid")
     if len("{}".format(customer_card.get_registration_date())) < 5:
         raise InvalidCustomerCardException("Date is not valid")
     birth_date = customer_card.get_birth_date().split(sep="/")
     if len(birth_date) != 3:
         raise InvalidCustomerCardException("Use only two /")
     if int(birth_date[0]) < 1 or int(birth_date[0]) > 31:
         raise InvalidCustomerCardException(
             "Day {} must be between 1 and 31".format(int(birth_date[0])))
     if int(birth_date[1]) < 1 or int(birth_date[1]) > 12:
         raise InvalidCustomerCardException(
             "Month {} must be between 1 and 12".format(int(birth_date[1])))
     if int(birth_date[2]) > 2019:
         raise InvalidCustomerCardException(
             "Year cannot be in the future {}".format(int(birth_date[2])))
     registration_date = customer_card.get_registration_date().split(
         sep="/")
     if int(registration_date[0]) < 1 or int(registration_date[0]) > 31:
         raise InvalidCustomerCardException(
             "Day {} must be between 1 and 31".format(
                 int(registration_date[0])))
     if int(registration_date[1]) < 1 or int(registration_date[1]) > 12:
         raise InvalidCustomerCardException(
             "Month {} must be between 1 and 12".format(
                 int(registration_date[1])))
     if int(registration_date[2]) > 2019:
         raise InvalidCustomerCardException(
             "Year cannot be in the future {}".format(
                 int(registration_date[2])))
     if len(registration_date) != 3:
         raise InvalidCustomerCardException("Use only two /")