<commit_before>"""Test Operator."""
import unittest

import util
from operator import Operator

class TestOperator(unittest.TestCase):
  expect = {
    'geometry': {'coordinates': [[[-117.133162, 36.425288],
                                   [-116.40094, 36.641496],
                                   [-116.751677, 36.915682],
                                   [-116.76821, 36.914893],
                                   [-116.81797, 36.88108],
                                   [-117.133162, 36.425288]]],
                 'type': 'Polygon'},
   'identifiers': ['gtfs://unknown/o/DTA'],
   'name': 'Demo Transit Authority',
   'onestopId': 'o-9qs-demotransitauthority',
   'properties': {},
   'serves': ['s-9qscv9zzb5-bullfrogdemo',
               's-9qt0rnrkjt-amargosavalleydemo',
               's-9qscwx8n60-nyecountyairportdemo',
               's-9qkxnx40xt-furnacecreekresortdemo',
               's-9qscyz5vqg-doing~dndemo',
               's-9qsfp2212t-stagecoachhotel~casinodemo',
               's-9qsfp00vhs-north~nademo',
               's-9qsfnb5uz6-north~dndemo',
               's-9qsczn2rk0-emain~sirvingdemo'],
   'tags': {},
   'type': 'FeatureCollection'
  }
  
  def _sanity(self, entity):
    """Perform sanity checks! After from_gtfs or from_json..."""
    # More extensive checks, since json export includes nearly everything.
    assert entity.geohash() == '9qs'
    assert entity.onestop() == self.expect['onestopId']
    assert len(entity.identifiers()) == 1
    # Routes
    assert len(entity.routes()) == 5
    expect = ['r-9qsczp-40', 'r-9qt1-50', 
      'r-9qsb-20', 'r-9qscy-30', 'r-9qscy-10']
    for i in entity.routes():
      assert i.onestop() in expect
      assert len(i.identifiers()) == 1
    # Stops
    assert len(entity.stops()) == 9
    for i in entity.stops():
      assert i.onestop() in self.expect['serves']
      assert len(i.identifiers()) == 1    

  def test_init(self):
    entity = Operator()
      
  def test_geohash(self):
    entity = util.example_feed().operator(self.expect['onestopId'])
    assert entity.geohash() == '9qs'
  
  def test_from_gtfs(self):
    feed = util.example_gtfs_feed()
    agency = feed.agency('DTA')
    agency.preload()
    entity = Operator.from_gtfs(agency)
    self._sanity(entity)
    
  def test_from_json(self):
    feed = util.example_feed()
    entity = util.example_feed().operator(self.expect['onestopId'])
    roundtrip = Operator.from_json(entity.json())
    self._sanity(roundtrip)
  <commit_msg>Update to new feed preload()<commit_after>"""Test Operator."""
import unittest

import util
from operator import Operator

class TestOperator(unittest.TestCase):
  expect = {
    'geometry': {'coordinates': [[[-117.133162, 36.425288],
                                   [-116.40094, 36.641496],
                                   [-116.751677, 36.915682],
                                   [-116.76821, 36.914893],
                                   [-116.81797, 36.88108],
                                   [-117.133162, 36.425288]]],
                 'type': 'Polygon'},
   'identifiers': ['gtfs://unknown/o/DTA'],
   'name': 'Demo Transit Authority',
   'onestopId': 'o-9qs-demotransitauthority',
   'properties': {},
   'serves': ['s-9qscv9zzb5-bullfrogdemo',
               's-9qt0rnrkjt-amargosavalleydemo',
               's-9qscwx8n60-nyecountyairportdemo',
               's-9qkxnx40xt-furnacecreekresortdemo',
               's-9qscyz5vqg-doing~dndemo',
               's-9qsfp2212t-stagecoachhotel~casinodemo',
               's-9qsfp00vhs-north~nademo',
               's-9qsfnb5uz6-north~dndemo',
               's-9qsczn2rk0-emain~sirvingdemo'],
   'tags': {},
   'type': 'FeatureCollection'
  }
  
  def _sanity(self, entity):
    """Perform sanity checks! After from_gtfs or from_json..."""
    # More extensive checks, since json export includes nearly everything.
    assert entity.geohash() == '9qs'
    assert entity.onestop() == self.expect['onestopId']
    assert len(entity.identifiers()) == 1
    # Routes
    assert len(entity.routes()) == 5
    expect = ['r-9qsczp-40', 'r-9qt1-50', 
      'r-9qsb-20', 'r-9qscy-30', 'r-9qscy-10']
    for i in entity.routes():
      assert i.onestop() in expect
      assert len(i.identifiers()) == 1
    # Stops
    assert len(entity.stops()) == 9
    for i in entity.stops():
      assert i.onestop() in self.expect['serves']
      assert len(i.identifiers()) == 1    

  def test_init(self):
    entity = Operator()
      
  def test_geohash(self):
    entity = util.example_feed().operator(self.expect['onestopId'])
    assert entity.geohash() == '9qs'
  
  def test_from_gtfs(self):
    feed = util.example_gtfs_feed()
    feed.preload()
    agency = feed.agency('DTA')
    entity = Operator.from_gtfs(agency)
    self._sanity(entity)
    
  def test_from_json(self):
    feed = util.example_feed()
    entity = util.example_feed().operator(self.expect['onestopId'])
    roundtrip = Operator.from_json(entity.json())
    self._sanity(roundtrip)
  
{'fraction_non_alphanumeric': 0.14915254237288136, 'fraction_numerical': 0.05982053838484546, 'mean_word_length': 2.2805755395683454, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 10, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5873515', 'n_tokens_mistral': 1943, 'n_tokens_neox': 1700, 'n_words': 294}