Karim shoair commited on
Commit
77fe3eb
·
1 Parent(s): ee99d12

Moving parser tests to a better structure

Browse files
tests/parser/__init__.py ADDED
File without changes
tests/parser/test_automatch.py ADDED
@@ -0,0 +1,56 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import unittest
2
+
3
+ from scrapling import Adaptor
4
+
5
+
6
+ class TestParserAutoMatch(unittest.TestCase):
7
+
8
+ def test_element_relocation(self):
9
+ """Test relocating element after structure change"""
10
+ original_html = '''
11
+ <div class="container">
12
+ <section class="products">
13
+ <article class="product" id="p1">
14
+ <h3>Product 1</h3>
15
+ <p class="description">Description 1</p>
16
+ </article>
17
+ <article class="product" id="p2">
18
+ <h3>Product 2</h3>
19
+ <p class="description">Description 2</p>
20
+ </article>
21
+ </section>
22
+ </div>
23
+ '''
24
+ changed_html = '''
25
+ <div class="new-container">
26
+ <div class="product-wrapper">
27
+ <section class="products">
28
+ <article class="product new-class" data-id="p1">
29
+ <div class="product-info">
30
+ <h3>Product 1</h3>
31
+ <p class="new-description">Description 1</p>
32
+ </div>
33
+ </article>
34
+ <article class="product new-class" data-id="p2">
35
+ <div class="product-info">
36
+ <h3>Product 2</h3>
37
+ <p class="new-description">Description 2</p>
38
+ </div>
39
+ </article>
40
+ </section>
41
+ </div>
42
+ </div>
43
+ '''
44
+
45
+ old_page = Adaptor(original_html, url='example.com', auto_match=True, debug=True)
46
+ new_page = Adaptor(changed_html, url='example.com', auto_match=True, debug=True)
47
+
48
+ # 'p1' was used as ID and now it's not and all the path elements have changes
49
+ # Also at the same time testing auto-match vs combined selectors
50
+ _ = old_page.css('#p1, #p2', auto_save=True)[0]
51
+ relocated = new_page.css('#p1', auto_match=True)
52
+
53
+ self.assertIsNotNone(relocated)
54
+ self.assertEqual(relocated[0].attrib['data-id'], 'p1')
55
+ self.assertTrue(relocated[0].has_class('new-class'))
56
+ self.assertEqual(relocated[0].css('.new-description')[0].text, 'Description 1')
tests/{test_parser_functions.py → parser/test_general.py} RENAMED
@@ -112,7 +112,7 @@ class TestParser(unittest.TestCase):
112
 
113
  def test_find_similar_elements(self):
114
  """Test Finding similar elements of an element"""
115
- first_product = self.page.css('.product')[0]
116
  similar_products = first_product.find_similar()
117
  self.assertEqual(len(similar_products), 2)
118
 
@@ -265,56 +265,6 @@ class TestParser(unittest.TestCase):
265
  self.assertEqual(attr_json, {'jsonable': 'data'})
266
  self.assertEqual(type(self.page.css('#products')[0].attrib.json_string), bytes)
267
 
268
- def test_element_relocation(self):
269
- """Test relocating element after structure change"""
270
- original_html = '''
271
- <div class="container">
272
- <section class="products">
273
- <article class="product" id="p1">
274
- <h3>Product 1</h3>
275
- <p class="description">Description 1</p>
276
- </article>
277
- <article class="product" id="p2">
278
- <h3>Product 2</h3>
279
- <p class="description">Description 2</p>
280
- </article>
281
- </section>
282
- </div>
283
- '''
284
- changed_html = '''
285
- <div class="new-container">
286
- <div class="product-wrapper">
287
- <section class="products">
288
- <article class="product new-class" data-id="p1">
289
- <div class="product-info">
290
- <h3>Product 1</h3>
291
- <p class="new-description">Description 1</p>
292
- </div>
293
- </article>
294
- <article class="product new-class" data-id="p2">
295
- <div class="product-info">
296
- <h3>Product 2</h3>
297
- <p class="new-description">Description 2</p>
298
- </div>
299
- </article>
300
- </section>
301
- </div>
302
- </div>
303
- '''
304
-
305
- old_page = Adaptor(original_html, url='example.com', auto_match=True, debug=True)
306
- new_page = Adaptor(changed_html, url='example.com', auto_match=True, debug=True)
307
-
308
- # 'p1' was used as ID and now it's not and all the path elements have changes
309
- # Also at the same time testing auto-match vs combined selectors
310
- _ = old_page.css('#p1, #p2', auto_save=True)[0]
311
- relocated = new_page.css('#p1', auto_match=True)
312
-
313
- self.assertIsNotNone(relocated)
314
- self.assertEqual(relocated[0].attrib['data-id'], 'p1')
315
- self.assertTrue(relocated[0].has_class('new-class'))
316
- self.assertEqual(relocated[0].css('.new-description')[0].text, 'Description 1')
317
-
318
  def test_performance(self):
319
  """Test parsing and selecting speed"""
320
  import time
 
112
 
113
  def test_find_similar_elements(self):
114
  """Test Finding similar elements of an element"""
115
+ first_product = self.page.css_first('.product')
116
  similar_products = first_product.find_similar()
117
  self.assertEqual(len(similar_products), 2)
118
 
 
265
  self.assertEqual(attr_json, {'jsonable': 'data'})
266
  self.assertEqual(type(self.page.css('#products')[0].attrib.json_string), bytes)
267
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
268
  def test_performance(self):
269
  """Test parsing and selecting speed"""
270
  import time