Пример #1
0
        public async Task multi_stream_projections_should_work()
        {
            // --------------------------------
            // Create Licenses
            // --------------------------------
            // Free License
            // Premium License
            // --------------------------------

            var freeLicenseCreated = new LicenseCreated(Guid.NewGuid(), "Free Licence");

            theSession.Events.Append(freeLicenseCreated.LicenseId, freeLicenseCreated);

            var premiumLicenseCreated = new LicenseCreated(Guid.NewGuid(), "Premium Licence");

            theSession.Events.Append(premiumLicenseCreated.LicenseId, premiumLicenseCreated);

            await theSession.SaveChangesAsync();

            // --------------------------------
            // Create Users
            // --------------------------------
            // Anna
            // John
            // Maggie
            // Alan
            // --------------------------------

            var annaRegistered = new UserRegistered(Guid.NewGuid(), "Anna");

            theSession.Events.Append(annaRegistered.UserId, annaRegistered);

            var johnRegistered = new UserRegistered(Guid.NewGuid(), "John");

            theSession.Events.Append(johnRegistered.UserId, johnRegistered);

            var maggieRegistered = new UserRegistered(Guid.NewGuid(), "Maggie");

            theSession.Events.Append(maggieRegistered.UserId, maggieRegistered);

            var alanRegistered = new UserRegistered(Guid.NewGuid(), "Alan");

            theSession.Events.Append(alanRegistered.UserId, alanRegistered);

            await theSession.SaveChangesAsync();

            // --------------------------------
            // Assign users' licences
            // --------------------------------
            // Anna, Maggie => Premium
            // John, Alan   => Free
            // --------------------------------

            var annaAssignedToPremiumLicense =
                new UserLicenseAssigned(annaRegistered.UserId, premiumLicenseCreated.LicenseId);

            theSession.Events.Append(annaRegistered.UserId, annaAssignedToPremiumLicense);

            var johnAssignedToFreeLicense =
                new UserLicenseAssigned(johnRegistered.UserId, freeLicenseCreated.LicenseId);

            theSession.Events.Append(johnRegistered.UserId, johnAssignedToFreeLicense);

            var maggieAssignedToPremiumLicense =
                new UserLicenseAssigned(maggieRegistered.UserId, premiumLicenseCreated.LicenseId);

            theSession.Events.Append(maggieAssignedToPremiumLicense.UserId, maggieAssignedToPremiumLicense);

            var alanAssignedToFreeLicense =
                new UserLicenseAssigned(alanRegistered.UserId, freeLicenseCreated.LicenseId);

            theSession.Events.Append(alanAssignedToFreeLicense.UserId, alanAssignedToFreeLicense);

            await theSession.SaveChangesAsync();

            // --------------------------------
            // Assign feature toggle to license
            // --------------------------------
            // Login  => Free, Premium
            // Invite => Premium
            // --------------------------------

            var loginFeatureToggle = "Login";
            var loginFeatureToggledOnFreeLicense =
                new LicenseFeatureToggled(freeLicenseCreated.LicenseId, loginFeatureToggle);

            theSession.Events.Append(loginFeatureToggledOnFreeLicense.LicenseId, loginFeatureToggledOnFreeLicense);

            var loginFeatureToggledOnPremiumLicense =
                new LicenseFeatureToggled(premiumLicenseCreated.LicenseId, loginFeatureToggle);

            theSession.Events.Append(loginFeatureToggledOnPremiumLicense.LicenseId,
                                     loginFeatureToggledOnPremiumLicense);

            var inviteFeatureToggle           = "Invite";
            var inviteToggledOnPremiumLicense =
                new LicenseFeatureToggled(premiumLicenseCreated.LicenseId, inviteFeatureToggle);

            theSession.Events.Append(inviteToggledOnPremiumLicense.LicenseId, inviteToggledOnPremiumLicense);

            await theSession.SaveChangesAsync();

            // --------------------------------
            // Check users' feature toggles
            // --------------------------------
            // Anna, Maggie => Premium => Login, Invite
            // John, Alan   => Free    => Login
            // --------------------------------

            var annaFeatureToggles = await theSession.LoadAsync <UserFeatureToggles>(annaRegistered.UserId);

            annaFeatureToggles.ShouldNotBeNull();
            annaFeatureToggles.Id.ShouldBe(annaRegistered.UserId);
            annaFeatureToggles.LicenseId.ShouldBe(premiumLicenseCreated.LicenseId);
            annaFeatureToggles.FeatureToggles.ShouldHaveTheSameElementsAs(loginFeatureToggle, inviteFeatureToggle);

            var maggieFeatureToggles = await theSession.LoadAsync <UserFeatureToggles>(maggieRegistered.UserId);

            maggieFeatureToggles.ShouldNotBeNull();
            maggieFeatureToggles.Id.ShouldBe(maggieRegistered.UserId);
            maggieFeatureToggles.LicenseId.ShouldBe(premiumLicenseCreated.LicenseId);
            maggieFeatureToggles.FeatureToggles.ShouldHaveTheSameElementsAs(loginFeatureToggle, inviteFeatureToggle);

            var johnFeatureToggles = await theSession.LoadAsync <UserFeatureToggles>(johnRegistered.UserId);

            johnFeatureToggles.ShouldNotBeNull();
            johnFeatureToggles.Id.ShouldBe(johnRegistered.UserId);
            johnFeatureToggles.LicenseId.ShouldBe(freeLicenseCreated.LicenseId);
            johnFeatureToggles.FeatureToggles.ShouldHaveTheSameElementsAs(loginFeatureToggle);

            var alanFeatureToggles = await theSession.LoadAsync <UserFeatureToggles>(alanRegistered.UserId);

            alanFeatureToggles.ShouldNotBeNull();
            alanFeatureToggles.Id.ShouldBe(alanRegistered.UserId);
            alanFeatureToggles.LicenseId.ShouldBe(freeLicenseCreated.LicenseId);
            alanFeatureToggles.FeatureToggles.ShouldHaveTheSameElementsAs(loginFeatureToggle);
        }
Пример #2
0
 public void Apply(UserLicenseAssigned @event, UserFeatureToggles view)
 {
     view.LicenseId = @event.LicenseId;
 }