1
/*
2
Copyright (C) 2020-2021 Kunal Mehta <legoktm@debian.org>
3

            
4
This program is free software: you can redistribute it and/or modify
5
it under the terms of the GNU General Public License as published by
6
the Free Software Foundation, either version 3 of the License, or
7
(at your option) any later version.
8

            
9
This program is distributed in the hope that it will be useful,
10
but WITHOUT ANY WARRANTY; without even the implied warranty of
11
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
GNU General Public License for more details.
13

            
14
You should have received a copy of the GNU General Public License
15
along with this program.  If not, see <http://www.gnu.org/licenses/>.
16
 */
17

            
18
use crate::tests::test_client::{enwp_client, mw_client};
19
use crate::{Error, Result};
20

            
21
#[tokio::test]
22
3
async fn test_get() -> Result<()> {
23
3
    let client = mw_client();
24
3
    let html = client.get_raw("MediaWiki").await?;
25
3
    assert!(html.contains("Special:MyLanguage/Download"));
26
3
    match client.get("ThisPageDoesNotExist").await {
27
3
        Err(Error::PageDoesNotExist(title)) => {
28
3
            assert_eq!("ThisPageDoesNotExist".to_string(), title);
29
2
        }
30
2
        _ => {
31
2
            panic!("Test did not fail with Error::PageDoesNotExist()");
32
2
        }
33
2
    }
34
3
    Ok(())
35
2
}
36

            
37
#[tokio::test]
38
3
async fn test_get_revision() -> Result<()> {
39
3
    let client = enwp_client();
40
3
    let code = client
41
3
        .get_revision("HomePage", 908493298)
42
3
        .await?
43
3
        .into_mutable();
44
3
    assert_eq!(
45
3
        code.text_contents(),
46
3
        "This is the new WikiPedia!".to_string()
47
3
    );
48
3
    assert_eq!(code.revision_id(), Some(908493298));
49
3
    Ok(())
50
2
}
51

            
52
#[tokio::test]
53
3
async fn test_get_redirect() -> Result<()> {
54
3
    let client = enwp_client();
55
3
    let code = client.get("Main page").await?.into_mutable();
56
3
    let redirect = code.redirect();
57
3
    assert!(redirect.is_some());
58
3
    assert_eq!(&redirect.unwrap().target(), "Main Page");
59
3
    Ok(())
60
2
}
61

            
62
#[tokio::test]
63
3
async fn test_transform_to_html() -> Result<()> {
64
3
    let client = mw_client();
65
3
    let html = client
66
3
        .transform_to_html_raw("{{1x|This is HTML now}}")
67
3
        .await?;
68
3
    assert!(html.contains("This is HTML now"));
69
3
    Ok(())
70
2
}
71

            
72
#[tokio::test]
73
3
async fn test_transform_to_wikitext() -> Result<()> {
74
3
    let client = mw_client();
75
3
    let wikitext = client
76
3
        .transform_to_wikitext_raw(
77
3
            "<a rel=\"mw:WikiLink\" href=\"./Foo\">Foo bar</a>",
78
3
            None,
79
3
            None,
80
3
            None,
81
3
        )
82
3
        .await?;
83
3
    assert_eq!(wikitext, "[[Foo|Foo bar]]".to_string());
84
3
    Ok(())
85
2
}
86

            
87
#[tokio::test]
88
3
async fn test_immutable() -> Result<()> {
89
3
    let client = enwp_client();
90
3
    let code = client.get("Main Page").await?;
91
3
    let wikitext = client.transform_to_wikitext(&code).await?;
92
3
    assert!(wikitext.contains("Wikipedia"));
93
3
    Ok(())
94
2
}