import logging
from tests import FlexGetBase, with_filecopy

log = logging.getLogger(__name__)

try:
    import pyrocore
except ImportError:
    log.warning("Condition tests disabled (pyrocore>=0.4 not installed)")
    pyrocore = None


class TestCondition(FlexGetBase):

    __yaml__ = """
        presets:
          global:
            disable_builtins: [seen]
            mock:
              - {title: 'test', year: 2000}
              - {title: 'brilliant', rating: 9.9}
              - {title: 'fresh', year: 2011}

        feeds:
          test_condition_reject:
            reject_if: year<2011

          test_condition_accept:
            accept_if:
              - ?year>=2010
              - ?rating>9

          test_condition_and1:
            accept_if: '*t ?rating>9'
          test_condition_and2:
            accept_if: '*t'
    """

    def test_reject(self):
        if pyrocore:
            self.execute_feed('test_condition_reject')
            count = len(self.feed.rejected) 
            assert count == 1

    def test_accept(self):
        if pyrocore:
            self.execute_feed('test_condition_accept')
            count = len(self.feed.accepted)
            assert count == 2

    def test_implicit_and(self):
        if pyrocore:
            for i in "12":
                self.execute_feed('test_condition_and' + i)
                count = len(self.feed.accepted)
                assert count == int(i)


class TestDownloadCondition(FlexGetBase):

    __yaml__ = """
        presets:
          global:
            disable_builtins: [seen]
            mock:
              - {title: 'test', file: 'test.torrent'}
              - {title: 'prv', file: 'private.torrent'}
              - {title: 'not_a_torrent'}

        feeds:
          test_condition_field_access1:
            reject_if_download: ?torrent.content.info.?private=1

          test_condition_field_access2:
            reject_if_download: ?torrent.content.announce=*ubuntu.com[/:]*
    """

    def test_field_access(self):
        for i in "12":
            self.execute_feed('test_condition_field_access' + i)
            count = len(self.feed.rejected)
            assert count == int(i), "Expected %s rejects, got %d" % (i, count)
            assert i != "1" or self.feed.rejected[0]["title"] == "prv"

@@ -2005,32 +2005,57 @@
 d_access(self):%0A
+        if pyrocore:%0A    
         for i in
@@ -2053,32 +2053,36 @@
  for i in %2212%22:%0A
+    
             self
@@ -2134,32 +2134,36 @@
  i)%0A            
+  
+  
 count = len(self
@@ -2174,24 +2174,28 @@
 d.rejected)%0A
+    
             
@@ -2261,16 +2261,20 @@
  count)%0A
+    
         

test is conditional