def test_update_counts(self): with self.app.app_context(): stock.update_counts('TE0002', 5, 'mybatch', 'this works') obj = self.app.mongo.db.stock.find_one({'_id': 'TE0001'}) self.assertIsNotNone(obj) self.assertEqual(90, obj.get('quantity')) # only the direct BOM children are considered obj = self.app.mongo.db.stock.find_one({'_id': 'TE0002'}) self.assertIsNotNone(obj) self.assertEqual(40, obj.get('quantity')) obj = self.app.mongo.db.stock.find_one({'_id': 'TE0003'}) self.assertIsNotNone(obj) self.assertEqual(15, obj.get('quantity')) obj = self.app.mongo.db.stock_batches.find_one({ 'partno': 'TE0002', 'name': 'mybatch' }) self.assertIsNotNone(obj) self.assertEqual(5, obj.get('quantity')) obj = self.app.mongo.db.stock_history.find_one({ 'partno': 'TE0002', 'delta': 5, 'message': 'this works' }) self.assertIsNotNone(obj) obj = self.app.mongo.db.stock.find_one({'_id': 'TE0004'}) self.assertIsNone(obj) stock.update_counts('TE0004', 5, '', 'inserted') obj = self.app.mongo.db.stock.find_one({'_id': 'TE0004'}) self.assertIsNotNone(obj) self.assertEqual(5, obj.get('quantity')) with self.assertRaises(ValueError): stock.update_counts('TE0005', 10, '', '') # component does not exist
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')