<s> from __future__ import unicode_literals <EOL> import os <EOL> import logging <EOL> import unittest <EOL> from datetime import datetime <EOL> from django . test import TestCase , TransactionTestCase <EOL> from django . conf import settings <EOL> from django . db import transaction <EOL> from docutil . commands_util import load_model <EOL> from docutil . test_util import clean_test_dir <EOL> from project . models import Project <EOL> from project . actions import create_project_local , create_project_db , create_release_db , STHREAD_PATH <EOL> from codebase . models import CodeElementKind , SingleCodeReference , CodeSnippet <EOL> from codebase . actions import create_code_element_kinds <EOL> from channel . models import SupportChannel , Message <EOL> from channel . actions import create_channel_local , create_channel_db , list_channels_db , list_channels_local , get_channel_path , toc_refresh , toc_download_section , toc_download_entries , parse_channel , post_process_channel <EOL> class ChannelSetup ( TestCase ) : <EOL> def setUp ( self ) : <EOL> logging . basicConfig ( level = logging . WARNING ) <EOL> settings . PROJECT_FS_ROOT = settings . PROJECT_FS_ROOT_TEST <EOL> create_project_local ( '<STR_LIT>' ) <EOL> create_project_db ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> create_release_db ( '<STR_LIT>' , '<STR_LIT>' , True ) <EOL> create_release_db ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> def tearDown ( self ) : <EOL> Project . objects . all ( ) . delete ( ) <EOL> CodeElementKind . objects . all ( ) . delete ( ) <EOL> SingleCodeReference . objects . all ( ) . delete ( ) <EOL> CodeSnippet . objects . all ( ) . delete ( ) <EOL> clean_test_dir ( ) <EOL> def test_create_doc_local ( self ) : <EOL> create_channel_local ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> create_channel_local ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> path = get_channel_path ( '<STR_LIT>' , root = True ) <EOL> self . assertEqual ( <NUM_LIT:2> , len ( os . listdir ( path ) ) ) <EOL> def test_list_doc_local ( self ) : <EOL> create_channel_local ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' ) <EOL> create_channel_local ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' ) <EOL> self . assertEqual ( <NUM_LIT:2> , len ( list_channels_local ( '<STR_LIT>' ) ) ) <EOL> def test_create_doc_db ( self ) : <EOL> create_channel_db ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' ) <EOL> create_channel_db ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' ) <EOL> self . assertEqual ( <NUM_LIT:2> , SupportChannel . objects . count ( ) ) <EOL> def test_list_doc_db ( self ) : <EOL> create_channel_db ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' ) <EOL> create_channel_db ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' ) <EOL> self . assertEqual ( <NUM_LIT:2> , len ( list_channels_db ( '<STR_LIT>' ) ) ) <EOL> def test_apache_syncer ( self ) : <EOL> create_channel_db ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' <EOL> ) <EOL> create_channel_local ( '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' <EOL> ) <EOL> pname = '<STR_LIT>' <EOL> cname = '<STR_LIT>' <EOL> toc_refresh ( pname , cname ) <EOL> model = load_model ( pname , STHREAD_PATH , cname ) <EOL> self . assertEqual ( <EOL> '<STR_LIT>' , <EOL> model . toc_sections [ <NUM_LIT:0> ] . url ) <EOL> self . assertFalse ( model . toc_sections [ <NUM_LIT:0> ] . downloaded ) <EOL> self . assertTrue ( len ( model . toc_sections ) >= <NUM_LIT> ) <EOL> for i in xrange ( <NUM_LIT:0> , <NUM_LIT> ) : <EOL> self . assertEqual ( i , model . toc_sections [ i ] . index ) <EOL> toc_download_section ( pname , cname , start = <NUM_LIT:0> , end = <NUM_LIT:4> ) <EOL> model = load_model ( pname , STHREAD_PATH , cname ) <EOL> self . assertTrue ( model . toc_sections [ <NUM_LIT:0> ] . downloaded ) <EOL> self . assertTrue ( model . toc_sections [ <NUM_LIT:1> ] . downloaded ) <EOL> self . assertTrue ( model . toc_sections [ <NUM_LIT:2> ] . downloaded ) <EOL> self . assertTrue ( model . toc_sections [ <NUM_LIT:3> ] . downloaded ) <EOL> self . assertFalse ( model . toc_sections [ <NUM_LIT:4> ] . downloaded ) <EOL> self . assertEqual ( <NUM_LIT> , len ( model . entries ) ) <EOL> self . assertEqual ( <NUM_LIT:0> , model . entries [ <NUM_LIT:0> ] . index ) <EOL> self . assertFalse ( model . entries [ <NUM_LIT:0> ] . downloaded ) <EOL> self . assertEqual ( <NUM_LIT:1000> , model . entries [ <NUM_LIT> ] . index ) <EOL> self . assertEqual ( <NUM_LIT> , model . entries [ <NUM_LIT> ] . index ) <EOL> self . assertTrue ( model . entries [ <NUM_LIT> ] . url . find ( '<STR_LIT>' ) > - <NUM_LIT:1> ) <EOL> toc_download_entries ( pname , cname , <NUM_LIT:0> , <NUM_LIT:1> ) <EOL> model = load_model ( pname , STHREAD_PATH , cname ) <EOL> self . assertTrue ( model . entries [ <NUM_LIT:0> ] . downloaded ) <EOL> self . assertFalse ( model . entries [ <NUM_LIT:1> ] . downloaded ) <EOL> path = os . path . join ( settings . PROJECT_FS_ROOT , <EOL> model . entries [ <NUM_LIT:0> ] . local_paths [ <NUM_LIT:0> ] ) <EOL> self . assertTrue ( os . path . exists ( path ) ) <EOL> def test_phpbb_syncer ( self ) : <EOL> create_channel_db ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' <EOL> ) <EOL> create_channel_local ( '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' <EOL> ) <EOL> pname = '<STR_LIT>' <EOL> cname = '<STR_LIT>' <EOL> toc_refresh ( pname , cname ) <EOL> model = load_model ( pname , STHREAD_PATH , cname ) <EOL> self . assertEqual ( <EOL> '<STR_LIT>' , <EOL> model . toc_sections [ <NUM_LIT:0> ] . url ) <EOL> self . assertFalse ( model . toc_sections [ <NUM_LIT:0> ] . downloaded ) <EOL> self . assertTrue ( len ( model . toc_sections ) > <NUM_LIT> ) <EOL> for i in xrange ( <NUM_LIT:0> , <NUM_LIT> ) : <EOL> self . assertEqual ( i , model . toc_sections [ i ] . index ) <EOL> toc_download_section ( pname , cname , start = <NUM_LIT:0> , end = <NUM_LIT:4> ) <EOL> model = load_model ( pname , STHREAD_PATH , cname ) <EOL> self . assertTrue ( model . toc_sections [ <NUM_LIT:0> ] . downloaded ) <EOL> self . assertTrue ( model . toc_sections [ <NUM_LIT:1> ] . downloaded ) <EOL> self . assertTrue ( model . toc_sections [ <NUM_LIT:2> ] . downloaded ) <EOL> self . assertTrue ( model . toc_sections [ <NUM_LIT:3> ] . downloaded ) <EOL> self . assertFalse ( model . toc_sections [ <NUM_LIT:4> ] . downloaded ) <EOL> self . assertEqual ( <NUM_LIT:100> , len ( model . entries ) ) <EOL> self . assertEqual ( <NUM_LIT:0> , model . entries [ <NUM_LIT:0> ] . index ) <EOL> self . assertFalse ( model . entries [ <NUM_LIT:0> ] . downloaded ) <EOL> self . assertEqual ( <NUM_LIT:1000> , model . entries [ <NUM_LIT> ] . index ) <EOL> self . assertEqual ( <NUM_LIT> , model . entries [ <NUM_LIT> ] . index ) <EOL> self . assertTrue ( model . entries [ <NUM_LIT> ] . url . find ( '<STR_LIT>' ) > - <NUM_LIT:1> ) <EOL> toc_download_entries ( pname , cname , <NUM_LIT> , <NUM_LIT> ) <EOL> model = load_model ( pname , STHREAD_PATH , cname ) <EOL> self . assertTrue ( model . entries [ <NUM_LIT> ] . downloaded ) <EOL> self . assertFalse ( model . entries [ <NUM_LIT:50> ] . downloaded ) <EOL> path = os . path . join ( settings . PROJECT_FS_ROOT , <EOL> model . entries [ <NUM_LIT> ] . local_paths [ <NUM_LIT:0> ] ) <EOL> self . assertTrue ( os . path . exists ( path ) ) <EOL> path = os . path . join ( settings . PROJECT_FS_ROOT , <EOL> model . entries [ <NUM_LIT> ] . local_paths [ <NUM_LIT:1> ] ) <EOL> self . assertTrue ( os . path . exists ( path ) ) <EOL> def test_fudeclipse_syncer ( self ) : <EOL> create_channel_db ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' <EOL> ) <EOL> create_channel_local ( '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' <EOL> ) <EOL> pname = '<STR_LIT>' <EOL> cname = '<STR_LIT>' <EOL> toc_refresh ( pname , cname ) <EOL> model = load_model ( pname , STHREAD_PATH , cname ) <EOL> self . assertEqual ( <EOL> '<STR_LIT>' , <EOL> model . toc_sections [ <NUM_LIT:0> ] . url ) <EOL> self . assertFalse ( model . toc_sections [ <NUM_LIT:0> ] . downloaded ) <EOL> self . assertTrue ( len ( model . toc_sections ) >= <NUM_LIT> ) <EOL> for i in xrange ( <NUM_LIT:0> , <NUM_LIT> ) : <EOL> self . assertEqual ( i , model . toc_sections [ i ] . index ) <EOL> toc_download_section ( pname , cname , start = <NUM_LIT:0> , end = <NUM_LIT:4> ) <EOL> model = load_model ( pname , STHREAD_PATH , cname ) <EOL> self . assertTrue ( model . toc_sections [ <NUM_LIT:0> ] . downloaded ) <EOL> self . assertTrue ( model . toc_sections [ <NUM_LIT:1> ] . downloaded ) <EOL> self . assertTrue ( model . toc_sections [ <NUM_LIT:2> ] . downloaded ) <EOL> self . assertTrue ( model . toc_sections [ <NUM_LIT:3> ] . downloaded ) <EOL> self . assertFalse ( model . toc_sections [ <NUM_LIT:4> ] . downloaded ) <EOL> self . assertEqual ( <NUM_LIT> , len ( model . entries ) ) <EOL> self . assertEqual ( <NUM_LIT:0> , model . entries [ <NUM_LIT:0> ] . index ) <EOL> self . assertFalse ( model . entries [ <NUM_LIT:0> ] . downloaded ) <EOL> self . assertEqual ( <NUM_LIT:1000> , model . entries [ <NUM_LIT> ] . index ) <EOL> self . assertEqual ( <NUM_LIT> , model . entries [ <NUM_LIT> ] . index ) <EOL> toc_download_entries ( pname , cname , <NUM_LIT> , <NUM_LIT> ) <EOL> model = load_model ( pname , STHREAD_PATH , cname ) <EOL> self . assertTrue ( model . entries [ <NUM_LIT> ] . downloaded ) <EOL> self . assertFalse ( model . entries [ <NUM_LIT> ] . downloaded ) <EOL> path = os . path . join ( settings . PROJECT_FS_ROOT , <EOL> model . entries [ <NUM_LIT> ] . local_paths [ <NUM_LIT:0> ] ) <EOL> self . assertTrue ( os . path . exists ( path ) ) <EOL> class ChannelParserTest ( TransactionTestCase ) : <EOL> @ transaction . commit_on_success <EOL> def setUp ( self ) : <EOL> logging . basicConfig ( level = logging . WARNING ) <EOL> settings . PROJECT_FS_ROOT = settings . PROJECT_FS_ROOT_TEST <EOL> create_code_element_kinds ( ) <EOL> create_project_local ( '<STR_LIT>' ) <EOL> create_project_db ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> create_release_db ( '<STR_LIT>' , '<STR_LIT>' , True ) <EOL> create_release_db ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> @ transaction . commit_on_success <EOL> def tearDown ( self ) : <EOL> Project . objects . all ( ) . delete ( ) <EOL> CodeElementKind . objects . all ( ) . delete ( ) <EOL> SingleCodeReference . objects . all ( ) . delete ( ) <EOL> CodeSnippet . objects . all ( ) . delete ( ) <EOL> clean_test_dir ( ) <EOL> @ transaction . autocommit <EOL> def test_apache_parser ( self ) : <EOL> create_channel_db ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' <EOL> ) <EOL> create_channel_local ( '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' <EOL> ) <EOL> pname = '<STR_LIT>' <EOL> cname = '<STR_LIT>' <EOL> toc_refresh ( pname , cname ) <EOL> toc_download_section ( pname , cname , start = <NUM_LIT:0> , end = <NUM_LIT:1> ) <EOL> toc_download_entries ( pname , cname , <NUM_LIT:9> , <NUM_LIT> ) <EOL> parse_channel ( pname , cname , True ) <EOL> self . assertEqual ( <NUM_LIT:8> , Message . objects . all ( ) . count ( ) ) <EOL> messages = list ( Message . objects . all ( ) ) <EOL> for message in messages : <EOL> print ( '<STR_LIT>' . format ( <EOL> message . title , message . author , message . msg_date , <EOL> message . word_count ) ) <EOL> print ( '<STR_LIT>' . format ( <EOL> message . code_snippets . count ( ) , <EOL> message . code_references . count ( ) ) ) <EOL> print ( '<STR_LIT>' ) <EOL> for code_snippet in message . code_snippets . all ( ) : <EOL> print ( '<STR_LIT>' . format ( code_snippet . language ) ) <EOL> for ref in message . code_references . all ( ) : <EOL> print ( '<STR_LIT>' . format ( ref . kind_hint . kind , ref . content ) ) <EOL> third_to_last = messages [ - <NUM_LIT:3> ] <EOL> self . assertEqual ( '<STR_LIT:l>' , third_to_last . code_snippets . all ( ) [ <NUM_LIT:0> ] . language ) <EOL> self . assertEqual ( '<STR_LIT>' , third_to_last . code_snippets . all ( ) [ <NUM_LIT:1> ] . language ) <EOL> fourth_to_last = messages [ - <NUM_LIT:4> ] <EOL> refs = [ ref . content . strip ( ) for ref in <EOL> fourth_to_last . code_references . all ( ) ] <EOL> self . assertEqual ( <NUM_LIT:4> , len ( refs ) ) <EOL> self . assertTrue ( '<STR_LIT>' in refs ) <EOL> self . assertTrue ( '<STR_LIT>' in refs ) <EOL> channel = post_process_channel ( pname , cname ) <EOL> self . assertEqual ( <NUM_LIT:4> , channel . threads . count ( ) ) <EOL> second_thread = channel . threads . all ( ) [ <NUM_LIT:1> ] <EOL> self . assertEqual ( <NUM_LIT:3> , second_thread . messages . count ( ) ) <EOL> indexes = [ msg . index for msg in second_thread . messages . all ( ) ] <EOL> self . assertEqual ( [ <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:2> ] , indexes ) <EOL> self . assertFalse ( second_thread . messages . all ( ) [ <NUM_LIT:0> ] . title . lower ( ) <EOL> . startswith ( '<STR_LIT>' ) ) <EOL> self . assertTrue ( second_thread . messages . all ( ) [ <NUM_LIT:1> ] . title . lower ( ) <EOL> . startswith ( '<STR_LIT>' ) ) <EOL> @ transaction . autocommit <EOL> def test_phpbb_parser ( self ) : <EOL> create_channel_db ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' <EOL> ) <EOL> create_channel_local ( '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' <EOL> ) <EOL> pname = '<STR_LIT>' <EOL> cname = '<STR_LIT>' <EOL> toc_refresh ( pname , cname ) <EOL> toc_download_section ( pname , cname , start = <NUM_LIT:0> , end = <NUM_LIT:2> ) <EOL> toc_download_entries ( pname , cname , <NUM_LIT> , <NUM_LIT> ) <EOL> parse_channel ( pname , cname , True ) <EOL> self . assertEqual ( <NUM_LIT> , Message . objects . all ( ) . count ( ) ) <EOL> messages = list ( Message . objects . all ( ) ) <EOL> for message in messages : <EOL> print ( message . url ) <EOL> print ( '<STR_LIT>' . format ( <EOL> message . title , message . author , message . msg_date , <EOL> message . word_count ) ) <EOL> print ( '<STR_LIT>' . format ( <EOL> message . code_snippets . count ( ) , <EOL> message . code_references . count ( ) ) ) <EOL> print ( '<STR_LIT>' ) <EOL> for code_snippet in message . code_snippets . all ( ) : <EOL> print ( '<STR_LIT>' . format ( code_snippet . language ) ) <EOL> for ref in message . code_references . all ( ) : <EOL> print ( '<STR_LIT>' . format ( ref . kind_hint . kind , ref . content ) ) <EOL> first_message = messages [ <NUM_LIT:0> ] <EOL> self . assertEqual ( '<STR_LIT:x>' , first_message . code_snippets . all ( ) [ <NUM_LIT:0> ] . language ) <EOL> self . assertEqual ( first_message . author . nickname , '<STR_LIT>' ) <EOL> self . assertEqual ( first_message . msg_date , datetime ( <NUM_LIT> , <NUM_LIT:8> , <NUM_LIT> , <NUM_LIT:10> , <NUM_LIT:16> ) ) <EOL> second_message = messages [ <NUM_LIT:1> ] <EOL> refs = [ ref . content . strip ( ) for ref in <EOL> second_message . code_references . all ( ) ] <EOL> self . assertEqual ( <NUM_LIT:4> , len ( refs ) ) <EOL> self . assertTrue ( '<STR_LIT>' in refs ) <EOL> self . assertTrue ( '<STR_LIT>' in refs ) <EOL> @ transaction . autocommit <EOL> def test_fudeclipse_parser ( self ) : <EOL> create_channel_db ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' <EOL> ) <EOL> create_channel_local ( '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' <EOL> ) <EOL> pname = '<STR_LIT>' <EOL> cname = '<STR_LIT>' <EOL> toc_refresh ( pname , cname ) <EOL> toc_download_section ( pname , cname , start = <NUM_LIT:0> , end = <NUM_LIT:2> ) <EOL> toc_download_entries ( pname , cname , <NUM_LIT:0> , <NUM_LIT:6> ) <EOL> parse_channel ( pname , cname , True ) <EOL> self . assertEqual ( <NUM_LIT> , Message . objects . all ( ) . count ( ) ) <EOL> messages = list ( Message . objects . all ( ) ) <EOL> for message in messages : <EOL> print ( '<STR_LIT>' . format ( <EOL> message . title , message . author , message . msg_date , <EOL> message . word_count ) ) <EOL> print ( '<STR_LIT>' . format ( <EOL> message . code_snippets . count ( ) , <EOL> message . code_references . count ( ) ) ) <EOL> print ( '<STR_LIT>' ) <EOL> for code_snippet in message . code_snippets . all ( ) : <EOL> print ( '<STR_LIT>' . format ( code_snippet . language ) ) <EOL> for ref in message . code_references . all ( ) : <EOL> print ( '<STR_LIT>' . format ( ref . kind_hint . kind , ref . content ) ) <EOL> first_message = messages [ <NUM_LIT:0> ] <EOL> self . assertEqual ( first_message . title , <EOL> '<STR_LIT>' ) <EOL> self . assertEqual ( first_message . author . nickname , '<STR_LIT>' ) <EOL> self . assertEqual ( first_message . msg_date , datetime ( <NUM_LIT> , <NUM_LIT:8> , <NUM_LIT> , <NUM_LIT:7> , <NUM_LIT:50> ) ) <EOL> refs = [ ref . content . strip ( ) for ref in <EOL> first_message . code_references . all ( ) ] <EOL> self . assertEqual ( <NUM_LIT:3> , len ( refs ) ) <EOL> self . assertTrue ( '<STR_LIT>' in refs ) </s>
