1+ using System ;
2+ using System . Collections . Generic ;
3+ using System . Threading . Tasks ;
4+ using Microsoft . VisualStudio . TestTools . UnitTesting ;
5+ using Parse . Abstractions . Platform . Objects ;
6+ using Parse . Infrastructure ;
7+ using Parse . Platform . LiveQueries ;
8+
9+ namespace Parse . Tests ;
10+
11+ [ TestClass ]
12+ public class LiveQueryMessageBuilderTests
13+ {
14+ private ParseClient Client { get ; } = new ParseClient (
15+ new ServerConnectionData { Test = true } ,
16+ new LiveQueryServerConnectionData { ApplicationID = "TestApp" , Key = "t3stK3y" , Test = true } ) ;
17+
18+ public LiveQueryMessageBuilderTests ( )
19+ {
20+ Client . Publicize ( ) ;
21+ }
22+
23+ [ TestMethod ]
24+ public void TestConstructor ( )
25+ {
26+ ParseLiveQueryMessageBuilder builder = new ParseLiveQueryMessageBuilder ( ) ;
27+
28+ Assert . IsNotNull ( builder , "Builder should not be null after construction." ) ;
29+ }
30+
31+ [ TestMethod ]
32+ public async Task TestBuildConnectMessage ( )
33+ {
34+ ParseLiveQueryMessageBuilder builder = new ParseLiveQueryMessageBuilder ( ) ;
35+ IDictionary < string , object > message = await builder . BuildConnectMessage ( ) ;
36+
37+ Assert . IsNotNull ( message ) ;
38+ Assert . IsTrue ( message . ContainsKey ( "op" ) ) ;
39+ Assert . IsTrue ( message . ContainsKey ( "applicationId" ) ) ;
40+ Assert . IsTrue ( message . ContainsKey ( "windowsKey" ) ) ;
41+ Assert . HasCount ( 3 , message ) ;
42+ Assert . AreEqual ( "connect" , message [ "op" ] ) ;
43+ Assert . AreEqual ( Client . Services . LiveQueryServerConnectionData . ApplicationID , message [ "applicationId" ] ) ;
44+ Assert . AreEqual ( Client . Services . LiveQueryServerConnectionData . Key , message [ "windowsKey" ] ) ;
45+ }
46+
47+ [ TestMethod ]
48+ public void TestBuildUnsubscribeMessage ( )
49+ {
50+ int requestId = 2 ;
51+ ParseLiveQueryMessageBuilder builder = new ParseLiveQueryMessageBuilder ( ) ;
52+ IDictionary < string , object > message = builder . BuildUnsubscribeMessage ( requestId ) ;
53+
54+ Assert . IsNotNull ( message ) ;
55+ Assert . IsTrue ( message . ContainsKey ( "op" ) ) ;
56+ Assert . IsTrue ( message . ContainsKey ( "requestId" ) ) ;
57+ Assert . HasCount ( 2 , message ) ;
58+ Assert . AreEqual ( "unsubscribe" , message [ "op" ] ) ;
59+ Assert . AreEqual ( requestId , message [ "requestId" ] ) ;
60+
61+ Assert . ThrowsExactly < ArgumentOutOfRangeException > ( ( ) => builder . BuildUnsubscribeMessage ( 0 ) ) ;
62+ }
63+
64+ [ TestMethod ]
65+ public async Task TestBuildSubscribeMessage ( )
66+ {
67+ int requestId = 2 ;
68+ ParseLiveQuery < ParseObject > liveQuery = new ParseLiveQuery < ParseObject > (
69+ Client . Services ,
70+ "DummyClass" ,
71+ new Dictionary < string , object > { { "foo" , "bar" } } ,
72+ [ "foo" ] ,
73+ [ "foo" ] ) ;
74+ ParseLiveQueryMessageBuilder builder = new ParseLiveQueryMessageBuilder ( ) ;
75+ IDictionary < string , object > message = await builder . BuildSubscribeMessage < ParseObject > ( requestId , liveQuery ) ;
76+
77+ Assert . IsNotNull ( message ) ;
78+ Assert . HasCount ( 3 , message ) ;
79+
80+ Assert . IsTrue ( message . ContainsKey ( "op" ) ) ;
81+ Assert . AreEqual ( "subscribe" , message [ "op" ] ) ;
82+
83+ Assert . IsTrue ( message . ContainsKey ( "requestId" ) ) ;
84+ Assert . AreEqual ( requestId , message [ "requestId" ] ) ;
85+
86+ Assert . IsTrue ( message . ContainsKey ( "query" ) ) ;
87+ Assert . IsInstanceOfType < IDictionary < string , object > > ( message [ "query" ] , "The 'query' value should be a Dictionary<string, object>." ) ;
88+ Assert . HasCount ( 4 , ( IDictionary < string , object > ) message [ "query" ] ) ;
89+ IDictionary < string , object > query = message [ "query" ] as IDictionary < string , object > ;
90+
91+ Assert . IsTrue ( query . ContainsKey ( "className" ) , "The 'query' dictionary should contain the 'className' key." ) ;
92+ Assert . AreEqual ( "DummyClass" , query [ "className" ] , "The 'className' property should be 'DummyClass'." ) ;
93+
94+ Assert . IsTrue ( query . ContainsKey ( "where" ) , "The 'query' dictionary should contain the 'where' key." ) ;
95+ Assert . IsInstanceOfType < IDictionary < string , object > > ( query [ "where" ] , "The 'where' property should be a Dictionary<string, object>." ) ;
96+ IDictionary < string , object > where = ( IDictionary < string , object > ) query [ "where" ] ;
97+ Assert . HasCount ( 1 , where , "The 'where' dictionary should contain exactly one key-value pair." ) ;
98+ Assert . IsTrue ( where . ContainsKey ( "foo" ) , "The 'where' dictionary should contain the 'foo' key." ) ;
99+ Assert . AreEqual ( "bar" , where [ "foo" ] , "The 'foo' property in 'where' should be 'bar'." ) ;
100+
101+ Assert . IsTrue ( query . ContainsKey ( "keys" ) , "The 'query' dictionary should contain the 'keys' key." ) ;
102+ Assert . IsInstanceOfType < string [ ] > ( query [ "keys" ] , "The 'keys' property should be a string array." ) ;
103+ Assert . HasCount ( 1 , ( string [ ] ) query [ "keys" ] , "The 'keys' array should contain exactly one element." ) ;
104+ Assert . AreEqual ( "foo" , ( ( string [ ] ) query [ "keys" ] ) [ 0 ] , "The 'keys' parameter should contain 'foo'." ) ;
105+
106+ Assert . IsTrue ( query . ContainsKey ( "watch" ) , "The 'query' dictionary should contain the 'watch' key." ) ;
107+ Assert . IsInstanceOfType < string [ ] > ( query [ "watch" ] , "The 'watch' property should be a string array." ) ;
108+ Assert . HasCount ( 1 , ( string [ ] ) query [ "watch" ] , "The 'watch' array should contain exactly one element." ) ;
109+ Assert . AreEqual ( "foo" , ( ( string [ ] ) query [ "watch" ] ) [ 0 ] , "The 'watch' parameter should contain 'foo'." ) ;
110+
111+ await Assert . ThrowsExactlyAsync < ArgumentOutOfRangeException > ( async ( ) => await builder . BuildSubscribeMessage < ParseObject > ( 0 , liveQuery ) ) ;
112+ await Assert . ThrowsExactlyAsync < ArgumentNullException > ( async ( ) => await builder . BuildSubscribeMessage < ParseObject > ( requestId , null ) ) ;
113+ }
114+
115+ [ TestMethod ]
116+ public async Task TestBuildUpdateSubscriptionMessage ( )
117+ {
118+ int requestId = 2 ;
119+ ParseLiveQuery < ParseObject > liveQuery = new ParseLiveQuery < ParseObject > (
120+ Client . Services ,
121+ "DummyClass" ,
122+ new Dictionary < string , object > { { "foo" , "bar" } } ,
123+ [ "foo" ] ,
124+ [ "foo" ] ) ;
125+ ParseLiveQueryMessageBuilder builder = new ParseLiveQueryMessageBuilder ( ) ;
126+ IDictionary < string , object > message = await builder . BuildUpdateSubscriptionMessage < ParseObject > ( requestId , liveQuery ) ;
127+
128+ Assert . IsNotNull ( message ) ;
129+ Assert . HasCount ( 3 , message ) ;
130+
131+ Assert . IsTrue ( message . ContainsKey ( "op" ) ) ;
132+ Assert . AreEqual ( "update" , message [ "op" ] ) ;
133+
134+ Assert . IsTrue ( message . ContainsKey ( "requestId" ) ) ;
135+ Assert . AreEqual ( requestId , message [ "requestId" ] ) ;
136+
137+ Assert . IsTrue ( message . ContainsKey ( "query" ) ) ;
138+ Assert . IsInstanceOfType < IDictionary < string , object > > ( message [ "query" ] , "The 'query' value should be a Dictionary<string, object>." ) ;
139+ Assert . HasCount ( 4 , ( IDictionary < string , object > ) message [ "query" ] ) ;
140+ IDictionary < string , object > query = message [ "query" ] as IDictionary < string , object > ;
141+
142+ Assert . IsTrue ( query . ContainsKey ( "className" ) , "The 'query' dictionary should contain the 'className' key." ) ;
143+ Assert . AreEqual ( "DummyClass" , query [ "className" ] , "The 'className' property should be 'DummyClass'." ) ;
144+
145+ Assert . IsTrue ( query . ContainsKey ( "where" ) , "The 'query' dictionary should contain the 'where' key." ) ;
146+ Assert . IsInstanceOfType < IDictionary < string , object > > ( query [ "where" ] , "The 'where' property should be a Dictionary<string, object>." ) ;
147+ IDictionary < string , object > where = ( IDictionary < string , object > ) query [ "where" ] ;
148+ Assert . HasCount ( 1 , where , "The 'where' dictionary should contain exactly one key-value pair." ) ;
149+ Assert . IsTrue ( where . ContainsKey ( "foo" ) , "The 'where' dictionary should contain the 'foo' key." ) ;
150+ Assert . AreEqual ( "bar" , where [ "foo" ] , "The 'foo' property in 'where' should be 'bar'." ) ;
151+
152+ Assert . IsTrue ( query . ContainsKey ( "keys" ) , "The 'query' dictionary should contain the 'keys' key." ) ;
153+ Assert . IsInstanceOfType < string [ ] > ( query [ "keys" ] , "The 'keys' property should be a string array." ) ;
154+ Assert . HasCount ( 1 , ( string [ ] ) query [ "keys" ] , "The 'keys' array should contain exactly one element." ) ;
155+ Assert . AreEqual ( "foo" , ( ( string [ ] ) query [ "keys" ] ) [ 0 ] , "The 'keys' parameter should contain 'foo'." ) ;
156+
157+ Assert . IsTrue ( query . ContainsKey ( "watch" ) , "The 'query' dictionary should contain the 'watch' key." ) ;
158+ Assert . IsInstanceOfType < string [ ] > ( query [ "watch" ] , "The 'watch' property should be a string array." ) ;
159+ Assert . HasCount ( 1 , ( string [ ] ) query [ "watch" ] , "The 'watch' array should contain exactly one element." ) ;
160+ Assert . AreEqual ( "foo" , ( ( string [ ] ) query [ "watch" ] ) [ 0 ] , "The 'watch' parameter should contain 'foo'." ) ;
161+
162+ await Assert . ThrowsExactlyAsync < ArgumentOutOfRangeException > ( async ( ) => await builder . BuildUpdateSubscriptionMessage < ParseObject > ( 0 , liveQuery ) ) ;
163+ await Assert . ThrowsExactlyAsync < ArgumentNullException > ( async ( ) => await builder . BuildUpdateSubscriptionMessage < ParseObject > ( requestId , null ) ) ;
164+ }
165+ }
0 commit comments