"Fossies" - the Fresh Open Source Software Archive

Member "Buku-4.2.2/tests/test_import_firefox_json.py" (2 May 2019, 10000 Bytes) of package /linux/privat/Buku-4.2.2.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Python source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file.

    1 import json
    2 from buku import import_firefox_json
    3 
    4 
    5 def test_load_from_empty():
    6     """test method."""
    7     # Arrange
    8     data = json.loads("{}")
    9 
   10     # Act
   11     items = import_firefox_json(data)
   12 
   13     # Assert
   14     count = sum(1 for _ in items)
   15     assert count == 0
   16 
   17 def test_load_full_entry():
   18     """test method."""
   19 
   20     # Arrange
   21     data = json.loads("""
   22         {
   23             "title" : "main",
   24             "typeCode": 2,
   25             "children": [
   26                 {
   27                 "title" : "title",
   28                 "typeCode": 2,
   29                 "children": [
   30                     {
   31                       "dateAdded": 1269200039653000,
   32                       "guid": "xxxydfalkj",
   33                       "id": 113,
   34                       "index": 0,
   35                       "lastModified": 1305978154986000,
   36                       "title": "entry title",
   37                       "type": "text/x-moz-place",
   38                       "typeCode": 1,
   39                       "tags" : "x,y",
   40                       "uri": "http://uri.com/abc?234&536",
   41                       "annos" : [{
   42                              "name": "bookmarkProperties/description",
   43                              "value": "desc"
   44                        }]
   45                     }]
   46              }]
   47         }""")
   48 
   49     # Act
   50     items = import_firefox_json(data)
   51 
   52     # Assert
   53     result = []
   54     for item in items:
   55         result.append(item)
   56 
   57     assert len(result) == 1
   58     assert result[0][0] == 'http://uri.com/abc?234&536'
   59     assert result[0][1] == 'entry title'
   60     assert result[0][2] == ',x,y,'
   61     assert result[0][3] == 'desc'
   62 
   63 
   64 def test_load_no_typecode():
   65     """test method."""
   66     # Arrange
   67     data = json.loads("""
   68         {
   69             "title" : "main",
   70             "typeCode": 2,
   71             "children": [
   72                 {
   73                 "title" : "title",
   74                 "children": [
   75                     {
   76                         "title" : "title1",
   77                         "uri" : "http://uri1",
   78                         "annos" : [{
   79                              "name": "bookmarkProperties/description",
   80                              "value": "desc"
   81                          }]
   82                     }]
   83                 }]
   84         }""")
   85 
   86     # Act
   87     items = import_firefox_json(data)
   88 
   89     # Assert
   90     result = []
   91     for item in items:
   92         result.append(item)
   93 
   94     assert len(result) == 0
   95 
   96 
   97 def test_load_invalid_typecode():
   98     """test method."""
   99     # Arrange
  100     data = json.loads("""
  101         {
  102             "title" : "title",
  103             "children": [
  104                 {
  105                     "title" : "title1",
  106                     "typeCode" : 99,
  107                     "uri" : "http://uri1",
  108                     "annos" : [{
  109                          "name": "bookmarkProperties/description",
  110                          "value": "desc"
  111                      }]
  112                 }]
  113         }""")
  114     # Act
  115     items = import_firefox_json(data)
  116 
  117     # Assert
  118     result = []
  119     for item in items:
  120         result.append(item)
  121 
  122     assert len(result) == 0
  123 
  124 def test_load_folder_with_no_children():
  125     """test method."""
  126 
  127     # Arrange
  128     data = json.loads("""
  129         {
  130             "title" : "title",
  131             "typeCode" : 2
  132         } """)
  133 
  134     # Act
  135     items = import_firefox_json(data)
  136 
  137     # Assert
  138     result = []
  139     for item in items:
  140         result.append(item)
  141 
  142     assert len(result) == 0
  143 
  144 def test_load_one_child():
  145     """test method."""
  146 
  147     # Arrange
  148     data = json.loads("""
  149         {
  150             "title" : "main",
  151             "typeCode" : 2,
  152             "children": [
  153                 {
  154                 "title" : "title",
  155                 "typeCode" : 2,
  156                 "children": [
  157                     {
  158                         "title" : "title1",
  159                         "typeCode" : 1,
  160                         "uri" : "http://uri1",
  161                         "annos" : [{
  162                              "name": "bookmarkProperties/description",
  163                              "value": "desc"
  164                           }]
  165                      }
  166                 ]}
  167            ]
  168         }
  169     """)
  170 
  171     # Act
  172     items = import_firefox_json(data)
  173 
  174     # Assert
  175     result = []
  176     for item in items:
  177         result.append(item)
  178 
  179     assert len(result) == 1
  180     assert result[0][0] == 'http://uri1'
  181     assert result[0][1] == 'title1'
  182     assert result[0][2] == ','
  183     assert result[0][3] == 'desc'
  184 
  185 def test_load_one_container_child():
  186     """test method."""
  187 
  188     # Arrange
  189     data = json.loads("""
  190         {
  191             "title" : "main",
  192             "typeCode": 2,
  193             "children": [
  194                 {
  195                 "title" : "title",
  196                 "typeCode" : 2,
  197                 "children": [
  198                     {
  199                         "title":"bookmark folder",
  200                         "typeCode":2
  201                     }]
  202              }]
  203          }""")
  204 
  205     # Act
  206     items = import_firefox_json(data)
  207 
  208     # Assert
  209     result = []
  210     for item in items:
  211         result.append(item)
  212 
  213     assert len(result) == 0
  214 
  215 def test_load_many_children():
  216     """test method."""
  217 
  218     # Arrange
  219     data = json.loads("""
  220         {
  221             "title" : "main",
  222             "typeCode" : 2,
  223             "children": [
  224                 {
  225                 "title":"Weitere Lesezeichen",
  226                 "typeCode":2,
  227                 "children": [
  228                     {"title":"title1","typeCode":1,"uri":"http://uri1.com/#more-74"},
  229                     {"title":"title2","typeCode":1,"uri":"http://uri2.com/xyz"},
  230                     {"title":"title3","typeCode":1,"uri":"http://uri3.com"}
  231                 ]}
  232             ]
  233         }
  234     """)
  235 
  236     # Act
  237     items = import_firefox_json(data)
  238 
  239     # Assert
  240     result = []
  241     for item in items:
  242         result.append(item)
  243 
  244     assert len(result) == 3
  245 
  246 def test_load_container_no_title():
  247     """test method."""
  248 
  249     # Arrange
  250     data = json.loads("""
  251         {
  252             "title" : "main",
  253             "typeCode" : 2,
  254             "children": [
  255                 {
  256                 "typeCode" : 2,
  257                 "children": [
  258                         {"title":"title1","typeCode":1,"uri":"http://uri.com"}
  259                 ]}
  260             ]
  261         }
  262     """)
  263 
  264     # Act
  265     items = import_firefox_json(data, add_bookmark_folder_as_tag=True)
  266 
  267     # Assert
  268     result = []
  269     for item in items:
  270         result.append(item)
  271 
  272     assert len(result) == 1
  273     assert result[0][0] == 'http://uri.com'
  274     assert result[0][2] == ',<no title>,'
  275 
  276 def test_load_hierarchical_container_without_ignore():
  277     """test method."""
  278 
  279     # Arrange
  280     data = json.loads("""
  281         {
  282             "title" : "main",
  283             "typeCode" : 2,
  284             "children": [
  285                 {
  286                 "title" : "title",
  287                 "typeCode" : 2,
  288                 "children": [
  289                      {"title":"title1","typeCode":1,"uri":"http://uri.com"}
  290                 ]
  291             }]
  292        }
  293     """)
  294 
  295     # Act
  296     items = import_firefox_json(data, add_bookmark_folder_as_tag=True)
  297 
  298     # Assert
  299     result = []
  300     for item in items:
  301         result.append(item)
  302 
  303     assert len(result) == 1
  304     assert result[0][0] == 'http://uri.com'
  305     assert result[0][2] == ',title,'
  306 
  307 def test_load_hierarchical_container_with_ignore():
  308     """test method."""
  309 
  310     # Arrange
  311     data = json.loads("""
  312         {
  313             "title" : "main",
  314             "typeCode" : 2,
  315             "children": [
  316                 {
  317                 "title" : "title",
  318                 "typeCode" : 2,
  319                 "root": "bookmarksMenuFolder",
  320                 "children": [
  321                         {
  322                             "title" : "title2",
  323                             "typeCode" : 2,
  324                             "children": [
  325                                 {"title":"title1","typeCode":1,"uri":"http://uri1.com/#more-74"}
  326                             ]
  327                         },
  328                         {"title":"title4","typeCode":1,"uri":"http://uri4.com/#more-74"}
  329                 ]
  330             }]
  331        }
  332     """)
  333 
  334     # Act
  335     items = import_firefox_json(data, add_bookmark_folder_as_tag=True)
  336 
  337     # Assert
  338     result = []
  339     for item in items:
  340         result.append(item)
  341 
  342     assert len(result) == 2
  343     assert result[0][0] == 'http://uri1.com/#more-74'
  344     assert result[1][0] == 'http://uri4.com/#more-74'
  345 
  346     assert result[0][2] == ',title2,'
  347     assert result[1][2] == ','
  348 
  349 def test_load_separator():
  350     """test method."""
  351 
  352     # Arrange
  353     data = json.loads("""
  354         {
  355             "title" : "main",
  356             "typeCode" : 2,
  357             "children": [
  358                 {
  359                 "title" : "title",
  360                 "typeCode" : 2,
  361                 "children": [
  362                     {
  363                       "title": "",
  364                       "type": "text/x-moz-place-separator",
  365                       "typeCode": 3
  366                     }]
  367             }]
  368          }""")
  369 
  370     # Act
  371     items = import_firefox_json(data)
  372 
  373     # Assert
  374     result = []
  375     for item in items:
  376         result.append(item)
  377 
  378     assert len(result) == 0
  379 
  380 def test_load_multiple_tags():
  381     """test method."""
  382     # Arrange
  383     data = json.loads("""
  384         {
  385             "title" : "main",
  386             "typeCode": 2,
  387             "children": [
  388                 {
  389                 "title" : "title",
  390                 "typeCode": 2,
  391                 "children": [
  392                     {
  393                         "title" : "title1",
  394                         "uri" : "http://uri1",
  395                         "tags" : "tag1, tag2",
  396                         "typeCode": 1,
  397                         "annos" : [{
  398                              "name": "bookmarkProperties/description",
  399                              "value": "desc"
  400                          }]
  401                     }]
  402             }]
  403         }""")
  404 
  405     # Act
  406     items = import_firefox_json(data)
  407 
  408     # Assert
  409     result = []
  410     for item in items:
  411         result.append(item)
  412 
  413     assert len(result) == 1
  414     assert result[0][2] == ",tag1,tag2,"