def test_is_package(self, mock_get_data): PostnordPackage.set_apikey("FAKE_APIKEY") mock_get_data.return_value = json.load(open(os.path.join(self.dir, "postnord_invalid_id.json"), "r")) self.assertFalse(PostnordPackage.is_package("FAKE_ID")) mock_get_data.return_value = json.load(open(os.path.join(self.dir, "postnord_no_package.json"), "r")) self.assertFalse(PostnordPackage.is_package("FAKE_ID")) mock_get_data.return_value = json.load(open(os.path.join(self.dir, "postnord_package.json"), "r")) self.assertTrue(PostnordPackage.is_package("FAKE_ID"))
def test_package_format(self, mock_get_data): PostnordPackage.set_apikey("FAKE_APIKEY") package = PostnordPackage("FAKE_ID") mock_get_data.return_value = json.load(open(os.path.join(self.dir, "postnord_package.json"))) package.update() self.assertEqual(mock_get_data.call_count, 1) self.assertEqual(package.id, "FAKE_ID") self.assertEqual(package.consignee, "22738 LUND, Sweden") self.assertEqual(package.consignor, "Dustin Sverige, Metallvägen 36, 19572 Rosersberg, Sweden")
def test_is_package(self, mock_get_data): PostnordPackage.set_apikey("FAKE_APIKEY") mock_get_data.return_value = json.load( open(os.path.join(self.dir, "postnord_invalid_id.json"), "r")) self.assertFalse(PostnordPackage.is_package("FAKE_ID")) mock_get_data.return_value = json.load( open(os.path.join(self.dir, "postnord_no_package.json"), "r")) self.assertFalse(PostnordPackage.is_package("FAKE_ID")) mock_get_data.return_value = json.load( open(os.path.join(self.dir, "postnord_package.json"), "r")) self.assertTrue(PostnordPackage.is_package("FAKE_ID"))
def test_is_package(self, mock_requests): PostnordPackage.set_apikey("FAKE_APIKEY") with open(os.path.join(self.dir, "postnord_invalid_id.json"), "r") as f: mock_requests.get(PostnordPackage._get_url("FAKE_ID"), text=f.read()) self.assertFalse(PostnordPackage.is_package("FAKE_ID")) with open(os.path.join(self.dir, "postnord_no_package.json"), "r") as f: mock_requests.get(PostnordPackage._get_url("FAKE_ID"), text=f.read()) self.assertFalse(PostnordPackage.is_package("FAKE_ID")) with open(os.path.join(self.dir, "postnord_broken_package.json"), "r") as f: mock_requests.get(PostnordPackage._get_url("FAKE_ID"), text=f.read()) self.assertFalse(PostnordPackage.is_package("FAKE_ID")) with open(os.path.join(self.dir, "postnord_package.json"), "r") as f: mock_requests.get(PostnordPackage._get_url("FAKE_ID"), text=f.read()) self.assertTrue(PostnordPackage.is_package("FAKE_ID"))
def test_fetch(self, mock_requests): PostnordPackage.set_apikey("FAKE_APIKEY") package = PostnordPackage("FAKE_ID") package.on_event = mock.MagicMock() with open(os.path.join(self.dir, "postnord_package_event1.json"), "r") as f: mock_requests.get(PostnordPackage._get_url("FAKE_ID"), text=f.read()) package.update() self.assertEqual(package.on_event.call_args[0][0].description, "The electronic shipping instructions have been received (Dustin Sverige)") self.assertEqual(package.on_event.call_count, 1) with open(os.path.join(self.dir, "postnord_package_event2.json"), "r") as f: mock_requests.get(PostnordPackage._get_url("FAKE_ID"), text=f.read()) package.update() self.assertEqual(package.on_event.call_args[0][0].description, "The shipment item is under transportation (Veddesta)") self.assertEqual(package.on_event.call_count, 2)
def test_fetch_broken(self, mock_requests): PostnordPackage.set_apikey("FAKE_APIKEY") package = PostnordPackage("FAKE_ID") with open(os.path.join(self.dir, "postnord_broken_package.json"), "r") as f: mock_requests.get(PostnordPackage._get_url("FAKE_ID"), text=f.read()) package.update()
def test_fetch(self, mock_get_data): PostnordPackage.set_apikey("FAKE_APIKEY") package = PostnordPackage("FAKE_ID") package.on_event = mock.MagicMock() mock_get_data.return_value = json.load( open(os.path.join(self.dir, "postnord_package_event1.json"), "r")) package.update() self.assertEqual( package.on_event.call_args[0][0].description, "The electronic shipping instructions have been received (Dustin Sverige)" ) self.assertEqual(package.on_event.call_count, 1) mock_get_data.return_value = json.load( open(os.path.join(self.dir, "postnord_package_event2.json"), "r")) package.update() self.assertEqual( package.on_event.call_args[0][0].description, "The shipment item is under transportation (Veddesta)") self.assertEqual(package.on_event.call_count, 2)
def test_package_format(self, mock_get_data): PostnordPackage.set_apikey("FAKE_APIKEY") package = PostnordPackage("FAKE_ID") mock_get_data.return_value = json.load( open(os.path.join(self.dir, "postnord_package.json"))) package.update() self.assertEqual(mock_get_data.call_count, 1) self.assertEqual(package.id, "FAKE_ID") self.assertEqual(package.consignee, "22738 LUND, Sweden") self.assertEqual( package.consignor, "Dustin Sverige, Metallvägen 36, 19572 Rosersberg, Sweden")
def test_package_format(self, mock_requests): PostnordPackage.set_apikey("FAKE_APIKEY") package = PostnordPackage("FAKE_ID") with open(os.path.join(self.dir, "postnord_package.json"), "r") as f: mock_requests.get(PostnordPackage._get_url("FAKE_ID"), text=f.read()) package.update() self.assertEqual(package.id, "FAKE_ID") self.assertEqual(package.consignee, "22738 LUND, Sweden") self.assertEqual(package.consignor, "Dustin Sverige, Metallvägen 36, 19572 Rosersberg, Sweden") package2 = PostnordPackage("FAKE_ID") with open(os.path.join(self.dir, "postnord_package2.json"), "r") as f: mock_requests.get(PostnordPackage._get_url("FAKE_ID"), text=f.read()) package2.update() self.assertEqual(package2.id, "FAKE_ID") self.assertEqual(package2.consignee, "41762") self.assertEqual(package2.consignor, "CDON.COM") package3 = PostnordPackage("FAKE_ID2") with open(os.path.join(self.dir, "postnord_package_import.json"), "r") as f: mock_requests.get(PostnordPackage._get_url("FAKE_ID2"), text=f.read()) package3.update() self.assertEqual(package3.id, "FAKE_ID2") self.assertEqual(package3.consignee, "41327 GÖTEBORG, Sweden") self.assertEqual(package3.consignor, "Posten Logistik AB / IMPORT, 10500 STOCKHOLM")