<commit_before>#!/usr/bin/env python
#-*- coding: utf-8 -*-

"""
Contains unit tests for the Python Parse REST API wrapper
"""
from __future__ import print_function

import os
import sys
import subprocess
import unittest
import datetime
import six
from itertools import chain

from parse_rest.core import ResourceRequestNotFound
from parse_rest.connection import register, ParseBatcher
from parse_rest.datatypes import GeoPoint, Object, Function, Pointer
from parse_rest.user import User
from parse_rest import query
from parse_rest.installation import Push

try:
    import settings_local
except ImportError:
    sys.exit('You must create a settings_local.py file with APPLICATION_ID, ' \
                 'REST_API_KEY, MASTER_KEY variables set')


register(
    getattr(settings_local, 'APPLICATION_ID'),
    getattr(settings_local, 'REST_API_KEY'),
    master_key=getattr(settings_local, 'MASTER_KEY')
)

GLOBAL_JSON_TEXT = """{
    "applications": {
        "_default": {
            "link": "parseapi"
        },
        "parseapi": {
            "applicationId": "%s",
            "masterKey": "%s"
        }
    },
    "global": {
        "parseVersion": "1.1.16"
    }
}
"""


class Game(Object):
    pass


class GameScore(Object):
    pass


class GameMap(Object):
    pass


class GameMode(Object):
    pass


class City(Object):
    pass


class Review(Object):
    pass


class CollectedItem(Object):
    pass


class TestObject(unittest.TestCase):
    def setUp(self):
        self.score = GameScore(score=1337, player_name='<NAME>', cheat_mode=False)
        self.sao_paulo = City(name='São Paulo', location=GeoPoint(-23.5, -46.6167))
        self.collected_item = CollectedItem(type="Sword", isAwesome=True)

    def tearDown(self):
        city_name = getattr(self.sao_paulo, 'name', None)
        game_score = getattr(self.score, 'score', None)
        collected_item_type = getattr(self.collected_item, 'type', None)
        if city_name:
            ParseBatcher().batch_delete(City.Query.filter(name=city_name))
        if game_score:
            ParseBatcher().batch_delete(GameScore.Query.filter(score=game_score))
        if collected_item_type:
            ParseBatcher().batch_delete(CollectedItem.Query.filter(type=collected_item_type))
        
    def testCanInitialize(self):
        self.assertEqual(self.score.score, 1337, 'Could not set score')

    def testCanInstantiateParseType(self):
        self.assertEqual(self.sao_paulo.location.latitude, -23.5)

    def testFactory(self):
        self.assertEqual(Object.factory('_User'), User)
        self.assertEqual(Object.factory('GameScore'), GameScore)

    def testCanSaveDates(self):
        now = datetime.datetime.now()
        self.score.last_played = now
        self.score.save()
        self.assertEqual(self.score.last_played, now, 'Could not save date')

    def testCanCreateNewObject(self):
        self.score.save()
        object_id = self.score.objectId

        self.assertIsNotNone(object_id, 'Can not create object')
        self.assertIsInstance(object_id, six.string_types)
        self.assertIsInstance(self.score.createdAt, datetime.datetime)
        self.assertTrue(GameScore.Query.filter(objectId=object_id).exists(), 'Can not create object')

    def testCanUpdateExistingObject(self):
        self.sao_paulo.save()
        self.sao_paulo.country = 'Brazil'
        self.sao_paulo.save()
        self.assertIsInstance(self.sao_paulo.updatedAt, datetime.datetime)

        city = City.Query.get(name='São Paulo')
        self.assertEqual(city.country, 'Brazil', 'Could not update object')

    def testCanDeleteExistingObject(self):
        self.score.save()
        object_id = self.score.objectId
        self.score.delete()
        self.assertFalse(GameScore.Query.filter(objectId=object_id).exists(),
                         'Failed to delete object %s on Parse ' % self.score)

    def testCanIncrementField(self):
        previous_score = self.score.score
        self.score.save()
        self.score.increment('score')
        self.assertTrue(GameScore.Query.filter(score=previous_score + 1).exists(),
                     'Failed to increment score on backend')

    def testAssociatedObject(self):
        """test saving and associating a different object"""

        self.collected_item.save()
        self.score.item = self.collected_item
        self.score.save()

        # get the object, see if it has saved
        qs = GameScore.Query.get(objectId=self.score.objectId)
        self.assertIsInstance(qs.item, CollectedItem)<commit_msg>Added `testCanRemoveField`<commit_after>#!/usr/bin/env python
#-*- coding: utf-8 -*-

"""
Contains unit tests for the Python Parse REST API wrapper
"""
from __future__ import print_function

import os
import sys
import subprocess
import unittest
import datetime
import six
from itertools import chain

from parse_rest.core import ResourceRequestNotFound
from parse_rest.connection import register, ParseBatcher
from parse_rest.datatypes import GeoPoint, Object, Function, Pointer
from parse_rest.user import User
from parse_rest import query
from parse_rest.installation import Push

try:
    import settings_local
except ImportError:
    sys.exit('You must create a settings_local.py file with APPLICATION_ID, ' \
                 'REST_API_KEY, MASTER_KEY variables set')


register(
    getattr(settings_local, 'APPLICATION_ID'),
    getattr(settings_local, 'REST_API_KEY'),
    master_key=getattr(settings_local, 'MASTER_KEY')
)

GLOBAL_JSON_TEXT = """{
    "applications": {
        "_default": {
            "link": "parseapi"
        },
        "parseapi": {
            "applicationId": "%s",
            "masterKey": "%s"
        }
    },
    "global": {
        "parseVersion": "1.1.16"
    }
}
"""


class Game(Object):
    pass


class GameScore(Object):
    pass


class GameMap(Object):
    pass


class GameMode(Object):
    pass


class City(Object):
    pass


class Review(Object):
    pass


class CollectedItem(Object):
    pass


class TestObject(unittest.TestCase):
    def setUp(self):
        self.score = GameScore(score=1337, player_name='<NAME>', cheat_mode=False)
        self.sao_paulo = City(name='São Paulo', location=GeoPoint(-23.5, -46.6167))
        self.collected_item = CollectedItem(type="Sword", isAwesome=True)

    def tearDown(self):
        city_name = getattr(self.sao_paulo, 'name', None)
        game_score = getattr(self.score, 'score', None)
        collected_item_type = getattr(self.collected_item, 'type', None)
        if city_name:
            ParseBatcher().batch_delete(City.Query.filter(name=city_name))
        if game_score:
            ParseBatcher().batch_delete(GameScore.Query.filter(score=game_score))
        if collected_item_type:
            ParseBatcher().batch_delete(CollectedItem.Query.filter(type=collected_item_type))
        
    def testCanInitialize(self):
        self.assertEqual(self.score.score, 1337, 'Could not set score')

    def testCanInstantiateParseType(self):
        self.assertEqual(self.sao_paulo.location.latitude, -23.5)

    def testFactory(self):
        self.assertEqual(Object.factory('_User'), User)
        self.assertEqual(Object.factory('GameScore'), GameScore)

    def testCanSaveDates(self):
        now = datetime.datetime.now()
        self.score.last_played = now
        self.score.save()
        self.assertEqual(self.score.last_played, now, 'Could not save date')

    def testCanCreateNewObject(self):
        self.score.save()
        object_id = self.score.objectId

        self.assertIsNotNone(object_id, 'Can not create object')
        self.assertIsInstance(object_id, six.string_types)
        self.assertIsInstance(self.score.createdAt, datetime.datetime)
        self.assertTrue(GameScore.Query.filter(objectId=object_id).exists(), 'Can not create object')

    def testCanUpdateExistingObject(self):
        self.sao_paulo.save()
        self.sao_paulo.country = 'Brazil'
        self.sao_paulo.save()
        self.assertIsInstance(self.sao_paulo.updatedAt, datetime.datetime)

        city = City.Query.get(name='São Paulo')
        self.assertEqual(city.country, 'Brazil', 'Could not update object')

    def testCanDeleteExistingObject(self):
        self.score.save()
        object_id = self.score.objectId
        self.score.delete()
        self.assertFalse(GameScore.Query.filter(objectId=object_id).exists(),
                         'Failed to delete object %s on Parse ' % self.score)

    def testCanIncrementField(self):
        previous_score = self.score.score
        self.score.save()
        self.score.increment('score')
        self.assertTrue(GameScore.Query.filter(score=previous_score + 1).exists(),
                     'Failed to increment score on backend')
                     
    def testCanRemoveField(self):
        self.score.save()
        self.score.remove('score')
        self.assertTrue(GameScore.Query.filter(score=None).exists(),
                     'Failed to remove score on backend')


    def testAssociatedObject(self):
        """test saving and associating a different object"""

        self.collected_item.save()
        self.score.item = self.collected_item
        self.score.save()

        # get the object, see if it has saved
        qs = GameScore.Query.get(objectId=self.score.objectId)
        self.assertIsInstance(qs.item, CollectedItem)
{'fraction_non_alphanumeric': 0.10906183368869936, 'fraction_numerical': 0.005543710021321962, 'mean_word_length': 3.0210030004286326, 'pattern_counts': {'":': 16, '<': 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': '309186', 'n_tokens_mistral': 2883, 'n_tokens_neox': 2775, 'n_words': 571}