# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.

from nose.plugins.attrib import attr

from socorro.external.postgresql.bugs import Bugs, MissingOrBadArgumentError

from .unittestbase import PostgreSQLTestCase


#==============================================================================
@attr(integration='postgres')  # for nosetests
class IntegrationTestBugs(PostgreSQLTestCase):
    """Test socorro.external.postgresql.bugs.Bugs class. """

    #--------------------------------------------------------------------------
    def setUp(self):
        """Set up this test class by populating the reports table with fake
        data. """
        super(IntegrationTestBugs, self).setUp()

        cursor = self.connection.cursor()

        # Insert data
        cursor.execute("""
            INSERT INTO bugs VALUES
            (1),
            (2),
            (3),
            (4);
        """)

        cursor.execute("""
            INSERT INTO bug_associations
            (signature, bug_id)
            VALUES
            (
                'sign1',
                1
            ),
            (
                'js',
                1
            ),
            (
                'mysignature',
                2
            ),
            (
                'mysignature',
                3
            );
        """)

        self.connection.commit()

    #--------------------------------------------------------------------------
    def tearDown(self):
        """Clean up the database, delete tables and functions. """
        cursor = self.connection.cursor()
        cursor.execute("""
            TRUNCATE bug_associations, bugs
            CASCADE
        """)
        self.connection.commit()
        super(IntegrationTestBugs, self).tearDown()

    #--------------------------------------------------------------------------
    def test_get(self):
        bugs = Bugs(config=self.config)

        #......................................................................
        # Test 1: a valid signature with 2 bugs
        params = {
            "signatures": "mysignature"
        }
        res = bugs.get(**params)
        res_expected = {
            "hits": [
                {
                    "id": 2,
                    "signature": "mysignature"
                },
                {
                    "id": 3,
                    "signature": "mysignature"
                }
            ],
            "total": 2
        }

        self.assertEqual(res, res_expected)

        #......................................................................
        # Test 2: several signatures with bugs
        params = {
            "signatures": ["mysignature", "js"]
        }
        res = bugs.get(**params)
        res_expected = {
            "hits": [
                {
                    "id": 1,
                    "signature": "sign1"
                },
                {
                    "id": 1,
                    "signature": "js"
                },
                {
                    "id": 2,
                    "signature": "mysignature"
                },
                {
                    "id": 3,
                    "signature": "mysignature"
                }
            ],
            "total": 4
        }

        self.assertEqual(res, res_expected)

        #......................................................................
        # Test 3: a signature without bugs
        params = {
            "signatures": "unknown"
        }
        res = bugs.get(**params)
        res_expected = {
            "hits": [],
            "total": 0
        }

        self.assertEqual(res, res_expected)

        #......................................................................
        # Test 4: missing argument
        params = {}
        self.assertRaises(MissingOrBadArgumentError, bugs.get, **params)

@@ -2267,34 +2267,35 @@
      res = bugs.
-ge
+pos
 t(**params)%0A    
@@ -2614,51 +2614,323 @@
   %7D%0A
-%0A        self.assertEqual(res, res_expected
+        self.assertEqual(res%5B'total'%5D, res_expected%5B'total'%5D)%0A        # by convert the hits to sets we can be certain order doesn't matter%0A        self.assertEqual(%0A            set(%5B(x%5B'id'%5D, x%5B'signature'%5D) for x in res%5B'hits'%5D%5D),%0A            set(%5B(x%5B'id'%5D, x%5B'signature'%5D) for x in res_expected%5B'hits'%5D%5D)%0A        
 )%0A%0A 
@@ -3143,34 +3143,35 @@
      res = bugs.
-ge
+pos
 t(**params)%0A    
@@ -3718,32 +3718,41 @@
 .assertEqual(res
+%5B'total'%5D
 , res_expected)%0A
@@ -3741,32 +3741,219 @@
 '%5D, res_expected
+%5B'total'%5D)%0A        self.assertEqual(%0A            set(%5B(x%5B'id'%5D, x%5B'signature'%5D) for x in res%5B'hits'%5D%5D),%0A            set(%5B(x%5B'id'%5D, x%5B'signature'%5D) for x in res_expected%5B'hits'%5D%5D)%0A        
 )%0A%0A        #....
@@ -4146,18 +4146,19 @@
  = bugs.
-ge
+pos
 t(**para
@@ -4482,18 +4482,19 @@
 r, bugs.
-ge
+pos
 t, **par

fix test that depends on order, r=selenamarie