Esempio n. 1
0
    def test_update_batch(self):
        with self.app.app_context():
            stock.update_batch('TE0001', 'batch1', 10)
            stock.update_batch('TE0001', 'newbatch', 15)
            stock.update_batch('TE0001', 'otherbatch', 0)  # should be a no-op
            stock.update_batch('TE0001', '', 20)  # should be a no-op

            obj = self.app.mongo.db.stock_batches.find_one({
                'partno': 'TE0001',
                'name': 'batch1'
            })
            self.assertIsNotNone(obj)
            self.assertEqual(20, obj.get('quantity'))
            obj = self.app.mongo.db.stock_batches.find_one({
                'partno': 'TE0001',
                'name': 'newbatch'
            })
            self.assertIsNotNone(obj)
            self.assertEqual(15, obj.get('quantity'))
            obj = self.app.mongo.db.stock_batches.find_one({
                'partno': 'TE0001',
                'name': 'otherbatch'
            })
            self.assertIsNone(obj)
            obj = self.app.mongo.db.stock_batches.find_one({
                'partno': 'TE0001',
                'name': ''
            })
            self.assertIsNone(obj)
Esempio n. 2
0
def _store_items(data):
    """
    Saves the provided items in the database.
    Applied transformations:
    The 'serial' key is transformed to the '_id' key
    A 'comment' key is pushed to the comments array
    """
    quantities = defaultdict(int)  # for the stock update
    now = datetime.now()
    for idx, item in enumerate(data):
        assert 'serial' in item
        assert 'partno' in item
        item['_id'] = item.pop('serial')
        if 'project' not in item:
            item['project'] = ''
        if 'status' in item:
            item['available'] = not _is_unavailable(item['partno'], item['status'])
        else:
            item['status'] = ''
            item['available'] = True
        comments = [create_comment('[Auto] created', now)]
        comment = item.pop('comment', None)
        if comment:
            comments.append(create_comment(comment, now))
        item['comments'] = comments
        current_app.mongo.db.items.insert(item)

        # count the number of occurrences per model number
        partno = PartNumber(item.get('partno'))
        quantities[partno.base_number] += 1

        # update the batch information if present
        batch = item.get('batch')
        if batch:
            update_batch(partno.base_number, batch, 1)

    # add the items to the stock as well
    for partno, value in quantities.items():
        update_counts(partno, value, None, 'items added')