File: BaseTest.py

package info (click to toggle)
python-digitalocean 1.16.0-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, sid
  • size: 912 kB
  • sloc: python: 4,961; makefile: 46
file content (49 lines) | stat: -rw-r--r-- 1,362 bytes parent folder | download | duplicates (3)
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
import os
import unittest


DEFAULT_PER_PAGE = 200


class BaseTest(unittest.TestCase):

    def setUp(self):
        self.base_url = "https://api.digitalocean.com/v2/"
        self.token = "afaketokenthatwillworksincewemockthings"

    def load_from_file(self, json_file):
        cwd = os.path.dirname(__file__)
        with open(os.path.join(cwd, 'data/%s' % json_file), 'r') as f:
            return f.read()

    def split_url(self, url):
        bits = url.split('?')
        if len(bits) == 1:
            return url, []

        qlist = bits[1].split('&')
        qlist.sort()
        return bits[0], qlist

    def assert_url_query_equal(self, url1, url2):
        """ Test if two URL queries are equal

        The key=value pairs after the ? in a URL can occur in any order
        (especially since dicts in python 3 are not deterministic across runs).
        The method sorts the key=value pairs and then compares the URLs.
        """

        base1, qlist1 = self.split_url(url1)
        base2, qlist2 = self.split_url(url2)

        self.assertEqual(base1, base2)
        self.assertEqual(qlist1, qlist2)

    def assert_get_url_equal(self, url1, url2):
        if "?" in url2:
            url2 += "&"
        else:
            url2 += "?"

        url2 += "per_page=%d" % DEFAULT_PER_PAGE
        return self.assert_url_query_equal(url1, url2)