<commit_before># -*- coding: utf-8 -*-
import os
import sys
import unittest
import httmock
import itertools

sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..'))
import clever
from clever import importer
json = importer.import_json()

import requests
from httmock import response, HTTMock


def functional_test(auth):
  class FunctionalTests(CleverTestCase):

    def setUp(self):
      super(FunctionalTests, self).setUp()
      clever.api_base = os.environ.get('CLEVER_API_BASE', 'https://api.clever.com')
      if auth.get("token", None):
        clever.set_token(auth["token"])
  return FunctionalTests


class CleverTestCase(unittest.TestCase):

  def setUp(self):
    super(CleverTestCase, self).setUp()
    clever.api_base = os.environ.get('CLEVER_API_BASE', 'https://api.clever.com')
    clever.set_token('DEMO_TOKEN')


class FunctionalTests(CleverTestCase):

  def test_dns_failure(self):
    api_base = clever.api_base
    try:
      clever.api_base = 'https://my-invalid-domain.ireallywontresolve/v1'
      self.assertRaises(clever.APIConnectionError, clever.District.all)
    finally:
      clever.api_base = api_base

  def test_list_accessors(self):
    district = clever.District.all()[0]
    self.assertEqual(district['name'], district.name)

  def test_iter(self):
    for district in clever.District.iter():
      self.assertTrue(district.id)

  def test_starting_after(self):
    allstudents = clever.Student.all()
    second_to_last_id = allstudents[len(allstudents)-2]['id']
    students = clever.Student.iter(starting_after=second_to_last_id)
    count = len(list(students))
    self.assertTrue(count==1)

  def test_ending_before(self):
    allstudents = clever.Student.all()
    second_id = allstudents[1]['id']
    students = clever.Student.iter(ending_before=second_id)
    count = len(list(students))
    self.assertTrue(count==1)

  def test_iter_count(self):
      r = requests.get('https://api.clever.com/v1.1/students?count=true',
          headers={'Authorization': 'Bearer DEMO_TOKEN'})

      req_count = json.loads(r.text)["count"]
      iter_count = len([x for x in clever.Student.iter()])

      self.assertTrue(req_count > clever.Student.ITER_LIMIT)
      self.assertEqual(req_count, iter_count)

  def test_unsupported_params(self):
    self.assertRaises(clever.CleverError, lambda: clever.District.all(page=2))
    self.assertRaises(clever.CleverError, lambda: clever.District.all(limit=10))
    self.assertRaises(clever.CleverError, lambda: clever.District.all(page=2, limit=10))

  def test_unicode(self):
    # Make sure unicode requests can be sent
    self.assertRaises(clever.APIError, clever.District.retrieve, id=u'☃')<commit_msg>Add test for unicode responses<commit_after># -*- coding: utf-8 -*-
import os
import sys
import unittest
import httmock
import itertools

sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..'))
import clever
from clever import importer
json = importer.import_json()

import requests
from httmock import response, HTTMock


def functional_test(auth):
  class FunctionalTests(CleverTestCase):

    def setUp(self):
      super(FunctionalTests, self).setUp()
      clever.api_base = os.environ.get('CLEVER_API_BASE', 'https://api.clever.com')
      if auth.get("token", None):
        clever.set_token(auth["token"])
  return FunctionalTests


class CleverTestCase(unittest.TestCase):

  def setUp(self):
    super(CleverTestCase, self).setUp()
    clever.api_base = os.environ.get('CLEVER_API_BASE', 'https://api.clever.com')
    clever.set_token('DEMO_TOKEN')

#generates httmock responses for test_unicode_receive
def unicode_content(url, request):
  return {'status_code': 200, 'content': '{"data": {"name": "<NAME>"}}'}

class FunctionalTests(CleverTestCase):

  def test_dns_failure(self):
    api_base = clever.api_base
    try:
      clever.api_base = 'https://my-invalid-domain.ireallywontresolve/v1'
      self.assertRaises(clever.APIConnectionError, clever.District.all)
    finally:
      clever.api_base = api_base

  def test_list_accessors(self):
    district = clever.District.all()[0]
    self.assertEqual(district['name'], district.name)

  def test_iter(self):
    for district in clever.District.iter():
      self.assertTrue(district.id)

  def test_starting_after(self):
    allstudents = clever.Student.all()
    second_to_last_id = allstudents[len(allstudents)-2]['id']
    students = clever.Student.iter(starting_after=second_to_last_id)
    count = len(list(students))
    self.assertTrue(count==1)

  def test_ending_before(self):
    allstudents = clever.Student.all()
    second_id = allstudents[1]['id']
    students = clever.Student.iter(ending_before=second_id)
    count = len(list(students))
    self.assertTrue(count==1)

  def test_iter_count(self):
      r = requests.get('https://api.clever.com/v1.1/students?count=true',
          headers={'Authorization': 'Bearer DEMO_TOKEN'})

      req_count = json.loads(r.text)["count"]
      iter_count = len([x for x in clever.Student.iter()])

      self.assertTrue(req_count > clever.Student.ITER_LIMIT)
      self.assertEqual(req_count, iter_count)

  def test_unsupported_params(self):
    self.assertRaises(clever.CleverError, lambda: clever.District.all(page=2))
    self.assertRaises(clever.CleverError, lambda: clever.District.all(limit=10))
    self.assertRaises(clever.CleverError, lambda: clever.District.all(page=2, limit=10))

  def test_unicode_send(self):
    # Make sure unicode requests can be sent. 404 error is a clever.APIError
    self.assertRaises(clever.APIError, clever.District.retrieve, id=u'☃')

  def test_unicode_receive(self):
    with HTTMock(unicode_content):
      # Make sure unicode responses can be received.
      self.assertEqual(u'Oh haiô', clever.District.retrieve('something').name)
{'fraction_non_alphanumeric': 0.1297828335056877, 'fraction_numerical': 0.006549465701482247, 'mean_word_length': 4.423364485981309, 'pattern_counts': {'":': 2, '<': 4, '<?xml version=': 0, '>': 6, 'https://': 8, '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': '1958985', 'n_tokens_mistral': 1957, 'n_tokens_neox': 1907, 'n_words': 335}