PageRenderTime 55ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/MongoDB.Driver.Core.Tests/WriteConcernTests.cs

http://github.com/mongodb/mongo-csharp-driver
C# | 809 lines | 653 code | 139 blank | 17 comment | 9 complexity | 2521265bca3a0d8fc8c535f2b6cf9f96 MD5 | raw file
Possible License(s): Apache-2.0
  1. /* Copyright 2013-present MongoDB Inc.
  2. *
  3. * Licensed under the Apache License, Version 2.0 (the "License");
  4. * you may not use this file except in compliance with the License.
  5. * You may obtain a copy of the License at
  6. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. *
  9. * Unless required by applicable law or agreed to in writing, software
  10. * distributed under the License is distributed on an "AS IS" BASIS,
  11. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. * See the License for the specific language governing permissions and
  13. * limitations under the License.
  14. */
  15. using System;
  16. using System.Collections.Generic;
  17. using System.Linq;
  18. using System.Text;
  19. using System.Threading.Tasks;
  20. using FluentAssertions;
  21. using MongoDB.Bson;
  22. using MongoDB.Bson.TestHelpers.XunitExtensions;
  23. using Xunit;
  24. namespace MongoDB.Driver
  25. {
  26. [Trait("Category", "WriteConcern")]
  27. public class WriteConcernTests
  28. {
  29. [Fact]
  30. public void Acknowledged_should_return_expected_result()
  31. {
  32. var result = WriteConcern.Acknowledged;
  33. result.FSync.Should().NotHaveValue();
  34. result.Journal.Should().NotHaveValue();
  35. result.W.Should().BeNull();
  36. result.WTimeout.Should().NotHaveValue();
  37. }
  38. [Theory]
  39. [ParameterAttributeData]
  40. public void constructor_with_fsync_should_initialize_instance(
  41. [Values(false, true, null)]
  42. bool? fsync)
  43. {
  44. var result = new WriteConcern(fsync: fsync);
  45. result.FSync.Should().Be(fsync);
  46. result.Journal.Should().NotHaveValue();
  47. result.W.Should().BeNull();
  48. result.WTimeout.Should().NotHaveValue();
  49. }
  50. [Theory]
  51. [ParameterAttributeData]
  52. public void constructor_with_journal_should_initialize_instance(
  53. [Values(false, true, null)]
  54. bool? journal)
  55. {
  56. var result = new WriteConcern(journal: journal);
  57. result.FSync.Should().NotHaveValue();
  58. result.Journal.Should().Be(journal);
  59. result.W.Should().BeNull();
  60. result.WTimeout.Should().NotHaveValue();
  61. }
  62. [Theory]
  63. [ParameterAttributeData]
  64. public void constructor_with_mode_and_fsync_should_initialize_instance(
  65. [Values("abc", "def")]
  66. string mode,
  67. [Values(false, true, null)]
  68. bool? fsync)
  69. {
  70. var result = new WriteConcern(mode, fsync: fsync);
  71. result.FSync.Should().Be(fsync);
  72. result.Journal.Should().NotHaveValue();
  73. result.W.Should().Be(new WriteConcern.WMode(mode));
  74. result.WTimeout.Should().NotHaveValue();
  75. }
  76. [Theory]
  77. [ParameterAttributeData]
  78. public void constructor_with_mode_and_journal_should_initialize_instance(
  79. [Values("abc", "def")]
  80. string mode,
  81. [Values(false, true, null)]
  82. bool? journal)
  83. {
  84. var result = new WriteConcern(mode, journal: journal);
  85. result.FSync.Should().NotHaveValue();
  86. result.Journal.Should().Be(journal);
  87. result.W.Should().Be(new WriteConcern.WMode(mode));
  88. result.WTimeout.Should().NotHaveValue();
  89. }
  90. [Theory]
  91. [ParameterAttributeData]
  92. public void constructor_with_mode_and_wTimeout_should_initialize_instance(
  93. [Values("abc", "def")]
  94. string mode,
  95. [Values(1, null)]
  96. int? wTimeoutSeconds)
  97. {
  98. var wTimeout = ToWTimeout(wTimeoutSeconds);
  99. var result = new WriteConcern(mode, wTimeout: wTimeout);
  100. result.FSync.Should().NotHaveValue();
  101. result.Journal.Should().NotHaveValue();
  102. result.W.Should().Be(new WriteConcern.WMode(mode));
  103. result.WTimeout.Should().Be(wTimeout);
  104. }
  105. [Fact]
  106. public void constructor_with_mode_should_initialize_instance()
  107. {
  108. var result = new WriteConcern("mode");
  109. result.FSync.Should().NotHaveValue();
  110. result.Journal.Should().NotHaveValue();
  111. result.W.Should().Be(new WriteConcern.WMode("mode"));
  112. result.WTimeout.Should().NotHaveValue();
  113. }
  114. [Fact]
  115. public void constructor_with_mode_should_throw_when_mode_is_empty()
  116. {
  117. Action action = () => new WriteConcern(mode: "");
  118. action.ShouldThrow<ArgumentException>().And.ParamName.Should().Be("mode");
  119. }
  120. [Fact]
  121. public void constructor_with_mode_should_throw_when_mode_is_null()
  122. {
  123. Action action = () => new WriteConcern(mode: null);
  124. action.ShouldThrow<ArgumentNullException>().And.ParamName.Should().Be("mode");
  125. }
  126. [Fact]
  127. public void constructor_with_no_arguments_should_initialize_instance()
  128. {
  129. var result = new WriteConcern();
  130. result.FSync.Should().NotHaveValue();
  131. result.Journal.Should().NotHaveValue();
  132. result.W.Should().BeNull();
  133. result.WTimeout.Should().NotHaveValue();
  134. }
  135. [Theory]
  136. [ParameterAttributeData]
  137. public void constructor_with_w_should_initialize_instance(
  138. [Values(0, 1)]
  139. int w)
  140. {
  141. var result = new WriteConcern(w);
  142. result.FSync.Should().NotHaveValue();
  143. result.Journal.Should().NotHaveValue();
  144. result.W.Should().Be(new WriteConcern.WCount(w));
  145. result.WTimeout.Should().NotHaveValue();
  146. }
  147. [Fact]
  148. public void constructor_with_w_should_throw_when_w_is_negative()
  149. {
  150. Action action = () => new WriteConcern(-1);
  151. action.ShouldThrow<ArgumentException>().And.ParamName.Should().Be("w");
  152. }
  153. [Theory]
  154. [ParameterAttributeData]
  155. public void constructor_with_w_and_fsync_should_initialize_instance(
  156. [Values(0, 1)]
  157. int w,
  158. [Values(false, true, null)]
  159. bool? fsync)
  160. {
  161. var result = new WriteConcern(w, fsync: fsync);
  162. result.FSync.Should().Be(fsync);
  163. result.Journal.Should().NotHaveValue();
  164. result.W.Should().Be(new WriteConcern.WCount(w));
  165. result.WTimeout.Should().NotHaveValue();
  166. }
  167. // exclude case where w = 0 and journal = true per new spec
  168. [Theory]
  169. [InlineData(0, null)]
  170. [InlineData(1, null)]
  171. [InlineData(0, false)]
  172. [InlineData(1, true)]
  173. [InlineData(1, false)]
  174. public void constructor_with_w_and_journal_should_initialize_instance(
  175. int w,
  176. bool? journal)
  177. {
  178. var result = new WriteConcern(w, journal: journal);
  179. result.FSync.Should().NotHaveValue();
  180. result.Journal.Should().Be(journal);
  181. result.W.Should().Be(new WriteConcern.WCount(w));
  182. result.WTimeout.Should().NotHaveValue();
  183. }
  184. [Theory]
  185. [ParameterAttributeData]
  186. public void constructor_with_w_and_wTimeout_should_initialize_instance(
  187. [Values(0, 1)]
  188. int w,
  189. [Values(1, null)]
  190. int? wTimeoutSeconds)
  191. {
  192. var wTimeout = ToWTimeout(wTimeoutSeconds);
  193. var result = new WriteConcern(w, wTimeout: wTimeout);
  194. result.FSync.Should().NotHaveValue();
  195. result.Journal.Should().NotHaveValue();
  196. result.W.Should().Be(new WriteConcern.WCount(w));
  197. result.WTimeout.Should().Be(wTimeout);
  198. }
  199. [Theory]
  200. [ParameterAttributeData]
  201. public void constructor_with_wTimeout_should_initialize_instance(
  202. [Values(1, null)]
  203. int? wTimeoutSeconds)
  204. {
  205. var wTimeout = ToWTimeout(wTimeoutSeconds);
  206. var result = new WriteConcern(wTimeout: wTimeout);
  207. result.FSync.Should().NotHaveValue();
  208. result.Journal.Should().NotHaveValue();
  209. result.W.Should().BeNull();
  210. result.WTimeout.Should().Be(wTimeout);
  211. }
  212. [Fact]
  213. public void constructor_with_wTimeout_should_throw_when_wTimeout_is_negative()
  214. {
  215. Action action = () => new WriteConcern(wTimeout: TimeSpan.FromSeconds(-1));
  216. action.ShouldThrow<ArgumentException>().And.ParamName.Should().Be("wTimeout");
  217. }
  218. [Theory]
  219. [ParameterAttributeData]
  220. public void constructor_with_wValue_should_initialize_instance(
  221. [Values(1, "abc", null)]
  222. object w)
  223. {
  224. var wValue = ToWValue(w);
  225. var result = new WriteConcern(wValue);
  226. result.FSync.Should().NotHaveValue();
  227. result.Journal.Should().NotHaveValue();
  228. result.W.Should().Be(wValue);
  229. result.WTimeout.Should().NotHaveValue();
  230. }
  231. [Theory]
  232. [ParameterAttributeData]
  233. public void Equals_should_return_false_when_any_fields_are_not_equal(
  234. [Values("fsync", "journal", "w", "wTimeout")]
  235. string notEqualFieldName)
  236. {
  237. var subject1 = new WriteConcern(1, TimeSpan.FromSeconds(1), false, false);
  238. WriteConcern subject2;
  239. switch (notEqualFieldName)
  240. {
  241. case "fsync": subject2 = subject1.With(fsync: true); break;
  242. case "journal": subject2 = subject1.With(journal: true); break;
  243. case "w": subject2 = subject1.With(w: 2); break;
  244. case "wTimeout": subject2 = subject1.With(wTimeout: TimeSpan.FromSeconds(2)); break;
  245. default: throw new ArgumentException("notEqualFieldName");
  246. }
  247. var result1 = subject1.Equals(subject2);
  248. var result2 = subject1.Equals((object)subject2);
  249. var hashCode1 = subject1.GetHashCode();
  250. var hashCode2 = subject2.GetHashCode();
  251. result1.Should().BeFalse();
  252. result2.Should().BeFalse();
  253. hashCode1.Should().NotBe(hashCode2);
  254. }
  255. [Fact]
  256. public void Equals_should_return_false_when_other_is_null()
  257. {
  258. var subject = new WriteConcern(1, TimeSpan.FromSeconds(1), false, false);
  259. var result1 = subject.Equals((WriteConcern)null);
  260. var result2 = subject.Equals((object)null);
  261. result1.Should().BeFalse();
  262. result2.Should().BeFalse();
  263. }
  264. [Fact]
  265. public void Equals_should_return_false_when_other_is_wrong_type()
  266. {
  267. var subject = new WriteConcern(1, TimeSpan.FromSeconds(1), false, false);
  268. var other = new object();
  269. var result = subject.Equals(other);
  270. result.Should().BeFalse();
  271. }
  272. [Fact]
  273. public void Equals_should_return_true_when_all_fields_are_equal()
  274. {
  275. var subject1 = new WriteConcern(1, TimeSpan.FromSeconds(1), false, false);
  276. var subject2 = new WriteConcern(1, TimeSpan.FromSeconds(1), false, false);
  277. var result1 = subject1.Equals(subject2);
  278. var result2 = subject1.Equals((object)subject2);
  279. var hashCode1 = subject1.GetHashCode();
  280. var hashCode2 = subject2.GetHashCode();
  281. result1.Should().BeTrue();
  282. result2.Should().BeTrue();
  283. hashCode1.Should().Be(hashCode2);
  284. }
  285. // exclude w = 0 and journal = true per new spec
  286. [Theory]
  287. [InlineData(0, null, null, null, false)]
  288. [InlineData(1, null, null, null, true)]
  289. [InlineData(0, false, null, null, false)]
  290. [InlineData(0, true, null, null, true)]
  291. [InlineData(0, null, false, null, false)]
  292. [InlineData(0, null, null, 1, false)]
  293. public void IsAcknowledged_should_return_expected_result_with_w(
  294. int? w,
  295. bool? fsync,
  296. bool? journal,
  297. int? wTimeoutSeconds,
  298. bool expectedResult)
  299. {
  300. var wValue = ToWValue(w);
  301. var wTimeout = ToWTimeout(wTimeoutSeconds);
  302. var subject = new WriteConcern(wValue, fsync: fsync, journal: journal, wTimeout: wTimeout);
  303. var result = subject.IsAcknowledged;
  304. result.Should().Be(expectedResult);
  305. }
  306. [Theory]
  307. [InlineData(null, null, null)]
  308. [InlineData(false, null, null)]
  309. [InlineData(true, null, null)]
  310. [InlineData(null, false, null)]
  311. [InlineData(null, true, null)]
  312. [InlineData(null, null, 1)]
  313. public void IsAcknowledged_should_return_expected_result_with_mode(
  314. bool? fsync,
  315. bool? journal,
  316. int? wTimeoutSeconds)
  317. {
  318. var wTimeout = ToWTimeout(wTimeoutSeconds);
  319. var subject = new WriteConcern("mode", fsync: fsync, journal: journal, wTimeout: wTimeout);
  320. var result = subject.IsAcknowledged;
  321. result.Should().BeTrue();
  322. }
  323. [Theory]
  324. [InlineData(null, null, null, null, "{ }")]
  325. [InlineData(1, null, null, null, "{ w : 1 }")]
  326. [InlineData(null, 2, null, null, "{ wtimeout : 2000 }")]
  327. [InlineData(null, null, true, null, "{ fsync : true }")]
  328. [InlineData(null, null, null, true, "{ j : true }")]
  329. [InlineData(1, 2, true, true, "{ w : 1, wtimeout : 2000, fsync : true, j : true }")]
  330. [InlineData("majority", 2, true, true, "{ w : \"majority\", wtimeout : 2000, fsync : true, j : true }")]
  331. public void ToBsonDocument_should_return_expected_result(object w, int? wTimeoutSeconds, bool? fsync, bool? journal, string expectedResult)
  332. {
  333. var wValue = ToWValue(w);
  334. var wTimeout = ToWTimeout(wTimeoutSeconds);
  335. var subject = new WriteConcern(wValue, wTimeout, fsync, journal);
  336. var result = subject.ToBsonDocument();
  337. result.Should().Be(expectedResult);
  338. }
  339. [Theory]
  340. [InlineData(null, null, null, null, "{ }")]
  341. [InlineData(1, null, null, null, "{ w : 1 }")]
  342. [InlineData(null, 2, null, null, "{ wtimeout : 2s }")]
  343. [InlineData(null, null, true, null, "{ fsync : true }")]
  344. [InlineData(null, null, null, true, "{ journal : true }")]
  345. [InlineData(1, 2, true, true, "{ w : 1, wtimeout : 2s, fsync : true, journal : true }")]
  346. [InlineData("majority", 2, true, true, "{ w : \"majority\", wtimeout : 2s, fsync : true, journal : true }")]
  347. public void ToString_should_return_expected_result(object w, int? wTimeoutSeconds, bool? fsync, bool? journal, string expectedResult)
  348. {
  349. var wValue = ToWValue(w);
  350. var wTimeout = ToWTimeout(wTimeoutSeconds);
  351. var subject = new WriteConcern(wValue, wTimeout, fsync, journal);
  352. var result = subject.ToString();
  353. result.Should().Be(expectedResult);
  354. }
  355. [Fact]
  356. public void Unacknowledged_should_return_expected_result()
  357. {
  358. var result = WriteConcern.Unacknowledged;
  359. result.FSync.Should().NotHaveValue();
  360. result.Journal.Should().NotHaveValue();
  361. result.W.Should().Be((WriteConcern.WValue)0);
  362. result.WTimeout.Should().NotHaveValue();
  363. }
  364. [Fact]
  365. public void W1_should_return_expected_result()
  366. {
  367. var result = WriteConcern.W1;
  368. result.FSync.Should().NotHaveValue();
  369. result.Journal.Should().NotHaveValue();
  370. result.W.Should().Be((WriteConcern.WValue)1);
  371. result.WTimeout.Should().NotHaveValue();
  372. }
  373. [Fact]
  374. public void W2_should_return_expected_result()
  375. {
  376. var result = WriteConcern.W2;
  377. result.FSync.Should().NotHaveValue();
  378. result.Journal.Should().NotHaveValue();
  379. result.W.Should().Be((WriteConcern.WValue)2);
  380. result.WTimeout.Should().NotHaveValue();
  381. }
  382. [Fact]
  383. public void W3_should_return_expected_result()
  384. {
  385. var result = WriteConcern.W3;
  386. result.FSync.Should().NotHaveValue();
  387. result.Journal.Should().NotHaveValue();
  388. result.W.Should().Be((WriteConcern.WValue)3);
  389. result.WTimeout.Should().NotHaveValue();
  390. }
  391. [Theory]
  392. [ParameterAttributeData]
  393. public void With_should_return_new_instance_when_any_value_is_not_equal(
  394. [Values("w", "wTimeout", "fsync", "journal")]
  395. string notEqualFieldName)
  396. {
  397. var w = (WriteConcern.WValue)1;
  398. var wTimeout = TimeSpan.FromSeconds(2);
  399. var fsync = false;
  400. var journal = false;
  401. var subject = new WriteConcern(w, wTimeout, fsync, journal);
  402. switch (notEqualFieldName)
  403. {
  404. case "w": w = (WriteConcern.WValue)2; break;
  405. case "wTimeout": wTimeout = TimeSpan.FromSeconds(3); break;
  406. case "fsync": fsync = true; break;
  407. case "journal": journal = true; break;
  408. default: throw new ArgumentException("notEqualFieldName");
  409. }
  410. var result = subject.With(w, wTimeout, fsync, journal);
  411. result.Should().NotBeSameAs(subject);
  412. result.W.Should().Be(w);
  413. result.WTimeout.Should().Be(wTimeout);
  414. result.FSync.Should().Be(fsync);
  415. result.Journal.Should().Be(journal);
  416. }
  417. [Fact]
  418. public void With_should_return_same_instance_when_all_values_are_equal()
  419. {
  420. var subject = new WriteConcern(1, TimeSpan.FromSeconds(2), true, true);
  421. var result = subject.With(1, TimeSpan.FromSeconds(2), true, true);
  422. result.Should().BeSameAs(subject);
  423. }
  424. [Fact]
  425. public void With_should_return_same_instance_when_no_values_are_provided()
  426. {
  427. var subject = new WriteConcern();
  428. var result = subject.With();
  429. result.Should().BeSameAs(subject);
  430. }
  431. [Theory]
  432. [ParameterAttributeData]
  433. public void With_should_return_same_instance_when_value_is_equal(
  434. [Values("w", "mode", "wValue", "wTimeout", "fsync", "journal")]
  435. string fieldName)
  436. {
  437. var wValue = fieldName == "mode" ? (WriteConcern.WValue)new WriteConcern.WMode("mode") : new WriteConcern.WCount(1);
  438. var wTimeout = TimeSpan.FromSeconds(2);
  439. var fsync = false;
  440. var journal = true;
  441. var subject = new WriteConcern(wValue, wTimeout, fsync, journal);
  442. WriteConcern result;
  443. switch (fieldName)
  444. {
  445. case "w": result = subject.With(w: 1); break;
  446. case "mode": result = subject.With(mode: "mode"); break;
  447. case "wValue": result = subject.With(w: wValue); break;
  448. case "wTimeout": result = subject.With(wTimeout: wTimeout); break;
  449. case "fsync": result = subject.With(fsync: fsync); break;
  450. case "journal": result = subject.With(journal: journal); break;
  451. default: throw new ArgumentException("providedFieldName");
  452. }
  453. result.Should().BeSameAs(subject);
  454. }
  455. [Fact]
  456. public void WMajority_should_return_expected_result()
  457. {
  458. var result = WriteConcern.WMajority;
  459. result.FSync.Should().NotHaveValue();
  460. result.Journal.Should().NotHaveValue();
  461. result.W.Should().Be((WriteConcern.WMode)"majority");
  462. result.WTimeout.Should().NotHaveValue();
  463. }
  464. // helper methods
  465. private TimeSpan? ToWTimeout(int? wtimeoutSeconds)
  466. {
  467. if (wtimeoutSeconds.HasValue)
  468. {
  469. return TimeSpan.FromSeconds(wtimeoutSeconds.Value);
  470. }
  471. else
  472. {
  473. return null;
  474. }
  475. }
  476. private WriteConcern.WValue ToWValue(object w)
  477. {
  478. if (w == null)
  479. {
  480. return null;
  481. }
  482. else if (w is int)
  483. {
  484. return new WriteConcern.WCount((int)w);
  485. }
  486. else
  487. {
  488. return new WriteConcern.WMode((string)w);
  489. }
  490. }
  491. }
  492. public class WriteConcernWValueTests
  493. {
  494. [Fact]
  495. public void implicit_conversion_from_int_should_return_expected_result()
  496. {
  497. WriteConcern.WValue result = 1;
  498. result.Should().BeOfType<WriteConcern.WCount>();
  499. ((WriteConcern.WCount)result).Value.Should().Be(1);
  500. }
  501. [Fact]
  502. public void implicit_conversion_from_nullable_int_should_return_expected_result_when_value_is_not_null()
  503. {
  504. WriteConcern.WValue result = (int?)1;
  505. result.Should().BeOfType<WriteConcern.WCount>();
  506. ((WriteConcern.WCount)result).Value.Should().Be(1);
  507. }
  508. [Fact]
  509. public void implicit_conversion_from_nullable_int_should_return_expected_result_when_value_is_null()
  510. {
  511. WriteConcern.WValue result = (int?)null;
  512. result.Should().BeNull();
  513. }
  514. [Fact]
  515. public void implicit_conversion_from_string_should_return_expected_result_when_value_is_not_null()
  516. {
  517. WriteConcern.WValue result = (string)null;
  518. result.Should().BeNull();
  519. }
  520. [Fact]
  521. public void implicit_conversion_from_string_should_return_expected_result_when_value_is_null()
  522. {
  523. WriteConcern.WValue result = "mode";
  524. result.Should().BeOfType<WriteConcern.WMode>();
  525. ((WriteConcern.WMode)result).Value.Should().Be("mode");
  526. }
  527. [Fact]
  528. public void Parse_should_return_expected_result_when_value_is_not_numeric()
  529. {
  530. var result = WriteConcern.WValue.Parse("mode");
  531. result.Should().Be(new WriteConcern.WMode("mode"));
  532. }
  533. [Theory]
  534. [InlineData("0", 0)]
  535. [InlineData("1", 1)]
  536. public void Parse_should_return_expected_result_when_value_is_numeric(
  537. string value,
  538. int w)
  539. {
  540. var result = WriteConcern.WValue.Parse(value);
  541. result.Should().Be(new WriteConcern.WCount(w));
  542. }
  543. }
  544. public class WriteConcernWCountTests
  545. {
  546. [Theory]
  547. [InlineData(0)]
  548. [InlineData(1)]
  549. public void constructor_should_initialize_instance(int w)
  550. {
  551. var result = new WriteConcern.WCount(w);
  552. result.Value.Should().Be(w);
  553. }
  554. [Fact]
  555. public void constructor_should_throw_when_w_is_negative()
  556. {
  557. Action action = () => new WriteConcern.WCount(-1);
  558. action.ShouldThrow<ArgumentOutOfRangeException>().And.ParamName.Should().Be("w");
  559. }
  560. [Fact]
  561. public void Equals_should_return_false_if_any_fields_are_not_equal()
  562. {
  563. var subject1 = new WriteConcern.WCount(0);
  564. var subject2 = new WriteConcern.WCount(1);
  565. var result1 = subject1.Equals(subject2);
  566. var result2 = subject1.Equals((object)subject2);
  567. var hashCode1 = subject1.GetHashCode();
  568. var hashCode2 = subject2.GetHashCode();
  569. result1.Should().BeFalse();
  570. result2.Should().BeFalse();
  571. hashCode1.Should().NotBe(hashCode2);
  572. }
  573. [Fact]
  574. public void Equals_should_return_true_if_all_fields_are_equal()
  575. {
  576. var subject1 = new WriteConcern.WCount(1);
  577. var subject2 = new WriteConcern.WCount(1);
  578. var result1 = subject1.Equals(subject2);
  579. var result2 = subject1.Equals((object)subject2);
  580. var hashCode1 = subject1.GetHashCode();
  581. var hashCode2 = subject2.GetHashCode();
  582. result1.Should().BeTrue();
  583. result2.Should().BeTrue();
  584. hashCode1.Should().Be(hashCode2);
  585. }
  586. [Fact]
  587. public void ToBsonValue_should_return_expected_result()
  588. {
  589. var subject = new WriteConcern.WCount(1);
  590. var result = subject.ToBsonValue();
  591. result.Should().BeOfType<BsonInt32>();
  592. result.AsInt32.Should().Be(1);
  593. }
  594. [Fact]
  595. public void ToString_should_return_expected_result()
  596. {
  597. var subject = new WriteConcern.WCount(1);
  598. var result = subject.ToString();
  599. result.Should().Be("1");
  600. }
  601. }
  602. public class WriteConcernWModeTests
  603. {
  604. [Fact]
  605. public void constructor_should_initialize_instance()
  606. {
  607. var result = new WriteConcern.WMode("mode");
  608. result.Value.Should().Be("mode");
  609. }
  610. [Fact]
  611. public void constructor_should_throw_when_mode_is_empty()
  612. {
  613. Action action = () => new WriteConcern.WMode("");
  614. action.ShouldThrow<ArgumentException>().And.ParamName.Should().Be("mode");
  615. }
  616. [Fact]
  617. public void constructor_should_throw_when_mode_is_null()
  618. {
  619. Action action = () => new WriteConcern.WMode(null);
  620. action.ShouldThrow<ArgumentNullException>().And.ParamName.Should().Be("mode");
  621. }
  622. [Fact]
  623. public void Majority_should_return_expected_result()
  624. {
  625. var result = WriteConcern.WMode.Majority;
  626. result.Value.Should().Be("majority");
  627. }
  628. [Fact]
  629. public void Equals_should_return_false_when_any_fields_are_not_equal()
  630. {
  631. var subject1 = new WriteConcern.WMode("mode1");
  632. var subject2 = new WriteConcern.WMode("mode2");
  633. var result1 = subject1.Equals(subject2);
  634. var result2 = subject1.Equals((object)subject2);
  635. var hashCode1 = subject1.GetHashCode();
  636. var hashCode2 = subject2.GetHashCode();
  637. result1.Should().BeFalse();
  638. result2.Should().BeFalse();
  639. hashCode1.Should().NotBe(hashCode2);
  640. }
  641. [Fact]
  642. public void Equals_should_return_true_when_all_fields_are_equal()
  643. {
  644. var subject1 = new WriteConcern.WMode("mode");
  645. var subject2 = new WriteConcern.WMode("mode");
  646. var result1 = subject1.Equals(subject2);
  647. var result2 = subject1.Equals((object)subject2);
  648. var hashCode1 = subject1.GetHashCode();
  649. var hashCode2 = subject2.GetHashCode();
  650. result1.Should().BeTrue();
  651. result2.Should().BeTrue();
  652. hashCode1.Should().Be(hashCode2);
  653. }
  654. [Fact]
  655. public void ToBsonValue_should_return_expected_result()
  656. {
  657. var subject = new WriteConcern.WMode("mode");
  658. var result = subject.ToBsonValue();
  659. result.Should().BeOfType<BsonString>();
  660. result.AsString.Should().Be("mode");
  661. }
  662. [Fact]
  663. public void ToString_should_return_expected_result()
  664. {
  665. var subject = new WriteConcern.WMode("mode");
  666. var result = subject.ToString();
  667. result.Should().Be("mode");
  668. }
  669. }
  670. }