<commit_before>from __future__ import absolute_import

import json
import responses
import pytest

from freight import checks
from freight.exceptions import CheckFailed, CheckPending
from freight.testutils import TestCase


class GitHubCheckBase(TestCase):
    def setUp(self):
        self.check = checks.get('github')
        self.user = self.create_user()
        self.repo = self.create_repo()
        self.app = self.create_app(repository=self.repo)


class GitHubContextCheckTest(GitHubCheckBase):
    @responses.activate
    def test_not_passing(self):
        body = json.dumps([
            {
                "state": "failed",
                "context": "travisci",
            }
        ])

        responses.add(responses.GET, 'https://api.github.com/repos/getsentry/freight/commits/abcdefg/statuses',
                      body=body)

        config = {'contexts': ['travisci'], 'repo': 'getsentry/freight'}

        with pytest.raises(CheckFailed):
            self.check.check(self.app, 'abcdefg', config)

    @responses.activate
    def test_not_finished(self):
        body = json.dumps([
            {
                "state": "pending",
                "context": "travisci",
            }
        ])

        responses.add(responses.GET, 'https://api.github.com/repos/getsentry/freight/commits/abcdefg/statuses',
                      body=body)

        config = {'contexts': ['travisci'], 'repo': 'getsentry/freight'}

        with pytest.raises(CheckPending):
            self.check.check(self.app, 'abcdefg', config)

    @responses.activate
    def test_missing_context(self):
        body = json.dumps([])

        responses.add(responses.GET, 'https://api.github.com/repos/getsentry/freight/commits/abcdefg/statuses',
                      body=body)

        config = {'contexts': ['travisci'], 'repo': 'getsentry/freight'}

        with pytest.raises(CheckFailed):
            self.check.check(self.app, 'abcdefg', config)

    @responses.activate
    def test_success(self):
        body = json.dumps([
            {
                "state": "success",
                "context": "travisci",
            }
        ])

        responses.add(responses.GET, 'https://api.github.com/repos/getsentry/freight/commits/abcdefg/statuses',
                      body=body)

        config = {'contexts': ['travisci'], 'repo': 'getsentry/freight'}

        self.check.check(self.app, 'abcdefg', config)<commit_msg>Add tests for context changes<commit_after>from __future__ import absolute_import

import json
import responses
import pytest

from freight import checks
from freight.exceptions import CheckFailed, CheckPending
from freight.testutils import TestCase


class GitHubCheckBase(TestCase):
    def setUp(self):
        self.check = checks.get('github')
        self.user = self.create_user()
        self.repo = self.create_repo()
        self.app = self.create_app(repository=self.repo)


class GitHubContextCheckTest(GitHubCheckBase):
    @responses.activate
    def test_not_passing(self):
        body = json.dumps([
            {
                "state": "failed",
                "context": "travisci",
            }
        ])

        responses.add(responses.GET, 'https://api.github.com/repos/getsentry/freight/commits/abcdefg/statuses',
                      body=body)

        config = {'contexts': ['travisci'], 'repo': 'getsentry/freight'}

        with pytest.raises(CheckFailed):
            self.check.check(self.app, 'abcdefg', config)

    @responses.activate
    def test_not_finished(self):
        body = json.dumps([
            {
                "state": "pending",
                "context": "travisci",
            }
        ])

        responses.add(responses.GET, 'https://api.github.com/repos/getsentry/freight/commits/abcdefg/statuses',
                      body=body)

        config = {'contexts': ['travisci'], 'repo': 'getsentry/freight'}

        with pytest.raises(CheckPending):
            self.check.check(self.app, 'abcdefg', config)

    @responses.activate
    def test_missing_required_context(self):
        body = json.dumps([
            {
                "state": "success",
                "context": "other",
            }
        ])

        responses.add(responses.GET, 'https://api.github.com/repos/getsentry/freight/commits/abcdefg/statuses',
                      body=body)

        config = {'contexts': ['travisci'], 'repo': 'getsentry/freight'}

        with pytest.raises(CheckFailed):
            self.check.check(self.app, 'abcdefg', config)

    @responses.activate
    def test_success(self):
        body = json.dumps([
            {
                "state": "success",
                "context": "travisci",
            }
        ])

        responses.add(responses.GET, 'https://api.github.com/repos/getsentry/freight/commits/abcdefg/statuses',
                      body=body)

        config = {'contexts': ['travisci'], 'repo': 'getsentry/freight'}

        self.check.check(self.app, 'abcdefg', config)

    @responses.activate
    def test_no_contexts_with_none_defined(self):
        body = json.dumps([])

        responses.add(responses.GET, 'https://api.github.com/repos/getsentry/freight/commits/abcdefg/statuses',
                      body=body)

        config = {'repo': 'getsentry/freight'}

        with pytest.raises(CheckFailed):
            self.check.check(self.app, 'abcdefg', config)

    @responses.activate
    def test_any_failing_context_with_none_define(self):
        body = json.dumps([
            {
                "state": "failing",
                "context": "travisci",
            }
        ])

        responses.add(responses.GET, 'https://api.github.com/repos/getsentry/freight/commits/abcdefg/statuses',
                      body=body)

        config = {'repo': 'getsentry/freight'}

        with pytest.raises(CheckFailed):
            self.check.check(self.app, 'abcdefg', config)
{'fraction_non_alphanumeric': 0.13110322689643522, 'fraction_numerical': 0.0, 'mean_word_length': 2.1273111463285788, 'pattern_counts': {'":': 16, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 10, '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': '2239142', 'n_tokens_mistral': 1812, 'n_tokens_neox': 1759, 'n_words': 281}