Commits

Jayson Reis committed 4139412

added __getstate__ to Visitor so if user serialize it before unique_id is acessed, it wont be null

  • Participants
  • Parent commits 112283c

Comments (0)

Files changed (3)

File pyga/entities.py

                 self.unique_id = self.generate_unique_id()
         return object.__getattribute__(self, name)
 
+    def __getstate__(self):
+        state = self.__dict__
+        if state.get('user_agent') is None:
+            state['unique_id'] = self.generate_unique_id()
+
+        return state
+
     def extract_from_utma(self, utma):
         '''
         Will extract information for the "unique_id", "first_visit_time", "previous_visit_time",
                     ips = meta.get(key, '').split(',')
                     ip = ips[len(ips) - 1].strip()
                     if not utils.is_valid_ip(ip):
-                        ip = '' 
+                        ip = ''
                     if utils.is_private_ip(ip):
                         ip = ''
             if ip:

File tests/__init__.py

+from .serialize import *

File tests/serialize.py

+import unittest
+from cPickle import loads, dumps
+import sys
+sys.path.insert(0, '..')
+
+from pyga.requests import Visitor
+
+
+class TestSerialize(unittest.TestCase):
+    def test_unique_id_must_be_equals(self):
+        """
+        This will test if the unique_id is equals after deserialization
+        even if it is not acessed before serialization
+        """
+        visitor = Visitor()
+        serialized_visitor = dumps(visitor)
+        deserialized_visitor = loads(serialized_visitor)
+        self.assertEqual(visitor.unique_id, deserialized_visitor.unique_id)