当前位置: 首页>>代码示例>>Golang>>正文


Golang Backend.Create方法代码示例

本文整理汇总了Golang中github.com/theduke/go-dukedb.Backend.Create方法的典型用法代码示例。如果您正苦于以下问题:Golang Backend.Create方法的具体用法?Golang Backend.Create怎么用?Golang Backend.Create使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在github.com/theduke/go-dukedb.Backend的用法示例。


在下文中一共展示了Backend.Create方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。

示例1: TestBackend

func TestBackend(skipFlag *bool, backendBuilder func() (db.Backend, apperror.Error)) {
	doSkip := false
	var backend db.Backend

	BeforeEach(func() {
		if *skipFlag || doSkip {
			Skip("Skipping due to previous error.")
		}

		var err apperror.Error
		backend, err = backendBuilder()
		Expect(err).ToNot(HaveOccurred())

		backend.SetDebug(true)

		backend.RegisterModel(&Tag{})
		backend.RegisterModel(&Project{})
		backend.RegisterModel(&Task{})
		backend.RegisterModel(&File{})

		backend.RegisterModel(&TestModel{})
		backend.RegisterModel(&TestParent{})
		backend.RegisterModel(&HooksModel{})
		backend.RegisterModel(&ValidationsModel{})
		backend.RegisterModel(&MarshalledModel{})
		backend.Build()
	})

	It("Should drop all collections", func() {
		doSkip = true
		err := backend.DropAllCollections()
		Expect(err).ToNot(HaveOccurred())
		doSkip = false
	})

	It("Should create collections", func() {
		doSkip = true
		err := backend.CreateCollection(
			"test_models",
			"test_parents",
			"hooks_models",
			"validations_models",
			"marshalled_models",
			"tags",
			"projects",
			"tasks",
			"files",
		)
		Expect(err).ToNot(HaveOccurred())
		doSkip = false
	})

	It("Should count with zero entries", func() {
		Expect(backend.Q("test_models").Count()).To(Equal(0))
	})

	It("Should insert and set Id, then FindOne()", func() {
		testModel := NewTestModel(1)
		testModel.Id = 0

		err := backend.Create(&testModel)
		Expect(err).ToNot(HaveOccurred())
		Expect(testModel.Id).ToNot(Equal(0))

		m, err := backend.FindOne("test_models", testModel.Id)
		Expect(err).ToNot(HaveOccurred())
		Expect(*m.(*TestModel)).To(Equal(testModel))
	})

	It("Should count with 1 entry", func() {
		Expect(backend.Create(&Project{Name: "Test"})).ToNot(HaveOccurred())
		Expect(backend.Q("projects").Count()).To(Equal(1))
	})

	It("Should update", func() {
		// Persist model to update.
		testModel := NewTestModel(2)
		err := backend.Create(&testModel)
		Expect(err).ToNot(HaveOccurred())

		testModel.IntVal = 33
		testModel.StrVal = "str33"
		err = backend.Update(&testModel)
		Expect(err).ToNot(HaveOccurred())

		m, err := backend.FindOne("test_models", testModel.Id)
		Expect(err).ToNot(HaveOccurred())
		Expect(m).To(Equal(&testModel))
	})

	It("Should delete", func() {
		// Persist model to update.
		testModel := NewTestModel(3)
		err := backend.Create(&testModel)
		Expect(err).ToNot(HaveOccurred())

		err = backend.Delete(&testModel)
		Expect(err).ToNot(HaveOccurred())

		m, err := backend.FindOne("test_models", testModel.Id)
//.........这里部分代码省略.........
开发者ID:theduke,项目名称:go-dukedb,代码行数:101,代码来源:tests.go


注:本文中的github.com/theduke/go-dukedb.Backend.Create方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。