File size: 2,030 Bytes
31af2b2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import pytest
import wikipedia

def test_get_wikipedia_table_of_contents_happy():
    # use the function defined earlier
    url = "https://en.wikipedia.org/wiki/Mercedes_Sosa"
    toc = wikipedia.get_wikipedia_article_sections(url)

    expect = """Sections within article:
- Life
- Awards
- Death
- Tributes
- Discography
  - Studio_albums
  - EPs
  - Live_albums
  - Compilation_albums
- Filmography
- Further_reading
- References
- External_links"""
    assert toc == expect


def test_get_wikipedia_table_of_contents_404():
    # Test with an invalid url
    bad_url = "https://en.wikipedia.org/wiki/ThisArticleDoesNotExist123456"
    toc_bad = wikipedia.get_wikipedia_article_sections(bad_url)
    assert "could not extract table of contents" in toc_bad or "error" in toc_bad.lower()

def test_get_wikipedia_table_of_contents_not_wikipedia():

    # Test with a non-Wikipedia url
    not_wiki = "https://www.google.com"
    toc_not_wiki = wikipedia.get_wikipedia_article_sections(not_wiki)
    assert "the provided url does not appear to be a valid wikipedia page." in toc_not_wiki.lower()

def test_extract_wikipedia_section_1():
    section = wikipedia.extract_wikipedia_section("https://en.wikipedia.org/wiki/Mercedes_Sosa", "Discography")
    assert "Sosa in 2005, with Argentina's then-First Lady (later president from 2007 to 2015), Cristina Fernández de Kirchner" in section
    assert "Con Sabor A Mercedes Sosa  * Label: Philips " in section
    assert "Niño De Mañana  * Label: Philips" in section
    assert section.endswith("| 2013 | Siempre En Ti  * Label: Universal Music |")

def test_extract_wikipedia_section():
    section = wikipedia.extract_wikipedia_section("https://en.wikipedia.org/wiki/Mercedes_Sosa", "Studio_albums")
    assert section.endswith("| 2015 | Lucerito  * Label: RCA |")
    assert section.startswith("| Year | Album details |")
    assert """| 1962 | [La Voz De La Zafra](/wiki/La_Voz_De_La_Zafra "La Voz De La Zafra")  * Label: [RCA](/wiki/RCA_Records "RCA Records") |""" in section