Ejemplo n.º 1
0
    def test__create_items_from_assets(self):
        items_keys = [
            'classification', 'additionalClassifications', 'address', 'unit',
            'quantity', 'location', 'id'
        ]
        documents_keys = [
            'hash', 'description', 'title', 'format', 'documentType'
        ]
        with open('{}/asset.json'.format(self.test_files_path), 'r') as af:
            asset_dict = json.loads(af.read())
        with open('{}/document.json'.format(self.test_files_path), 'r') as df:
            document_dict = json.loads(df.read())
        with open('{}/register_response.json'.format(self.test_files_path),
                  'r') as rf:
            register_response_dict = json.loads(rf.read())

        # Prepare mocked clients
        mock_rc = mock.MagicMock()
        mock_rc.get_asset.return_value = munchify(asset_dict)
        mock_rc.ds_client.register_document_upload.return_value = \
            munchify(register_response_dict)
        asset_ids = ['580d38b347134ac6b0ee3f04e34b9770']

        convoy = Convoy(self.config)
        convoy.assets_client = mock_rc
        convoy.api_client = mock_rc
        items, documents = convoy._create_items_from_assets(asset_ids)
        self.assertEqual(convoy.documents_transfer_queue.qsize(), 2)
        transfer_item = convoy.documents_transfer_queue.get()
        self.assertEqual(transfer_item['get_url'],
                         document_dict['data']['url'])
        self.assertNotEqual(transfer_item['get_url'],
                            transfer_item['upload_url'])
        self.assertEqual(len(items), 2)
        self.assertEqual(len(documents), 2)
        for k in items_keys:
            self.assertEqual(asset_dict['data'].get(k), items[0].get(k))
        for k in documents_keys:
            self.assertEqual(documents[0].get(k),
                             asset_dict['data']['documents'][0].get(k))
        self.assertNotEqual(documents[0]['url'],
                            asset_dict['data']['documents'][0]['url'])
        self.assertEqual(documents[0]['url'],
                         register_response_dict['data']['url'])

        # Test with asset without documents
        del asset_dict['data']['documents']
        del asset_dict['data']['items'][0]['documents']
        mock_rc.get_asset.return_value = munchify(asset_dict)
        items, documents = convoy._create_items_from_assets(asset_ids)
        self.assertEqual(len(items), 2)
        self.assertEqual(len(documents), 0)
        self.assertEqual(convoy.documents_transfer_queue.qsize(), 1)
    def test_prepare_auction(self, mock_raise, mock_request):
        a_doc = Munch({
            'id': uuid4().hex,  # this is auction id
            'merchandisingObject': uuid4().hex
        })
        api_auction_doc = {
            'data': {
                'id': a_doc['id'],
                'status': 'pending.verifcation',
                'merchandisingObject': a_doc['merchandisingObject']
            }
        }
        lc = mock.MagicMock()
        lc.get_lot.return_value = munchify({
            'data': {
                'id': a_doc['merchandisingObject'],
                'lotIdentifier': u'Q81318b19827',
                'status': u'verification',
                'assets': ['580d38b347134ac6b0ee3f04e34b9770']
            }
        })
        api_client = mock.MagicMock()
        api_client.get_resource_item.return_value = munchify(api_auction_doc)
        convoy = Convoy(self.config)
        convoy.api_client = api_client
        convoy.lots_client = lc
        auction_doc = convoy.prepare_auction(a_doc)
        self.assertEqual(None, auction_doc)
        convoy.lots_client.get_lot.assert_called_with(
            a_doc['merchandisingObject'])

        # Preparing for test with valid status and documents
        with open('{}/asset.json'.format(self.test_files_path), 'r') as af:
            asset_dict = json.loads(af.read())
        with open('{}/register_response.json'.format(self.test_files_path),
                  'r') as rf:
            register_response_dict = json.loads(rf.read())

        # Prepare mocked clients
        mock_rc = mock.MagicMock()
        mock_rc.get_asset.return_value = munchify(asset_dict)
        mock_rc.ds_client.register_document_upload.return_value = \
            munchify(register_response_dict)
        asset_ids = ['580d38b347134ac6b0ee3f04e34b9770']

        convoy.assets_client = mock_rc
        lc.get_lot.return_value = munchify({
            'data': {
                'id': a_doc['merchandisingObject'],
                'lotIdentifier': u'Q81318b19827',
                'status': u'active.salable',
                'assets': ['580d38b347134ac6b0ee3f04e34b9770']
            }
        })
        items, documents = convoy._create_items_from_assets(asset_ids)
        convoy._create_items_from_assets = mock.MagicMock(
            return_value=(items, documents))

        # Needed to call mock function before prepare_auction, to
        # check if parameted of this call and call from prepare_auction is equal
        expected = {'data': {'items': items, 'dgfID': u'Q81318b19827'}}
        # convoy.api_client.patch_resource_item(expected)

        # convoy.prepare_auction(a_doc)
        lot = convoy._receive_lot(a_doc)
        convoy._form_auction(lot, a_doc)
        convoy.api_client.patch_resource_item.assert_called_with(
            a_doc['id'], expected)
        convoy._activate_auction(lot, a_doc)
        self.assertEqual(convoy.documents_transfer_queue.qsize(), 2)
        convoy.lots_client.get_lot.assert_called_with(
            a_doc['merchandisingObject'])
        convoy.lots_client.patch_resource_item.assert_called_with(
            a_doc['merchandisingObject'],
            {'data': {
                'status': 'active.auction'
            }})
        convoy._create_items_from_assets.assert_called_with(asset_ids)
        patched_api_auction_doc = {'data': {'status': 'active.tendering'}}
        convoy.api_client.get_resource_item.assert_called_with(a_doc['id'])
        convoy.api_client.patch_resource_item.assert_called_with(
            api_auction_doc['data']['id'], patched_api_auction_doc)
        convoy.api_client.create_resource_item_subitem.assert_called_with(
            a_doc['id'], {'data': documents[1]}, 'documents')

        # convoy.prepare_auction(a_doc) with active.awaiting lot
        lc.get_lot.return_value = munchify({
            'data': {
                'id': a_doc['merchandisingObject'],
                'lotIdentifier': u'Q81318b19827',
                'status': u'active.awaiting',
                'assets': ['580d38b347134ac6b0ee3f04e34b9770'],
                'auctions': [a_doc['id']]
            }
        })
        items, documents = convoy._create_items_from_assets(asset_ids)
        convoy._create_items_from_assets = mock.MagicMock(
            return_value=(items, documents))

        lot = convoy._receive_lot(a_doc)
        self.assertEqual(len(lot.auctions), 1)
        convoy._form_auction(lot, a_doc)
        convoy.api_client.patch_resource_item.assert_called_with(
            a_doc['id'], expected)
        convoy._activate_auction(lot, a_doc)
        self.assertEqual(convoy.documents_transfer_queue.qsize(), 2)
        convoy.lots_client.get_lot.assert_called_with(
            a_doc['merchandisingObject'])
        convoy.lots_client.patch_resource_item.assert_called_with(
            a_doc['merchandisingObject'],
            {'data': {
                'status': 'active.auction'
            }})
        convoy._create_items_from_assets.assert_called_with(asset_ids)
        convoy.api_client.get_resource_item.assert_called_with(a_doc['id'])
        convoy.api_client.patch_resource_item.assert_called_with(
            api_auction_doc['data']['id'], patched_api_auction_doc)
        convoy.api_client.create_resource_item_subitem.assert_called_with(
            a_doc['id'], {'data': documents[1]}, 'documents')

        # convoy.prepare_auction(a_doc) with active.auction lot
        lc.get_lot.return_value = munchify({
            'data': {
                'id': a_doc['merchandisingObject'],
                'lotIdentifier': u'Q81318b19827',
                'status': u'active.auction',
                'assets': ['580d38b347134ac6b0ee3f04e34b9770'],
                'auctions': [a_doc['id']]
            }
        })
        convoy._receive_lot(a_doc)
        patched_api_auction_doc = {'data': {'status': 'active.tendering'}}
        convoy.api_client.patch_resource_item.assert_called_with(
            api_auction_doc['data']['id'], patched_api_auction_doc)
Ejemplo n.º 3
0
    def test_prepare_auction(self):
        a_doc = Munch({
            'id': uuid4().hex,  # this is auction id
            'merchandisingObject': uuid4().hex
        })
        api_auction_doc = {
            'data': {
                'id': a_doc['id'],
                'status': 'pending.verifcation',
                'merchandisingObject': a_doc['merchandisingObject']
            }
        }
        lc = mock.MagicMock()
        lc.get_lot.return_value = munchify({
            'data': {
                'id': a_doc['merchandisingObject'],
                'status': u'verification',
                'assets': ['580d38b347134ac6b0ee3f04e34b9770']
            }
        })
        api_client = mock.MagicMock()
        api_client.get_resource_item.return_value = munchify(api_auction_doc)
        convoy = Convoy(self.config)
        convoy.api_client = api_client
        convoy.lots_client = lc
        auction_doc = convoy.prepare_auction(a_doc)
        self.assertEqual(None, auction_doc)
        convoy.lots_client.get_lot.assert_called_with(
            a_doc['merchandisingObject'])

        # Preparing for test with valid status and documents
        with open('{}/asset.json'.format(self.test_files_path), 'r') as af:
            asset_dict = json.loads(af.read())
        with open('{}/register_response.json'.format(self.test_files_path),
                  'r') as rf:
            register_response_dict = json.loads(rf.read())

        # Prepare mocked clients
        mock_rc = mock.MagicMock()
        mock_rc.get_asset.return_value = munchify(asset_dict)
        mock_rc.ds_client.register_document_upload.return_value = \
            munchify(register_response_dict)
        asset_ids = ['580d38b347134ac6b0ee3f04e34b9770']

        convoy.assets_client = mock_rc
        lc.get_lot.return_value = munchify({
            'data': {
                'id': a_doc['merchandisingObject'],
                'status': u'active.salable',
                'assets': ['580d38b347134ac6b0ee3f04e34b9770']
            }
        })
        items, documents = convoy._create_items_from_assets(asset_ids)
        convoy._create_items_from_assets = mock.MagicMock(
            return_value=(items, documents))
        auction_doc = convoy.prepare_auction(a_doc)
        self.assertEqual(convoy.documents_transfer_queue.qsize(), 2)
        convoy.lots_client.get_lot.assert_called_with(
            a_doc['merchandisingObject'])
        convoy.lots_client.patch_resource_item.assert_called_with(
            a_doc['merchandisingObject'],
            {'data': {
                'status': 'active.auction'
            }})
        convoy._create_items_from_assets.assert_called_with(asset_ids)
        patched_api_auction_doc = {'data': {'status': 'active.tendering'}}
        convoy.api_client.get_resource_item.assert_called_with(a_doc['id'])
        convoy.api_client.patch_resource_item.assert_called_with(
            api_auction_doc['data']['id'], patched_api_auction_doc)
        convoy.api_client.create_resource_item_subitem.assert_called_with(
            a_doc['id'], {'data': documents[1]}, 'documents')