GDTCORTransformerTest.m 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /*
  2. * Copyright 2018 Google
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #import "GDTCORTests/Unit/GDTCORTestCase.h"
  17. #import <GoogleDataTransport/GDTCOREvent.h>
  18. #import <GoogleDataTransport/GDTCOREventTransformer.h>
  19. #import "GDTCORLibrary/Private/GDTCORStorage.h"
  20. #import "GDTCORLibrary/Private/GDTCORTransformer.h"
  21. #import "GDTCORLibrary/Private/GDTCORTransformer_Private.h"
  22. #import "GDTCORTests/Unit/Helpers/GDTCORAssertHelper.h"
  23. #import "GDTCORTests/Unit/Helpers/GDTCORDataObjectTesterClasses.h"
  24. #import "GDTCORTests/Common/Fakes/GDTCORStorageFake.h"
  25. @interface GDTCORTransformerTestNilingTransformer : NSObject <GDTCOREventTransformer>
  26. @end
  27. @implementation GDTCORTransformerTestNilingTransformer
  28. - (GDTCOREvent *)transform:(GDTCOREvent *)eventEvent {
  29. return nil;
  30. }
  31. @end
  32. @interface GDTCORTransformerTestNewEventTransformer : NSObject <GDTCOREventTransformer>
  33. @end
  34. @implementation GDTCORTransformerTestNewEventTransformer
  35. - (GDTCOREvent *)transform:(GDTCOREvent *)eventEvent {
  36. return [[GDTCOREvent alloc] initWithMappingID:@"new" target:1];
  37. }
  38. @end
  39. @interface GDTCORTransformerTest : GDTCORTestCase
  40. @end
  41. @implementation GDTCORTransformerTest
  42. - (void)setUp {
  43. [super setUp];
  44. dispatch_sync([GDTCORTransformer sharedInstance].eventWritingQueue, ^{
  45. [GDTCORTransformer sharedInstance].storageInstance = [[GDTCORStorageFake alloc] init];
  46. });
  47. }
  48. - (void)tearDown {
  49. [super tearDown];
  50. dispatch_sync([GDTCORTransformer sharedInstance].eventWritingQueue, ^{
  51. [GDTCORTransformer sharedInstance].storageInstance = [GDTCORStorage sharedInstance];
  52. });
  53. }
  54. /** Tests the default initializer. */
  55. - (void)testInit {
  56. XCTAssertNotNil([[GDTCORTransformer alloc] init]);
  57. }
  58. /** Tests the pointer equality of result of the -sharedInstance method. */
  59. - (void)testSharedInstance {
  60. XCTAssertEqual([GDTCORTransformer sharedInstance], [GDTCORTransformer sharedInstance]);
  61. }
  62. /** Tests writing a event without a transformer. */
  63. - (void)testWriteEventWithoutTransformers {
  64. GDTCORTransformer *transformer = [GDTCORTransformer sharedInstance];
  65. GDTCOREvent *event = [[GDTCOREvent alloc] initWithMappingID:@"1" target:1];
  66. event.dataObject = [[GDTCORDataObjectTesterSimple alloc] init];
  67. XCTAssertNoThrow([transformer transformEvent:event
  68. withTransformers:nil
  69. onComplete:^(BOOL wasWritten, NSError *error) {
  70. XCTAssertTrue(wasWritten);
  71. }]);
  72. }
  73. /** Tests writing a event with a transformer that nils out the event. */
  74. - (void)testWriteEventWithTransformersThatNilTheEvent {
  75. GDTCORTransformer *transformer = [GDTCORTransformer sharedInstance];
  76. GDTCOREvent *event = [[GDTCOREvent alloc] initWithMappingID:@"2" target:1];
  77. event.dataObject = [[GDTCORDataObjectTesterSimple alloc] init];
  78. NSArray<id<GDTCOREventTransformer>> *transformers =
  79. @[ [[GDTCORTransformerTestNilingTransformer alloc] init] ];
  80. XCTAssertNoThrow([transformer transformEvent:event
  81. withTransformers:transformers
  82. onComplete:^(BOOL wasWritten, NSError *error) {
  83. XCTAssertFalse(wasWritten);
  84. }]);
  85. }
  86. /** Tests writing a event with a transformer that creates a new event. */
  87. - (void)testWriteEventWithTransformersThatCreateANewEvent {
  88. GDTCORTransformer *transformer = [GDTCORTransformer sharedInstance];
  89. GDTCOREvent *event = [[GDTCOREvent alloc] initWithMappingID:@"2" target:1];
  90. event.dataObject = [[GDTCORDataObjectTesterSimple alloc] init];
  91. NSArray<id<GDTCOREventTransformer>> *transformers =
  92. @[ [[GDTCORTransformerTestNewEventTransformer alloc] init] ];
  93. XCTAssertNoThrow([transformer transformEvent:event
  94. withTransformers:transformers
  95. onComplete:^(BOOL wasWritten, NSError *error) {
  96. XCTAssertTrue(wasWritten);
  97. XCTAssertNil(error);
  98. }]);
  99. }
  100. @end