<commit_before>from rest_framework import status
from rest_framework.test import APITestCase, APIClient
from django.core.urlresolvers import reverse

from time import sleep
from django.test import TestCase
from django.contrib.auth.models import User
from django.core.exceptions import ObjectDoesNotExist
from django.contrib.contenttypes.models import ContentType

from gis_csdt.models import Location, GeoCoordinates, DatasetNameField, Dataset, MapPoint, Sensor, DataPoint, PhoneNumber
from gis_csdt.views import DataToGSM7
from django.contrib.auth import get_user_model
from django.test import LiveServerTestCase
import urllib
User = get_user_model()

class AllViewTestsNoData(APITestCase):
    def test_no_datasets(self):
        response = self.client.get('/api-ds/')
        self.assertEqual(response.status_code, 200)
    def test_no_mappoints(self):
        response = self.client.get('/api-mp/')
        self.assertEqual(response.status_code, 200)
    def test_no_newtags(self):
        response = self.client.get('/api-newtag/')
        self.assertEqual(response.status_code, 200)
    def test_no_polygons(self):
        response = self.client.get('/api-poly/')
        self.assertEqual(response.status_code, 200)
    def test_no_mappoints_geojson(self):
        response = self.client.get('/api-test/')
        self.assertEqual(response.status_code, 200)
    def test_no_mappolygons_count_of_points(self):
        response = self.client.get('/api-count/')
        self.assertEqual(response.status_code, 200)
    def test_no_mappolygons_analysis_around_point(self):
        response = self.client.get('/api-dist/')
        self.assertEqual(response.status_code, 400)

class TestDataset(TestCase):

    fixtures = ['test_data.json']

    def test_can_create_new_dataset(self):
        original_count = Dataset.objects.all().count()       
        geo1 = GeoCoordinates(lat_field="43.0831N", lon_field="73.7846W")
        geo1.save()
        dataset1 = Dataset(name="<NAME>", cached="2017-10-12T12:46:00.258Z", coordinates=geo1)
        dataset1.save()
        sleep(1)
        self.assertEqual(Dataset.objects.all().count(), original_count + 1)
        self.assertEqual(dataset1.coordinates.lat_field, "43.0831N")
        original_count += 1

        loc2 = Location(city_field="Troy", state_field="NY")
        loc2.save()
        dataset2 = Dataset(name="RPI", location=loc2)
        dataset2.save()
        self.assertEqual(Dataset.objects.all().count(), original_count + 1)
        self.assertEqual(dataset2.location.city_field, "Troy")
        self.assertEqual(dataset2.coordinates, None)

class SMSCreateData(LiveServerTestCase):
    def setUp(self):
        self.client = APIClient()
        self.user = User.objects.create_superuser(username='test',
                                                  email='<EMAIL>',
                                                  password='<PASSWORD>')
        self.assertTrue(self.client.login(username='test', password='<PASSWORD>'))

    def test_SMS(self):
        data = [1,1,0,128,129,300,10001]
        string = DataToGSM7(data)
        set = Dataset.objects.create(name='test')
        set.save()
        point = MapPoint.objects.create(lat=0,lon=0)
        point.save()
        sensor = Sensor.objects.create(name='test')
        sensor.save()
        phNum = PhoneNumber.objects.create(phone_number=11111111111,user=self.user)
        phNum.save()
        postData = {'Body': string.encode('utf-8'), 'From': phNum.phone_number}
        response = self.client.post('/api-SMS/', urllib.urlencode(postData), content_type='application/x-www-form-urlencoded')
        self.assertEqual(DataPoint.objects.get(pk=5).value, 10001)
<commit_msg>Add test for API<commit_after>from rest_framework import status
from rest_framework.test import APITestCase, APIClient
from django.core.urlresolvers import reverse

from time import sleep
from django.test import TestCase
from django.contrib.auth.models import User
from django.core.exceptions import ObjectDoesNotExist
from django.contrib.contenttypes.models import ContentType

from gis_csdt.models import Location, GeoCoordinates, DatasetNameField, Dataset, MapPoint, Sensor, DataPoint, PhoneNumber
from gis_csdt.views import DataToGSM7
from django.contrib.auth import get_user_model
from django.test import LiveServerTestCase
import urllib
from decimal import Decimal
User = get_user_model()

class AllViewTestsNoData(APITestCase):
    def test_no_datasets(self):
        response = self.client.get('/api-ds/')
        self.assertEqual(response.status_code, 200)
    def test_no_mappoints(self):
        response = self.client.get('/api-mp/')
        self.assertEqual(response.status_code, 200)
    def test_no_newtags(self):
        response = self.client.get('/api-newtag/')
        self.assertEqual(response.status_code, 200)
    def test_no_polygons(self):
        response = self.client.get('/api-poly/')
        self.assertEqual(response.status_code, 200)
    def test_no_mappoints_geojson(self):
        response = self.client.get('/api-test/')
        self.assertEqual(response.status_code, 200)
    def test_no_mappolygons_count_of_points(self):
        response = self.client.get('/api-count/')
        self.assertEqual(response.status_code, 200)
    def test_no_mappolygons_analysis_around_point(self):
        response = self.client.get('/api-dist/')
        self.assertEqual(response.status_code, 400)

class TestDataset(TestCase):
    fixtures = ['test_data.json']

    def test_can_create_new_dataset(self):
        original_count = Dataset.objects.all().count()       
        geo1 = GeoCoordinates(lat_field="43.0831N", lon_field="73.7846W")
        geo1.save()
        dataset1 = Dataset(name="<NAME>", cached="2017-10-12T12:46:00.258Z", coordinates=geo1)
        dataset1.save()
        sleep(1)
        self.assertEqual(Dataset.objects.all().count(), original_count + 1)
        self.assertEqual(dataset1.coordinates.lat_field, "43.0831N")
        original_count += 1

        loc2 = Location(city_field="Troy", state_field="NY")
        loc2.save()
        dataset2 = Dataset(name="RPI", location=loc2)
        dataset2.save()
        self.assertEqual(Dataset.objects.all().count(), original_count + 1)
        self.assertEqual(dataset2.location.city_field, "Troy")
        self.assertEqual(dataset2.coordinates, None)

class SMSCreateData(LiveServerTestCase):
    def setUp(self):
        self.client = APIClient()
        self.user = User.objects.create_superuser(username='test',
                                                  email='<EMAIL>',
                                                  password='<PASSWORD>')
        self.assertTrue(self.client.login(username='test', password='<PASSWORD>'))

    def test_SMS(self):       
        set = Dataset.objects.create(name='test')
        set.save()
        point = MapPoint.objects.create(lat=0,lon=0)
        point.save()
        print "here1", MapPoint.objects.all().count()
        sensor = Sensor.objects.create(name='test')
        sensor.save()
        phNum = PhoneNumber.objects.create(phone_number=11111111111,user=self.user)
        phNum.save()
        data = [1,2,0,128,129,300,10001]
        data[1] = point.id
        string = DataToGSM7(data)
        postData = {'Body': string.encode('utf-8'), 'From': phNum.phone_number}
        response = self.client.post('/api-SMS/', urllib.urlencode(postData), content_type='application/x-www-form-urlencoded')
        self.assertEqual(DataPoint.objects.get(pk=5).value, 10001)

class TestMapPoint(TestCase):
    def test_can_create_new_mappoint(self):
        original_count = MapPoint.objects.all().count()       
        point = MapPoint(lat=43.0831, lon=73.7846)
        point.save()
        sleep(1)
        self.assertEqual(MapPoint.objects.all().count(), original_count + 1)
        self.assertEqual(point.lat, Decimal(43.0831))


class TestAddMapPointAPI(TestCase):
    def setUp(self):
        self.client = APIClient()
        self.user = User.objects.create_superuser(username='test',
                                                  email='<EMAIL>',
                                                  password='<PASSWORD>')
        self.assertTrue(self.client.login(username='test', password='<PASSWORD>'))        

    def test_api_can_add_mappoint(self):
        original_count = MapPoint.objects.all().count()
        self.mp_data = {'lat': 31.7, 'lon': 68.9}
        self.response = self.client.post('/api-addmp/', self.mp_data, format="json")
        self.assertEqual(self.response.status_code, 204)
        print "here2", MapPoint.objects.all().count()
        self.assertEqual(MapPoint.objects.all().count(), original_count + 1)
{'fraction_non_alphanumeric': 0.12340131351538196, 'fraction_numerical': 0.029611706417790068, 'mean_word_length': 3.0617688348151613, 'pattern_counts': {'":': 0, '<': 14, '<?xml version=': 0, '>': 14, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 2, 'word_list_counts': {'cursed_substrings.json': 12, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2309788', 'n_tokens_mistral': 2760, 'n_tokens_neox': 2620, 'n_words': 431}