aboutsummaryrefslogtreecommitdiff
path: root/gn2/tests/unit/wqflask/test_collect.py
blob: 6c1bafa5d68ff56f581d85fac1513eec6c6e5053 (plain)
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
"""Test cases for some methods in collect.py"""

import unittest
from unittest import mock

from flask import Flask
from gn2.wqflask.collect import process_traits

app = Flask(__name__)


class MockSession:
    """Helper class for mocking wqflask.collect.g.user_session.logged_in"""

    def __init__(self, is_logged_in=False):
        self.is_logged_in = is_logged_in

    @property
    def logged_in(self):
        return self.is_logged_in


class MockFlaskG:
    """Helper class for mocking wqflask.collect.g.user_session"""

    def __init__(self, is_logged_in=False):
        self.is_logged_in = is_logged_in

    @property
    def user_session(self):
        if self.is_logged_in:
            return MockSession(is_logged_in=True)
        return MockSession()


class TestCollect(unittest.TestCase):

    def setUp(self):
        self.app_context = app.app_context()
        self.app_context.push()

    def tearDown(self):
        self.app_context.pop()

    @mock.patch("gn2.wqflask.collect.g", MockFlaskG())
    def test_process_traits_with_bytestring(self):
        """
        Test that the correct traits are returned when the user is logged
        out and bytes are used.
        """
        self.assertEqual(sorted(process_traits(
            b'1452452_at:HC_M2_0606_P:163d04f7db7c9e110de6,'
            b'1452447_at:HC_M2_0606_P:eeece8fceb67072debea,'
            b'1451401_a_at:HC_M2_0606_P:a043d23b3b3906d8318e,'
            b'1429252_at:HC_M2_0606_P:6fa378b349bc9180e8f5')),
            sorted(['1429252_at:HC_M2_0606_P',
                 '1451401_a_at:HC_M2_0606_P',
                 '1452447_at:HC_M2_0606_P',
                 '1452452_at:HC_M2_0606_P']))

    @mock.patch("gn2.wqflask.collect.g", MockFlaskG())
    def test_process_traits_with_normal_string(self):
        """
        Test that the correct traits are returned when the user is logged
        out and a normal string is used.
        """
        self.assertEqual(sorted(process_traits(
            '1452452_at:HC_M2_0606_P:163d04f7db7c9e110de6,'
            '1452447_at:HC_M2_0606_P:eeece8fceb67072debea,'
            '1451401_a_at:HC_M2_0606_P:a043d23b3b3906d8318e,'
            '1429252_at:HC_M2_0606_P:6fa378b349bc9180e8f5')),
            sorted(['1429252_at:HC_M2_0606_P',
                 '1451401_a_at:HC_M2_0606_P',
                 '1452447_at:HC_M2_0606_P',
                 '1452452_at:HC_M2_0606_P']))