def test_compile_txt(self, mock_open: mock.Mock) -> None: """Tests compiling tabbed text to AnimData.D2.""" self.maxDiff = 5000 # pylint: disable=invalid-name txt_file = StringIO(VALID_TXT, newline="") animdata_d2_file = BytesIO() animdata_d2_file.close = mock.Mock(spec=animdata_d2_file.close) mock_open.side_effect = [txt_file, animdata_d2_file] main("compile --txt AnimData.txt-file-name AnimData.D2-file-name".split()) mock_open.assert_has_calls( [ mock.call("AnimData.txt-file-name", newline=""), mock.call("AnimData.D2-file-name", mode="wb"), ] ) animdata_d2_file.close.assert_called_once_with() self.assertEqual(animdata_d2_file.getvalue(), VALID_ANIMDATA)
def test_decompile_json(self, mock_open: mock.Mock) -> None: """Tests decompiling AnimData.D2 to JSON.""" self.maxDiff = 1000 animdata_d2_file = BytesIO(VALID_ANIMDATA) json_file = StringIO() json_file.close = mock.Mock(spec=json_file.close) mock_open.side_effect = [animdata_d2_file, json_file] main("decompile --json AnimData.D2-file-name AnimData.json-file-name".split()) mock_open.assert_has_calls( [ mock.call("AnimData.D2-file-name", mode="rb"), mock.call("AnimData.json-file-name", mode="w"), ] ) json_file.close.assert_called_once_with() self.assertEqual(json_file.getvalue(), VALID_JSON)
def test_compile_no_dedupe(self, mock_open: mock.Mock) -> None: """By default, records with duplicate COF names cause warnings to be logged, but are not deduplicated.""" animdata_d2_file = BytesIO(DUPLICATE_COF_ANIMDATA) json_file = StringIO() json_file.close = mock.Mock(spec=json_file.close) mock_open.side_effect = [animdata_d2_file, json_file] with self.assertLogs(d2animdata.logger, logging.WARNING) as logs: main("decompile --json AnimData.D2-file AnimData.json-file".split()) self.assertEqual( logs.output, ["WARNING:d2animdata:Duplicate entry found: BVS1HTH"] ) mock_open.assert_has_calls( [ mock.call("AnimData.D2-file", mode="rb"), mock.call("AnimData.json-file", mode="w"), ] ) json_file.close.assert_called_once_with() self.assertEqual(json_file.getvalue(), DUPLICATE_COF_JSON)
def test_compile_dedupe(self, mock_open: mock.Mock) -> None: """Specifying --dedupe causes records with duplicate COF names to be deduplicated, always favoring the first item.""" json_file = StringIO(DUPLICATE_COF_JSON) animdata_d2_file = BytesIO() animdata_d2_file.close = mock.Mock(spec=animdata_d2_file.close) mock_open.side_effect = [json_file, animdata_d2_file] with self.assertLogs(d2animdata.logger, logging.WARNING) as logs: main("compile --json --dedupe AnimData.json-file AnimData.D2-file".split()) self.assertEqual( logs.output, ["WARNING:d2animdata:Duplicate entry found: BVS1HTH"] ) mock_open.assert_has_calls( [ mock.call("AnimData.json-file"), mock.call("AnimData.D2-file", mode="wb"), ] ) animdata_d2_file.close.assert_called_once_with() self.assertEqual(animdata_d2_file.getvalue(), DEDUPED_ANIMDATA)