# -*- coding: utf-8 -*-
from __future__ import print_function

import dedupe
import unittest
import codecs
from collections import OrderedDict
import simplejson as json

import sys


class SerializerTest(unittest.TestCase) :
  def test_writeTraining(self) :
    if sys.version < '3' :
      from StringIO import StringIO
      output = StringIO()
      encoded_file = codecs.EncodedFile(output, data_encoding='utf8', file_encoding='ascii')
    else :
      from io import StringIO
      encoded_file = StringIO()

      training_pairs = OrderedDict(
        {u"distinct":[
          (dedupe.core.frozendict(OrderedDict(((u'bar', frozenset([u'barë'])),
                                               (u'foo', u'baz')))), 
           dedupe.core.frozendict({u'foo' : u'baz'}))], 
         u"match" : []})
      
      json.dump(training_pairs, 
                encoded_file, 
                default=dedupe.serializer._to_json,
                ensure_ascii = True)

      encoded_file.seek(0)

      loaded_training_pairs = json.load(encoded_file, 
                                        cls=dedupe.serializer.dedupe_decoder)

      assert loaded_training_pairs["distinct"][0] ==\
          training_pairs["distinct"][0]

      assert isinstance(loaded_training_pairs["distinct"][0][0]["bar"], 
                        frozenset)

      deduper = dedupe.Dedupe([{'field' : 'foo', 'type' : 'String'}])

      encoded_file.seek(0)

      deduper.readTraining(encoded_file)
      print(deduper.training_pairs)
      print(training_pairs)
      assert deduper.training_pairs == training_pairs

      encoded_file.close()


if __name__ == "__main__":
    unittest.main()


@@ -218,16 +218,18 @@
 Case) :%0A
+  
   def te
@@ -249,24 +249,28 @@
 ing(self) :%0A
+    
     if sys.v
@@ -286,24 +286,30 @@
 '3' :%0A      
+  
+    
 from StringI
@@ -332,16 +332,22 @@
 O%0A      
+   
+   
 output =
@@ -358,16 +358,22 @@
 ingIO()%0A
+      
       en
@@ -457,16 +457,20 @@
 ascii')%0A
+    
     else
@@ -474,24 +474,30 @@
 lse :%0A      
+ 
+     
 from io impo
@@ -510,24 +510,30 @@
 ingIO%0A      
+   
+   
 encoded_file
@@ -547,16 +547,18 @@
 ngIO()%0A%0A
+  
       tr
@@ -593,16 +593,20 @@
         
+  
+  
 %7Bu%22disti
@@ -612,16 +612,22 @@
 inct%22:%5B%0A
+      
         
@@ -744,16 +744,58 @@
         
+      
+                                    
 (u'foo',
@@ -808,16 +808,22 @@
 ')))), %0A
+      
         
@@ -880,16 +880,20 @@
         
+ 
+   
 u%22match%22
@@ -904,15 +904,11 @@
 %5D%7D)%0A
-    
+%0A
   
-%0A
     
@@ -944,32 +944,44 @@
                 
+     
+       
 encoded_file, %0A 
@@ -971,32 +971,44 @@
  encoded_file, %0A
+            
                 
@@ -1059,16 +1059,28 @@
         
+          
+  
 ensure_a
@@ -1093,16 +1093,18 @@
  True)%0A%0A
+  
       en
@@ -1129,16 +1129,18 @@
 %0A%0A      
+  
 loaded_t
@@ -1220,16 +1220,52 @@
         
+                       
+             
 cls=dedu
@@ -1295,16 +1295,18 @@
 coder)%0A%0A
+  
       as
@@ -1353,32 +1353,38 @@
 %5D ==%5C%0A          
+      
 training_pairs%5B%22
@@ -1398,16 +1398,18 @@
 t%22%5D%5B0%5D%0A%0A
+  
       as
@@ -1497,16 +1497,36 @@
         
+      
+              
 frozense
@@ -1531,24 +1531,26 @@
 set)%0A%0A      
+  
 deduper = de
@@ -1600,32 +1600,34 @@
 ring'%7D%5D)%0A%0A      
+  
 encoded_file.see
@@ -1628,24 +1628,26 @@
 le.seek(0)%0A%0A
+  
       dedupe
@@ -1677,24 +1677,26 @@
 file)%0A      
+ 
+ 
 print(dedupe
@@ -1715,24 +1715,26 @@
 airs)%0A      
+  
 print(traini
@@ -1745,24 +1745,26 @@
 airs)%0A      
+ 
+ 
 assert dedup
@@ -1800,16 +1800,18 @@
 _pairs%0A%0A
+  
       en

fix indents