Repository: jazzband/tablib
Pull request #535 (2023-01-21 10:18:24)

fix bug when checking empty file. add more coverage
While adding some coverage discovered the using `len(pickle)` will raise and error since for empty files `pickle` is `None`.

Change the if condition to just `not pickle` since it will also work for checking if the list is empty.

Added two test cases to cover more branches.

codecov[bot]: # [Codecov](https://codecov.io/gh/jazzband/tablib/pull/535?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=jazzband) Report
> Merging [#535](https://codecov.io/gh/jazzband/tablib/pull/535?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=jazzband) (281fea9) into [master](https://codecov.io/gh/jazzband/tablib/commit/0a3511fb9789b2e9f0d58739fccffde8db9f2635?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=jazzband) (0a3511f) will **increase** coverage by `0.18%`.
> The diff coverage is `100.00%`.

```diff
@@            Coverage Diff             @@
##           master     #535      +/-   ##
==========================================
+ Coverage   90.97%   91.16%   +0.18%     
==========================================
  Files          28       28              
  Lines        2660     2671      +11     
==========================================
+ Hits         2420     2435      +15     
+ Misses        240      236       -4     
```


| [Impacted Files](https://codecov.io/gh/jazzband/tablib/pull/535?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=jazzband) | Coverage Δ | |
|---|---|---|
| [src/tablib/core.py](https://codecov.io/gh/jazzband/tablib/pull/535?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=jazzband#diff-c3JjL3RhYmxpYi9jb3JlLnB5) | `85.01% <100.00%> (+0.93%)` | :arrow_up: |
| [tests/test\_tablib.py](https://codecov.io/gh/jazzband/tablib/pull/535?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=jazzband#diff-dGVzdHMvdGVzdF90YWJsaWIucHk=) | `98.66% <100.00%> (+0.01%)` | :arrow_up: |

:mega: We’re building smart automated test selection to slash your CI/CD build times. [Learn more](https://about.codecov.io/iterative-testing/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=jazzband)

claudep: Maybe just adapt the commit message to specify this concerns the yaml format.
claudep: Ah yes, NamedTemporaryFile constructor is already returning a file-like object, so no need to reopen later (and Windows cannot reopen when it's already open).
llazzaro: * git commit message fixed
* fix windows broken test

Patch:
diff --git a/src/tablib/core.py b/src/tablib/core.py
index 0a67570..5d198f7 100644
--- a/src/tablib/core.py
+++ b/src/tablib/core.py
@@ -334,7 +334,8 @@ class Dataset:
 
         """
         error_details = "Please check format documentation https://tablib.readthedocs.io/en/stable/formats.html#yaml"
-        if not len(pickle):
+
+        if not pickle:
             return
         
         if not isinstance(pickle, list):


Test patch:
diff --git a/tests/test_tablib.py b/tests/test_tablib.py
index 9704845..1039871 100755
--- a/tests/test_tablib.py
+++ b/tests/test_tablib.py
@@ -5,6 +5,7 @@ import datetime
 import doctest
 import json
 import pickle
+import tempfile
 import unittest
 from collections import OrderedDict
 from decimal import Decimal
@@ -318,6 +319,11 @@ class TablibTestCase(BaseTestCase):
             dset = tablib.Dataset().load(fh, 'xlsx')
         self.assertEqual(eval(dset.json)[0]['last_name'], 'Adams')
 
+    def test_empty_file(self):
+        tmp_file = tempfile.NamedTemporaryFile()
+        dset = tablib.Dataset().load(tmp_file, 'yaml')
+        self.assertEqual(dset.json, '[]')
+
     def test_auto_format_detect(self):
         """Test auto format detection."""
         # html, jira, latex, rst are export only.
@@ -1171,6 +1177,12 @@ class JSONTests(BaseTestCase):
 
         self.assertEqual(founders_json, expected_json)
 
+    def test_json_list_of_lists(self):
+        input_json = "[[1,2],[3,4]]"
+        expected_yaml = "- [1, 2]\n- [3, 4]\n"
+        dset = tablib.Dataset().load(in_stream=input_json, format="json")
+        self.assertEqual(dset.export("yaml"), expected_yaml)
+
 
 class YAMLTests(BaseTestCase):
     def test_yaml_format_detect(self):