<commit_before># -*- coding: utf-8 -*-
import unittest
import time
from app import create_app, db
from app.models import Category, Item, User, load_user


class ModelTestCase(unittest.TestCase):
  def setUp(self):
    self.app = create_app('testing')
    self.app_context = self.app.app_context()
    self.app_context.push()
    db.create_all()

  def tearDown(self):
    db.session.remove()
    db.drop_all()
    self.app_context.pop()


class CategoryModelTestCase(ModelTestCase):
  def test_insert_categories(self):
    categories = Category.query.all()
    self.assertEqual(categories, [])
    Category.insert_categories()
    c = Category.query.filter_by(name='soccer').one()
    self.assertEqual(c.name, 'soccer')

  def test_insert_categories_no_dup(self):
    '''make sure that if a category is already in db, it is not
    added again'''
    c = Category(name='soccer')
    db.session.add(c)
    db.session.commit()
    Category.insert_categories()
    self.assertEqual(Category.query.filter_by(name='soccer').count(), 1)


class UserModelTestCase(ModelTestCase):
  def test_username(self):
    u = User(fb_id='23', email='<EMAIL>', name='<NAME>',
            username='john', avatar_url='avatar.jpg')
    db.session.add(u)
    db.session.commit()
    #the username already exists, append the next uid to the name
    self.assertTrue(User.create_username('john') == 'john2')
    #username doesn't exist, so it can be assigned
    self.assertTrue(User.create_username('jacky') == 'jacky')

  def test_ping(self):
    u = User(fb_id='23', email='<EMAIL>', name='<NAME>',
            username='john', avatar_url='avatar.jpg')
    db.session.add(u)
    db.session.commit()
    time.sleep(2)
    last_seen_before = u.last_seen
    u.ping()
    self.assertTrue(u.last_seen > last_seen_before)

  def test_get_user(self):
    u = User(fb_id='23', email='<EMAIL>', name='<NAME>',
            username='john', avatar_url='avatar.jpg')
    db.session.add(u)
    db.session.commit()
    self.assertEqual(User.get_user('<EMAIL>'), u)
    self.assertEqual(User.get_user('<EMAIL>'), None)

  def test_load_user(self):
    u = User(fb_id='23', email='<EMAIL>', name='<NAME>',
            username='john', avatar_url='avatar.jpg')
    db.session.add(u)
    db.session.commit()
    self.assertEqual(load_user(u.id), u)

  def test_get_avatar(self):
    u = User(fb_id='23', email='<EMAIL>', name='<NAME>',
            username='john', avatar_url='avatar.jpg')
    db.session.add(u)
    db.session.commit()
    avatar = self.app.config['S3_LOCATION'] + "/" + \
                self.app.config['S3_BUCKET'] + \
                self.app.config['S3_UPLOAD_AVATAR_DIR'] + "/" + u.avatar_url
    self.assertEqual(u.avatar(), avatar)


class ItemModelTestCase(ModelTestCase):
  def test_get_image(self):
    item = Item(name='ball', description='small and round', price=23,
                image_url='ball.jpg')
    db.session.add(item)
    db.session.commit()
    image = self.app.config['S3_LOCATION'] + "/" + \
                self.app.config['S3_BUCKET'] + \
                self.app.config['S3_UPLOAD_ITEM_DIR'] + "/" + item.image_url
    self.assertEqual(item.image(), image)
<commit_msg>added tests for testing location method<commit_after># -*- coding: utf-8 -*-
import unittest
import time
from mock import patch
from app import create_app, db
from app.models import Category, Item, User, load_user
from app.geolocation import Geolocation

class ModelTestCase(unittest.TestCase):
  def setUp(self):
    self.app = create_app('testing')
    self.app_context = self.app.app_context()
    self.app_context.push()
    db.create_all()

  def tearDown(self):
    db.session.remove()
    db.drop_all()
    self.app_context.pop()


class CategoryModelTestCase(ModelTestCase):
  def test_insert_categories(self):
    categories = Category.query.all()
    self.assertEqual(categories, [])
    Category.insert_categories()
    c = Category.query.filter_by(name='soccer').one()
    self.assertEqual(c.name, 'soccer')

  def test_insert_categories_no_dup(self):
    '''make sure that if a category is already in db, it is not
    added again'''
    c = Category(name='soccer')
    db.session.add(c)
    db.session.commit()
    Category.insert_categories()
    self.assertEqual(Category.query.filter_by(name='soccer').count(), 1)


class UserModelTestCase(ModelTestCase):
  def test_username(self):
    u = User(fb_id='23', email='<EMAIL>', name='<NAME>',
            username='john', avatar_url='avatar.jpg')
    db.session.add(u)
    db.session.commit()
    #the username already exists, append the next uid to the name
    self.assertTrue(User.create_username('john') == 'john2')
    #username doesn't exist, so it can be assigned
    self.assertTrue(User.create_username('jacky') == 'jacky')

  def test_ping(self):
    u = User(fb_id='23', email='<EMAIL>', name='<NAME>',
            username='john', avatar_url='avatar.jpg')
    db.session.add(u)
    db.session.commit()
    time.sleep(2)
    last_seen_before = u.last_seen
    u.ping()
    self.assertTrue(u.last_seen > last_seen_before)

  def test_get_user(self):
    u = User(fb_id='23', email='<EMAIL>', name='<NAME>',
            username='john', avatar_url='avatar.jpg')
    db.session.add(u)
    db.session.commit()
    self.assertEqual(User.get_user('<EMAIL>'), u)
    self.assertEqual(User.get_user('<EMAIL>'), None)

  def test_load_user(self):
    u = User(fb_id='23', email='<EMAIL>', name='<NAME>',
            username='john', avatar_url='avatar.jpg')
    db.session.add(u)
    db.session.commit()
    self.assertEqual(load_user(u.id), u)

  def test_get_avatar(self):
    u = User(fb_id='23', email='<EMAIL>', name='<NAME>',
            username='john', avatar_url='avatar.jpg')
    db.session.add(u)
    db.session.commit()
    avatar = self.app.config['S3_LOCATION'] + "/" + \
                self.app.config['S3_BUCKET'] + \
                self.app.config['S3_UPLOAD_AVATAR_DIR'] + "/" + u.avatar_url
    self.assertEqual(u.avatar(), avatar)

  def test_location_user_with_city(self):
    u = User(fb_id='23', email='<EMAIL>', name='<NAME>',
            username='john', avatar_url='avatar.jpg', city='Mountain View')
    db.session.add(u)
    db.session.commit()
    u.location('10.0.0.2')
    self.assertTrue(u.latitude == None)

  @patch('app.geolocation.Geolocation', return_value={'key': 'value'})
  @patch('app.geolocation.Geolocation.get_country', return_value=(True, 'US'))
  @patch('app.geolocation.Geolocation.get_state', return_value=(True, 'CA'))
  @patch('app.geolocation.Geolocation.get_city', return_value=(True, 'MV'))
  @patch('app.geolocation.Geolocation.get_latitude', return_value=(True, 123))
  @patch('app.geolocation.Geolocation.get_longitude', return_value=(True, -98))
  def test_location_user_no_city(self, m_geo, m_country, m_state, m_city,
      m_latitude, m_longitude):
    u = User(fb_id='23', email='<EMAIL>', name='<NAME>',
            username='john', avatar_url='avatar.jpg')
    db.session.add(u)
    db.session.commit()
    u.location('10.0.0.2')
    self.assertTrue(u.latitude == 123)

  @patch('app.geolocation.Geolocation', return_value={'key': 'value'})
  @patch('app.geolocation.Geolocation.get_country', return_value=(True, ''))
  def test_location_user_no_city_no_country(self, m_geo, m_country):
    u = User(fb_id='23', email='<EMAIL>', name='<NAME>',
            username='john', avatar_url='avatar.jpg')
    db.session.add(u)
    db.session.commit()
    u.location('10.0.0.2')
    self.assertTrue(u.latitude == None)


class ItemModelTestCase(ModelTestCase):
  def test_get_image(self):
    item = Item(name='ball', description='small and round', price=23,
                image_url='ball.jpg')
    db.session.add(item)
    db.session.commit()
    image = self.app.config['S3_LOCATION'] + "/" + \
                self.app.config['S3_BUCKET'] + \
                self.app.config['S3_UPLOAD_ITEM_DIR'] + "/" + item.image_url
    self.assertEqual(item.image(), image)

{'fraction_non_alphanumeric': 0.16438699962354122, 'fraction_numerical': 0.009160496925586648, 'mean_word_length': 3.6882352941176473, 'pattern_counts': {'":': 0, '<': 33, '<?xml version=': 0, '>': 35, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 3, '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': '927220', 'n_tokens_mistral': 2790, 'n_tokens_neox': 2744, 'n_words': 453}