<commit_before>from unittest.mock import patch

from pmg.sharpspring import Sharpspring
from tests import PMGTestCase
from tests.fixtures import dbfixture, UserData, OrganisationData
from pmg.models import User, Organisation


SHARPSRING_URL = "http://api.sharpspring.com/pubapi/v1/"


class MockResponse:
    def __init__(self, json_data, status_code):
        self.json_data = json_data
        self.status_code = status_code

    def raise_for_status(self):
        pass

    def json(self):
        return self.json_data


def mocked_requests_post_success(*args, **kwargs):
    return MockResponse(
        {"result": {"creates": [{"success": True,}]}, "error": False}, 200
    )


class TestSharpspring(PMGTestCase):
    def setUp(self):
        super(TestSharpspring, self).setUp()

        self.fx = dbfixture.data(UserData, OrganisationData)
        self.fx.setup()

    def tearDown(self):
        self.fx.teardown()
        super(TestSharpspring, self).tearDown()

    @patch("pmg.sharpspring.requests.post", side_effect=mocked_requests_post_success)
    def test_make_sharpspring_request(self, post_mock):
        sharpspring = Sharpspring()
        details = {
            "emailAddress": "<EMAIL>",
            "companyName": "Test Company",
        }
        result = sharpspring.call("createLeads", {"objects": [details]})
        post_mock.assert_called()
        self.assertTrue(result["result"]["creates"][0])

    @patch.object(
        Sharpspring,
        "call",
        return_value={"result": {"creates": [{"success": True,}]}, "error": False},
    )
    def test_subscribe_to_list_success(self, sharpspring_call_mock):
        user = self.fx.UserData.admin

        sharpspring = Sharpspring()
        sharpspring.subscribeToList(user, "1234")
        sharpspring_call_mock.assert_any_call(
            "createLeads",
            {
                "objects": [
                    {"emailAddress": user.email, "companyName": user.organisation.name}
                ]
            },
        )
        sharpspring_call_mock.assert_any_call(
            "addListMemberEmailAddress", {"emailAddress": user.email, "listID": "1234"}
        )

    @patch.object(
        Sharpspring,
        "call",
        return_value={
            "result": {"creates": [{"success": False, "error": {"code": 500}}]}
        },
    )
    def test_subscribe_to_list_fail(self, sharpspring_call_mock):
        user = self.fx.UserData.admin

        sharpspring = Sharpspring()
        self.assertRaises(ValueError, sharpspring.subscribeToList, user, "1234")
<commit_msg>Remove unused code from Sharpspring test<commit_after>from unittest.mock import patch

from pmg.sharpspring import Sharpspring
from tests import PMGTestCase
from tests.fixtures import dbfixture, UserData, OrganisationData


class MockResponse:
    def __init__(self, json_data, status_code):
        self.json_data = json_data
        self.status_code = status_code

    def raise_for_status(self):
        pass

    def json(self):
        return self.json_data


def mocked_requests_post_success(*args, **kwargs):
    return MockResponse(
        {"result": {"creates": [{"success": True,}]}, "error": False}, 200
    )


class TestSharpspring(PMGTestCase):
    def setUp(self):
        super(TestSharpspring, self).setUp()

        self.fx = dbfixture.data(UserData, OrganisationData)
        self.fx.setup()

    def tearDown(self):
        self.fx.teardown()
        super(TestSharpspring, self).tearDown()

    @patch("pmg.sharpspring.requests.post", side_effect=mocked_requests_post_success)
    def test_make_sharpspring_request(self, post_mock):
        sharpspring = Sharpspring()
        details = {
            "emailAddress": "<EMAIL>",
            "companyName": "Test Company",
        }
        result = sharpspring.call("createLeads", {"objects": [details]})
        post_mock.assert_called()
        self.assertTrue(result["result"]["creates"][0])

    @patch.object(
        Sharpspring,
        "call",
        return_value={"result": {"creates": [{"success": True,}]}, "error": False},
    )
    def test_subscribe_to_list_success(self, sharpspring_call_mock):
        user = self.fx.UserData.admin

        sharpspring = Sharpspring()
        sharpspring.subscribeToList(user, "1234")
        sharpspring_call_mock.assert_any_call(
            "createLeads",
            {
                "objects": [
                    {"emailAddress": user.email, "companyName": user.organisation.name}
                ]
            },
        )
        sharpspring_call_mock.assert_any_call(
            "addListMemberEmailAddress", {"emailAddress": user.email, "listID": "1234"}
        )

    @patch.object(
        Sharpspring,
        "call",
        return_value={
            "result": {"creates": [{"success": False, "error": {"code": 500}}]}
        },
    )
    def test_subscribe_to_list_fail(self, sharpspring_call_mock):
        user = self.fx.UserData.admin

        sharpspring = Sharpspring()
        self.assertRaises(ValueError, sharpspring.subscribeToList, user, "1234")

{'fraction_non_alphanumeric': 0.12283123028391167, 'fraction_numerical': 0.007689274447949527, 'mean_word_length': 2.7246696035242293, 'pattern_counts': {'":': 42, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3197951', 'n_tokens_mistral': 1644, 'n_tokens_neox': 1599, 'n_words': 280}