summaryrefslogtreecommitdiff
path: root/tests/functional-tests/01-insertion.py
blob: 3e2247db44c0d30cdfe16e6fe779b407fee67013 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
#!/usr/bin/env python2.5
#
# Copyright (C) 2010, Nokia <ivan.frade@nokia.com>
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
# 02110-1301, USA.
#

import sys,os,dbus
import unittest
import time
import random
import string
import datetime

TRACKER = 'org.freedesktop.Tracker1'
TRACKER_OBJ = '/org/freedesktop/Tracker1/Resources'
RESOURCES_IFACE = "org.freedesktop.Tracker1.Resources"


class TestUpdate (unittest.TestCase):
	
	def setUp(self):
		bus = dbus.SessionBus()
	        tracker = bus.get_object(TRACKER, TRACKER_OBJ)
		self.resources = dbus.Interface (tracker,
		                                 dbus_interface=RESOURCES_IFACE)


	def sparql_update(self,query):
		return self.resources.SparqlUpdate(query)

	def query(self,query):
		return self.resources.SparqlQuery(query)
	

""" Insertion test cases """
class s_insert(TestUpdate):
	

	def test_insert_01(self):

		"""
                1. Insert a InformationElement with title.
                2. TEST: Query the title of that information element
                3. Remove the InformationElement to keep everything as it was before
                """

                uri = "tracker://test_insert_01/" + str(random.randint (0, 100))
                insert = """
                INSERT { <%s> a nie:InformationElement;
                        nie:title \"test_insert_01\". }
                """ % (uri)
                self.sparql_update (insert)

		""" verify the inserted item """
                query = """
                SELECT ?t WHERE {
                <%s> a nie:InformationElement ;
                nie:title ?t .
                }
                """ % (uri)
                results = self.query (query)

                self.assertEquals (str(results[0][0]), "test_insert_01")

		""" delete the inserted item """
                delete = """
                DELETE { <%s> a nie:InformationElement. }
                """ % (uri)
                self.sparql_update (delete)

		
	def test_insert_02(self):
                ''' SparqlUpdate: Insert triples and check using SparqlQuery'''

                self.sparql_update('INSERT {<urn:uuid:7646007> a nco:Contact; \
                nco:fullname "Artist_1_update". \
                <file:///media/PIKKUTIKKU/5000_songs_with_metadata_and_album_arts/Artist_1/1_Album/10_song3.mp3> a nmm:MusicPiece,nfo:FileDataObject,nmm:MusicAlbum;\
                nfo:fileName "10_song.mp3"; \
                nfo:fileLastModified "2008-10-23T13:47:02" ; \
                nfo:fileCreated "2008-12-16T12:41:20"; \
                nfo:fileSize 17630; \
                nmm:length 219252; \
                nmm:albumTitle "anything".}')

                self.verify_test_insert_02()


        def verify_test_insert_02(self):

                result = self.query('SELECT ?artist ?date  WHERE{ \
                ?contact nco:fullname ?artist . \
		?time nfo:fileCreated ?date . \
                FILTER (?artist = "Artist_1_update") \
                }')
		print result

                for i in range(len(result)):
                        if result[i][0] == 'Artist_1_update':
                                if  result[i][1] == 'anything' and result[i][2] == '219252':
                                        self.assert_(True,'Pass')
					return
                                else:
					if i < range(len(result) - 1):
						continue
					else:
                                        	self.fail('Fail %s' %result)


        def test_insert_03(self):
                ''' SparqlUpdate: Insert triples and check using SparqlQuery'''

		self.sparql_update('INSERT {<urn:uuid:7646004> a nco:Contact; \
                nco:fullname "Artist_4_update". \
                <file:///media/PIKKUTIKKU/5000_songs_with_metadata_and_album_arts/Artist_4/4_Album/4_song_1.mp3> a nmm:MusicPiece,nfo:FileDataObject;\
                nfo:fileName "4_song_1.mp3"; \
                nfo:fileCreated "2008-12-16T12:41:20"; \
                nfo:fileLastModified "2008-12-23T13:47:02" ; \
                nfo:fileSize 17630; \
                nmm:musicAlbum "4_Album_update"; \
                nmm:trackNumber "11"; \
                nmm:length 219252; \
                nmm:performer <urn:uuid:7646004>.}')

                self.verify_test_insert_03()


        def verify_test_insert_03(self):

                result = self.query('SELECT ?artist ?album ?len ?trkNo ?fname ?fSz  WHERE{ \
                ?contact nco:fullname ?artist . \
                ?song nmm:musicAlbum ?album ; \
                nmm:length ?len  ;\
                nmm:trackNumber ?trkNo.\
                ?songfile nfo:fileName ?fname ;\
                nfo:fileSize ?fSz .\
                FILTER (?fname = "4_song_1.mp3") \
                }')
                print len(result)

                for i in range(len(result)):
                        if result[i][0] == 'Artist_4_update':
                                if  result[i][1] == '4_Album_update' and result[i][2] == '219252' and result[i][3] == '11' and result[i][4] == '4_song_1.mp3' and result[i][5] == '17630':
                                        self.assert_(True,'Pass')
					return
                                else:
					if i < range(len(result) - 1):
						continue
					else:
                                        	self.fail('Fail %s' %result)

	def test_insert_04(self):
                """Insert, delete same single valued properties multiple times."""
		
		""" Delete single valued properties of music file.""" 	
		self.sparql_update('DELETE { <file:///media/PIKKUTIKKU/album_4/4_song4.mp3> nie:usageCounter ?v } WHERE { \
		<file:///media/PIKKUTIKKU/album_4/4_song4.mp3> nie:usageCounter ?v . }')
		self.sparql_update('DELETE { <file:///media/PIKKUTIKKU/album_4/4_song4.mp3> nie:contentAccessed ?v } WHERE { \
		<file:///media/PIKKUTIKKU/album_4/4_song4.mp3> nie:contentAccessed ?v . }')
		

		""" Insert the same single valued properties of music file.""" 	
                self.sparql_update('INSERT { \
                <file:///media/PIKKUTIKKU/album_4/4_song4.mp3> a nmm:MusicPiece,nfo:FileDataObject;\
		nie:usageCounter "1"; \
		nie:contentAccessed "2000-01-01T00:40:47Z" . }')

		""" Delete again the single valued properties of music file.""" 	
		self.sparql_update('DELETE { <file:///media/PIKKUTIKKU/album_4/4_song4.mp3> nie:usageCounter ?v } WHERE { \
		<file:///media/PIKKUTIKKU/album_4/4_song4.mp3> nie:usageCounter ?v . }')
		self.sparql_update('DELETE { <file:///media/PIKKUTIKKU/album_4/4_song4.mp3> nie:contentAccessed ?v } WHERE { \
		<file:///media/PIKKUTIKKU/album_4/4_song4.mp3> nie:contentAccessed ?v . }')
		

		""" Insert the same single valued properties of music file with different values.""" 	
                self.sparql_update('INSERT { \
                <file:///media/PIKKUTIKKU/album_4/4_song4.mp3> a nmm:MusicPiece,nfo:FileDataObject;\
		nie:usageCounter "2"; \
		nie:contentAccessed "2000-01-01T00:40:48Z" . }')

		""" Query for the property values and verify whether the last change is applied."""
                result = self.query('SELECT ?song ?time  WHERE{ \
		?song nie:usageCounter "2"; \
		nie:contentAccessed ?time . \
                }')
                print len(result)

                for i in range(len(result)):
			if result[i][0] == 'file:///media/PIKKUTIKKU/album_4/4_song4.mp3':
				if  result[i][1] == '2000-01-01T00:40:48Z':
                                        self.assert_(True,'Pass')
                                else:
                                        self.fail('Fail %s' %result)


	"""Date-Time storage testing """

	def test_insert_date_01(self):

		"""
                1. Insert a InformationElement with date having local timezone info.
                2. TEST: Query and verify the various componentes of date
                """

                uri = "tracker:test_date_01"
                insert = """
                INSERT { <%s> a nie:InformationElement;
			nie:informationElementDate "2004-05-06T13:14:15+0400". }
                """ % (uri)
                self.sparql_update (insert)

		""" verify the inserted item """

		query = 'SELECT ?s fn:year-from-dateTime (?v) \
		fn:month-from-dateTime (?v) \
		fn:day-from-dateTime (?v) \
		fn:hours-from-dateTime (?v) \
		fn:minutes-from-dateTime (?v) \
		fn:seconds-from-dateTime (?v) \
		fn:timezone-from-dateTime (?v) \
		WHERE { ?s a nie:InformationElement; \
		nie:informationElementDate ?v . \
		}'
                result = self.query (query)
		print result

                for i in range(len(result)):
			if result[i][0] == 'tracker:test_date_01':
				if  result[i][1] == '2004' and result[i][2] == '05' and result[i][3] == '06' and result[i][4] == '13' and result[i][5] == '14' and result[i][6] == '15' and result[i][7] == '14400' :
                                        self.assert_(True,'Pass')
                                else:
                                        self.fail('Fail %s' %result)

	def test_insert_date_02(self):

		"""
                1. Insert a InformationElement with invalid year in date.
                2. TEST: Query and verify the various componentes of date
                """

                uri = "tracker:test_date_02"
                insert = """
                INSERT { <%s> a nie:InformationElement;
			nie:informationElementDate "204-05-06T13:14:15+0400". }
                """ % (uri)
		try : 
			self.resources.SparqlUpdate(insert)

		except :
			print "error in query execution"
			self.assert_(True,'error in query execution')
			
		""" verify whether the item is inserted"""

		query = 'SELECT ?s fn:year-from-dateTime (?v) \
		fn:month-from-dateTime (?v) \
		fn:day-from-dateTime (?v) \
		fn:hours-from-dateTime (?v) \
		fn:minutes-from-dateTime (?v) \
		fn:seconds-from-dateTime (?v) \
		fn:timezone-from-dateTime (?v) \
		WHERE { ?s a nie:InformationElement; \
		nie:informationElementDate ?v . \
		}'
                result = self.query (query)
		print result

                for i in range(len(result)):
			if result[i][0] == 'tracker:test_date_02':
				if  result[i][1] != '204':
                                        self.assert_(True,'Pass')
                                else:
                                        self.fail('Fail %s' %result)


	def test_insert_date_03(self):

		"""
                1. Insert a InformationElement with date ending with "Z" in TZD.
                2. TEST: Query and verify the various componentes of date
                """

                uri = "tracker:test_date_03"
                insert = """
                INSERT { <%s> a nie:InformationElement;
			nie:informationElementDate "2004-05-06T13:14:15Z". }
                """ % (uri)
                self.sparql_update (insert)

		""" verify the inserted item """

		query = 'SELECT ?s fn:year-from-dateTime (?v) \
		fn:month-from-dateTime (?v) \
		fn:day-from-dateTime (?v) \
		fn:hours-from-dateTime (?v) \
		fn:minutes-from-dateTime (?v) \
		fn:seconds-from-dateTime (?v) \
		fn:timezone-from-dateTime (?v) \
		WHERE { ?s a nie:InformationElement; \
		nie:informationElementDate ?v . \
		}'
                result = self.query (query)
		print result

                for i in range(len(result)):
			if result[i][0] == 'tracker:test_date_03':
				if  result[i][1] == '2004' and result[i][2] == '05' and result[i][3] == '06' and result[i][4] == '13' and result[i][5] == '14' and result[i][6] == '15' and result[i][7] == '0' :
                                        self.assert_(True,'Pass')
                                else:
                                        self.fail('Fail %s' %result)


	def test_insert_date_04(self):

		"""
                1. Insert a InformationElement with date without TZD.
                2. TEST: Query and verify the various componentes of date
                """

                uri = "tracker:test_date_04"
                insert = """
                INSERT { <%s> a nie:InformationElement;
			nie:informationElementDate "2004-05-06T13:14:15". }
                """ % (uri)
                self.sparql_update (insert)

		""" verify the inserted item """

		query = 'SELECT ?s fn:year-from-dateTime (?v) \
		fn:month-from-dateTime (?v) \
		fn:day-from-dateTime (?v) \
		fn:hours-from-dateTime (?v) \
		fn:minutes-from-dateTime (?v) \
		fn:seconds-from-dateTime (?v) \
		fn:timezone-from-dateTime (?v) \
		WHERE { ?s a nie:InformationElement; \
		nie:informationElementDate ?v . \
		}'
                result = self.query (query)
		print result

                for i in range(len(result)):
			if result[i][0] == 'tracker:test_date_04':
				if  result[i][1] == '2004' and result[i][2] == '05' and result[i][3] == '06' and result[i][4] == '13' and result[i][5] == '14' and result[i][6] == '15':
                                        self.assert_(True,'Pass')
                                else:
                                        self.fail('Fail %s' %result)


	def test_insert_date_05(self):

		"""
                1. Insert a InformationElement with date without time.
                2. TEST: Query and verify the various componentes of date
                """

                uri = "tracker:test_date_05"
                insert = """
                INSERT { <%s> a nie:InformationElement;
			nie:informationElementDate "2004-05-06". }
                """ % (uri)
		try : 
			self.resources.SparqlUpdate(insert)

		except :
			print "error in query execution"
			self.assert_(True,'error in query execution')

		else:
			self.fail('Query successfully executed')		
			


	def test_insert_date_06(self):

		"""
                1. Insert a InformationElement with date without time but only the "T" separator.
                """

                uri = "tracker:test_date_06"
                insert = """
                INSERT { <%s> a nie:InformationElement;
			nie:informationElementDate "2004-05-06T". }
                """ % (uri)

		try : 
			self.resources.SparqlUpdate(insert)

		except :
			print "error in query execution"
			self.assert_(True,'error in query execution')

		else:
			self.fail('Query successfully executed')		
			

	def test_insert_date_07(self):

		"""
                1. Insert a InformationElement with date having local timezone info
		   with some minutes in it.
                2. TEST: Query and verify the various componentes of date
                """

                uri = "tracker:test_date_07"
                insert = """
                INSERT { <%s> a nie:InformationElement;
			nie:informationElementDate "2004-05-06T13:14:15+0230". }
                """ % (uri)
                self.sparql_update (insert)

		""" verify the inserted item """

		query = 'SELECT ?s fn:year-from-dateTime (?v) \
		fn:month-from-dateTime (?v) \
		fn:day-from-dateTime (?v) \
		fn:hours-from-dateTime (?v) \
		fn:minutes-from-dateTime (?v) \
		fn:seconds-from-dateTime (?v) \
		fn:timezone-from-dateTime (?v) \
		WHERE { ?s a nie:InformationElement; \
		nie:informationElementDate ?v . \
		}'
                result = self.query (query)

                for i in range(len(result)):
			if result[i][0] == 'tracker:test_date_07':
				if  result[i][1] == '2004' and result[i][2] == '05' and result[i][3] == '06' and result[i][4] == '13' and result[i][5] == '14' and result[i][6] == '15' and result[i][7] == '9000':
                                        self.assert_(True,'Pass')
                                else:
					print result[i]
                                        self.fail('Fail %s' %result)

	def test_insert_date_08(self):

		"""
                1. Insert a InformationElement with date having 
		local timezone info in negative.
                2. TEST: Query and verify the various componentes of date
                """

                uri = "tracker:test_date_08"
                insert = """
                INSERT { <%s> a nie:InformationElement;
			nie:informationElementDate "2004-05-06T13:14:15-0230". }
                """ % (uri)
                self.sparql_update (insert)

		""" verify the inserted item """

		query = 'SELECT ?s fn:year-from-dateTime (?v) \
		fn:month-from-dateTime (?v) \
		fn:day-from-dateTime (?v) \
		fn:hours-from-dateTime (?v) \
		fn:minutes-from-dateTime (?v) \
		fn:seconds-from-dateTime (?v) \
		fn:timezone-from-dateTime (?v) \
		WHERE { ?s a nie:InformationElement; \
		nie:informationElementDate ?v . \
		}'
                result = self.query (query)

                for i in range(len(result)):
			if result[i][0] == 'tracker:test_date_08':
				if  result[i][1] == '2004' and result[i][2] == '05' and result[i][3] == '06' and result[i][4] == '13' and result[i][5] == '14' and result[i][6] == '15' and result[i][7] == '-9000':
                                        self.assert_(True,'Pass')
                                else:
					print result[i]
                                        self.fail('Fail %s' %result)

	def test_insert_date_09(self):

		"""
                1. Insert a InformationElement with date having some letters instead of numbers
                2. TEST: Query and verify the various componentes of date
                """

                uri = "tracker:test_date_09"
                insert = """
                INSERT { <%s> a nie:InformationElement;
			nie:informationElementDate "2004-05-06T1g:14:15-0200". }
                """ % (uri)

		try : 
			self.resources.SparqlUpdate(insert)

		except :
			print "error in query execution"
			self.assert_(True,'error in query execution')

		else:
			self.fail('Query successfully executed')		
			

""" Deletion test cases """
class s_delete(TestUpdate):

        def test_delete_01(self):

                ''' Insert triples and Delete a triple. Verify the deletion with a query'''

		"""first insert """
                self.sparql_update('INSERT {<urn:uuid:7646001> a nco:Contact; \
                nco:fullname "Artist_1_delete". \
                <file:///media/PIKKUTIKKU/5000_songs_with_metadata_and_album_arts/Artist_1/1_Album/11_song_del.mp3> a nmm:MusicPiece,nfo:FileDataObject;\
                nfo:fileName "11_song_del.mp3"; \
                nfo:genre "Classic delete"; \
                nmm:musicAlbum "1_Album_delete"; \
                nmm:performer <urn:uuid:7646001>.}')

		"""verify the insertion """
                self.verify_test_insert_delete_01()

		"""now  delete """
                self.sparql_update('DELETE { \
                <file:///media/PIKKUTIKKU/5000_songs_with_metadata_and_album_arts/Artist_1/1_Album/11_song_del.mp3> nfo:genre "Classic delete".}')
                print " After deleting a triple"
		"""verify the deletion """
        	self.verify_test_delete_01()


        def verify_test_insert_delete_01(self):
                result = self.query('SELECT ?fname ?genre WHERE  { \
                ?songfile nfo:fileName ?fname ;\
                nfo:genre ?genre .\
                FILTER (?genre = "Classic delete") \
                }')
                if result != []:
                        for i in range(len(result)):
                                if result[i][0] == '11_song_del.mp3':
                                        if result[i][1] == "Classic delete":
                                                self.assert_(True,'Pass')
					else:
                                                self.fail('File not inserted, so failing the \'Delete genre \' testcase')
                else:
                        self.fail('File not inserted, so failing the \'Delete genre \' testcase')

        def verify_test_delete_01(self):

                result = self.query('SELECT ?fn  WHERE { \
                ?f a nmm:MusicPiece . \
                ?f nfo:fileName ?fn ;\
                nfo:genre ?genre .\
                FILTER (?genre = "Classic delete") \
                 }' )

                if result != []:
                        for i in range(len(result)):
                                if result[i][0] == '11_song_del.mp3':
                                        self.fail('Fail %s' %result)
                                else:
                                        self.assert_(True,'Pass')
                else:
                        self.assert_(True,'Pass')


	def test_delete_02 (self):

		"""Delete a MusicAlbum and count the album """
		"""
		1. add a music album.
		2. count the number of albums
		3. delete an album
		2. count the number of albums
		"""

		"""Add a music album """
                self.sparql_update('INSERT {<06_Album_delete> a nmm:MusicAlbum;\
                nmm:albumTitle "06_Album_delete".}')

		"""get the count of music albums"""
                result = self.query('SELECT ?album  WHERE  { \
                ?album a nmm:MusicAlbum. \
                } ')
                count_before_del = len(result)
                print len(result)
                print result


		"""Delete the added  music album """
                self.sparql_update('DELETE { \
                <06_Album_delete> a nmm:MusicAlbum.}')

		"""get the count of music albums"""
                result = self.query('SELECT ?album  WHERE  { \
                ?album a nmm:MusicAlbum. \
                } ')

                count_after_del = len(result)
                print len(result)

                self.assertEquals (count_before_del - 1, count_after_del)


""" Batch Update test cases """
class s_batch_update(TestUpdate):

	def test_batch_insert_01(self):
		"""batch insertion of 100 contacts:
		1. delete those existing contacts which we want to insert again.
		2. insert 100 contacts.
		3. delete the inserted contacts.
		"""
		

		"""delete those existing contacts which we want to insert again.
		the uid creation is same here as it's created during insertion."""
		
     		for j in range(100) :
			uid = j*1000+1234
			delete = 'DELETE {?contact a rdfs:Resource} WHERE {?contact nco:contactUID  <%s> }' %uid
			self.sparql_update (delete)

		""" querry no. of existing contacts. """
		result = self.query ('SELECT ?c ?Fname ?Gname ?number WHERE { \
					?c a  nco:PersonContact ; \
					nco:nameGiven ?Gname ; \
					nco:nameFamily ?Fname; \
					nco:hasPhoneNumber ?number. \
					} LIMIT 1000')

		count_before_insert = len(result)
		print "contact count before insert %d" %count_before_insert

		"""insert 100 contacts."""
		INSERT_SPARQL = "; ".join([
				"<%s> a nco:PersonContact",
				"nco:nameGiven '%s'",
				"nco:nameFamily '%s'",
				"nco:contactUID %s",
				"nco:hasPhoneNumber <tel:%s>."
				])
		a="\n"
     		"""Preparing a list of Contacts """
     		for j in range(100) :
       			contact=  str(random.randint(0, sys.maxint))
       			names1=['christopher','paul','timothy','stephen','michael','andrew','harold','douglas','timothy','walter','kevin','joshua','robert','matthew','broderick','lacy','rashad','darro','antonia','chas']
       
       			names2=['cyril','ronny','stevie','lon','freeman','erin','duncan','kennith','carmine','augustine','young','chadwick','wilburn','jonas','lazaro','brooks','ariel','dusty','tracey','scottie','seymour']
       			firstname=random.choice(names1)
       			lastname=random.choice(names2)
       			contactUID= j*1000+1234
       			PhoneNumber= str(random.randint(0, sys.maxint))
       			sparql_insert=INSERT_SPARQL % (contact,firstname,lastname, contactUID,PhoneNumber)
       			a=a+sparql_insert
     		INSERT= "INSERT{" + a + "}"

       		self.resources.BatchSparqlUpdate(INSERT)

		""" querry no. of existing contacts. """
		result = self.query ('SELECT ?c ?Fname ?Gname ?number WHERE { \
					?c a  nco:PersonContact ; \
					nco:nameGiven ?Gname ; \
					nco:nameFamily ?Fname; \
					nco:hasPhoneNumber ?number. \
					} LIMIT 1000')

		count_after_insert = len(result)
		print "contact count after insert %d" %count_after_insert

		""" cleanup the inserted contacts """
     		for j in range(100) :
			uid = j*1000+1234
			delete = 'DELETE {?contact a rdfs:Resource} WHERE {?contact nco:contactUID  <%s> }' %uid
			self.sparql_update (delete)

		"""test verification """
		if count_after_insert == 1000:
			if count_after_insert >= count_before_insert:
                		self.assert_(True,'Pass')
			else:
                		self.fail('batch insertion failed')
		else:
			if count_after_insert == count_before_insert + 100:
                		self.assert_(True,'Pass')
			else:
                		self.fail('batch insertion failed')
			

class phone_no (TestUpdate):
	def test_phone_01 (self):
		"""1. Setting the maemo:localPhoneNumber property to last 7 digits of phone number.
		   2. Receiving a message  from a contact whose localPhoneNumber is saved.
		   3. Querying for the local phone number.
		"""   
		PhoneNumber = str(random.randint (0, sys.maxint))
		UUID	    = str(time.time())
		UUID1	    = str(random.randint (0, sys.maxint))
		UUID2	    = str(random.randint (0, sys.maxint))
		localNumber = PhoneNumber[-7:]
		d=datetime.datetime.now()
	        Received=d.isoformat()
		ID	    = int(time.time())%1000
		Given_Name  = 'test_GN_' + `ID`
		Family_Name = 'test_FN_' + `ID`

		INSERT_SPARQL = """ INSERT { 
				<tel:%s> a nco:PhoneNumber ; 
				nco:phoneNumber  '%s' .
				<urn:uuid:%s> a nco:PersonContact;
				nco:contactUID <contact:test_%s>;
				nco:nameFamily '%s'  ; 
				nco:nameGiven '%s'.  
				<urn:uuid:%s>  nco:hasPhoneNumber <tel:%s>. 
				<tel:%s> maemo:localPhoneNumber '%s' 
				}"""
		sparql_insert = INSERT_SPARQL % (PhoneNumber,PhoneNumber,UUID,UUID1,Given_Name,Family_Name,UUID,PhoneNumber,PhoneNumber,localNumber)               
		try :
	            self.resources.SparqlUpdate(sparql_insert)
		except :
		    self.fail('Insertion is not successful')			

		INSERT_SPARQL1 = """ INSERT { 
				 <urn:uuid:%s> a nmo:Message ; 
				 nmo:from [a nco:Contact ; 
				 nco:hasPhoneNumber <tel:%s>];
			 	 nmo:receivedDate '%s' ; 
				 nmo:plainTextMessageContent 'hello' }"""

		sparql_insert = INSERT_SPARQL1 % ( UUID2,PhoneNumber,Received)
		try :
	            self.resources.SparqlUpdate(sparql_insert)
		except :
		    self.fail('Insertion is not successful')			

		QUERY_SPARQL = """ SELECT  ?local 
				WHERE { ?msg a nmo:Message .
				?c a nco:Contact;
				nco:hasPhoneNumber <tel:%s>.
				<tel:%s> maemo:localPhoneNumber ?local

				} """
		QUERY= QUERY_SPARQL %(PhoneNumber,PhoneNumber)
                result = self.resources.SparqlQuery(QUERY) 
		self.assert_(result[0][0].find(localNumber)!=-1 , 'Query is not succesful')

	def test_phone_02 (self):

		""" Inserting a local phone number which have spaces """

		INSERT_SPARQL = """ INSERT { 
				<tel+3333333333> a nco:PhoneNumber ; 
				nco:phoneNumber  <tel+3333333333> . 
				<urn:uuid:9876> a nco:PersonContact;
				nco:nameFamily 'test_name_01' ; 
				nco:nameGiven 'test_name_02'.  
				<urn:uuid:98765>  nco:hasPhoneNumber <tel+3333333333>. 
				<tel+3333333333> maemo:localPhoneNumber <333 333> }"""

		try:
		  self.resources.SparqlUpdate(INSERT_SPARQL)
                except :                                                                                                     
                   print "error in query execution"                                                                     
                   self.assert_(True,'error in query execution')                                                        
                else:                                                                                                        
                    self.fail('Query successfully executed')



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