File: test_location.py

package info (click to toggle)
bumblebee-status 2.2.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,844 kB
  • sloc: python: 13,430; sh: 68; makefile: 29
file content (57 lines) | stat: -rw-r--r-- 1,449 bytes parent folder | download
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
51
52
53
54
55
56
57
import pytest
import json

import util.location


@pytest.fixture
def urllib_req(mocker):
    util.location.reset()
    return mocker.patch("util.location.urllib.request")


@pytest.fixture
def secondaryLocation():
    return {
        "country": "Middle Earth",
        "lon": "10.0",
        "lat": "20.5",
        "query": "127.0.0.1",
    }


@pytest.fixture
def primaryLocation():
    return {
        "country": "Rivia",
        "longitude": "-10.0",
        "latitude": "-23",
        "ip": "127.0.0.6",
    }


def test_primary_provider(urllib_req, primaryLocation):
    urllib_req.urlopen.return_value.read.return_value = json.dumps(primaryLocation)

    assert util.location.country() == primaryLocation["country"]
    assert util.location.coordinates() == (
        primaryLocation["latitude"],
        primaryLocation["longitude"],
    )
    assert util.location.public_ip() == primaryLocation["ip"]


def test_secondary_provider(mocker, urllib_req, secondaryLocation):
    urlopen = mocker.MagicMock()
    urlopen.read.return_value = json.dumps(secondaryLocation)
    urllib_req.urlopen.side_effect = [RuntimeError(), urlopen]

    assert util.location.country() == secondaryLocation["country"]
    assert util.location.coordinates() == (
        secondaryLocation["lat"],
        secondaryLocation["lon"],
    )
    assert util.location.public_ip() == secondaryLocation["query"]


# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4