def test_search_sex_female(factory, freq_results):
    """Test if searching by female sex name field works"""
    get_params = {"search[value]": "Žena"}
    request = factory.get("", get_params)
    request.subdomain = "testing-campaign"
    response = CompetitionResultListJson.as_view()(
        request, competition_slug="competition"
    )
    expected_json = {
        "recordsTotal": 2,
        "data": [
            [
                "1. - 2.",
                "100,0",
                "2",
                "1",
                "Jan Novák",
                "bar team",
                "bar company",
                "-",
                "Žena",
                "bar city",
            ],
        ],
        "draw": 0,
        "result": "ok",
        "recordsFiltered": 1,
    }
    assert json.loads(response.content) == expected_json
def test_paging_filter(factory, freq_results, campaign, single_frequency_competition):
    """
    Test if single user frequency competition result JSON is returned
    Test if paging works correctly when filter is enabled.
    This test ensures, that ranks are correctly counted for filtered items.
    """
    mommy.make(
        "dpnk.CompetitionResult",
        result="1",
        result_divident="2",
        result_divisor="1",
        competition=single_frequency_competition,
        user_attendance__userprofile__nickname="baz user",
        user_attendance__userprofile__occupation__name="Foo ocupation",
        user_attendance__campaign=campaign,
        user_attendance__team__campaign=campaign,
        user_attendance__team__member_count=1,
        user_attendance__team__subsidiary__city__name="baz city",
        user_attendance__team__subsidiary__company__name="baz company",
        user_attendance__team__name="baz team",
        id=1,
    )
    get_params = {
        "length": 1,
        "search[value]": "baz user",
    }
    request = factory.get("", get_params)
    request.subdomain = "testing-campaign"
    response = CompetitionResultListJson.as_view()(
        request, competition_slug="competition"
    )
    expected_json = {
        "recordsTotal": 3,
        "data": [
            [
                "1. - 3.",
                "100,0",
                "2",
                "1",
                "baz user",
                "baz team",
                "baz company",
                "Foo ocupation",
                "",
                "baz city",
            ],
        ],
        "draw": 0,
        "result": "ok",
        "recordsFiltered": 1,
    }
    assert json.loads(response.content) == expected_json
 def test_search_sex_male(self):
     """ Test if searching by male sex name field works """
     get_params = {'search[value]': 'Muž'}
     request = self.factory.get('', get_params)
     request.subdomain = "testing-campaign"
     response = CompetitionResultListJson.as_view()(request, competition_slug='competition')
     expected_json = {
         "recordsTotal": 2,
         "data": [],
         "draw": 0,
         "result": "ok",
         "recordsFiltered": 0,
     }
     self.assertJSONEqual(response.content.decode(), expected_json)
 def test_team_length_search(self):
     """ Test if searching by string works """
     get_params = {'search[value]': 'oo cit'}
     request = self.factory.get('', get_params)
     request.subdomain = "testing-campaign"
     response = CompetitionResultListJson.as_view()(request, competition_slug='competition')
     expected_json = {
         "recordsTotal": 2,
         "data": [
             ["1.", "1,0", "1,2", "0", "foo team", "bar company", "foo city"],
         ],
         "draw": 0,
         "result": "ok",
         "recordsFiltered": 1,
     }
     self.assertJSONEqual(response.content.decode(), expected_json)
 def test_team_length(self):
     """ Test if team length competition result JSON is returned """
     request = self.factory.get('')
     request.subdomain = "testing-campaign"
     response = CompetitionResultListJson.as_view()(request, competition_slug='competition')
     expected_json = {
         "recordsTotal": 2,
         "data": [
             ["1.", "1,0", "1,2", "0", "foo team", "bar company", "foo city"],
             ["2.", "0,5", "0,5", "0", "a team", "a company", "a city"],
         ],
         "draw": 0,
         "result": "ok",
         "recordsFiltered": 2,
     }
     self.assertJSONEqual(response.content.decode(), expected_json)
 def test_search_sex_female(self):
     """ Test if searching by female sex name field works """
     get_params = {'search[value]': 'Žena'}
     request = self.factory.get('', get_params)
     request.subdomain = "testing-campaign"
     response = CompetitionResultListJson.as_view()(request, competition_slug='competition')
     expected_json = {
         "recordsTotal": 2,
         "data": [
             ["1. - 2.", "100,0", "2", "1", "Jan Novák", "bar team", "bar company", "-", "Žena", "bar city"],
         ],
         "draw": 0,
         "result": "ok",
         "recordsFiltered": 1,
     }
     self.assertJSONEqual(response.content.decode(), expected_json)
 def test_get(self):
     """ Test if single user frequency competition result JSON is returned """
     request = self.factory.get('')
     request.subdomain = "testing-campaign"
     response = CompetitionResultListJson.as_view()(request, competition_slug='competition')
     expected_json = {
         "recordsTotal": 2,
         "data": [
             ["1. - 2.", "100,0", "3", "1", "foo user", "foo team", "foo company", "-", '', "foo city"],
             ["1. - 2.", "100,0", "2", "1", "Jan Novák", "bar team", "bar company", "-", "Žena", "bar city"],
         ],
         "draw": 0,
         "result": "ok",
         "recordsFiltered": 2,
     }
     self.assertJSONEqual(response.content.decode(), expected_json)
def test_search_sex_male(factory, freq_results):
    """Test if searching by male sex name field works"""
    get_params = {"search[value]": "Muž"}
    request = factory.get("", get_params)
    request.subdomain = "testing-campaign"
    response = CompetitionResultListJson.as_view()(
        request, competition_slug="competition"
    )
    expected_json = {
        "recordsTotal": 2,
        "data": [],
        "draw": 0,
        "result": "ok",
        "recordsFiltered": 0,
    }
    assert json.loads(response.content) == expected_json
 def test_search_user_name_not_found(self):
     """
     If the user has nickname, we can't find by his name,
     otherwise the name can reverse engeneered.
     """
     get_params = {'search[value]': 'Rosa'}
     request = self.factory.get('', get_params)
     request.subdomain = "testing-campaign"
     response = CompetitionResultListJson.as_view()(request, competition_slug='competition')
     expected_json = {
         "recordsTotal": 2,
         "data": [],
         "draw": 0,
         "result": "ok",
         "recordsFiltered": 0,
     }
     self.assertJSONEqual(response.content.decode(), expected_json)
def test_team_length_company_search_quotes(factory, results):
    """Test if searching by exact company name works"""
    get_params = {"columns[0][search][value]": '"bar company"'}
    request = factory.get("", get_params)
    request.subdomain = "testing-campaign"
    response = CompetitionResultListJson.as_view()(
        request, competition_slug="competition"
    )
    expected_json = {
        "recordsTotal": 2,
        "data": [
            ["1.", "1,0", "1,2", "0", "foo team", "bar company", "foo city"],
        ],
        "draw": 0,
        "result": "ok",
        "recordsFiltered": 1,
    }
    json.loads(response.content) == expected_json
def test_team_length(factory, results):
    """Test if team length competition result JSON is returned"""
    request = factory.get("")
    request.subdomain = "testing-campaign"
    response = CompetitionResultListJson.as_view()(
        request, competition_slug="competition"
    )
    expected_json = {
        "recordsTotal": 2,
        "data": [
            ["1.", "1,0", "1,2", "0", "foo team", "bar company", "foo city"],
            ["2.", "0,5", "0,5", "0", "a team", "a company", "a city"],
        ],
        "draw": 0,
        "result": "ok",
        "recordsFiltered": 2,
    }
    assert json.loads(response.content) == expected_json
def test_search_user_name_not_found(factory, freq_results):
    """
    If the user has nickname, we can't find by his name,
    otherwise the name can reverse engeneered.
    """
    get_params = {"search[value]": "Rosa"}
    request = factory.get("", get_params)
    request.subdomain = "testing-campaign"
    response = CompetitionResultListJson.as_view()(
        request, competition_slug="competition"
    )
    expected_json = {
        "recordsTotal": 2,
        "data": [],
        "draw": 0,
        "result": "ok",
        "recordsFiltered": 0,
    }
    assert json.loads(response.content) == expected_json
def test_get(factory, freq_results):
    """Test if single user frequency competition result JSON is returned"""
    request = factory.get("")
    request.subdomain = "testing-campaign"
    response = CompetitionResultListJson.as_view()(
        request, competition_slug="competition"
    )
    expected_json = {
        "recordsTotal": 2,
        "data": [
            [
                "1. - 2.",
                "100,0",
                "3",
                "1",
                "foo user",
                "foo team",
                "foo company",
                "-",
                "",
                "foo city",
            ],
            [
                "1. - 2.",
                "100,0",
                "2",
                "1",
                "Jan Novák",
                "bar team",
                "bar company",
                "-",
                "Žena",
                "bar city",
            ],
        ],
        "draw": 0,
        "result": "ok",
        "recordsFiltered": 2,
    }
    json.loads(response.content) == expected_json
 def test_paging_filter(self):
     """
     Test if single user frequency competition result JSON is returned
     Test if paging works correctly when filter is enabled.
     This test ensures, that ranks are correctly counted for filtered items.
     """
     mommy.make(
         'dpnk.CompetitionResult',
         result="1",
         result_divident="2",
         result_divisor="1",
         competition=self.single_frequency_competition,
         user_attendance__userprofile__nickname="baz user",
         user_attendance__userprofile__occupation__name='Foo ocupation',
         user_attendance__campaign=self.campaign,
         user_attendance__team__campaign=self.campaign,
         user_attendance__team__member_count=1,
         user_attendance__team__subsidiary__city__name="baz city",
         user_attendance__team__subsidiary__company__name="baz company",
         user_attendance__team__name="baz team",
         id=1,
     )
     get_params = {
         'length': 1,
         'search[value]': 'baz user',
     }
     request = self.factory.get('', get_params)
     request.subdomain = "testing-campaign"
     response = CompetitionResultListJson.as_view()(request, competition_slug='competition')
     expected_json = {
         "recordsTotal": 3,
         "data": [
             ["1. - 3.", "100,0", "2", "1", "baz user", "baz team", "baz company", "Foo ocupation", '', "baz city"],
         ],
         "draw": 0,
         "result": "ok",
         "recordsFiltered": 1,
     }
     self.assertJSONEqual(response.content.decode(), expected_json)