예제 #1
0
파일: subtask.go 프로젝트: altlinux/webery
func Write(st db.Session, t *SubTask) error {
	col, err := st.Coll(CollName)
	if err != nil {
		return err
	}

	id, err := t.GetID()
	if err != nil {
		return err
	}

	t.ObjType.Set("subtask")

	if t.Search == nil {
		t.Search = kwd.NewKeywords()
	}

	if t.Owner.IsDefined() {
		t.Search["owner"] = t.Owner.String()
	}

	if t.PkgName.IsDefined() {
		t.Search["pkgname"] = t.PkgName.String()
	}

	_, err = col.Upsert(id, t)
	return err
}
예제 #2
0
파일: task.go 프로젝트: altlinux/webery
func Write(st db.Session, t *Task) error {
	col, err := st.Coll(CollName)
	if err != nil {
		return err
	}

	id, err := t.GetID()
	if err != nil {
		return err
	}

	t.ObjType.Set("task")

	if t.Search == nil {
		t.Search = kwd.NewKeywords()
	}

	if t.Events == nil {
		t.Events = evtype.NewEventList()
	}

	if t.TaskID.IsDefined() {
		t.Search["taskid"] = t.TaskID.String()
	}

	if t.Repo.IsDefined() {
		t.Search["repo"] = t.Repo.String()
	}

	_, err = col.Upsert(id, t)
	return err
}
예제 #3
0
파일: main.go 프로젝트: altlinux/webery
func oldNew() *oldTask {
	t := &oldTask{}

	t.ObjType.Set("task")
	t.Search = kwd.NewKeywords()

	return t
}
예제 #4
0
파일: subtask.go 프로젝트: altlinux/webery
func New() *SubTask {
	t := &SubTask{}

	t.ObjType.Set("subtask")
	t.Search = kwd.NewKeywords()

	return t
}
예제 #5
0
파일: task.go 프로젝트: altlinux/webery
func New() *Task {
	t := &Task{}

	t.ObjType.Set("task")
	t.Search = kwd.NewKeywords()
	t.Events = evtype.NewEventList()

	return t
}
예제 #6
0
func TestJSON(t *testing.T) {
	testcases := map[string]struct {
		data     *Task
		expected interface{}
	}{
		"empty document": {
			data:     &Task{},
			expected: struct{}{},
		},
		"document with only one bool=true defined": {
			data: &Task{
				Foo: *NewBool(true),
			},
			expected: struct {
				Foo bool
			}{
				Foo: true,
			},
		},
		"document with only one bool=false defined": {
			data: &Task{
				Foo: *NewBool(false),
			},
			expected: struct {
				Foo bool
			}{
				Foo: false,
			},
		},
		"document with only one string defined": {
			data: &Task{
				Baz: *NewBaseString("zzz"),
			},
			expected: struct {
				Baz string
			}{
				Baz: "zzz",
			},
		},
		"document with two fields defined": {
			data: &Task{
				Foo: *NewBool(true),
				Bar: *NewInt64(int64(123)),
			},
			expected: struct {
				Foo bool
				Bar int64
			}{
				Foo: true,
				Bar: 123,
			},
		},
		"document with list of keywords": {
			data: &Task{
				Foo: *NewBool(true),
				Far: kwd.NewKeywords(kwd.Keyword{Key: "AAA", Group: "repo"}),
			},
			expected: struct {
				Foo bool
				Far []struct {
					Key   string
					Group string
				}
			}{
				Foo: true,
				Far: []struct {
					Key   string
					Group string
				}{
					{
						Key:   "AAA",
						Group: "repo",
					},
				},
			},
		},
	}

	for title, test := range testcases {
		expectOut, err := json.Marshal(&test.expected)
		if err != nil {
			t.Fatalf("Unexpected error: %v", err)
		}

		resultOut, err := json.Marshal(&test.data)
		if err != nil {
			t.Fatalf("Unexpected error: %v", err)
		}

		var resultTask Task
		if err := json.Unmarshal(resultOut, &resultTask); err != nil {
			t.Fatalf("Unexpected error: %v", err)
		}

		var expectTask Task
		if err := json.Unmarshal(expectOut, &expectTask); err != nil {
			t.Fatalf("Unexpected error: %v", err)
		}

		if !reflect.DeepEqual(&resultTask, &expectTask) {
			t.Errorf("Unexpected difference in %s", title)

			t.Logf("expected: %+v\n", &expectTask)
			t.Logf("got     : %+v\n", &resultTask)

			t.Logf("expected (json): %+v\n", string(expectOut))
			t.Logf("got      (json): %+v\n", string(resultOut))
		}
	}
}
예제 #7
0
func TestBSON(t *testing.T) {
	testcases := map[string]struct {
		data     *Task
		expected bson.M
	}{
		"empty document": {
			data:     &Task{},
			expected: bson.M{},
		},
		"document with only one bool=true defined": {
			data: &Task{
				Foo: *NewBool(true),
			},
			expected: bson.M{
				"foo": true,
			},
		},
		"document with only one bool=false defined": {
			data: &Task{
				Foo: *NewBool(false),
			},
			expected: bson.M{
				"foo": false,
			},
		},
		"document with only one int64 defined": {
			data: &Task{
				Bar: *NewInt64(int64(123)),
			},
			expected: bson.M{
				"bar": int64(123),
			},
		},
		"document with only one string defined": {
			data: &Task{
				Baz: *NewBaseString("zzz"),
			},
			expected: bson.M{
				"baz": "zzz",
			},
		},
		"document with two fields defined": {
			data: &Task{
				Foo: *NewBool(true),
				Bar: *NewInt64(int64(123)),
			},
			expected: bson.M{
				"foo": true,
				"bar": int64(123),
			},
		},
		"document with list of keywords": {
			data: &Task{
				Foo: *NewBool(true),
				Far: kwd.NewKeywords(kwd.Keyword{Key: "AAA", Group: "repo"}),
			},
			expected: bson.M{
				"foo": true,
				"far": []bson.M{
					bson.M{"key": "AAA", "group": "repo"},
				},
			},
		},
	}

	for title, test := range testcases {
		expectOut, err := bson.Marshal(&test.expected)
		if err != nil {
			t.Fatalf("Unexpected error: %s: %v", title, err)
		}

		resultOut, err := bson.Marshal(&test.data)
		if err != nil {
			t.Fatalf("Unexpected error: %s: %v", title, err)
		}

		resultTask := &Task{}
		if err := bson.Unmarshal(resultOut, resultTask); err != nil {
			t.Fatalf("Unexpected error: %s: %v", title, err)
		}

		expectTask := &Task{}
		if err := bson.Unmarshal(expectOut, expectTask); err != nil {
			t.Fatalf("Unexpected error: %s: %v", title, err)
		}

		if !reflect.DeepEqual(resultTask, expectTask) {
			t.Errorf("Unexpected difference in %s", title)

			t.Logf("expected: %+v\n", expectTask)
			t.Logf("got     : %+v\n", resultTask)

			b0, err := json.Marshal(expectTask)
			if err != nil {
				t.Fatalf("Unexpected error: %s: %v", title, err)
			}

			b1, err := json.Marshal(resultTask)
			if err != nil {
				t.Fatalf("Unexpected error: %s: %v", title, err)
			}

			t.Logf("expected (json): %+v\n", string(b0))
			t.Logf("got      (json): %+v\n", string(b1))
		}
	}
}