Example #1
0
def test_two():
    clear_tables()
    m.add_librarian(Packager(l1))
    m.add_librarian(Packager(l2))
    m.add_librarian(Packager(l3))
    assert (m.get_count('librarians') == 3)
    return True
    def add_document(self, doc, key, by_who_id=0):
        if (self.testing):

            if (self.DBmanager.get_label('privileges', 'librarians', by_who_id)
                    < 2):
                #print('Librarian has not enough privileges')
                return False

        if doc.keys().__contains__('free_count'):
            doc['free_count'] = doc['count']
        if key == 'book':
            self.DBmanager.add_book(Packager(doc))
        elif key == 'article':
            self.DBmanager.add_article(Packager(doc))
        elif key == 'media':
            self.DBmanager.add_media(Packager(doc))
        elif key == 'reference_book':
            self.DBmanager.add_reference_book(Packager(doc))
        elif key == 'reference_article':
            self.DBmanager.add_reference_article(Packager(doc))

        by_who = 'UNKNOW' if by_who_id == 0 else self.get_user(
            by_who_id)['name']
        self.log(
            'INFO', '{} \'{}\' is added to system by {}.'.format(
                key.capitalize(), doc['title'], by_who))
Example #3
0
def test_fifth():
    cntrl = create_controller(5)

    test_user_1 = {
        'id': 1,
        'name': 'test',
        'address': 'test',
        'status': 'Student',
        'phone': '987',
        'history': [],
        'current_docs': []
    }
    test_user_2 = {
        'id': 2,
        'name': 'test',
        'address': 'test',
        'status': 'Student',
        'phone': '987',
        'history': [],
        'current_docs': []
    }
    test_user_3 = {
        'id': 3,
        'name': 'test',
        'address': 'test',
        'status': 'Student',
        'phone': '987',
        'history': [],
        'current_docs': []
    }

    cntrl.DBmanager.add_patron(Packager(test_user_1))
    cntrl.DBmanager.add_patron(Packager(test_user_2))
    cntrl.DBmanager.add_patron(Packager(test_user_3))

    test_book = {
        'title': 'Test',
        'description': 'TESTTEST',
        'authors': 'tEsT',
        'count': 2,
        'price': 123,
        'keywords': ''
    }

    cntrl.add_document(test_book, 'book')

    book_id = cntrl.DBmanager.get_by('title', 'book', test_book['title'])[0][0]

    is_first_user_check_out = cntrl.check_out_doc(test_user_1['id'], book_id)
    is_second_user_check_out = cntrl.check_out_doc(test_user_3['id'], book_id)
    is_third_user_check_out = cntrl.check_out_doc(test_user_2['id'], book_id)
    clear_tables()
    assert (is_first_user_check_out[0] and is_second_user_check_out[0]
            and not is_third_user_check_out[0])
Example #4
0
def test_check_out_article():
    cntrl = create_controller('test_check_out_article')

    test_article = {
        'title': 'test_title',
        'authors': 'test_author',
        'journal': 'journala',
        'issue': 'satohue',
        'editors': 'editor1',
        'date': '2017-08-01',
        'keywords': '',
        'price': 1,
        'count': 3,
        'best_seller': 0
    }
    test_user = {
        'id': 1,
        'name': 'test',
        'address': 'tEsT',
        'status': 'Student',
        'phone': '987',
        'history': [],
        'current_docs': []
    }
    cntrl.add_document(test_article, 'article')

    cntrl.DBmanager.add_patron(Packager(test_user))
    doc_id = cntrl.get_documents_by_title(test_article['title'],
                                          'article')[0]['id']
    cntrl.check_out_doc(test_user['id'], doc_id, 'article')
    is_decremented = cntrl.get_document(
        doc_id, 'article')['free_count'] == test_article['count'] - 1
    clear_tables()
    assert is_decremented
Example #5
0
def test_sixth():
    cntrl = create_controller(6)

    test_user = {
        'id': 1,
        'name': 'test',
        'address': 'test',
        'status': 'Student',
        'phone': '987',
        'history': [],
        'current_docs': []
    }
    test_book = {
        'title': 'Test',
        'description': 'TESTTEST',
        'authors': 'tEsT',
        'count': 2,
        'price': 123,
        'keywords': ''
    }

    cntrl.DBmanager.add_patron(Packager(test_user))
    cntrl.add_document(test_book, 'book')
    book_id = cntrl.DBmanager.get_by('title', 'book', test_book['title'])[0][0]

    first_copy = cntrl.check_out_doc(test_user['id'], book_id)[0]
    second_copy = cntrl.check_out_doc(test_user['id'], book_id)[0]
    clear_tables()
    assert (first_copy and not second_copy)
Example #6
0
def add_all():
    m.add_patron(Packager(p1))
    m.add_patron(Packager(p2))
    m.add_patron(Packager(p3))
    m.add_patron(Packager(s))
    m.add_patron(Packager(v))
    m.add_book(Packager(d1))
    m.add_book(Packager(d2))
    m.add_book(Packager(d3))
Example #7
0
 def upto_librarian(self, user_id):
     user_info = self.get_user(user_id)
     user_info.pop('current_docs', 0)
     user_info.pop('history', 0)
     self.delete_user(user_id)
     self.DBmanager.add_librarian(Packager(user_info))
     self.log('INFO',
              'User {} is upgraded to librarian'.format(user_info['name']))
Example #8
0
def test_four():
    test_two()
    c.add_document(d1, 'book', l2['id'])
    c.add_document(d2, 'book', l2['id'])
    c.add_document(d3, 'book', l2['id'])

    c.add_copies_of_document('book', 1, 3, by_who_id=l2['id'])
    c.add_copies_of_document('book', 2, 3, by_who_id=l2['id'])
    c.add_copies_of_document('book', 3, 3, by_who_id=l2['id'])
    m.add_patron(Packager(p1), 2)
    m.add_patron(Packager(p2), 2)
    m.add_patron(Packager(p3), 2)
    m.add_patron(Packager(s), 2)
    m.add_patron(Packager(v), 2)

    assert (m.get_count('patrons') == 5)
    return True
Example #9
0
    def add_document(self, doc, key, by_who_id=0):
        if doc.keys().__contains__('free_count'):
            doc['free_count'] = doc['count']
        if key == 'book':
            self.DBmanager.add_book(Packager(doc))
        elif key == 'article':
            self.DBmanager.add_article(Packager(doc))
        elif key == 'media':
            self.DBmanager.add_media(Packager(doc))
        elif key == 'reference_book':
            self.DBmanager.add_reference_book(Packager(doc))
        elif key == 'reference_article':
            self.DBmanager.add_reference_article(Packager(doc))

        by_who = 'UNKNOW' if by_who_id == 0 else self.get_user(
            by_who_id)['name']
        self.log(
            'INFO', '{} \'{}\' is added to system by {}.'.format(
                key.capitalize(), doc['title'], by_who))
Example #10
0
def test_one(info=False):
    d=Manager()
    clear_tables()
    add_document(b1, 'book', 3, 1)
    add_document(b2, 'book', 2, 2)
    add_document(b3, 'book', 1, 3)
    add_document(av1, 'media', 1, 1)
    add_document(av2, 'media', 1, 2)
    d.add_patron(Packager(p1))
    d.add_patron(Packager(p2))
    d.add_patron(Packager(p3))
   # print(c.get_all_patrons())
   # print(c.get_patron(1010))
    try:
        count_of_docs = get_count_of_docs()
        count_of_users = get_count_of_users()
        assert (count_of_docs == 8 and count_of_users == 4)
        return True
    except:
        return False
Example #11
0
def test_fourth():
    cntrl = create_controller(4)

    test_user = {
        'id': 1,
        'name': 'test',
        'address': 'test',
        'status': 'Student',
        'phone': '987',
        'history': [],
        'current_docs': []
    }
    test_book = {
        'title': 'Test',
        'description': 'TESTTEST',
        'authors': 'tEsT',
        'count': 2,
        'price': 123,
        'best_seller': 1,
        'keywords': ''
    }

    cntrl.DBmanager.add_patron(Packager(test_user))
    cntrl.add_document(test_book, 'book')
    book_id = cntrl.DBmanager.get_by('title', 'book', test_book['title'])[0][0]

    cntrl.check_out_doc(test_user['id'], book_id)

    user_db = cntrl.get_user(test_user['id'])
    book_db_t = list(
        cntrl.DBmanager.get_by('title', 'book', test_book['title'])[0])
    book_db = dict(
        zip([
            'id', 'title', 'authors', 'description', 'count', 'free_count',
            'price', 'keywords'
        ], book_db_t))
    order_id = int(eval(user_db['current_docs'])[0])
    order = dict(
        zip(['id', 'time', 'table', 'userId', 'docId', 'out_of_time'],
            list(cntrl.DBmanager.get_by('id', 'orders', order_id)[0])))

    order['time'] = datetime.strptime(order['time'], '%Y-%m-%d')
    order['out_of_time'] = datetime.strptime(order['out_of_time'], '%Y-%m-%d')

    is_user_have_book = order['docId'] == book_id
    is_book_free_count_decremented = book_db[
        'free_count'] == book_db['count'] - 1
    is_out_of_time_equality = order[
        'out_of_time'] == order['time'] + timedelta(weeks=3)
    clear_tables()
    assert (is_user_have_book and is_book_free_count_decremented
            and is_out_of_time_equality)
def test_add_queue_order():
      #m.clear_table('patrons')
     # m.clear_table('book')
      try:
            m.add_patron(Packager(p1))
      except:
            print('Warning!Patron already added')

      c.add_document(b2,'book')
      book_id=m.get_max_id('book')
      c.add_queue_order(1,'book', book_id)

      assert(len(c.get_user_queue(1))==1)
      assert(len(eval(m.get_label('queue','book', book_id))[0])==1)
Example #13
0
 def confirm_user(self, user_id, librarian_id=-1):
     user = self.get_user(user_id)
     if not user:
         return False
     user['history'] = str([])
     user['current_docs'] = str([])
     self.delete_user(user_id)
     self.DBmanager.add_patron(Packager(user))
     by_who = 'UNKNOW' if librarian_id == -1 else self.get_user(
         librarian_id)['name']
     self.log(
         'INFO',
         'User status {} is confirmed by {}.'.format(user['name'], by_who))
     return True
Example #14
0
def test_get_orders():
    cntrl = create_controller('get_orders')

    test_book = {
        'title': 'Test',
        'description': 'TESTTEST',
        'authors': 'tEsT',
        'count': 2,
        'price': 123,
        'keywords': '0'
    }
    test_user = {
        'id': 1,
        'name': 'test',
        'address': 'tEsT',
        'status': 'Student',
        'phone': '987',
        'history': [],
        'current_docs': []
    }

    cntrl.add_document(test_book, 'book')

    cntrl.DBmanager.add_patron(Packager(test_user))

    book_id = cntrl.DBmanager.get_by('title', 'book', test_book['title'])[0][0]

    success, _ = cntrl.check_out_doc(test_user['id'], book_id)
    if not success:
        clear_tables()
        assert success

    orders = cntrl.get_all_waiting_doc(-1)
    if len(orders) != 1:
        clear_tables()
        assert False

    cntrl.user_get_doc(orders[0]['id'])
    orders = cntrl.get_all_active_orders(-1)
    if len(orders) != 1:
        clear_tables()
        assert False

    cntrl.return_doc(orders[0]['id'])
    orders = cntrl.get_all_returned_orders(-1)
    if len(orders) != 1:
        clear_tables()
        assert False
    clear_tables()
    assert True
Example #15
0
def test_return_doc():
    cntrl = create_controller('test_return_doc')

    test_media = {
        'title': 'Teste',
        'authors': 'XY',
        'keywords': 'oansedi',
        'price': 123,
        'best_seller': 1,
        'count': 1
    }
    test_user = {
        'id': 1,
        'name': 'test',
        'address': 'test',
        'status': 'Student',
        'phone': '987',
        'history': [],
        'current_docs': []
    }

    cntrl.DBmanager.add_patron(Packager(test_user))
    cntrl.add_document(test_media, 'media')

    media_id = cntrl.DBmanager.get_by('title', 'media',
                                      test_media['title'])[0][0]

    if not type(media_id) is int:
        clear_tables()
        assert False

    success, msg = cntrl.check_out_doc(test_user['id'], media_id, 'media')

    if not success:
        clear_tables()
        assert False
    order_id = cntrl.get_all_orders(test_user['id'])[0]['id']
    success, msg = cntrl.return_doc(order_id)

    if not success:
        clear_tables()
        assert False

    user_current_docs = eval(
        cntrl.DBmanager.get_label('current_docs', 'patrons', test_user['id']))
    media_count = cntrl.DBmanager.get_label('free_count', 'media', media_id)
    clear_currents_doc = user_current_docs == []
    count_of_media = media_count == test_media['count']
    clear_tables()
    assert (clear_currents_doc and count_of_media)
Example #16
0
def test_second():
    cntrl = create_controller(2)

    id_book_A = 1
    test_user = {
        'id': 1,
        'name': 'test',
        'address': 'test',
        'status': 'Student',
        'phone': '987',
        'history': [],
        'current_docs': []
    }
    cntrl.DBmanager.add_patron(Packager(test_user))
    can_get_book = cntrl.check_out_doc(test_user['id'], id_book_A)
    clear_tables()
    assert (can_get_book)
Example #17
0
def test_first():
    cntrl = create_controller(1)

    test_user = {
        'id': 1,
        'name': 'test',
        'address': 'tEsT',
        'status': 'Student',
        'phone': '987',
        'history': [],
        'current_docs': []
    }
    test_book = {
        'title': 'Test',
        'description': 'TESTTEST',
        'authors': 'tEsT',
        'count': 2,
        'price': 123,
        'keywords': ''
    }

    cntrl.DBmanager.add_patron(Packager(test_user))
    cntrl.add_document(test_book, 'book')
    book_id = cntrl.DBmanager.get_by('title', 'book', test_book['title'])[0][0]

    cntrl.check_out_doc(test_user['id'], book_id)

    user_db = cntrl.get_user(test_user['id'])
    book_db_t = list(
        cntrl.DBmanager.get_by('title', 'book', test_book['title'])[0])
    book_db = dict(
        zip([
            'id', 'title', 'authors', 'description', 'count', 'free_count',
            'price'
        ], book_db_t))
    order_id = int(eval(user_db['current_docs'])[0])
    user_book_id = cntrl.DBmanager.get_by('id', 'orders', order_id)[0][3]

    is_user_have_book = user_book_id == book_id
    is_book_free_count_decremented = book_db[
        'free_count'] == book_db['count'] - 1
    clear_tables()
    assert (is_book_free_count_decremented and is_user_have_book)
Example #18
0
def test_check_out_media():
    cntrl = create_controller('test_check_out_media')

    test_media = {
        'title': 'Teste',
        'authors': 'XY',
        'keywords': 'oansedi',
        'price': 123,
        'best_seller': 1,
        'count': 1
    }
    test_user = {
        'id': 1,
        'name': 'test',
        'address': 'test',
        'status': 'Student',
        'phone': '987',
        'history': [],
        'current_docs': []
    }

    cntrl.add_document(test_media, 'media')
    cntrl.DBmanager.add_patron(Packager(test_user))

    media_id = cntrl.DBmanager.get_by('title', 'media', test_media['title'])

    if media_id == None:
        clear_tables()
        assert False
    media_id = media_id[0][0]
    success, msg = cntrl.check_out_doc(test_user['id'], media_id, 'media')
    if not success:
        clear_tables()
        assert (False)

    test_user = cntrl.get_user(test_user['id'])
    order = cntrl.DBmanager.select_label('orders',
                                         eval(test_user['current_docs'])[0])
    is_order_media = order[2] == 'media'
    is_ids_match = order[3] == media_id

    clear_tables()
    assert (is_order_media and is_ids_match)
Example #19
0
def test_tenth():
    cntrl = create_controller(10)

    test_user = {
        'id': 1,
        'name': 'test',
        'address': 'test',
        'status': 'Student',
        'phone': '987',
        'history': [],
        'current_docs': []
    }
    test_book_1 = {
        'title': 'Test',
        'description': 'TESTTEST',
        'authors': 'tEsT',
        'count': 2,
        'price': 123,
        'keywords': ''
    }
    test_book_2 = {
        'title': 'TEEST',
        'description': 'TESTTEST',
        'authors': 'tEsT',
        'count': 0,
        'price': 122,
        'keywords': ''
    }

    cntrl.DBmanager.add_patron(Packager(test_user))
    cntrl.add_document(test_book_1, 'book')
    cntrl.add_document(test_book_2, 'book')

    book_id_1 = cntrl.DBmanager.get_by('title', 'book',
                                       test_book_1['title'])[0][0]
    book_id_2 = cntrl.DBmanager.get_by('title', 'book',
                                       test_book_2['title'])[0][0]

    regular_book = cntrl.check_out_doc(test_user['id'], book_id_1)[0]
    references_book = not cntrl.check_out_doc(test_user['id'], book_id_2)[0]
    clear_tables()
    assert (regular_book and references_book)
Example #20
0
def test_get_user_orders():
    cntrl = create_controller('get_user_orders')

    test_book = {
        'title': 'Test',
        'description': 'TESTTEST',
        'authors': 'tEsT',
        'count': 2,
        'price': 123,
        'keywords': '0'
    }
    test_user = {
        'id': 1,
        'name': 'test',
        'address': 'tEsT',
        'status': 'Student',
        'phone': '987',
        'history': [],
        'current_docs': []
    }

    cntrl.add_document(test_book, 'book')

    cntrl.DBmanager.add_patron(Packager(test_user))

    book_id = cntrl.DBmanager.get_by('title', 'book', test_book['title'])[0][0]

    success, _ = cntrl.check_out_doc(test_user['id'], book_id)
    if not success:
        assert (success)

    doc = cntrl.get_user_orders(test_user['id'])[0]['doc']
    test_book.pop('free_count')
    doc.pop('free_count')
    doc.pop('id')
    clear_tables()
    assert (test_book == doc)
def test_bd_add_ref_article():
      #(title,authors,journal,keywords,issue,editors,date)
      m.clear_table('reference_article')
      m.add_reference_article(Packager(j1))
      assert(m.get_count('reference_article')==1)
Example #22
0
def test_ten():
    test_four()
    res_book = Packager(c.get_documents_by_title('Introduction to Algorithms', 'book')[0])
    assert (res_book.title == d1['title']
            and res_book.authors == d1['authors'])
    return True
def test_bd_add_ref_book():
      m.clear_table('reference_book')
      m.add_reference_book(Packager(ref_book_1))
      assert (m.get_count('reference_book')==1)
Example #24
0
 def registration(self, user_info):
     self.DBmanager.add_unconfirmed(Packager(user_info))
     self.log(
         'INFO',
         'User {} signed up. Waiting for librarians confirmation.'.format(
             user_info['name']))
Example #25
0
    def check_out_doc(self,
                      user_id,
                      doc_id,
                      type_bd='book',
                      returning_time=0,
                      date_when_took=datetime.now()):
        if self.DBmanager.select_label(type_bd, doc_id) is None:
            self.log('WARNING',
                     'Document with id {} not found.'.format(doc_id))
            return False, 'Document doesn`t exist'

        returning_time = self.get_returning_time(returning_time, type_bd,
                                                 doc_id, user_id)
        free_count = int(
            self.DBmanager.get_label("free_count", type_bd, doc_id))
        if free_count > 0:

            current_orders = eval(
                self.DBmanager.get_label("current_docs", "patrons", user_id))
            current_docs_id = []

            for order_id in current_orders:
                order = self.DBmanager.select_label('orders', order_id)
                if order[2] == type_bd:
                    current_docs_id.append(order[3])

            if doc_id in current_docs_id:
                self.log(
                    'INFO',
                    'User {} already have copy of document \'{}\''.format(
                        self.get_user(user_id)['name'],
                        self.get_document(doc_id, type_bd)['title']))
                return False, 'User alredy have copy of document'

            time = date_when_took
            out_of_time = time + timedelta(weeks=returning_time)
            time = str(time)
            out_of_time = str(out_of_time)
            time = time[:time.index(' ')]
            out_of_time = out_of_time[:out_of_time.index(' ')]

            order = {
                'date': time,
                'table': type_bd,
                "user_id": user_id,
                "doc_id": doc_id,
                "active": 0,
                'out_of_time': out_of_time,
                'renewed': 0
            }

            self.DBmanager.add_order(Packager(order))
            order_id = self.DBmanager.get_max_id('orders')
            history = eval(
                self.DBmanager.get_label("history", "patrons", user_id))
            current_orders += [order_id]
            history += [order_id]
            free_count -= 1

            self.DBmanager.edit_label(type_bd, ["free_count"], [free_count],
                                      doc_id)
            self.DBmanager.edit_label(
                "patrons", ["history", "current_docs"],
                [str(history), str(current_orders)], user_id)
            self.log(
                'INFO',
                'User {}({}) want to check out document \'{}\' for {} weeks. Returning time is {}'
                .format(
                    self.get_user(user_id)['name'],
                    self.get_user(user_id)['status'],
                    self.get_document(doc_id, type_bd)['title'],
                    returning_time, out_of_time))
            return True, 'OK'

        else:
            self.log(
                'INFO', 'Not enough copies of document \'{}\''.format(
                    self.get_document(doc_id, type_bd)['title']))
            return False, 'Not enough copies'